8*8 Rainbow LED Matrix Snake Game

标签:DIY

LUCY 发布于 2018-01-20 12:16

fp.jpg


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.




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



Connect Flexible 64 RGB 8*8 Rainbow LED Matrix to port 







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.


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]


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]


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]


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]


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]



 

 



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



在淘宝中搜索“恩孚电子”,你可以以最优惠的价格购买到英国原装正版micro:bit哦!

如果想联系我们,请发邮件至:louise@elecfreaks.com 。

 

作者

LUCY

广东,深圳

3粉丝 62作品 8随笔
  关注 私信

作者的最新作品


阅读数: 383