用c语言写一个贪吃蛇游戏的代码
时间: 2024-02-24 11:55:09 浏览: 35
以下是一个基本的贪吃蛇游戏的 C 语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#define WIDTH 30 // 宽度
#define HEIGHT 20 // 高度
#define SPEED 100 // 速度(毫秒)
#define SNAKE_MAX_LENGTH 50 // 蛇的最大长度
// 定义蛇的结构体
struct Snake {
int x[SNAKE_MAX_LENGTH];
int y[SNAKE_MAX_LENGTH];
int length;
};
// 定义食物的结构体
struct Food {
int x;
int y;
int exist;
};
// 定义游戏的状态
enum GameStatus {
GAME_OVER,
GAME_WIN,
GAME_RUNNING
};
// 初始化游戏状态
enum GameStatus game_status = GAME_RUNNING;
// 初始化蛇和食物
struct Snake snake = {0};
struct Food food = {0};
// 初始化得分和最高得分
int score = 0;
int high_score = 0;
// 打印地图
void print_map() {
system("cls"); // 清屏
printf("SCORE: %d\tHIGH SCORE: %d\n", score, high_score);
for (int i = 0; i <= HEIGHT + 1; i++) {
for (int j = 0; j <= WIDTH + 1; j++) {
if (i == 0 || i == HEIGHT + 1 || j == 0 || j == WIDTH + 1) {
printf("#");
} else if (i == food.y && j == food.x && food.exist) {
printf("O");
} else {
int flag = 0;
for (int k = 0; k < snake.length; k++) {
if (i == snake.y[k] && j == snake.x[k]) {
printf("X");
flag = 1;
break;
}
}
if (!flag) {
printf(" ");
}
}
}
printf("\n");
}
}
// 初始化蛇的位置
void init_snake() {
snake.length = 3;
snake.x[0] = WIDTH / 2;
snake.y[0] = HEIGHT / 2;
for (int i = 1; i < snake.length; i++) {
snake.x[i] = snake.x[i-1] - 1;
snake.y[i] = snake.y[i-1];
}
}
// 初始化食物的位置
void init_food() {
food.exist = 1;
while (food.exist) {
food.x = rand() % WIDTH + 1;
food.y = rand() % HEIGHT + 1;
food.exist = 0;
for (int i = 0; i < snake.length; i++) {
if (food.x == snake.x[i] && food.y == snake.y[i]) {
food.exist = 1;
break;
}
}
}
}
void update_snake(int x, int y) {
// 更新蛇的位置
for (int i = snake.length - 1; i > 0; i--) {
snake.x[i] = snake.x[i - 1];
snake.y[i] = snake.y[i - 1];
}
snake.x[0] = x;
snake.y[0] = y;
// 判断是否吃到食物
if (food.x == x && food.y == y) {
food.exist = 0;
snake.length++;
score += 10;
if (score > high_score) {
high_score = score;
}
}
// 判断是否撞墙或自撞
if (x < 1 || x > WIDTH || y < 1 || y > HEIGHT) {
game_status = GAME_OVER;
}
for (int i = 1; i < snake.length; i++) {
if (snake.x[0] == snake.x[i] && snake.y[0] == snake.y[i]) {
game_status = GAME_OVER;
break;
}
}
// 判断是否通关
if (snake.length >= SNAKE_MAX_LENGTH) {
game_status = GAME_WIN;
}
}
// 处理键盘输入
void handle_input() {
if (_kbhit()) {
int key = _getch();
switch (key) {
case 'w':
case 'W':
update_snake(snake.x[0], snake.y[0] - 1);
break;
case 's':
case 'S':
update_snake(snake.x[0], snake.y[0] + 1);
break;
case 'a':
case 'A':
update_snake(snake.x[0] - 1, snake.y[0]);
break;
case 'd':
case 'D':
update_snake(snake.x[0] + 1, snake.y[0]);
break;
default:
break;
}
}
}
int main() {
init_snake();
init_food();
while (game_status == GAME_RUNNING) {
print_map();
handle_input();
update_snake(snake.x[0], snake.y[0]);
Sleep(SPEED);
}
if (game_status == GAME_OVER) {
printf("GAME OVER!\n");
} else if (game_status == GAME_WIN) {
printf("YOU WIN!\n");
}
return 0;
}
```
这个代码使用了 Windows 系统的 `conio.h` 和 `windows.h` 库,所以需要在 Windows 环境下编译运行。如果在 Linux 环境下运行,需要修改相关库的引用,并可能需要修改一些 Windows 相关的 API 调用。
阅读全文