code cleanups
This commit is contained in:
@@ -1,5 +1,10 @@
|
||||
#include "Arduino_LED_Matrix.h"
|
||||
|
||||
#define BTNUP 13
|
||||
#define BTNRIGHT 12
|
||||
#define BTNBOTTOM 11
|
||||
#define BTNLEFT 10
|
||||
|
||||
// create LED matrix object
|
||||
ArduinoLEDMatrix matrix;
|
||||
|
||||
@@ -27,6 +32,7 @@ int snake_y[96]= {3, 3, 3, 3};
|
||||
int snake_len= 4;
|
||||
|
||||
char direction= 'r';
|
||||
// used to avoid a double change direction before LED turns on
|
||||
char new_direction= 'r';
|
||||
|
||||
int points= 0;
|
||||
@@ -38,49 +44,20 @@ int apple_y= 4;
|
||||
int loop_delay= 300;
|
||||
long exec_t2= millis();
|
||||
|
||||
|
||||
void setup() {
|
||||
//Serial.begin(115200);
|
||||
Serial.begin(9600);
|
||||
// stard LED matrix
|
||||
matrix.begin();
|
||||
// btn up
|
||||
pinMode(13, INPUT_PULLUP);
|
||||
// btn right
|
||||
pinMode(12, INPUT_PULLUP);
|
||||
// btn bottom
|
||||
pinMode(11, INPUT_PULLUP);
|
||||
// btn left
|
||||
pinMode(10, INPUT_PULLUP);
|
||||
pinMode(BTNUP, INPUT_PULLUP);
|
||||
pinMode(BTNRIGHT, INPUT_PULLUP);
|
||||
pinMode(BTNBOTTOM, INPUT_PULLUP);
|
||||
pinMode(BTNLEFT, INPUT_PULLUP);
|
||||
|
||||
randomSeed(analogRead(0));
|
||||
}
|
||||
|
||||
void add_tail_block() {
|
||||
snake_len++;
|
||||
snake_y[snake_len-1]= snake_y[snake_len-2];
|
||||
snake_x[snake_len-1]= snake_x[snake_len-2];
|
||||
}
|
||||
|
||||
void show_apple() {
|
||||
frame[apple_y][apple_x]= 1;
|
||||
}
|
||||
|
||||
void eat_apple() {
|
||||
points++;
|
||||
Serial.println(points);
|
||||
// increase difficult each 5 apples
|
||||
if (points % 5 == 0) loop_delay-= 50;
|
||||
|
||||
// generate new apple coordinates
|
||||
frame[apple_y][apple_x]= 0;
|
||||
apple_x= random(11);
|
||||
apple_y= random(7);
|
||||
show_apple();
|
||||
|
||||
// increase snake length
|
||||
add_tail_block();
|
||||
}
|
||||
|
||||
void game_over() {
|
||||
// show gameover frame
|
||||
matrix.loadFrame(frame_game_over);
|
||||
@@ -107,15 +84,39 @@ void game_over() {
|
||||
while (true) delay(10000);
|
||||
}
|
||||
|
||||
void move_snake() {
|
||||
int i=0;
|
||||
|
||||
void add_tail_block() {
|
||||
snake_len++;
|
||||
snake_y[snake_len-1]= snake_y[snake_len-2];
|
||||
snake_x[snake_len-1]= snake_x[snake_len-2];
|
||||
}
|
||||
|
||||
void show_apple() {
|
||||
frame[apple_y][apple_x]= 1;
|
||||
}
|
||||
|
||||
void eat_apple() {
|
||||
points++;
|
||||
Serial.println(points);
|
||||
// increase difficult each 5 apples
|
||||
if (points % 5 == 0) loop_delay= max(50, loop_delay-=50);
|
||||
|
||||
// generate new apple coordinates
|
||||
frame[apple_y][apple_x]= 0;
|
||||
apple_x= random(11);
|
||||
apple_y= random(7);
|
||||
|
||||
// increase snake length
|
||||
add_tail_block();
|
||||
}
|
||||
|
||||
void move_snake() {
|
||||
int y= snake_y[snake_len-1];
|
||||
int x= snake_x[snake_len-1];
|
||||
frame[y][x]= 0;
|
||||
|
||||
// move and render the snake tail
|
||||
for (i= snake_len-1; i > 0; i--) {
|
||||
for (int i= snake_len-1; i > 0; i--) {
|
||||
frame[snake_y[i-1]][snake_x[i-1]]= 1;
|
||||
if (snake_x[0] == snake_x[i] && snake_y[0] == snake_y[i])
|
||||
game_over();
|
||||
@@ -145,21 +146,18 @@ void move_snake() {
|
||||
snake_y[0]-= 1;
|
||||
if (snake_y[0] < 0) snake_y[0]= 7;
|
||||
}
|
||||
if (snake_y[0] == apple_y && snake_x[0] == apple_x) eat_apple();
|
||||
if (snake_y[0] == apple_y && snake_x[0] == apple_x)
|
||||
eat_apple();
|
||||
|
||||
// render the snake head
|
||||
frame[snake_y[0]][snake_x[0]]= 1;
|
||||
}
|
||||
|
||||
void change_direction() {
|
||||
// up
|
||||
int u= digitalRead(13);
|
||||
// right
|
||||
int r= digitalRead(12);
|
||||
// bottom
|
||||
int b= digitalRead(11);
|
||||
// left
|
||||
int l= digitalRead(10);
|
||||
int u= digitalRead(BTNUP);
|
||||
int r= digitalRead(BTNRIGHT);
|
||||
int b= digitalRead(BTNBOTTOM);
|
||||
int l= digitalRead(BTNLEFT);
|
||||
|
||||
if (u == LOW && direction != 'b') new_direction= 'u';
|
||||
else if (r == LOW && direction != 'l') new_direction= 'r';
|
||||
|
||||
Reference in New Issue
Block a user