snake grows at random.lol

This commit is contained in:
Pedro Costa 2020-02-12 22:50:57 +00:00
parent fc71a279d5
commit 347ece1f7d

View File

@ -1,4 +1,5 @@
#define BOARD_LENGTH 8 #define BOARD_LENGTH 8
#define SNAKE_MAX_SIZE 12
struct pair { struct pair {
int x; int x;
@ -32,7 +33,7 @@ public:
} }
//else ignore //else ignore
} }
void eat(pair p); void grow();
void snake_to_matrix(int (*pixels)[BOARD_LENGTH]){ void snake_to_matrix(int (*pixels)[BOARD_LENGTH]){
for(int i = 0; i < BOARD_LENGTH; i++){ for(int i = 0; i < BOARD_LENGTH; i++){
for(int j = 0; j < BOARD_LENGTH; j++){ for(int j = 0; j < BOARD_LENGTH; j++){
@ -80,7 +81,12 @@ void Snake::next_move() {
pos[0] = get_next_move(pos[0]); pos[0] = get_next_move(pos[0]);
} }
void Snake::eat(pair p) { pos[snake_size++] = p; } void Snake::grow() {
if (snake_size < SNAKE_MAX_SIZE) {
pos[snake_size] = pos[snake_size - 1];
++snake_size;
}
}
const int row[8] = {2, 7, 19, 5, 13, 18, 12, 16}; const int row[8] = {2, 7, 19, 5, 13, 18, 12, 16};
@ -114,15 +120,18 @@ void setup() {
pixels[x][y] = HIGH; pixels[x][y] = HIGH;
} }
} }
randomSeed(analogRead(0));
} }
void loop() { void loop() {
snake.change_state(read_sensors()); if(random(50) == 0) snake.grow();
snake.snake_to_matrix(pixels); snake.snake_to_matrix(pixels);
snake.next_move(); snake.next_move();
// draw the screen: // draw the screen:
for(int i = 0; i < 100; i++) for(int i = 0; i < 30; i++) {
Display(); Display();
snake.change_state(read_sensors());
}
} }
void readSensors() { void readSensors() {