snake
This commit is contained in:
parent
cd81b53a59
commit
076d06a307
16
du4/program.c
Normal file
16
du4/program.c
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include "game.h"
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
printf("Starting Snake Game...\n");
|
||||||
|
printf("Use arrow keys to move the snake.\n");
|
||||||
|
printf("Press 'q' to quit.\n");
|
||||||
|
printf("Eat all food (*) to win!\n\n");
|
||||||
|
sleep(2);
|
||||||
|
|
||||||
|
run_game();
|
||||||
|
|
||||||
|
printf("Thanks for playing!\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
22
du4/snake.c
22
du4/snake.c
@ -64,6 +64,12 @@ int step_state(struct state* state) {
|
|||||||
return END_WALL;
|
return END_WALL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if new position is on snake's body
|
||||||
|
if (is_snake(state->snake, new_x, new_y)) {
|
||||||
|
return END_SNAKE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find food at new position
|
||||||
int food_index = -1;
|
int food_index = -1;
|
||||||
int items_left = 0;
|
int items_left = 0;
|
||||||
|
|
||||||
@ -76,14 +82,9 @@ int step_state(struct state* state) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (food_index >= 0) {
|
if (food_index >= 0) {
|
||||||
|
// Food found - mark as eaten and grow snake
|
||||||
state->foodx[food_index] = -1;
|
state->foodx[food_index] = -1;
|
||||||
state->foody[food_index] = -1;
|
state->foody[food_index] = -1;
|
||||||
|
|
||||||
if (is_snake(state->snake, new_x, new_y)) {
|
|
||||||
state->snake = add_snake(state->snake, new_x, new_y);
|
|
||||||
return END_SNAKE;
|
|
||||||
}
|
|
||||||
|
|
||||||
state->snake = add_snake(state->snake, new_x, new_y);
|
state->snake = add_snake(state->snake, new_x, new_y);
|
||||||
|
|
||||||
if (items_left == 0) {
|
if (items_left == 0) {
|
||||||
@ -91,14 +92,9 @@ int step_state(struct state* state) {
|
|||||||
}
|
}
|
||||||
return END_CONTINUE;
|
return END_CONTINUE;
|
||||||
} else {
|
} else {
|
||||||
|
// No food - normal movement (remove tail and add head)
|
||||||
state->snake = remove_snake(state->snake);
|
state->snake = remove_snake(state->snake);
|
||||||
|
|
||||||
if (is_snake(state->snake, new_x, new_y)) {
|
|
||||||
state->snake = add_snake(state->snake, new_x, new_y);
|
|
||||||
return END_SNAKE;
|
|
||||||
}
|
|
||||||
|
|
||||||
state->snake = add_snake(state->snake, new_x, new_y);
|
state->snake = add_snake(state->snake, new_x, new_y);
|
||||||
return END_CONTINUE;
|
return END_CONTINUE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -2,7 +2,7 @@
|
|||||||
#define snake_h_INCLUDED
|
#define snake_h_INCLUDED
|
||||||
|
|
||||||
// Number of food items on the plane
|
// Number of food items on the plane
|
||||||
#define FOOD_COUNT 5
|
#define FOOD_COUNT 20
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* One part of the snake;
|
* One part of the snake;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user