I am trying to add collision detection to this simple game, but I am a little lost on where to begin. I know I need to use rects to do this but I am struggling to find out what I need to code to make this work. Any pseudocode or any hints pointing me in the right direction would be greatly appreciated.
Here is my code:
#Image Variables
bg = 'bg.jpg'
bunk = 'bunker.png'
enemytank = 'enemy-tank.png'
#Import Pygame Modules
import pygame, sys
from pygame.locals import *
#Initializing the Screen
pygame.init()
screen = pygame.display.set_mode((640,360), 0, 32)
#Loading Images and More Variables
background = pygame.image.load(bg).convert()
bunker = pygame.image.load(bunk).convert_alpha()
e_tank = pygame.image.load(enemytank).convert_alpha()
bunker_x, bunker_y = (160,0)
#Setting Up The Animation
x = 0
clock = pygame.time.Clock()
speed = 250
#Main Loop
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
screen.blit(background, (0,0))
screen.blit(bunker, (bunker_x, bunker_y))
screen.blit(e_tank, (x, 0))
#Animation
milli = clock.tick()
seconds = milli/1000.
dm = seconds*speed
x += dm
if x>640:
x=0
#Update the Screen
pygame.display.update()