Lazo Lab Sign up

App 6: Make a Game, Snake in Pygame / Build Snake

Lesson 7 of 10

Game over

The game ends if the head leaves the grid or hits the body. Check right after moving.

Key points

  • Check walls: 0 <= x < 30
  • Check body: head in snake[1:]
  • Stop the loop or show a message
hx, hy = snake[0]
if not (0 <= hx < 30 and 0 <= hy < 30) or snake[0] in snake[1:]:
    game_over = True

Your turn

Add game over for walls and self-collisions.

Watch a video on thisOpens YouTube search results for “Game over” in a new tab

Quiz · +10 XP

What does snake[0] in snake[1:] check?

Log in to save progress and earn XP.