Browse Source

moving snake

remotes/origin/David
David Moeller 11 months ago
parent
commit
60076577cc
  1. 25
      src/main/c/Snake/snake_start.c

25
src/main/c/Snake/snake_start.c

@ -11,7 +11,7 @@
void main_menu();
void game();
Snake initialize_snake();
void get_next_move(double limit, Snake *snake);
void get_next_move(double limit, Snake *snake, bool *running);
void move_snake(Snake *snake);
void draw(Snake *snake);
int part_of_snake(Snake *snake, unsigned char tile);
@ -56,25 +56,19 @@ void game(){
bool running = true;
clock_t t = clock();
//while (running){
for(int i = 0; i < 100; i++){
while (running){
system("clear");
draw(&snake);
t = clock() - t;
get_next_move(TIME_TURN - (double)t / CLOCKS_PER_SEC, &snake);
get_next_move(TIME_TURN - (double)t / CLOCKS_PER_SEC, &snake, &running);
t = clock();
move_snake(&snake);
running = false;
}
getchar();
getchar();
}
Snake initialize_snake(){
Snake snake = {{0, 1}, 3};
Snake snake = {{1, 0}, 3};
for(int i = 0; i < AREA; i++){
snake.segments[i] = 0;
}
@ -82,12 +76,10 @@ Snake initialize_snake(){
snake.segments[1] = 7 + 16 * 8;
snake.segments[2] = 6 + 16 * 8;
snake.direction[0] = 1;
snake.direction[1] = 0;
return snake;
}
void get_next_move(double limit, Snake *snake){
void get_next_move(double limit, Snake *snake, bool *running){
char c;
c = get_character(limit);
@ -113,6 +105,8 @@ void get_next_move(double limit, Snake *snake){
snake->direction[0] = 1;
snake->direction[1] = 0;
break;
case 'q':
*running = false;
default:
break;
@ -120,7 +114,10 @@ void get_next_move(double limit, Snake *snake){
}
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;
}
void draw(Snake *snake){

Loading…
Cancel
Save