Table of Contents
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.
The running game at a score of 5: a light green body, a dark green head with a mouth, the blue food and the white score.
What is it?
Day 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 squares in the middle: a light green body and a dark green head with a mouth at its front. 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,updateandsleep, and giving the head its own shape and colour - Writing the
Snakeclass and keeping the lectures' names - Binding the arrow keys and refusing a turn straight back
- Making
FoodandScoreboardinherit fromTurtle, 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, light green with a dark green head and a mouth, 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
Snake Game wiki
Languages
