diff --git a/snake.ino b/snake.ino index fa0b0ff..1bed8c5 100644 --- a/snake.ino +++ b/snake.ino @@ -1,4 +1,5 @@ #define BOARD_LENGTH 8 +#define SNAKE_MAX_SIZE 12 struct pair { int x; @@ -32,7 +33,7 @@ public: } //else ignore } - void eat(pair p); + void grow(); void snake_to_matrix(int (*pixels)[BOARD_LENGTH]){ for(int i = 0; i < BOARD_LENGTH; i++){ for(int j = 0; j < BOARD_LENGTH; j++){ @@ -80,7 +81,12 @@ void Snake::next_move() { 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}; @@ -114,15 +120,18 @@ void setup() { pixels[x][y] = HIGH; } } + randomSeed(analogRead(0)); } void loop() { - snake.change_state(read_sensors()); + if(random(50) == 0) snake.grow(); snake.snake_to_matrix(pixels); snake.next_move(); // draw the screen: - for(int i = 0; i < 100; i++) + for(int i = 0; i < 30; i++) { Display(); + snake.change_state(read_sensors()); + } } void readSensors() {