• UID1571
  • 登录2018-05-18
  • 粉丝4
  • 发帖134
  • 科研点数0点
zt07elecfreaks 发布于2018-01-12 10:54

8*8 Rainbow LED Matrix Snake Game



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:


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


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


The code we are going to make includes the following sections:

  1. Generate food;

  2. Time Delay;

  3. Read Joystick Value;

  4. Gameover, display the score;

  5. Display start-up picture;

  6. Snake Movement;

  7. 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"]

FX = random(0, 7);
FY = random(0, 7);
for (int i = 0; i < 3 + socre; i++)
if ( FX == sx && FY == sy)
goto A;

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;

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 = sx[i - 1];
sy = 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;

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)

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;

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 && SY == sy)

Experiment Result:


 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