funguje
This commit is contained in:
parent
626f4f7f7f
commit
26ef46eebe
132
a2/snake.c
132
a2/snake.c
@ -5,128 +5,82 @@
|
|||||||
|
|
||||||
#define WIDTH 20
|
#define WIDTH 20
|
||||||
#define HEIGHT 10
|
#define HEIGHT 10
|
||||||
|
#define INITIAL_SNAKE_LENGTH 5
|
||||||
|
#define FOOD_COUNT 20
|
||||||
|
|
||||||
void initialize_food(int foodx[], int foody[]) {
|
void initialize_world(struct state *state) {
|
||||||
|
// Initialize snake in the middle of the world
|
||||||
|
int initial_x = WIDTH / 2;
|
||||||
|
int initial_y = HEIGHT / 2;
|
||||||
|
state->snake = add_snake(NULL, initial_x, initial_y);
|
||||||
|
if (state->snake == NULL) {
|
||||||
|
fprintf(stderr, "Failed to initialize snake.\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialize food
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
for (int i = 0; i < FOOD_COUNT; i++) {
|
for (int i = 0; i < FOOD_COUNT; i++) {
|
||||||
foodx[i] = rand() % WIDTH;
|
state->foodx[i] = rand() % WIDTH;
|
||||||
foody[i] = rand() % HEIGHT;
|
state->foody[i] = rand() % HEIGHT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Initialize state parameters
|
||||||
|
state->sx = 1;
|
||||||
|
state->sy = 0;
|
||||||
|
state->width = WIDTH;
|
||||||
|
state->height = HEIGHT;
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_world(struct state *state) {
|
void print_world(struct state *state) {
|
||||||
for (int y = 0; y < HEIGHT; y++) {
|
for (int y = 0; y < HEIGHT; y++) {
|
||||||
for (int x = 0; x < WIDTH; x++) {
|
for (int x = 0; x < WIDTH; x++) {
|
||||||
if (is_snake(state->snake, x, y)) {
|
int is_snake_part = is_snake(state->snake, x, y);
|
||||||
printf("x");
|
int is_food = 0;
|
||||||
} else {
|
|
||||||
int food_found = 0;
|
|
||||||
for (int i = 0; i < FOOD_COUNT; i++) {
|
for (int i = 0; i < FOOD_COUNT; i++) {
|
||||||
if (state->foodx[i] == x && state->foody[i] == y) {
|
if (state->foodx[i] == x && state->foody[i] == y) {
|
||||||
if (state->foodx[i] >= 0 && state->foody[i] >= 0) {
|
is_food = 1;
|
||||||
printf("*");
|
|
||||||
} else {
|
|
||||||
printf(" ");
|
|
||||||
}
|
|
||||||
food_found = 1;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!food_found) {
|
if (is_snake_part) {
|
||||||
|
printf("x");
|
||||||
|
} else if (is_food) {
|
||||||
|
printf("*");
|
||||||
|
} else {
|
||||||
printf(" ");
|
printf(" ");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void move_snake(struct state* state, int dx, int dy) {
|
|
||||||
// Update snake position according to the direction
|
|
||||||
int new_x = state->snake->x + dx;
|
|
||||||
int new_y = state->snake->y + dy;
|
|
||||||
|
|
||||||
// Check if the new position is out of bounds
|
|
||||||
if (new_x < 0 || new_x >= WIDTH || new_y < 0 || new_y >= HEIGHT) {
|
|
||||||
printf("Snake hit the wall!\n");
|
|
||||||
exit(EXIT_SUCCESS);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if the new position is on the snake
|
|
||||||
if (is_snake(state->snake, new_x, new_y)) {
|
|
||||||
printf("Snake hit itself!\n");
|
|
||||||
exit(EXIT_SUCCESS);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if the new position is on food
|
|
||||||
for (int i = 0; i < FOOD_COUNT; i++) {
|
|
||||||
if (state->foodx[i] == new_x && state->foody[i] == new_y) {
|
|
||||||
state->foodx[i] = -1;
|
|
||||||
state->foody[i] = -1;
|
|
||||||
// Grow the snake
|
|
||||||
struct snake* new_head = add_snake(state->snake, new_x, new_y);
|
|
||||||
if (new_head == NULL) {
|
|
||||||
printf("Memory allocation failed!\n");
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
state->snake = new_head;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Move the snake
|
|
||||||
struct snake* new_head = add_snake(state->snake, new_x, new_y);
|
|
||||||
if (new_head == NULL) {
|
|
||||||
printf("Memory allocation failed!\n");
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
state->snake = new_head;
|
|
||||||
remove_snake(state->snake);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
// Initialize game state
|
|
||||||
struct state game_state;
|
struct state game_state;
|
||||||
game_state.snake = add_snake(NULL, WIDTH / 2, HEIGHT / 2);
|
initialize_world(&game_state);
|
||||||
if (game_state.snake == NULL) {
|
|
||||||
printf("Memory allocation failed!\n");
|
|
||||||
return EXIT_FAILURE;
|
|
||||||
}
|
|
||||||
game_state.width = WIDTH;
|
|
||||||
game_state.height = HEIGHT;
|
|
||||||
game_state.sx = 1;
|
|
||||||
game_state.sy = 0;
|
|
||||||
initialize_food(game_state.foodx, game_state.foody);
|
|
||||||
|
|
||||||
// Main game loop
|
|
||||||
while (1) {
|
while (1) {
|
||||||
system("clear"); // Clear the console
|
system("clear");
|
||||||
print_world(&game_state);
|
print_world(&game_state);
|
||||||
|
usleep(200000);
|
||||||
|
|
||||||
// Check if all food is eaten
|
// Move snake
|
||||||
int food_eaten = 1;
|
int end_game_reason = step_state(&game_state);
|
||||||
for (int i = 0; i < FOOD_COUNT; i++) {
|
if (end_game_reason != END_CONTINUE) {
|
||||||
if (game_state.foodx[i] >= 0 && game_state.foody[i] >= 0) {
|
if (end_game_reason == END_SNAKE) {
|
||||||
food_eaten = 0;
|
printf("Snake hit itself! Game over.\n");
|
||||||
break;
|
} else if (end_game_reason == END_WALL) {
|
||||||
}
|
printf("Snake hit the wall! Game over.\n");
|
||||||
}
|
} else if (end_game_reason == END_FOOD) {
|
||||||
if (food_eaten) {
|
|
||||||
printf("All food eaten! You win!\n");
|
printf("All food eaten! You win!\n");
|
||||||
|
} else {
|
||||||
|
printf("Game over due to unknown reason.\n");
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Move the snake according to the direction
|
|
||||||
move_snake(&game_state, game_state.sx, game_state.sy);
|
|
||||||
|
|
||||||
// Delay for a short time to slow down the game
|
|
||||||
// Adjust this value to change the game speed
|
|
||||||
usleep(200000); // 200 milliseconds
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Free memory
|
|
||||||
free_snake(game_state.snake);
|
free_snake(game_state.snake);
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user