Browse Source

Added fruit

remotes/origin/David
David Moeller 11 months ago
parent
commit
ca67976e79
  1. 20
      src/main/c/Snake/snake_start.c

20
src/main/c/Snake/snake_start.c

@ -13,9 +13,10 @@ void game();
Snake initialize_snake();
void get_next_move(double limit, Snake *snake, bool *running);
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);
bool check_if_dead(Snake *snake);
unsigned char spawn_fruit(Snake *snake);
void snake_start(){
@ -56,10 +57,11 @@ void game(){
Snake snake = initialize_snake();
bool running = true;
clock_t t = clock();
unsigned char fruit = spawn_fruit(&snake);
while (running){
system("clear");
draw(&snake);
draw(&snake, fruit);
if(check_if_dead(&snake)){
break;
@ -125,7 +127,8 @@ void move_snake(Snake *snake){
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("+");
for(int i = 0; i < WIDTH; i++){printf("-");}
printf("+\n");
@ -135,6 +138,7 @@ void draw(Snake *snake){
int index = part_of_snake(snake, i * 16 + j);
if(index == 0){printf("0");}
else if(index > 0){printf("O");}
else if(i * 16 + j == fruit){printf("X");}
else{printf(" ");}
}
printf("|\n");
@ -169,4 +173,14 @@ bool check_if_dead(Snake *snake){
}
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;
}
Loading…
Cancel
Save