From ace698b10e41ecf77e8155e72a8954f9ddacacc4 Mon Sep 17 00:00:00 2001 From: Diogo Cordeiro Date: Wed, 12 Feb 2020 17:36:02 +0000 Subject: [PATCH] Move snake with frontiers --- snake.ino | 73 ++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 61 insertions(+), 12 deletions(-) diff --git a/snake.ino b/snake.ino index 5af5485..34a4c23 100644 --- a/snake.ino +++ b/snake.ino @@ -55,36 +55,85 @@ void setup() { void loop() { delay(300); + if (!move_snake(read_sensors())) { + switch(cur_dir) + { + case 0: // iddle + // Just don't + break; + case 1: // up + // Move + if (snake_y+1 <= 7) { + pixels[snake_x][++snake_y] = LOW; + } + break; + case 2: // right + // Move + if (snake_x+1 <= 7) { + pixels[++snake_x][snake_y] = LOW; + } + break; + case 3: // down + // Move + if (snake_y-1 >= 0) { + pixels[snake_x][--snake_y] = LOW; + } + break; + case 4: // left + // Move + if (snake_x-1 >= 0) { + pixels[--snake_x][snake_y] = LOW; + } + break; + } + } + + // draw the screen: + refresh_screen(); +} + +// true if it has moved, false otherwise +bool move_snake(int dir) { // read input: - // up: 1 - // right: 2 - // down: 3 - // left: 4 - switch(read_sensors()) + switch(dir) { case 0: // iddle // Just don't break; case 1: // up // Move - pixels[snake_x][++snake_y] = LOW; + if (cur_dir != 3) { + pixels[snake_x][++snake_y] = LOW; + cur_dir = 1; + return true; + } break; case 2: // right // Move - pixels[++snake_x][snake_y] = LOW; + if (cur_dir != 4) { + pixels[++snake_x][snake_y] = LOW; + cur_dir = 2; + return true; + } break; case 3: // down // Move - pixels[snake_x][--snake_y] = LOW; + if (cur_dir != 1) { + pixels[snake_x][--snake_y] = LOW; + cur_dir = 3; + return true; + } break; case 4: // left // Move - pixels[--snake_x][snake_y] = LOW; + if (cur_dir != 2) { + pixels[--snake_x][snake_y] = LOW; + cur_dir = 4; + return true; + } break; } - - // draw the screen: - refresh_screen(); + return false; } // iddle: 0