A classic Snake game written in Python, designed specifically for the Raspberry Pi Zero W paired with the Waveshare 1.44inch LCD HAT (ST7735S Driver).
This project uses the luma.lcd library for rendering and RPi.GPIO for the HAT's joystick and button inputs.
-
MicroSD Card (8GB+).
-
Power supply (Micro USB).
If you have a brand new Raspberry Pi Zero W, follow this guide to set it up "headless" (without a monitor/keyboard attached).
You will need a computer (Windows/Mac/Linux) with an SD card reader.
-
Download and install Raspberry Pi Imager.
-
Insert your MicroSD card into your computer.
-
Open Raspberry Pi Imager.
-
Choose OS: Select
Raspberry Pi OS (other)->Raspberry Pi OS Lite (32-bit).- Note: The "Lite" version is best for this game as it uses less memory and boots faster.
-
Choose Storage: Select your SD card.
-
⚙️ IMPORTANT: Configure Settings (The Gear Icon) Click the gear icon (bottom right) to open Advanced Options. Set these up now so you don't need a monitor later:
-
Set Hostname: e.g.,
snake-pi. -
Enable SSH: Check this box. Select "Use password authentication".
-
Set username and password: Create a user (e.g., User:
pi, Pass:raspberry). -
Configure Wireless LAN: Enter your Wi-Fi Name (SSID) and Password. Select your country code.
-
-
Click Save, then click Write.
-
Wait for the verification to finish, then remove the SD card.
-
Insert the SD card into your Raspberry Pi Zero W.
-
Attach the Waveshare LCD HAT to the GPIO header.
-
Plug in the power cable. Wait about 2-3 minutes for the first boot.
-
Open Command Prompt (Windows), PowerShell, or Terminal (Mac/Linux) on your computer.
-
Type the following command (replace
piandsnake-piwith the username/hostname you set in Phase 1):-
If you didn't set a hostname, try
ssh [email protected]. -
If that doesn't work, check your router to find the Pi's IP address (e.g., 192.168.1.50) and run
ssh [email protected].
-
-
Type
yeswhen asked to continue connecting. -
Enter the password you created. You are now controlling your Pi remotely!
Once logged in via SSH, run:
sudo raspi-config
-
Navigate to Interface Options.
-
Select SPI.
-
Select Yes to enable the SPI interface.
-
Select Finish and ask it to Reboot.
-
Wait 1 minute, then SSH back in (
ssh [email protected]).
The screen drivers require specific system libraries. Copy and paste this command:
sudo apt-get update
sudo apt-get install python3-dev python3-pip python3-pil libjpeg-dev zlib1g-dev libfreetype6-dev liblcms2-dev libopenjp2-7 libtiff5 git -y
-
Clone the repository:
git clone [https://github.com/Shrees1ngh/pi-snake-game.git](https://github.com/Shrees1ngh/pi-snake-game.git) cd pi-snake-game -
Install Python requirements:
pip3 install -r requirements.txt -
Run the game:
python3 snake_game.py
pi-snake-game/
│── snake_game.py # Main game script
│── highscore.txt # Auto-created
│── requirements.txt # Python dependencies
│── README.md # Documentation
| Button | Action |
|---|---|
| Joystick Up | Move Up |
| Joystick Down | Move Down |
| Joystick Left | Move Left |
| Joystick Right | Move Right |
| Joystick Press | Pause Game |
| Key 1 | Pause Game |
| Key 2 | Start Game / Restart |
| Key 3 | Exit to Terminal |
-
High Score: Automatically saves your best score to
highscore.txt. -
Speed Ramp: The snake gets faster as you eat more food.
-
Colors: Dark Green head, gradient body, red food.
-
"Host not found" when SSHing? Ensure your computer and the Pi are on the exact same Wi-Fi network. If
snake-pi.localfails, you must find the IP address of the Pi using your router's admin page or a phone app like "Fing". -
Screen is black? Make sure you enabled SPI in
sudo raspi-config. -
Colors look inverted? Open
snake_game.pyand changebgr=Truetobgr=Falsein thedevice = st7735(...)line. -
Screen has static on edges? Adjust
v_offsetorh_offsetin thest7735initialization in the code (currently set tov_offset=2).
Open Source. Feel free to modify and improve!