2

I am currently working on a simple flappy bird-ish game on Python. For some reason, the collision code isn't working when the pipe and bird touch.

def collision():
    global distanceDown, distanceUp
    distanceUp = math.sqrt(math.pow(pipeUpX - birdX, 2) + math.pow(pipeUpY - birdY, 2))  # distance formula
    distanceDown = math.sqrt(math.pow(pipeDownX - birdX, 2) + math.pow(pipeDownX - birdY, 2))

    if distanceUp <= 20 or distanceDown <= 20:
        return True
    else:
        return False

I've called the function in the main game loop and asked python to end the game if true, but the bird just passes through the pipe.FYI, I haven't used OOP and classes.Here are the values..

pipeWidth = 50
pipeHeight = 130
pipeUpX = 800
pipeUpY = 0
pipeDownY = screenY - pipeHeight
pipeDownX = 900
pipeX_change = 1

Also, I'm quite new to python and programming as a whole, so please answer in easy to understand code. Thank You :)

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Fizy
  • 25
  • 5
  • is the code indented this way or just an error? – michmich112 Apr 05 '20 at 18:29
  • You have a typo in `distanceDown`, the second `pipeDownX` should probably by `pipeDownY` in `pipeDownX - birdY`. – Thierry Lathuille Apr 05 '20 at 18:30
  • I believe @michmich112 refers to the indentation of your code. – EnriqueBet Apr 05 '20 at 18:34
  • 2
    The code does not verify the distance to the pipe. The code verifies the distance to the start and the end of the pipe. That is something different. – Rabbid76 Apr 05 '20 at 20:48
  • @michmich112, its a typo in the question, but the indentation is fine in the actual code. Thanks! – Fizy Apr 06 '20 at 08:37
  • @ThierryLathuille, i fixed it..but no change :( – Fizy Apr 06 '20 at 08:40
  • @Rabbid76 oh , any idea on how to fix it? excuse my begginer skills at the language please :(( – Fizy Apr 06 '20 at 08:42
  • @Fizy Is the pipe just a vertical rectangle? – Rabbid76 Apr 06 '20 at 08:53
  • @Rabbid76 yes :(, im planning to switch it for pipe image when im done with the gamee.. same with the bird – Fizy Apr 06 '20 at 08:54
  • I recommend to use `pygame.Rect` objeccts and the `colliderect` method: `pipeRect=pygame.Rect(pipeLeft,pipeTop,pipeWidth,pipeHeight)` `birdRect=pygame.Rect(birdLeft,birdTop,birdWidth,birdHeight)` `if pipeRect.colliderect(birdRect): return True` – Rabbid76 Apr 06 '20 at 08:58
  • OK, will try.. thanks a lot! – Fizy Apr 06 '20 at 09:00
  • @Fizy Fiz for a collision of sprites you have to use a mask. See [Collision between masks in pygame](https://stackoverflow.com/questions/55817422/collision-between-masks-in-pygame/55818093#55818093), [How can I made a collision mask?](https://stackoverflow.com/questions/56043600/how-can-i-made-a-collision-mask/56045037#56045037) and [Pygame mask collision](https://stackoverflow.com/questions/60077813/pygame-mask-collision/60078039#60078039) – Rabbid76 Apr 06 '20 at 09:03
  • @Rabbid76, i dont know much about sprites,hence I have made them just as rectangles. Anyways, i used collideRect but bird passes straight throug them – Fizy Apr 06 '20 at 09:09

1 Answers1

1

The code does not verify the distance to the pipe. The code verifies the distance to the start and the end of the pipe. That is something different.

Since your objects just rectangles, I recommend to use pygame.Rect objeccts and the colliderect method. For instance some pseudo code:

def collision():  
    pipeRect = pygame.Rect(pipeLeft, pipeTop, pipeWidth, pipeHeight) 
    birdRect = pygame.Rect(birdLeft, birdTop, birdWidth, birdHeight) 
    return pipeRect.colliderect(birdRect)

For the collision of "images", I recommend to use pygame.sprite.Sprite / pygame.sprite.collide_mask() respectively pygame.mask.Mask / pygame.mask.Mask.overlap().

See further:
Collision between masks in pygame
How can I made a collision mask?
Pygame mask collision

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • thanks for the answer... just FYI, I could make it work with `def collision():` `if birdY > pipeHeight and abs(pipeDownX - birdX) <= pipeWidth-20:` `return True` `if birdY < pipeHeight and abs(pipeUpX - birdX) <= pipeWidth-20:` `return True` – Fizy Apr 06 '20 at 11:42