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() {
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