Make Your Own Microduino Snake Game



  • Project Description:
    As we all know, Snake is a popular video game. But can we use Microduino to implement it? The answer is obviously yes. It actually turns out that everyone can easily design their own version of Snake game on a 8x8 LED Matrix using Microduino very easily.

    The material needed for this project:
    ●A 8x8 LED matrix
    ●Microduino-Cube-S1 extension board
    ●Microduino-Core USB module
    ●Microduino-Sensor Hub (optional)
    ●Microduino-IR Receiver (optional)
    ●Microduino-Remote Control (optional)

    Step 1: Build the circuit
    ●Stack all the modules needed.
    ●If you want to use Remote Control:
    ----▶Remember to stack the Microduino Sensor Hub module on the top of the design.
    ----▶Connect the Microduino IR receiver module on pin 6/7 on the Sensor Hub module.

    Step 2: Write the code
    ●You can download the code in the attachment.0_1462352726184_snake.zip
    ●The code allows two ways to control the game: use the keyboard, or remote control.

    0_1462352580873_setup.png
    ●“randomSeed(analogRead(0))”
    ----▶Since the computer is pseudo-random, every time you reupload and then restart the game, the food will appear at locations in the same sequence. So, to avoid this situation happened, we use the randomSeed() function.
    ----▶randomSeed() is a function to decide the starting seed of the random number sequence.
    ----▶Since the analog input pin 0 is unconnected, analogRead(0) is a random analog noise that will let the randomSeed() to generate different seed numbers each run.
    ●“snake[...][...]=...”
    ----▶In the example code, we hard code the starting location for the snake of length 3. You are free to change the starting location and set the head of the snake.
    ----▶Inside of the “for loop”, we set the whole body of the snake to be green. But you can also choose to let the snake head has different color to make it become distinguishable with other parts of the snake body. If you chose to do so, then you also need to change your code slightly each time when you trying to update the location of the snake inside the loop().

    0_1462352934345_randfood.png
    ●“randFood()” function is used to generate a random food location.
    ----▶“random(0,255)%8”-------- we want to generate a random number from 0 to 7 for the X coordinate of the food. It’s actually equivalent to “random(0,7).”

    0_1462352973537_loop.png
    ●If you want to use keyboard to control the snake, then it falls into the case when “Serial.available()>0”
    ----▶In this case, we use “W”,”A”,”S”,”D” on the keyboard to represent “up”,”left”,”down”,”right”.
    ----▶You need to open the Serial Monitor when running the program and enter the key followed by “Enter” key.
    ----▶There’s an alternative way to use KeyListener, not shown in the sample code, which the program will directly check the key we pressed. (This is a fun part for you to explore.)
    ●If you want to use Remote Control to control the snake, then it falls into the case when “irrecv.decode(&results)”, which we can decode the IR signal received by the IR receiver.
    ----▶You need to change the hard coded decode value for the result.
    ---------▷Uncomment “Serial.println(results.value,HEX);”
    ---------▷Run the program and open the Serial Monitor
    ---------▷Press the 4 buttons, on the remote control, you want to use to control the snake. (UP,LEFT,DOWN,RIGHT)
    ---------▷Remember the value printed on the Serial Monitor, corresponding to each button.
    ---------▷Change the 4 values in the if statement comparing the result value.
    --------------○I.e. replace “results.value == 0xFF02FD” to the new HEX value you got when you pressed the button letting the snake move up.
    ---------▷Then comment back “Serial.println(results.value,HEX);”
    ---------▷Re-run the program. (You can control the snack with your remote control now.)

    0_1462353266297_loop2.png
    ●When “direction !=0”, the game starts, so you need to start updating the snack location.
    ●Here we set the condition changing length of the snake to be “num%3==0” , you can change the number to set the hardness of the game.
    ●So does the condition changing speed of the snake: “num%5==0”.
    ●We check if “speed>4” at the end of each iteration to set the delay because we need a upper bound for the speed of the snake. Base on the equation for delay wrote in this sample, “800-100*Speed”, the snake moves too fast for user to react. You can change the delay equation if you want.

    0_1462353314575_gameover.png
    ●The gameover() function prints the score user got in the Serial Monitor and displays the “END” animation.
    ●You can slightly change the code to let it display the score user got after display the “END” logo.
    ●After the “END” display, the program reset the location of the snake and food. (i.e. restart the game.)

    0_1462353337880_updatesnake.png
    ●The updatesnake() function updates the location of the snake stored in the 2d array.

    0_1462353354536_helperfunction.png
    ●Some useful helper function to help clear the screen, display the image, or set the color for the specific location on the LED matrix.

    After going through all the steps above, I believe you have already got enough knowledge to modify the program to make a new version of snake. Go ahead and make your own snake game now :)



  • the links don´t work :(


Log in to reply