#include
#include
#include
#include
// Constants
#define WIDTH 40
#define HEIGHT 20
#define SNAKE_MAX_LENGTH 100
// Structs
typedef struct {
int x;
int y;
} Point;
typedef struct {
Point head;
Point body[SNAKE_MAX_LENGTH];
int length;
int direction;
} Snake;
// Globals
char board[HEIGHT][WIDTH];
Snake snake;
Point food;
// Function prototypes
void init_board();
void draw_board();
void spawn_food();
int check_collision(Point p);
void update_snake();
void move_snake(int dx, int dy);
int get_input();
void game_over();
int main() {
// Initialize random number generator
srand(time(NULL));
// Initialize board and snake
init_board();
// Draw initial state of the board
draw_board();
// Main game loop
while (1) {
// Wait for user input
int input = get_input();
// Update snake direction based on input
if (input == 'w' && snake.direction != 's')
snake.direction = 'w';
else if (input == 'a' && snake.direction != 'd')
snake.direction = 'a';
else if (input == 's' && snake.direction != 'w')
snake.direction = 's';
else if (input == 'd' && snake.direction != 'a')
snake.direction = 'd';
// Update snake position and check for collision with food or walls
update_snake();
// Redraw board with updated state
draw_board();
}
return 0;
}
// Initialize the game board and snake
void init_board() {
// Initialize board to all empty spaces
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
board[i][j] = ' ';
}
}
// Initialize snake at center of board facing right
snake.head.x = WIDTH / 2;
snake.head.y = HEIGHT / 2;
snake.body[0].x = snake.head.x - 1;
snake.body[0].y = snake.head.y;
snake.body[1].x = snake.head.x - 2;
snake.body[1].y = snake.head.y;
snake.length = 2;
snake.direction = 'd';
// Spawn initial food
spawn_food();
}
// Draw the current state of the game board
void draw_board() {
system("cls"); // Clear console screen
// Draw top border of board
for (int i = 0; i < WIDTH + 2; i++) {
printf("#");
}
printf("\n");
// Draw board contents
for (int i = 0; i < HEIGHT; i++) {
printf("#"); // Left border of board
for (int j = 0; j < WIDTH; j++) {
if (i == snake.head.y && j == snake.head.x) {
printf("O"); // Draw snake head
} else if (board[i][j] == 'X') {
printf("X"); // Draw food
} else {
int is_body_segment = 0;
for (int k = 0; k < snake.length; k++) {
if (i == snake.body[k].y && j == snake.body[k].x) {
printf("o"); // Draw snake body
is_body_segment = 1;
break;
}
}
if (!is_body_segment) {
printf(" "); // Draw empty space
}
}
}
printf("#\n"); // Right border of board
}
// Draw bottom border of board
for (int i = 0; i < WIDTH + 2; i++) {
printf("#");
}
printf("\n");
// Print score
printf("Score: %d\n", snake.length - 2);
}
// Spawn a piece of food at a random location on the board
void spawn_food() {
int x, y;
do {
x = rand() % WIDTH;
y = rand() % HEIGHT;
} while (check_collision((Point) {x, y}));
food.x = x;
food.y = y;
board[y][x] = 'X';
}
// Check if the given point collides with the snake's head or body or any wall
int check_collision(Point p) {
// Check for collision with snake head or body
for (int i = 0; i < snake.length; i++) {
if (p.x == snake.body[i].x && p.y == snake.body[i].y) {
return 1;
}
}
if (p.x == snake.head.x && p.y == snake.head.y) {
return 1;
}
// Check for collision with walls
if (p.x < 0 || p.x >= WIDTH || p.y < 0 || p.y >= HEIGHT) {
return 1;
}
return 0;
}
// Update the snake's position and check for collision with food or walls
void update_snake() {
// Move snake body segments forward
for (int i = snake.length - 1; i > 0; i--) {
snake.body[i] = snake.body[i - 1];
}
snake.body[0] = snake.head;
// Move snake head in current direction
if (snake.direction == 'w') {
move_snake(0, -1);
} else if (snake.direction == 'a') {
move_snake(-1, 0);
} else if (snake.direction == 's') {
move_snake(0, 1);
} else if (snake.direction == 'd') {
move_snake(1, 0);
}
// Check for collision with food
if (snake.head.x == food.x && snake.head.y == food.y) {
snake.length++;
spawn_food();
}
// Check for collision with walls or snake body
if (check_collision(snake.head)) {
game_over();
}
}
// Move the snake by the given delta x and delta y
void move_snake(int dx, int dy) {
snake.head.x += dx;
snake.head.y += dy;
}
// Wait for user input and return the ASCII code of the key pressed
int get_input() {
if (_kbhit()) {
return _getch();
}
return 0;
}
// Game over screen
void game_over() {
system("cls"); // Clear console screen
printf("Game over!\n");
printf("Score: %d\n", snake.length - 2);
exit(0);
}
إرسال تعليق