A Creative Maker in Halloween and Elecfreaks Promotion Coming
January 7, 2019Flexible 64 RGB LED 8*8 Rainbow Gradient
January 8, 2019Introduction:
Remember the snake game we played on our mobile or game box during our childhood? Today we are going to learn how to use Flexible Rainbow 64 RGB 8*8 LED Matrix to make a snake game. We use Joystick breakout module to control it.
Components List: Hardware:
1 X Freaduino UNO Rev2.2 MB_EFUNO
1 X Flexible 64 RGB 8*8 Rainbow LED Matrix
1 X Joystick breakout module BKOUT_MD01
3 X Jumper Cable
Software:
Arduino IDE
Hardware Connection
Connect Flexible 64 RGB 8*8 Rainbow LED Matrix to port
D2. Joystick breakout module: x connect A0 y connect A1 k connect A2
Programming
The code we are going to make includes the following sections:
-
Generate food;
-
Time Delay;
-
Read Joystick Value;
-
Gameover, display the score;
-
Display start-up picture;
-
Snake Movement;
-
Calculate the score. Every 5 food eaten will increase snake speed for one time. But we have to limit the maximum speed.
Section 1: Generate Food
We use Random Number Function to produce two random numbers for X axle and Y axleof the food separately. We have to judge whether the axleof food and the axleof snake are the same. If it is the same, we need to regenerate food.
Code Example:
[cceN_cpp theme=”dawn”] A:
FX = random(0, 7); FY = random(0, 7); for (int i = 0; i < 3 + socre; i++) { if ( FX == sx[i] && FY == sy[i]) goto A; } [/cceN_cpp]
Section 2: Read Joystick Value
Set A0-A2 to be input mode and read its voltage. The middle value is around 510. We have to judge the value and assign it with relative movement.
Code Example:
[cceN_cpp theme=”dawn”] xValue = analogRead(A0);
yValue = analogRead(A1); if (yValue > 950 && KEY != 5) { K = 8; } [/cceN_cpp]
Section 3: Snake Movement
Firstly, we have to judge which direction the snake moves. Snake move is the axledata shift. That means the second section axleof snake equals the axleof snake head and the third equals the second. According to this rule, we assign the snake head new value. We have to pay attention that the snake body surpassed the screen frame will appear in the opposite frame.
Code Example:
[cceN_cpp theme=”dawn”] if (KEY == 8) // Snake upward
{ for (int i = 2 + socre; i > 0; i–) { sx[i] = sx[i – 1]; sy[i] = sy[i – 1]; } sy[0] = sy[0] – 1; if (sy[0] < 0) // The part of snake body surpassed screen frame will appear in the opposite frame. sy[0] = 7; } [/cceN_cpp]
Section 4: Judge Food Eaten
We only have to judge whether the axleof snake head equals the axleof food. In other words, the food was eaten.Then we have to generate food axleand display it. At the same time, we have to judge whether the score of the game is a multiple of 5. If it is, then increase speed for one time and decrease delay time.
Code Example:
[cceN_cpp theme=”dawn”] if (SY == FY && SX == FX)
{ RANDOM(); socre ++; Colour[0] = 40; // Colour[1] = 0; Colour[2] = 0; c = strip.Color(Colour[1], Colour[0], Colour[2]); x = light[FX][FY]; //Display Food strip.setPixelColor(x, c); strip.show();//Send data if ( !(socre % 5)) { //Increase snake speed according to the score. Every 5 food eaten, increase snake speed by 100ms. speedS = speedS – 50; if (speedS < 150) // The minimum speed is 200ms. If snake speed lower than 200ms, the speed remains unchanged. speedS = 150; } } [/cceN_cpp]
Section 5: Game Over
When snake head touches snake body, game is over. And we get the score by judging whether the axle of snake head equals to the axle of snake body.
Code Example:
[cceN_cpp theme=”dawn”] for (int i = 1; i <= 2 + socre; i++)
{ if ( SX == sx[i] && SY == sy[i]) gameover(); } [/cceN_cpp]
Relative Readings:
Light the First Bead on 8*8 Rainbow Matrix with Arduino Get to Know WS2812b 8*8 Rainbow Matrix Pattern Display 8*8 Rainbow Matrix Scrolling Caption