Introduction: Arduino Snake LED Game
In a previous project we created an interactive coffee table, which lit up leds under an object. In this project, we reuse that table to create a snake game from the 2D matrix of LEDs. If some parts of the project are unclear, it may be addressed in the previous project, so I recommend reviewing that for more information.
Supplies
1. ws2811 LED Bulbs - https://amzn.to/3uuYKrr
2. 5V Power Supply - https://amzn.to/2PGh6qq
3. Arduino I used the 2560 - https://amzn.to/39Kj7ci
4. Analog Joy Stick - https://amzn.to/2PDUzun
Step 1: Create the LED Matrix
This is the crux of the entire thing and the size of the matrix will not matter. We used a 8x12 led matrix, but you could make any size you prefer. To get started we took a 1/8 inch piece of paper composite board and drew a diagram out of where we wanted to drill a point for each bulb. We spaced each led about 3 inches apart and drilled a 1/2 inch hole to squeeze the bulb in. After stringing up the leds, we used hot glue to fix them in place.
You can put this matrix up anywhere but we decided to house the board and electronics into a coffee table to make it easier to handle, and more pleasant to view. You can use whatever you'd like.
Step 2: Create the Circuit
At a high level we have a matrix of ws2811 LEDs powered by an external 5V power supply. They were arranged in a serpentine pattern and form a matrix of leds 8 x 12. This game will work with any size matrix with minor adjustments to the code. We use an arduino to control the lights in the matrix. See the circuit diagram and material list for more information on the exact parts and wiring.
The only thing different about this setup from the previous project is we added a joy stick which will be used to direct the snake in the game. The analog stick is a basic analog stick that actually comes with an arduino starter pack, but you can also get it from Amazon. Refer to the circuit diagram for more information on how to wire up the circuit. Luckily the analog stick only requires 3 inputs for the ardunio, 2 analog inputs to read x and y direction of the stick, and a digital input to tell if the analog stick is pressed in. All of the wiring and power supply was hidden within the coffee table.
Step 3: Install the Code
You can find the code in the following repo: https://github.com/tmckay1/snake_game
Make sure to install any dependencies mentioned in the README, for now you will need the LinkedList Module
The code is pretty self explanatory and has a lot of comments, but I will explain a bit about it here. We have an object Point that we use to represent a point on the 2D matrix of leds. We initialize the location of the apple with a point and the snake with a point. The snake is a linked list of points. The point of the game is to move the snake to the apple and grow the snake. When the head touches the apple we generate a random point for the apple to spawn again and grow the linked list that represents the snake. If the head of the snake touches the tail or goes out of bounds, then you will have to start over. You can pause the game by clicking into the joystick. I have included a visual here of what it looks like when the game is paused (notice the change in color).4
The only adjustments you may have to make is the section of code that renders the direction to travel. Depending on the orientation of your joystick, it may be necessary to change the direction the snake moves, as it may be different than the orientation of our joysticks.
Step 4: Upload the Code and Enjoy!
After you have created the led matrix, the circuit, and modified the code to suit your specific setup, you can upload the code to the arduino. Upon uploading the game will start automatically with the snake in the bottom left corner as a red dot traveling to the right. An apple will randomly be generated and you can start moving the snake around with the analog stick. When the game resets it will again start in the bottom left corner except a flashing animation will trigger to indicate the game is over. See an image I attached as a reference
17 Comments
Question 9 months ago
How to I upgrade the stick resistive controller to a microswitch joystick?
Question 2 years ago
i need you help!!!!
Answer 2 years ago
Sorry for the late reply, just saw this. I remember I had issues with the latest version of linked list so I went to the github and manually installed version 1.2.3 in the Arduino IDE. Give that a shot
2 years ago
i wish i can play that snake game with my friend... upgrade to 2 player snake ?
Reply 2 years ago
That would be dope
2 years ago
I recreated it and it works fine.
But sometimes I have the problem that the direction is not correctly recognized by the joystick.
Do you have any idea what might be the cause?
Many Thanks
Reply 2 years ago
Do you mean the orientation of the screen is different so left goes to right and right goes to left (or similar)? To fix that just change the code to calibrate the right direction when the joystick moves a certain way.
Perhaps what you mean, I noticed the joystick is a little finicky as well, so when I push down sometimes it goes a different direction. Noticed that this is because the joystick is just cheap and the value the arduino gets from reading the direction flips up one direction quickly, then down, so it doesn't go down if it were to go up first. Didn't write code to handle that spike in wrong input from the analog stick, but you could change the code to only change direction if it is pointed in the same direction for 2 or 3 cycles or something like that. Just be aware that you don't want to slow it down so much where the game is not as reactive
Reply 2 years ago
I think I'll try to adapt the code so that I have to hold the joystick longer.
If that doesn't work, I would use push button´s instead of the joystick.
Reply 2 years ago
If everything works then I dare to implement more games.
Tetris would be cool.
Reply 2 years ago
Yes Tetris would be awesome. I think we'll make that game in a creative way in the future, I love tetris
Question 2 years ago
what s this?y
Answer 2 years ago
that is a diagram indicator for the ws2812b/ws2811 leds
Reply 2 years ago
thank you.
Tip 2 years ago
i want to make it? thank you
Reply 2 years ago
Answered below
2 years ago
great! the modern version of and old game!
Reply 2 years ago
:)