首页 > 文章资讯 > 游戏问答
贪吃蛇(C语言)
更新时间:2022-05-02 02:15:02 作者:云寺手游
2开元2022棋牌
开元2022棋牌
益智棋牌 | 47.56MB | v3.3.7
神殿娱乐
神殿娱乐
休闲益智 | 47.56MB | v3.3.7
我才是棋牌
我才是棋牌
休闲益智 | 47.56MB | v3.3.7
所谓棋牌
所谓棋牌
休闲益智 | 47.56MB | v3.3.7

用最简单的方式来教大家写一个小游戏(贪吃蛇)。保证学会哦。
首先我们需要一个二维数组来保存地图、蛇的位置和食物的位置。
地图空的位置用0表示,食物位置用-1来表示,蛇的位置用1、2、3、。。。来表示,其中1代表蛇头,2代表紧接着蛇头的一节身体,3代表再下一节身体,以此类推。下图是一个5*8的数组,表示了一个5*8的地图,一个长度为5的蛇,和一个用-1表示的食物的位置。


我们再看看游戏运行流程:
初始化游戏,包括初始化地图,食物的位置,蛇的位置
获取玩家输入的控制命令
根据输入的控制命令移动蛇的位置(左转、右转还是方向不变),并且返回移动的结果(是吃到食物,还是撞到障碍物,还是其他)
分析移动的结果,如果是吃到了食物,分数加一,撞到了障碍物结束游戏。
将蛇画到控制台
回到2过程


有了以上思路,我们就可以着手写代码了。
#include<stdio.h>
#include<Windows.h>
#include<time.h>
#include<conio.h>
#define SIZE 19 //定义地图大小
void init(int*, int*, int[SIZE][SIZE]);
int getCommand(int);
int move(int, int*, int[SIZE][SIZE]);
void draw(int[SIZE][SIZE]);
int main()
{
int map[SIZE][SIZE] = { 0 };
int command, result, length = 0; //length表示蛇的长度
init(&command, &length, map); //初始化游戏
while (1)
{
command = getCommand(command); //获取玩家输入的命令
result = move(command, &length, map); //根据命令移动蛇
if (result == 1) //分析移动的结果
{
break;
}
else
{
draw(map); //将蛇画到控制台
}
Sleep(1000);
}
}
void init(int *command, int *length, int map[SIZE][SIZE])
{
//初始化初始命令
*command = 2; //初始命令向下
//初始化蛇的长度
*length = 3;
//初始化蛇的位置
map[2][1] = 1;
map[1][1] = 2;
map[0][1] = 3;
//初始化食物的位置
map[3][3] = -1;
}
int getCommand(int command)
{
int temp = -1;
if (_kbhit()) //如果有键盘输入
{
switch (_getch())
{
case 'A': case 'a': temp = 3; break; //表示向左
case 'S': case 's': temp = 2; break; //表示向下
case 'D': case 'd': temp = 1; break; //表示向右
case 'W': case 'w': temp = 0; break; //表示向上
}
if (temp != -1 && abs(command - temp) != 2) //蛇不能反向
{
command = temp;
}
}
return command;
}
int move(int command, int *length, int map[SIZE][SIZE])
{
int i, j, head_i, head_j;
for (i = 0; i < SIZE; i++)
{
for (j = 0; j < SIZE; j++)
{
if (map[j] == *length) //如果是蛇的最后一节身体
{
map[j] = 0;
}
else if (map[j] > 1) //如果是蛇中间节的身体
{
map[j] += 1;
}
else if (map[j] == 1) //如果是蛇头
{
map[j] += 1;
//确定蛇头新的位置head_i和head_j
switch (command)
{
case 0: head_i = i - 1, head_j = j; break;
case 1: head_i = i, head_j = j + 1; break;
case 2: head_i = i + 1, head_j = j; break;
case 3: head_i = i, head_j = j - 1; break;
}
}
}
}
if (map[head_i][head_j] == -1) //如果吃到了食物
{
map[head_i][head_j] = 1;
*length = *length + 1; //蛇的长度加一
//重新生成食物
while (1)
{
i = rand() % SIZE, j = rand() % SIZE;
if (map[j] == 0)
{
map[j] = -1;
break;
}
}
}
//如果撞到了自己或者撞到了墙
else if (map[head_i][head_j] > 0 || head_i < 0 || head_i == SIZE || head_j < 0 ||head_j == SIZE)
{
return 1;
}
else
{
map[head_i][head_j] = 1;
}
return 0;
}
void draw(int map[SIZE][SIZE])
{
int i, j;
system("cls"); //清除屏幕
for (i = 0; i <= SIZE; i++) //输出上边框
{
if (i == 0)
{
printf("┏");
}
else if (i == SIZE)
{
printf(" ━━┓");
}
else
{
printf(" ━");
}
}
printf("\n");
for (i = 0; i < SIZE; i++)
{
for (j = 0; j < SIZE; j++)
{
if (j == 0) //输出左边框
{
printf("┃ ");
}
if (map[j] == 1) //输出蛇头
{
printf("○");
}
else if (map[j] > 1) //输出蛇身
{
printf("●");
}
else if (map[j] == -1) //输出食物
{
printf("★");
}
else
{
printf("");
}
if (j == SIZE - 1) //输出右边框
{
printf("┃ ");
}
}
printf("\n");
}
for (i = 0; i <= SIZE; i++) //输出下边框
{
if (i == 0)
{
printf("┗");
}
else if (i == SIZE)
{
printf(" ━━┛");
}
else
{
printf(" ━");
}
}
printf("\n");
}程序的运行效果是这样的:


<hr/>文章包含于


为什么我照着你的打出来却又错误呢?大神可否指导一下!