Browse Source

refactoring: movement of snake

remotes/origin/David
David Moeller 11 months ago
parent
commit
9a3247bf68
  1. 16
      src/main/c/Snake/snake_start.c
  2. 2
      src/main/c/Snake/snake_start.h

16
src/main/c/Snake/snake_start.c

@ -77,7 +77,7 @@ void game(){
} }
Snake initialize_snake(){ Snake initialize_snake(){
Snake snake = {{1, 0}, 3};
Snake snake = {1, 3};
for(int i = 0; i < AREA; i++){ for(int i = 0; i < AREA; i++){
snake.segments[i] = 0; snake.segments[i] = 0;
} }
@ -96,23 +96,19 @@ void get_next_move(double limit, Snake *snake, bool *running){
switch (c){ switch (c){
case 'w': case 'w':
case 'A': case 'A':
snake->direction[0] = 0;
snake->direction[1] = -1;
snake->direction = -16;
break; break;
case 'a': case 'a':
case 'D': case 'D':
snake->direction[0] = -1;
snake->direction[1] = 0;
snake->direction = -1;
break; break;
case 's': case 's':
case 'B': case 'B':
snake->direction[0] = 0;
snake->direction[1] = 1;
snake->direction = 16;
break; break;
case 'd': case 'd':
case 'C': case 'C':
snake->direction[0] = 1;
snake->direction[1] = 0;
snake->direction = 1;
break; break;
case 'q': case 'q':
*running = false; *running = false;
@ -126,7 +122,7 @@ void move_snake(Snake *snake){
for(int i = snake->length - 1; i > 0; i--){ for(int i = snake->length - 1; i > 0; i--){
snake->segments[i] = snake->segments[i - 1]; snake->segments[i] = snake->segments[i - 1];
} }
snake->segments[0] += snake->direction[0] + snake->direction[1] * 16;
snake->segments[0] += snake->direction;
} }
void draw(Snake *snake, unsigned char fruit){ void draw(Snake *snake, unsigned char fruit){

2
src/main/c/Snake/snake_start.h

@ -6,7 +6,7 @@
#define AREA HEIGHT * WIDTH #define AREA HEIGHT * WIDTH
typedef struct{ typedef struct{
signed char direction[2];
signed char direction;
char length; char length;
unsigned char segments[AREA]; unsigned char segments[AREA];
}Snake; }Snake;

Loading…
Cancel
Save