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

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

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

Loading…
Cancel
Save