1
Home
Tirsvad edited this page 2026-11-15 13:58:41 +01:00

English · Dansk · Bahasa Melayu

Snake Game

The classic Snake game with Python turtle graphics: steer the snake, eat the food and avoid the wall and your own tail.

What is it?

Days 20 and 21 of the Udemy course 100 Days of Code: The Complete Python Pro Bootcamp. A black 600 by 600 window titled "My Snake Game" opens with a snake of three white squares in the middle. The snake moves by itself, 20 pixels every 0.1 seconds, and you steer it with the arrow keys; it never turns straight back onto itself. A small blue circle, the food, appears at a random place. When the head comes closer to it than 15 pixels, the snake eats it, grows by one segment and the score at the top of the window goes up by 1. The game is over when the head passes the wall or touches the tail: the text GAME OVER appears and a click closes the window. The names of the lectures are kept (Snake, create_snake, move, up, down, left, right, segments, head, game_is_on, Food, Scoreboard, refresh, extend, increase_score and game_over).

What the exercise teaches

  • Setting up the turtle screen and drawing a snake of segments
  • Animating the segments by hand with tracer, update and sleep
  • Writing the Snake class and keeping the lectures' names
  • Binding the arrow keys and refusing a turn straight back
  • Making Food and Scoreboard inherit from Turtle, and detecting collisions by distance
  • Testing the game rules with fakes instead of a real screen, so no window opens

What it does

  • A snake of three squares that moves by itself and turns with the arrow keys
  • Food at a random place; eating it grows the snake and raises the score
  • A score shown at the top of the window
  • Game over at the wall or at the tail, with the text GAME OVER and a click to close
  • No runtime dependencies beyond the Python standard library

Next