Move snake with frontiers

This commit is contained in:
Diogo Cordeiro 2020-02-12 17:36:02 +00:00
parent 7375e8baca
commit ace698b10e

View File

@ -55,36 +55,85 @@ void setup() {
void loop() { void loop() {
delay(300); 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: // read input:
// up: 1 switch(dir)
// right: 2
// down: 3
// left: 4
switch(read_sensors())
{ {
case 0: // iddle case 0: // iddle
// Just don't // Just don't
break; break;
case 1: // up case 1: // up
// Move // Move
pixels[snake_x][++snake_y] = LOW; if (cur_dir != 3) {
pixels[snake_x][++snake_y] = LOW;
cur_dir = 1;
return true;
}
break; break;
case 2: // right case 2: // right
// Move // Move
pixels[++snake_x][snake_y] = LOW; if (cur_dir != 4) {
pixels[++snake_x][snake_y] = LOW;
cur_dir = 2;
return true;
}
break; break;
case 3: // down case 3: // down
// Move // Move
pixels[snake_x][--snake_y] = LOW; if (cur_dir != 1) {
pixels[snake_x][--snake_y] = LOW;
cur_dir = 3;
return true;
}
break; break;
case 4: // left case 4: // left
// Move // Move
pixels[--snake_x][snake_y] = LOW; if (cur_dir != 2) {
pixels[--snake_x][snake_y] = LOW;
cur_dir = 4;
return true;
}
break; break;
} }
return false;
// draw the screen:
refresh_screen();
} }
// iddle: 0 // iddle: 0