From 13a47abc2d8a5457118efd213006e6e6fa367b13 Mon Sep 17 00:00:00 2001 From: andrea Date: Sat, 4 Nov 2023 15:41:35 +0100 Subject: [PATCH] fix apple not always show if a disappearing tail turn off the apple LED --- snake_arduino/snake_arduino.ino | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/snake_arduino/snake_arduino.ino b/snake_arduino/snake_arduino.ino index 34b6197..6ca0868 100644 --- a/snake_arduino/snake_arduino.ino +++ b/snake_arduino/snake_arduino.ino @@ -59,6 +59,10 @@ void add_tail_block() { snake_x[snake_len-1]= snake_x[snake_len-2]; } +void show_apple() { + frame[apple_y][apple_x]= 1; +} + void eat_apple() { points++; Serial.println(points); @@ -69,7 +73,7 @@ void eat_apple() { frame[apple_y][apple_x]= 0; apple_x= random(11); apple_y= random(7); - frame[apple_y][apple_x]= 1; + show_apple(); // increase snake length add_tail_block(); @@ -103,7 +107,11 @@ void game_over() { void move_snake() { int i=0; - frame[snake_y[snake_len-1]][snake_x[snake_len-1]]= 0; + + int y= snake_y[snake_len-1]; + int x= snake_x[snake_len-1]; + frame[y][x]= 0; + // move and render the snake tail for (i= snake_len-1; i > 0; i--) { frame[snake_y[i-1]][snake_x[i-1]]= 1; @@ -158,8 +166,9 @@ void change_direction() { } void loop() { + show_apple(); change_direction(); matrix.renderBitmap(frame, 8, 12); - delay(loop_delay); move_snake(); + delay(loop_delay); }