Introduction: Snake Game From Magicbit
In this tutorial you will learn about haw to make a simple game using Magicbit dev board without using any external display.
- USB-A to Micro-USB Cable
Step 1: Story
I, am sure all you known about snake game. This game is very popular in 19' s and in early 20's. However there are many Nokia mobile phones have this game. So I am sure most of you heard about this game. In this tutorial we will learn about how to build that game using Magicbit and Arduino IDE with very simple code. So lets get start.
Step 2: Theory and Methodology
For this project we only use Magicbit. First plug that into your computer using micro USB cable.
Lets discuss what strategy. This can describe easily using Arduino code. First we thought how this snake was built. Actually this snake was built using small rectangular blocks. So at the start we have three blocks. When snake eat the food then that food will add to snake as a new block and the length of the snake will increase by one block.
In the code we declared two arrays for store X and X coordinates of the blocks of the snake. When you win the game snake had 20 blocks. Therefor our array size is 20. Because we only have 3 blocks we hard coded what is the X and Y coordinates for that block. So that is the where your snake is start to moving.
To move the snake we do very simple thing. At the first step we find what's it the direction of the snake by comparing X and y coordinates of the first and second blocks. As a example If first X and second X are equal while first Y is grater than second Y we can predict the snake orientation as bottom side. After predict that we decide next coordinates head of the snake should move to. Then we shift first X and Y indexes to second X and Y indexes in array and and do the same thing ever blocks. We ignore last X and Y coordinates. Then we add predicted next X and X values to first x and y coordinates. If the snake head's coordinate is out from the display then we defined where is the head should be in.
When the snake is moving on the screen we always checking the coordinates of the snake head and foods are equal. If they are equal we know this is the time for eat food and increase the length. At that time we shift all coordinates of the arrays by one index and append food coordinates to the first index of array. And also we count how many foods are snake is eating. That will use to generate player's score.
To generate food location we use random function in available in Arduino. When the snake ate the food we have to generate new location to food while generating that we make sure the location of the food in not in the snake body's coordinate.
All things which described in above is done by using Adafruit OLED graphics library. This is already included to Magicbit board library. After deciding all locations and coordinates we draw filled rectangles for snake blocks and filled circles for the food. By studying following tutorial you can learn about more, how to use Adafruit GFX library.
In additionally we used buzzer to generate wining sound, lost sound and food eating sound. To do that we used ESP32 servo library with tone function. Using tone function we can generate many sounds for certain time period.
To control the snake movements we use two inbuilt push buttons pf the Magicbit. To recognize triggering point(button is pressed), we used hardware interrupts. By using following link you can refer how hardware interrupts are working in ESP32.
When we push the button it will generate one or more triggering pulses. Because the physical contact of the button. So to prevent that noise we use debounce method. In this debounce method we calculate time period between first triggering point and the next triggering points. If that is smaller than some defined period we ignored that triggering. For that purpose we used millis function. That is one of inbuilt timer of the ESP32.
To create game wallpaper we used bitmap draw function of Adafriuit GFX library. At the first we resize our image to 128x64 and then convert it using image to bitmap converter software. Then we show our wallpaper by using bitmap function.
You can change moving speed of the snake by changing the delay in the loop.
So that is it. Now select correct com port and board type to Magicbit and upload the code. You can add more games and more update versions of the snake game by thinking the strategy behind game and OLED graphics.