1

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)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 3
    `if 400 < x < 200` will never be `True`. – Bill the Lizard Jan 21 '18 at 15:48
  • @ThomasKühn You should make that an answer. – Micheal O'Dwyer Jan 21 '18 at 16:51
  • Alright thanks! I changed the implications and it still does not work, any idea why? – Albin Thorn Jan 21 '18 at 19:14
  • Please post a [minimal, complete and verifiable example](https://stackoverflow.com/help/mcve). Besides the main problem, there seem to be issues with the indentation and order of the code. If you want to see a short platformer example, take a look at [this answer](https://stackoverflow.com/a/48069743/6220679). – skrx Jan 21 '18 at 19:37

0 Answers0