Introduction: Portable Snake
This is a tutorial on how to make your own portable snake game! All you need is an arduino, some electronics and some way to case the whole thing. Here is a list of all the needs:
- Arduino uno (1)
- Joystick module (1)
- Led Matrix (1)
- some wires (10 male to female and 2 male to male)
- batteries (to make it fully portable) (7-12V recommended)
- breadboard (for prototyping)
- materials to make a case (you can make a case in various ways).
Step 1: Step 1: Prototyping
Start with connecting your joystick and LED matrix to the arduino. The wiring schema is shown above, but here's a written tutorial anyway:
First you connect the 5v-pin at the arduino to a place on the breadboard, we'll call everything that connects to this point the volt-line. Then you connect a ground-pin to another place on the breadboard that doesn't connect to the volt-line, we'll call this the ground-line.
Now you take your LED matrix and connect the VCC-pin to the volt-line and the GND-pin to the ground-line. After this you can connect the DIN-, CS- and CLK-pin accordingly to the 13-, 12- and 11-pin on your arduino. Your LED matrix is operational now.
Finally you take your joystick module and connect the GND-pin to the ground-line and the +5V-pin to the volt-line. Then connect the VRx- and VRy-pin to analogpins 0 and 1 on your arduino (A0 and A1) and connect the SW-pin to the 2-pin.
Optionally, not very optionally if you want to make this fully portable, you can add some batteries (7-12V recommended, for example a 9V battery with a 9V battery snap connector). You can simply connect the + end of your battery to Arduino Vin and the - end to Arduino ground (fig 1). You can add a toggle switch between the battery ad the Vin-pin to easily toggle the Arduino on or of.
Your prototype is set up now! After you've done the coding (so you know everything works) you can replace the breadboard with a soldering plate to make it less likely to fall apart.
Step 2: Step 2: Coding
The coding of this project consists of 2 parts. In the first part we need to make sure that we can actually use the ledmatrix. I did not do this myself since this is some next level coding and I'm just an intermediate at best. If you want to play around with this, Instructable and arduino have some amazing tutorials on how to do this. I used this ones coding for the basis of my snake-game:
After following this tutorial, you can step right into making your snake game. If you don't want to wright all the codes yourself, you can download mine above. Just make sure your pins are set on the right ones. Here is a small tutorial on how to create the code:
I first copied the code MakeSpace_LEDMatrix from the tutorial. If you downloaded the zip-file from the tutorial you can find it in the examples. I removed all coding that drew something on the matrix because we're going to do that ourselves.
You can make a few variables:
- x and y position for the food.
- an array of x and y postions for the snake's body
- a direction variable
- a snake length variable
- a score variable (you could make this dependent on the length)
- a pause boolean
In the setup, start with drawing the food and the snake to the matrix and add a delay. Then go to the loop. First make sure the loop only runs when the game isn't paused and unpause the game whenever the joystick is pressed (SW-pin/pin-2). Make sure the snake's body follows the head by replacing the x and y position of the last bodypart by the x and y position of the bodypart that comes next. This can be easily done by using a for-loop.
Now you can make the head move towards the direction its going. When that's done, change the direction of the snake's head whenever the joystick is pressed in the concerning direction. Keep in mind that you don't have to be able to change the direction into the direction it's already going and the snake can't make a U-turn. Now make sure that whenever the snake leaves the matrix (-1 or 8) it returns on the other side of the matrix instead. Do this on both the x and y axis.
Whenever the snake's head reaches the coördinates of the food, add 1 to the length of the snake (which should spawn another bodypart) and give the food a new, random position on the matrix. At the end of the loop, draw the snake parts to the matrix and set a delay.
Finally we want to make a gameover screen. In your loop, make a for-loop which checks for every bodypart if it collides with the head. When it does, make it enter a new void called something like GameOver. Here you can write gameover to the matrix using the codes given in the matrix-tutorial, then you can draw the score and after that reset the game. Keep in mind that resetting the game will be achieved when you reset all starter variables.
Step 3: Step 3: Boxing
You can make a box in various ways. As I said, it's recommended to first solder the wiring together before putting everything in a box.
I wanted to make the box out of wood but due to a lack of time I made it out of cardboard, styrofoam, glue and colored paper. I first made a box out of cardboard by cutting and folding it. In this box I put my wiring, batteries and my arduino. The joystick and matrix were placed on top of the box, with wiring going into the box. After that I took some styrofoam to cover everything but the joystick and the matrix. I wrapped the whole thing in green paper, glueing it tight. Finally I got some decoration in the form of red stripes and blue letters.
And you're done! Now you have a portable snake game to take werever you want to go. You ain't sh*t Nintendo.