A tiny but fully‑functional “Snake” game written in pure Python + Pygame.
Date: 2025‑10‑17
This repository contains a single, self‑contained script (snake.py) that implements the classic Snake game.
It uses only the standard library and the popular Pygame package – no external frameworks or assets are required.
Pygame gives you an easy way to create windows, draw shapes, handle keyboard input and keep a consistent frame‑rate. The code is written in modern Python (≥ 3.8) so it’s readable and easily tweakable.
- Install the only dependency
pip install pygame- Run the game
python snake.pyYou’ll see a black window with a green “snake” that you can control with the arrow keys.
The game runs at 15 FPS by default – change FPS in the script if you want it faster or slower.
- Movement: Arrow keys (Up / Down / Left / Right)
- Pause / Resume: P
- Speed modes: select difficulty which changes the game FPS (affects snake speed)
- Easy: 5 FPS — press
1orE - Normal: 10 FPS — press
2orN(default) - Hard: 15 FPS — press
3orH
- Easy: 5 FPS — press
The chosen speed mode takes effect immediately during play.
Feature Description
Snake movement Arrow keys change direction; snake grows when eating food.
Food spawning Random empty cell each time the snake eats.
Drawing Snake is drawn in green, food in red.
Frame‑rate control pygame.time.Clock() keeps a steady FPS.
Pause Press P to pause/resume the game; while paused the game state is frozen and a message appears.
Scoring & Highscore Eating food increases the score by 1; the current score is shown at the top-center and the best (Top) score and username at the top-right. After a game over you can enter your name — the top score is saved to highscore.json.
snake.py ← main game script
README.md ← this file
Tip:
If you want to add more features (score counter, wrap‑around logic, etc.) just edit the corresponding sections in snake.py. The code is heavily commented so you can follow along.Section What it does SnakeGame.init Sets up window, clock and initial snake/food. spawn_food Picks a random empty cell for food. draw Renders every frame (snake + food). run Main loop that handles input, updates, draws & refreshes.
- Speed – change the constant FPS or CELL_SIZE.
- Snake length – change the initial snake size or behavior on collisions.
- UI – change fonts, colors, or add sound effects on eating/game over.
Feel free to fork, open issues or pull requests.
If you’d like to contribute, just add your changes to snake.py and submit a PR – I’ll review them promptly.