commit 9df7da3a963a2d605b4836b6caff076206a02074 Author: Crystal Date: Sat Nov 4 10:13:36 2023 +0100 initial snake commit, implemented led matrix and snake movements diff --git a/snake/Makefile b/snake/Makefile new file mode 100644 index 0000000..b223e2c --- /dev/null +++ b/snake/Makefile @@ -0,0 +1,12 @@ +compile: + arduino-cli compile -b arduino:renesas_uno:unor4wifi snake.ino + +upload: + arduino-cli upload -b arduino:renesas_uno:unor4wifi -p /dev/ttyACM0 + +upload_verbose: + arduino-cli upload -b arduino:renesas_uno:unor4wifi -p /dev/ttyACM0 -v + +run: + make compile + make upload diff --git a/snake/snake.ino b/snake/snake.ino new file mode 100644 index 0000000..373949a --- /dev/null +++ b/snake/snake.ino @@ -0,0 +1,91 @@ +#include "Arduino_LED_Matrix.h" + +// create LED matrix object +ArduinoLEDMatrix matrix; + +// initial snake frame +byte frame[8][12] = { + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } +}; + +const uint32_t game_over[] = { + 0x20410809, + 0x600600, + 0x90108204, +}; + +// snake initial coordinates +int snake_x[4]= {4, 3, 2, 1}; +int snake_y[4]= {3, 3, 3, 3}; +int snake_len= 4; +// direction 1 right, 2 bottom, 3 left, 4 top +int direction= 1; + +// apple initial position +//int apple_x= 9; +//int apple_y= 4; + +void setup() { + Serial.begin(115200); + // stard LED matrix + matrix.begin(); +} + +void move_snake() { + /* + starting from head, save curr position w and h + then for all the snake length the previuos + body block needs to take the previous block place + the current tail pos will be set to 0 + */ + + int i=0; + frame[snake_y[snake_len-1]][snake_x[snake_len-1]]= 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; + snake_x[i]= snake_x[i-1]; + snake_y[i]= snake_y[i-1]; + } + + //move the snake head + + //snake_y[0]= snake_y[0]; + // go right + if (direction == 1) { + snake_x[0]+= 1; + if (snake_x[0] > 11) snake_x[0]= 0; + } + // go down + else if (direction == 2) { + snake_y[0]+= 1; + if (snake_y[0] > 7) snake_y[0]= 0; + } + // go left + else if (direction == 3) { + snake_x[0]-= 1; + if (snake_x[0] < 0) snake_x[0]= 11; + } + // go up + else if (direction == 4) { + snake_y[0]-= 1; + if (snake_y[0] < 0) snake_y[0]= 7; + } + + // render the snake head + frame[snake_y[0]][snake_x[0]]= 1; +} + +void loop() { + matrix.renderBitmap(frame, 8, 12); + delay(300); + move_snake(); + direction= 2; +}