|
@ -13,9 +13,10 @@ void game(); |
|
|
Snake initialize_snake(); |
|
|
Snake initialize_snake(); |
|
|
void get_next_move(double limit, Snake *snake, bool *running); |
|
|
void get_next_move(double limit, Snake *snake, bool *running); |
|
|
void move_snake(Snake *snake); |
|
|
void move_snake(Snake *snake); |
|
|
void draw(Snake *snake); |
|
|
|
|
|
|
|
|
void draw(Snake *snake, unsigned char fruit); |
|
|
int part_of_snake(Snake *snake, unsigned char tile); |
|
|
int part_of_snake(Snake *snake, unsigned char tile); |
|
|
bool check_if_dead(Snake *snake); |
|
|
bool check_if_dead(Snake *snake); |
|
|
|
|
|
unsigned char spawn_fruit(Snake *snake); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void snake_start(){ |
|
|
void snake_start(){ |
|
@ -56,10 +57,11 @@ void game(){ |
|
|
Snake snake = initialize_snake(); |
|
|
Snake snake = initialize_snake(); |
|
|
bool running = true; |
|
|
bool running = true; |
|
|
clock_t t = clock(); |
|
|
clock_t t = clock(); |
|
|
|
|
|
unsigned char fruit = spawn_fruit(&snake); |
|
|
|
|
|
|
|
|
while (running){ |
|
|
while (running){ |
|
|
system("clear"); |
|
|
system("clear"); |
|
|
draw(&snake); |
|
|
|
|
|
|
|
|
draw(&snake, fruit); |
|
|
|
|
|
|
|
|
if(check_if_dead(&snake)){ |
|
|
if(check_if_dead(&snake)){ |
|
|
break; |
|
|
break; |
|
@ -125,7 +127,8 @@ void move_snake(Snake *snake){ |
|
|
snake->segments[0] += snake->direction[0] + snake->direction[1] * 16; |
|
|
snake->segments[0] += snake->direction[0] + snake->direction[1] * 16; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void draw(Snake *snake){ |
|
|
|
|
|
|
|
|
void draw(Snake *snake, unsigned char fruit){ |
|
|
|
|
|
printf("%d\n", fruit); |
|
|
printf("+"); |
|
|
printf("+"); |
|
|
for(int i = 0; i < WIDTH; i++){printf("-");} |
|
|
for(int i = 0; i < WIDTH; i++){printf("-");} |
|
|
printf("+\n"); |
|
|
printf("+\n"); |
|
@ -135,6 +138,7 @@ void draw(Snake *snake){ |
|
|
int index = part_of_snake(snake, i * 16 + j); |
|
|
int index = part_of_snake(snake, i * 16 + j); |
|
|
if(index == 0){printf("0");} |
|
|
if(index == 0){printf("0");} |
|
|
else if(index > 0){printf("O");} |
|
|
else if(index > 0){printf("O");} |
|
|
|
|
|
else if(i * 16 + j == fruit){printf("X");} |
|
|
else{printf(" ");} |
|
|
else{printf(" ");} |
|
|
} |
|
|
} |
|
|
printf("|\n"); |
|
|
printf("|\n"); |
|
@ -169,4 +173,14 @@ bool check_if_dead(Snake *snake){ |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return false; |
|
|
return false; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
unsigned char spawn_fruit(Snake *snake){ |
|
|
|
|
|
srand(time(NULL)); |
|
|
|
|
|
int r = 0; |
|
|
|
|
|
while (part_of_snake(snake, r) != -1 || r % 16 == 0 || (r / 16) % 16 == 0){ |
|
|
|
|
|
r = rand() % 256; |
|
|
|
|
|
printf("%d\n", r); |
|
|
|
|
|
} |
|
|
|
|
|
return r; |
|
|
} |
|
|
} |