Skip to content Skip to sidebar Skip to footer

Snake Game: Snake Colliding With Itself

Hi I am currently writing a snake game code and I am nearly finished however I am having difficulty writing a code which will cause the game to end if the head of the snake collide

Solution 1:

In your code it looks as though you are checking if the head of the snake collide with itself, which will always return True. You need to individually check that the head of snake does not collide with any of it's trailing segments:

for segment in SnakeSegments[2:]:
    if pygame.sprite.collide_rect(h, segment):
        pass # collision detected, game-over

The head of the snake is expected to collide with the segment directly behind it, which is why we need to start with the 3rd element in the list.

Post a Comment for "Snake Game: Snake Colliding With Itself"