A reconstruction of the classic Arcade Game using Python, focusing on Class Inheritance and File I/O.
Most tutorials teach turtle for drawing simple shapes. In Day 20 & 21, I pushed the library to build a dynamic game engine.
The challenge wasn't just "moving a square"; it was Memory Management and State Persistence.
- The Movement Problem: A snake isn't one object; it's a list of objects. Moving them requires "Reverse Slicing" logic (shifting the tail to the head's position).
- The Inheritance Solution: Instead of writing new rendering code for the Food and Scoreboard, I made them inherit from the
Turtleclass, giving them instant access to graphical methods while maintaining unique behaviors. - Persistence: Added a File I/O system to read/write high scores to
data.txt, so the record survives even after the program closes.
The game loop relies on three custom classes interacting in real-time:
| Class | Type | Responsibility |
|---|---|---|
Snake |
Object Manager |
Manages the list of body segments, controls direction, and handles the "Reverse Slicing" movement logic. |
Food |
Child Class |
Inherits from Turtle. Spawns at random Cartesian coordinates to be "eaten." |
Scoreboard |
Child Class |
Inherits from Turtle. Manages the UI, updates live score, and handles File I/O for the High Score. |
- Clone the repository:
git clone [https://github.com/CodeWithAnubhav-ICT/SnakeGame.git](https://github.com/CodeWithAnubhav-ICT/SnakeGame.git)
- Run the application:
python main.pyw
- ⬆️ Up Arrow: Move North
- ⬇️ Down Arrow: Move South
- ⬅️ Left Arrow: Move West
- ➡️ Right Arrow: Move East
Part of my 100 Days of Code Journey.