The Snake Game is a classic arcade-style game that challenges players to control a growing snake navigating a grid to collect items (commonly represented as apples or food) while avoiding collisions with obstacles, including its own body. Its simplicity and addictive gameplay have made it one of the most iconic and enduring games.
Primary Goal:
Eat as many apples as possible to increase your score.
Challenges:
Avoid colliding with the walls or the snake's own body.
Navigate the snake as it grows longer, which makes the game more challenging.
End Condition:
The game ends when the snake collides with itself or the boundaries of the grid.
1- Snake Movement:
The snake moves continuously in one of four directions: up, down, left, or right.
Players control the direction using input devices like arrow keys, a joystick, or buttons.
2- Apple Collection:
Apples appear randomly on the grid.
When the snake eats an apple, its length increases, and the player's score is incremented.
3- Collision Detection:
The game checks if the snake's head:
Hits the boundaries of the grid.
Collides with any part of its own body.
If a collision is detected, the game ends.
4- Scorekeeping:
Players earn points for each apple consumed.
The score is displayed on the screen, and some versions track high scores.
The objective of a Snake Game project using an ESP32 microcontroller, joystick, LCD screen, and MicroPython is to:
1. Create a Functional Retro Game:
Implement a playable version of the classic Snake Game where the player controls a snake that moves around the screen, consumes food, and grows longer while avoiding collisions with itself and the screen boundaries.
2. Learn MicroPython Programming:
Utilize the MicroPython language to program the ESP32, demonstrating its ability to handle real-time interactive tasks.
Gain experience in controlling peripherals such as displays and input devices with MicroPython.
3. Interface Hardware Components:
Interface an LCD screen (e.g., OLED or TFT) to display the game graphics.
Use a joystick as the input device to control the direction of the snake.
Leverage the ESP32's GPIO pins for connecting and managing these peripherals effectively.
4. Explore Microcontroller Capabilities:
Understand and utilize the computational power of the ESP32 for real-time gaming.
Explore the graphics rendering capabilities on the LCD screen and real-time processing for game logic.
5. Enhance Embedded Systems Skills:
Gain hands-on experience with embedded systems design, including hardware interfacing, software development, and debugging.
Learn about real-time constraints and how to optimize performance in embedded applications.
6. Provide Entertainment and Educational Value:
Create an engaging game that is fun to play while demonstrating core principles of hardware and software interaction.
Use the project as a teaching tool for beginners or students learning about MicroPython, ESP32, and embedded systems development.
7. Customizability and Scalability:
Implement a modular code structure to allow for future upgrades, such as:
Adding new game features (e.g., obstacles, power-ups, or multiple levels).
Enhancing graphics and animations.
Exploring wireless connectivity (Wi-Fi or Bluetooth) for multiplayer functionality.
This project is perfect for learning-by-doing, providing a hands-on introduction to game development on microcontrollers, and can be showcased in academic projects, maker fairs, or personal portfolios.
ESP32 board
Acts as the main control unit for running the game logic and interfacing with peripherals.
Joystick Module
Used as the input device to control the direction of the snake.
LCD Screen (16x2 or 20x4)
Displays the game grid, score, and messages.
Jumper Wires
Jumper wires will be used to make connections between the components.
Breadboard:
A breadboard can be used to create a temporary circuit for testing and prototyping.
1- Joystick to ESP32 :
GND → GND
VCC → 3V3
X-axis (VRx pin ) → GPIO34
Y-axis (VRy pin) → GPIO35
Button (SW pin) → GPIO33
LCD Screen (I2C-based) to ESP32 :
SDA → GPIO21 (I2C Data).
SCL → GPIO22 (I2C Clock).
VCC → 5V
GND → GND
1- Ensure MicroPython is installed on your ESP32
2- Flash your ESP32 with MicroPython using this file esp32-20210902-v1.17.bin
3- Import this two libraries : i2c_lcd and lcd_api for I2C LCD screen
4- Initialize peripherals (LCD, joystick) and game variables (score, snake position, etc.).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=10000) #initializing the I2C method for ESP32 lcd_snake = I2cLcd(i2c, I2C_ADDR, totalRows, totalColumns) lcd_food = I2cLcd(i2c, I2C_ADDR, totalRows, totalColumns) xAxis = ADC(Pin(34, Pin.IN)) # créer un objet ADC agissant sur une broche xAxis.atten(xAxis.ATTN_11DB) yAxis = ADC(Pin(35, Pin.IN)) # créer un objet ADC agissant sur une broche yAxis.atten(yAxis.ATTN_11DB) button = Pin(33, Pin.IN, Pin.PULL_UP) # Snake coordonee x=0 y=0 snake='>' # Food coordonne x1=10 y1=0 |
5- Continuously read joystick input.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
xValue = xAxis.read() # read an analog value yValue = yAxis.read() # read an analog value if xValue<1000 and sens != 'down': sens='up' snake='^' if yValue<1000 and sens != 'left': sens='right' snake='>' if yValue==4095 and sens != 'right' : sens='left' snake='<' if xValue==4095 and sens != 'up': sens='down' snake='v' |
6- Update the snake’s position.
1 2 3 4 5 6 7 8 9 10 11 |
if sens == 'up' and y>0: y=y-1 if sens=='right' and x<15: x=x+1 if sens=='left' and x>0: x=x-1 if sens=='down' and y<1: y=y+1 |
7- Check for collisions or food consumption.
1 2 3 |
if x==x1 and y==y1 : x1=random.randrange(0, 15) y1=random.randrange(0, 1) |
8- Refresh the display with updated graphics.
1 2 3 4 5 6 7 |
lcd_food.move_to(x1,y1) lcd_food.putstr('o')# Display food on LCD screen lcd_snake.move_to(x,y) lcd_snake.putstr(snake) # Display snake on LCD screen sleep(0.5) lcd_snake.clear() lcd_food.clear() |
Here is the full program :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
from machine import Pin, SoftI2C,ADC from lcd_api import LcdApi from i2c_lcd import I2cLcd from time import sleep import random I2C_ADDR = 0x27 totalRows = 4 totalColumns = 20 i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=10000) #initializing the I2C method for ESP32 lcd_snake = I2cLcd(i2c, I2C_ADDR, totalRows, totalColumns) lcd_food = I2cLcd(i2c, I2C_ADDR, totalRows, totalColumns) xAxis = ADC(Pin(34, Pin.IN)) # créer un objet ADC agissant sur une broche xAxis.atten(xAxis.ATTN_11DB) yAxis = ADC(Pin(35, Pin.IN)) # créer un objet ADC agissant sur une broche yAxis.atten(yAxis.ATTN_11DB) button = Pin(33, Pin.IN, Pin.PULL_UP) # Snake coordonee x=0 y=0 snake='>' # Food coordonne x1=10 y1=0 sens='droite' while True: xValue = xAxis.read() yValue = yAxis.read() if xValue<1000 and sens != 'down': sens='up' snake='^' if yValue<1000 and sens != 'left': sens='right' snake='>' if yValue==4095 and sens != 'right' : sens='left' snake='<' if xValue==4095 and sens != 'up': sens='down' snake='v' if sens == 'up' and y>0: y=y-1 if sens=='right' and x<15: x=x+1 if sens=='left' and x>0: x=x-1 if sens=='up' and y<1: y=y+1 if x==x1 and y==y1 : x1=random.randrange(0, 15) y1=random.randrange(0, 1) lcd_food.move_to(x1,y1) lcd_food.putstr('o') lcd_snake.move_to(x,y) lcd_snake.putstr(snake) sleep(0.5) lcd_snake.clear() lcd_food.clear() |
This project combines programming and hardware interfacing skills to create a retro Snake Game. By completing this project, you gain hands-on experience with MicroPython, ESP32, and peripherals, while also creating a fun and interactive game that can be further expanded with additional features.
Educational robotics refers to the use of robots and robotics technology to promote learning in educational settings. It involves the integration of technology, engineering, and computer science into the classroom, allowing students to engage in hands-on, project-based learning experiences.
In this context, our website represents an excellent resource for parents, teachers and children who wish to discover robotics.
Zaouiet Kontech-Jemmel-Monastir-Tunisia
+216 92 886 231
medaliprof@gmail.com
Robotic site created by MedAli-Teacher info