fix change direction before render

This commit is contained in:
andrea
2023-11-05 16:15:59 +01:00
parent 612b093f34
commit b3910f7725

View File

@@ -27,6 +27,7 @@ int snake_y[96]= {3, 3, 3, 3};
int snake_len= 4; int snake_len= 4;
char direction= 'r'; char direction= 'r';
char new_direction= 'r';
int points= 0; int points= 0;
@@ -160,10 +161,10 @@ void change_direction() {
// left // left
int l= digitalRead(10); int l= digitalRead(10);
if (u == LOW && direction != 'b') direction= 'u'; if (u == LOW && direction != 'b') new_direction= 'u';
else if (r == LOW && direction != 'l') direction= 'r'; else if (r == LOW && direction != 'l') new_direction= 'r';
else if (b == LOW && direction != 'u') direction= 'b'; else if (b == LOW && direction != 'u') new_direction= 'b';
else if (l == LOW && direction != 'r') direction= 'l'; else if (l == LOW && direction != 'r') new_direction= 'l';
} }
@@ -171,10 +172,11 @@ void loop() {
long exec_t1= millis(); long exec_t1= millis();
change_direction(); change_direction();
if (exec_t1 - exec_t2 > loop_delay) { if (exec_t1 - exec_t2 > loop_delay) {
direction= new_direction;
show_apple(); show_apple();
move_snake(); move_snake();
matrix.renderBitmap(frame, 8, 12); matrix.renderBitmap(frame, 8, 12);
exec_t2= exec_t1; exec_t2= exec_t1;
} }
delay(50); delay(10);
} }