C怎么做一个简单的贪吃蛇
时间: 2023-07-01 12:08:27 浏览: 33
下面是一个简单的贪吃蛇游戏的 C 代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#define width 40 //地图宽度
#define height 20 //地图高度
int map[height][width]; //地图数据
int snake[1000][2]; //蛇的身体,最多1000节
int score = 0; //分数
int snake_len = 3; //蛇的长度
int food_x, food_y; //食物坐标
int direction = 0; //蛇移动方向,0代表上,1代表右,2代表下,3代表左
void gotoxy(int x, int y) //移动光标到指定位置函数
{
COORD pos = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
void init_map() //初始化地图函数
{
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) {
map[i][j] = 1; //边界
} else {
map[i][j] = 0; //空地
}
}
}
}
void print_map() //打印地图函数
{
gotoxy(0, 0);
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (map[i][j] == 0) {
printf(" ");
} else if (map[i][j] == 1) {
printf("#"); //边界
} else if (map[i][j] == 2) {
printf("*"); //食物
} else if (map[i][j] == 3) {
printf("O"); //蛇的身体
}
}
printf("\n");
}
printf("Score: %d\n", score); //打印分数
}
void generate_food() //随机生成食物函数
{
while (1) {
food_x = rand() % (width-2) + 1; //随机生成食物坐标
food_y = rand() % (height-2) + 1;
if (map[food_y][food_x] == 0) {
map[food_y][food_x] = 2; //放置食物
break;
}
}
}
void init_snake() //初始化蛇函数
{
for (int i = 0; i < snake_len; i++) {
snake[i][0] = width/2; //蛇头在地图中央
snake[i][1] = height/2+i;
map[snake[i][1]][snake[i][0]] = 3; //放置蛇的身体
}
}
void move_snake() //移动蛇函数
{
int new_head[2]; //新的蛇头坐标
switch (direction) {
case 0: //向上移动
new_head[0] = snake[0][0];
new_head[1] = snake[0][1]-1;
break;
case 1: //向右移动
new_head[0] = snake[0][0]+1;
new_head[1] = snake[0][1];
break;
case 2: //向下移动
new_head[0] = snake[0][0];
new_head[1] = snake[0][1]+1;
break;
case 3: //向左移动
new_head[0] = snake[0][0]-1;
new_head[1] = snake[0][1];
break;
}
if (map[new_head[1]][new_head[0]] == 1 || map[new_head[1]][new_head[0]] == 3) { //撞墙或咬到自己
printf("Game Over!\n");
exit(0);
} else if (map[new_head[1]][new_head[0]] == 2) { //吃到食物
score++;
snake_len++;
map[new_head[1]][new_head[0]] = 3; //放置新的蛇头和身体
for (int i = snake_len-1; i >= 1; i--) {
snake[i][0] = snake[i-1][0];
snake[i][1] = snake[i-1][1];
}
snake[0][0] = new_head[0];
snake[0][1] = new_head[1];
generate_food(); //重新生成食物
} else { //正常移动
map[snake[snake_len-1][1]][snake[snake_len-1][0]] = 0; //删除尾巴
for (int i = snake_len-1; i >= 1; i--) {
snake[i][0] = snake[i-1][0];
snake[i][1] = snake[i-1][1];
}
snake[0][0] = new_head[0];
snake[0][1] = new_head[1];
map[snake[0][1]][snake[0][0]] = 3; //放置新的蛇头和身体
}
}
void input() //获取用户输入
{
if (_kbhit()) { //判断是否有键盘输入
char ch = _getch(); //获取按键
switch (ch) {
case 'w': //向上移动
if (direction != 2) {
direction = 0;
}
break;
case 'd': //向右移动
if (direction != 3) {
direction = 1;
}
break;
case 's': //向下移动
if (direction != 0) {
direction = 2;
}
break;
case 'a': //向左移动
if (direction != 1) {
direction = 3;
}
break;
}
}
}
int main()
{
srand((unsigned int)time(NULL)); //设置随机数种子
init_map(); //初始化地图
init_snake(); //初始化蛇
generate_food(); //随机生成食物
while (1) {
print_map(); //打印地图
input(); //获取用户输入
move_snake(); //移动蛇
Sleep(100); //等待0.1秒
}
return 0;
}
```
这个代码示例只是一个简单的贪吃蛇游戏,还有很多需要改进的地方,比如增加音效、增加关卡、增加障碍物等等,你可以根据自己的需求进行修改和扩展。