47 lines
1.5 KiB
C
47 lines
1.5 KiB
C
#include "world.h"
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
|
|
// Initializes the game world, including the snake and the placement of food
|
|
void init_world(struct state* world, int width, int height){
|
|
world->width = width;
|
|
world->height = height;
|
|
world->sx = 1;
|
|
world->sy = 0;
|
|
|
|
// Initialize snake at the center of the world
|
|
world->snake = add_snake(NULL, width / 2, height / 2);
|
|
|
|
// Place food randomly
|
|
for(int i = 0; i < FOOD_COUNT; i++){
|
|
world->foodx[i] = rand() % (width - 2) + 1;
|
|
world->foody[i] = rand() % (height - 2) + 1;
|
|
}
|
|
}
|
|
|
|
// Prints the current state of the game world, including snake and food
|
|
void print_world(struct state* world){
|
|
for(int y = 0; y < world->height; y++){
|
|
for(int x = 0; x < world->width; x++){
|
|
if(x == 0 || y == 0 || x == world->width - 1 || y == world->height - 1){
|
|
printf("#"); // Print walls around the game area
|
|
}else if(is_snake(world->snake, x, y)){
|
|
printf("O"); // Print snake parts
|
|
}else{
|
|
int food_found = 0;
|
|
for(int i = 0; i < FOOD_COUNT; i++){
|
|
if(world->foodx[i] == x && world->foody[i] == y){
|
|
printf("*"); // Print food
|
|
food_found = 1;
|
|
break;
|
|
}
|
|
}
|
|
if(!food_found){
|
|
printf(" "); // Empty space
|
|
}
|
|
}
|
|
}
|
|
printf("\n");
|
|
}
|
|
}
|