diff --git a/MCU/Final_Design/snake.c b/MCU/Final_Design/snake.c index 9a8802e..6ff1871 100644 --- a/MCU/Final_Design/snake.c +++ b/MCU/Final_Design/snake.c @@ -1,8 +1,82 @@ #include "snake.h" -Body head[40]={{0,0},0,-1}; +static Body snake[40] = {{0, 0}, 0}; +static unsigned char len = 0; void snake_init() { - + snake[len].d.x = 63; + snake[len].d.y = 31; + snake[len].m = R; + len++; +} + +void body_move(Body b) +{ + switch(b.m) + { + case R: + b.d.x++; + break; + + case L: + b.d.x--; + break; + + case U: + b.d.y--; + break; + + case D: + b.d.y++; + break; + } +} + +void snake_move() +{ + int i = 0; + for(; i <= len; i++) + { + body_move(snake[i]); + } + for(i = 1; i <= len; i++) + { + snake[i].m = snake[i - 1].m; + } + +} + +void body_add() +{ + len++ + snake[len].m = snake[len - 1].m; + snake[len].d.x = snake[len - 1].d.x; + snake[len].d.x = snake[len - 1].d.x; + switch(snake[len].m) + { + case R: + snake[len].d.x--; + break; + + case L: + snake[len].d.x++; + break; + + case U: + snake[len].d.y++; + break; + + case D: + snake[len].d.y--; + break; + } +} + + +void test() +{ + int i = 0; + for(; i < 15; i++) + body_add(); } \ No newline at end of file diff --git a/MCU/Final_Design/snake.h b/MCU/Final_Design/snake.h index 45f954c..7404864 100644 --- a/MCU/Final_Design/snake.h +++ b/MCU/Final_Design/snake.h @@ -2,10 +2,11 @@ #include #define bool char -typedef enum -{ - N=0,U, D, L, R -} MOVE_STATE; +#define MOVE_STATE char +#define R 0 +#define U 1 +#define L 2 +#define D 3 struct DOT @@ -13,14 +14,14 @@ struct DOT unsigned char x; unsigned char y; } ; +typedef struct DOT Dot; struct BODY { - dot d; - MOVE_STATE ms; - char id; + Dot d; + MOVE_STATE m; }; typedef struct BODY Body; -typedef struct DOT Dot; +