Here is my issue, I can't get my collision detection to work. You can see my attempt down here with the second if statement. The x
and y
coordinates are the ones for the first rectangle. But this code doesn't work and my character can easily pass through it.
How do I get it to work? I want the rectangle to be a block, so that my "guy" can't pass through it. All guidance is appreciated!
RELEVANT CODE:
for event in pygame.event.get():
if event.type == pygame.QUIT:
dead=True
print(event)
if 400 < x < 200 and 550 > y > 500:
global in_wallfyra
in_wallfyra = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
x1_change = False
elif event.key == pygame.K_RIGHT:
x2_change = False
elif event.key == pygame.K_UP:
y1_change = False
elif event.key == pygame.K_DOWN:
y2_change = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x1_change = True
elif event.key == pygame.K_RIGHT:
x2_change = True
elif event.key == pygame.K_UP:
y1_change = True
elif event.key == pygame.K_DOWN:
y2_change = True
if x1_change==True:
if not in_wallone:
x_change=-5
else:
x-=5
if x2_change==True:
if not in_walltwo:
x_change=5
else:
x-=-5
if y1_change==True:
if not in_wallthree:
y_change=-5
else:
y-=5
if y2_change==True:
if in_wallfour==False:
y_change=5
else:
y-=-5
if x1_change==False and x2_change==False:
x_change=0
if y1_change==False and y2_change==False:
y_change=0
x += x_change
y += y_change
if x > display_width - gubbe_width or x < 0:
dead=True
background()
gubbe(x,y)
pygame.display.update()
clock.tick(60)