用c语言写一个贪吃蛇游戏
时间: 2023-11-16 15:03:50 浏览: 64
用C语言写的贪吃蛇游戏
这里提供一个简单的贪吃蛇游戏的C语言代码,你可以根据自己的需求进行修改和完善。
```
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include <time.h>
#define WIDTH 40 // 游戏区域宽度
#define HEIGHT 20 // 游戏区域高度
// 定义方向
enum DIRECTION {
UP, DOWN, LEFT, RIGHT
};
struct Position {
int x;
int y;
};
struct Snake {
int length; // 蛇的长度
struct Position *body; // 蛇的身体坐标
enum DIRECTION direction; // 蛇的前进方向
};
struct Food {
int x;
int y;
};
// 初始化蛇的位置和方向
void initSnake(struct Snake *snake) {
snake->length = 3;
snake->direction = RIGHT;
snake->body = (struct Position *)malloc(snake->length * sizeof(struct Position));
snake->body[0].x = 3;
snake->body[0].y = 1;
snake->body[1].x = 2;
snake->body[1].y = 1;
snake->body[2].x = 1;
snake->body[2].y = 1;
}
// 初始化食物的位置
void initFood(struct Food *food) {
srand((unsigned)time(NULL));
food->x = rand() % (WIDTH - 2) + 1;
food->y = rand() % (HEIGHT - 2) + 1;
}
// 绘制游戏界面
void drawGame(struct Snake *snake, struct Food *food) {
system("cls");
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
if (i == 0 || i == HEIGHT - 1 || j == 0 || j == WIDTH - 1) {
printf("#");
} else if (i == food->y && j == food->x) {
printf("$");
} else {
int flag = 0;
for (int k = 0; k < snake->length; k++) {
if (snake->body[k].x == j && snake->body[k].y == i) {
printf("*");
flag = 1;
break;
}
}
if (!flag) {
printf(" ");
}
}
}
printf("\n");
}
printf("Length: %d\n", snake->length);
}
// 控制蛇的方向
void control(struct Snake *snake) {
char ch = getch();
if (ch == 'w' || ch == 'W') {
if (snake->direction != DOWN) {
snake->direction = UP;
}
} else if (ch == 's' || ch == 'S') {
if (snake->direction != UP) {
snake->direction = DOWN;
}
} else if (ch == 'a' || ch == 'A') {
if (snake->direction != RIGHT) {
snake->direction = LEFT;
}
} else if (ch == 'd' || ch == 'D') {
if (snake->direction != LEFT) {
snake->direction = RIGHT;
}
}
}
// 移动蛇
void move(struct Snake *snake, struct Food *food) {
// 蛇头移动
int x = snake->body[0].x;
int y = snake->body[0].y;
switch (snake->direction) {
case UP:
y--;
break;
case DOWN:
y++;
break;
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
}
// 判断是否吃到食物
if (x == food->x && y == food->y) {
snake->length++; // 蛇长度加1
snake->body = (struct Position *)realloc(snake->body, snake->length * sizeof(struct Position));
for (int i = snake->length - 1; i > 0; i--) {
snake->body[i] = snake->body[i - 1];
}
snake->body[0].x = x;
snake->body[0].y = y;
initFood(food); // 重新生成食物
} else {
// 蛇尾移动
for (int i = snake->length - 1; i > 0; i--) {
snake->body[i] = snake->body[i - 1];
}
snake->body[0].x = x;
snake->body[0].y = y;
}
}
// 判断游戏是否结束
int isGameOver(struct Snake *snake) {
// 判断是否撞墙
if (snake->body[0].x == 0 || snake->body[0].x == WIDTH - 1 || snake->body[0].y == 0 || snake->body[0].y == HEIGHT - 1) {
return 1;
}
// 判断是否咬到自己
for (int i = 1; i < snake->length; i++) {
if (snake->body[0].x == snake->body[i].x && snake->body[0].y == snake->body[i].y) {
return 1;
}
}
return 0;
}
int main() {
struct Snake snake;
struct Food food;
initSnake(&snake);
initFood(&food);
while (1) {
drawGame(&snake, &food);
control(&snake);
move(&snake, &food);
if (isGameOver(&snake)) {
printf("Game Over!\n");
break;
}
Sleep(200); // 控制游戏速度
}
free(snake.body);
return 0;
}
```
阅读全文