Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [here] and join today!
GAME - Snake
This example implements the classic Snake game on an 8x12 LED matrix using the R4 Wifi board. Players control the snake’s direction using a dual-axis joystick.
Required Components
In this project, we need the following components.
It’s definitely convenient to buy a whole kit, here’s the link:
Name |
ITEMS IN THIS KIT |
LINK |
---|---|---|
Elite Explorer Kit |
300+ |
You can also buy them separately from the links below.
COMPONENT INTRODUCTION |
PURCHASE LINK |
---|---|
- |
|
Wiring
Schematic
Code
Note
You can open the file
13_snake.ino
under the path ofelite-explorer-kit-main\fun_project\13_snake
directly.Or copy this code into Arduino IDE.
How it works?
Here’s a detailed explanation of the code:
Variable Definition and Initialization
Import the
Arduino_LED_Matrix
library for LED matrix operations. matrix is an instance of the LED matrix.frame
andflatFrame
are arrays used to store and process pixel information on the screen. The snake is represented as an array ofPoint
structures, where each point has an x and y coordinate. food represents the position of the food.direction
is the current movement direction of the snake.setup()
Initialize the X and Y axes of the joystick as inputs. Start the LED matrix. Initialize the snake’s starting position in the center of the screen. Generate the initial position of the food randomly.
loop()
Determine the snake’s direction based on the readings from the joystick. Move the snake. Check if the snake’s head collides with the food. If it does, the snake grows, and new food is generated at a new location. Check if the snake collides with itself. If it does, reset the game. Draw the current game state (snake and food positions) on the LED matrix. Add a delay to control the game’s speed.
moveSnake()
Move each part of the snake to the position of the previous part, starting from the tail and moving to the head. Move the snake’s head based on its direction.
generateFood()
Generate all possible food positions. Check if each position overlaps with any part of the snake. If it doesn’t overlap, the position is considered a possible food location. Randomly select a possible food location.
drawFrame()
Clear the current frame. Draw the snake and food on the frame. Flatten the two-dimensional frame array into a one-dimensional array (flatFrame) and load it onto the LED matrix.