A classic Snake Game built using Python's turtle
graphics module. Eat the food, grow the snake, and beat your high score—but don’t hit the walls or your own tail!
- Classic snake movement using arrow keys
- Random food generation
- Score tracking with real-time updates
- High score saved to file (
Data.txt
) - Collision detection:
- With walls
- With self (tail)
- Snake resets when collisions occur, high score persists
- Modular code using OOP (Object-Oriented Programming)
- Sets up the screen using
turtle.Screen()
- Creates instances of the
Snake
,Food
, andScoreboard
classes - Listens to arrow key inputs to control the snake
- Updates screen at each frame
- Moves the snake forward
- Detects collision with:
- Food → grows snake, updates score
- Wall or tail → resets snake and score (retains high score)
Handles everything about the snake:
- Snake is made up of segments (
Turtle
objects) - Manages:
- Movement logic
- Extending the snake
- Directional input (up, down, left, right)
- Resetting snake position after collision
Manages the food logic:
- Food is a small, randomly-placed
Turtle
object - On eating food:
- Moves to a new random location
- Triggers snake to grow
Displays and manages the score:
- Inherits from
Turtle
- Tracks:
- Current score
- High score (loaded from and saved to
Data.txt
)
- Updates the display when score changes or game resets
The score_board.py
reads and writes the highest score from Data.txt
.
If the current score exceeds the high score, it automatically updates the file.
✅ Ensure a
Data.txt
file exists in the project directory with a number inside (e.g.,0
).
Key | Action |
---|---|
Up |
Move Up |
Down |
Move Down |
Left |
Move Left |
Right |
Move Right |
- Python 3.x
- No external libraries needed
(Uses Python's built-inturtle
andrandom
modules)