【C语言】贪吃蛇(c语言含源码)
https://i-blog.csdnimg.cn/blog_migrate/d7aea2dd9473bae1b1d93ccdb2c50b04.gif简述
我们要实现一个在控制台上的贪吃蛇,制品是下图这种。
https://i-blog.csdnimg.cn/blog_migrate/86be672f09629a47115405d10fee623b.png
先调解情况
我们可以看到我在控制台上有贪吃蛇名字,怎样实现呢?
https://i-blog.csdnimg.cn/blog_migrate/4b7aabda5fe16f140f5caa29c7ed146e.png
起首我们要将我们的vs终端改为Windows控制台主机。步调如下
1)先鼠标右击上图终端的白色边框,当出现选项时。2)左击属性
3)点终端在有下面选择Windows控制台主机。最后如下图https://i-blog.csdnimg.cn/blog_migrate/f8214bea2e042db097900cfc76a58040.png
后续就通过函数来调解情况,在后续一步一步讲授
本地化
C语言为顺应国际化就提供了本地模式和尺度模式
setlocale函数
用此函数修改模式
改为本地模式
setlocale(LC_ALL," ");
改为尺度模式
setlocale(LC_ALL,"C");
宽字符的打印
使用wprintf函数,该函数打印的字符占两个空间,它和printf使用区别在于要在双引号前加上L。
//打印一个字符c
wprintf(L"%c",L'c');
但在我写的代码中就算使用了wprintf照旧只占一个空间,因为我的控制台情况没改。
https://i-blog.csdnimg.cn/blog_migrate/250a7ce5fde1e899e51a9dcd6fcc7473.png
将此处改为宋体就好。
创建蛇,用链表实现
当我们要实现这样一个贪吃蛇时,我们先要考虑用什么数据结构来实现,在这我们使用链表来搞。
可以看到我们蛇是一个一个节点连出来的。所以我们也将蛇的节点封装起来。
封装节点
那在节点中我们存储什么东西呢?在我们上述的终端中我们是可以用坐标把每一个地方表示出来的。所以我们就在节点中存储每一个节点的坐标和下一个节点的地点。因此代码实现就如下
//蛇的节点、坐标,指针
typedef struct SnakeNode
{
int x;
int y;//坐标信息
struct SnakeNode* next;//下一个节点
}SnakeNode;
typedef SnakeNode* pSnakeNode;
封装蛇
那我们要实现一个蛇,我们就要考虑内里要包罗哪些成员。起首我们肯定要有1.找到蛇身的蛇头指针,2.一个蛇活动的方向也需要,3.蛇每移动一下的状态也会有差别,4.从最后的原型图可以看出我们另有总分,5.一个食品的分,7,食品的分又是根据速度来的,速度我们就用当前代码休眠时间来表示,休眠时间约大速度越慢,8.食品我们也储存在内里。
蛇的方向和蛇的状态有很多种,我们就用enum罗列出来。
//方向上下左右
enum DIRECTION
{
UP = 1,
DOWN,
LEFT,
RIGHT
};
//蛇的状态,正常,撞墙死,撞自己死,正常退出
enum GAME_STATUS
{
NORMAL,
KILL_BY_WALL,
KILL_BY_SELF,
EXIT_NORMAL
};
//贪吃蛇对象
typedef struct Snake
{
pSnakeNode psnake;//蛇头
pSnakeNode pfood;//食物
enum DIRECTION direction;//蛇的方向
enum GAME_STATUS status;//蛇的状态
int food_wight;//一个食物分数
int sorce;//总分数
int sleep_time;//休息时间
}Snake;
typedef Snake* pSnake;
游戏初始化
初始化游戏函数
//初始化游戏
void GameStart(pSnake ps);
在这个函数中我们需要将1.控制台界面巨细安排好,2.光标隐藏起来,3. 打印情况界面和功能先容,4. 绘制地图,5. 创建蛇,6. 创建食品。
对界面的操纵详见我上一篇关于Win32API的文章
Win32API的文章
具体实现如下:
void GameStart(pSnake ps)
{
//1.修改界面大小
system("mode con cols=150 lines=30");
system("title 贪吃蛇");
//2.隐藏光标
HANDLE hOutPut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO CursorInfo ;
GetConsoleCursorInfo(hOutPut, &CursorInfo);//获取控制台光标信息
CursorInfo.bVisible = false;
SetConsoleCursorInfo(hOutPut, &CursorInfo);//隐藏控制台光标信息
//3. 打印环境界面和功能介绍
WelcomeToGame();
//4. 绘制地图
CreateMap();
//5. 创建蛇
IniteSnake(ps);
//6. 创建食物
IniteFood(ps);
}
光标设置函数
我们封装一个函数来设置光标坐标,以便后续使用。
void SetPos(short x, short y)
{
//获取一个句柄
HANDLE hOutPut = GetStdHandle(STD_OUTPUT_HANDLE);
//坐标
COORD pos = { x,y };
//设置光标坐标
SetConsoleCursorPosition(hOutPut, pos);
}
打印情况界面和功能先容函数
我们在这个函数中打印欢迎界面和游戏玩法。
void WelcomeToGame()
{
SetPos(40, 14);
wprintf(L"欢迎来到贪吃蛇小游戏\n");
SetPos(42, 20);
system("pause");
system("cls");
SetPos(25, 14);
wprintf(L"用 ↑. ↓ . ← . → 来控制蛇的移动,按F3加速,F4减速\n");
SetPos(25, 15);
wprintf(L"加速能够得到更高的分数\n");
SetPos(42, 20);
system("pause");
system("cls");
}
绘制地图函数
我们绘制一个80行27列的地图。我们先将上下两行界限绘制出来,在绘制左右两个界限,注意左右时我们要从1到25最前面的已经绘制好了。另有每次绘制的实在光标位置肯定要盘算好根据本身设置的地图巨细。
void CreateMap()
{
SetPos(0, 0);
int i = 0;
for (i = 0; i < 80; i++)
{
wprintf(L"%lc", WALL);
}
//下
SetPos(0, 26);
for (i = 0; i < 80; i++)
{
wprintf(L"%lc", WALL);
}
for ( i = 1; i <= 25; i++)
{
SetPos(0, i);
wprintf(L"%lc", WALL);
}
//右
for ( i = 1; i <= 25; i++)
{
SetPos(79, i);
wprintf(L"%lc", WALL);
}
system("pause");
}
创建蛇
在这个函数中我们初始蛇有5个节点,用malloc申请空间,然后用头插法将节点接洽起来,然后打印。在将蛇的状态等成员设置。在创建蛇时因为我是字符只占一个空间,我就随便可以创建,但是有占两个时要在设置x坐标是下一个是加2。
void IniteSnake(pSnake ps)
{
pSnakeNode node = NULL;
//初始蛇身有5个节点
for (int i = 0; i < 5; i++)
{
node = (pSnakeNode)malloc(sizeof(SnakeNode));
if (node == NULL)
{
perror("IniteSnake malloc");
exit(1);
}
//设置坐标
node->x = POS_X + i;
node->y = POS_Y;
node->next = NULL;
//头插
if (ps->psnake == NULL)
ps->psnake = node;
else
{
node->next = ps->psnake;
ps->psnake = node;
}
}
//打印蛇身
node = ps->psnake;
while (node)
{
SetPos(node->x, node->y);
wprintf(L"%c",BODY);
node = node->next;
}
//设置蛇状态
ps->direction = RIGHT;
ps->sleep_time = 300;
ps->status = NORMAL;
ps->sorce = 0;
ps->food_wight = 30;
}
创建食品
我们创建一个食品节点,通过随机数生成坐标,但是要该坐标不能是墙,不能是蛇身。最后打印出来。在创建食品时因为我是字符只占一个空间,我就不用考虑蛇是否可以吃到食品,但是有占两个时要在设置食品x坐标要和蛇节点吻合,可以让蛇蛇吃到。
void IniteFood(pSnake ps)
{
pSnakeNode food = (pSnakeNode)malloc(sizeof(SnakeNode));
if (food == NULL)
{
perror("IniteFood malloc");
exit(1);
}
//随机生成食物坐标,判断与蛇是否重合
int x = 0;
int y = 0;
again:
x = rand() % 78 + 1;
y = rand() % 23 + 1;
pSnakeNode cur = ps->psnake;
while (cur)
{
if (x == cur->x && y == cur->y)
{
goto again;
}
cur = cur->next;
}
food->x = x;
food->y = y;
food->next = NULL;
ps->pfood = food;
//打印食物
SetPos(food->x, food->y);
wprintf(L"%c", FOOD);
}
游戏运行
void GameRun(pSnake ps)
{
do {
SetPos(90, 10);
//右侧打印帮助信息
PrintInfo(ps);
//获取键盘信息
//移动方向
if (KEY_PRESS(VK_UP) && ps->direction != DOWN)
ps->direction = UP;
else if (KEY_PRESS(VK_DOWN) && ps->direction != UP)
ps->direction = DOWN;
else if (KEY_PRESS(VK_RIGHT) && ps->direction != LEFT)
ps->direction = RIGHT;
else if (KEY_PRESS(VK_LEFT) && ps->direction != RIGHT)
ps->direction = LEFT;
//加减速
else if (KEY_PRESS(VK_F3))
{
if (ps->food_wight <= 50 && ps->food_wight >= 10)
{
ps->sleep_time -= 10;
ps->food_wight += 2;
}
}
else if (KEY_PRESS(VK_F4))
{
if (ps->food_wight <= 50 && ps->food_wight >= 10)
{
ps->sleep_time += 10;
ps->food_wight -= 2;
}
}
//暂停
else if (KEY_PRESS(VK_SPACE))
pause();
//退出
else if (KEY_PRESS(VK_ESCAPE))
{
ps->status = EXIT_NORMAL;
break;
}
//判断分数超出限制调回
if (ps->food_wight > 50)
{
ps->food_wight = 50;
}
else if (ps->food_wight < 10)
{
ps->food_wight = 10;
}
//蛇移动
Sleep(ps->sleep_time);
SnakeMove(ps);
} while (ps->status == NORMAL);
}
打印帮助信息函数
我们在游戏界面右边打印帮助信息。打印完帮助信息后我们在通过获取键盘信息来判断下一步应该怎么做。
void PrintInfo(pSnake ps)
{
printf("当前总分:%d", ps->sorce);
SetPos(90, 11);
printf("当前一个食物分数: %2d", ps->food_wight);
SetPos(90, 12);
printf("不能穿墙,不能咬到自己");
SetPos(90, 13);
printf("空格键暂停,esc键退出");
SetPos(90, 14);
printf("用 ↑. ↓ . ← . → 来控制蛇的移动,按F3加速,F4减速");
SetPos(90, 15);
printf("加速减速最多10次,加速一次食物分数加2,减速一次食物分数减2");
}
停息函数
当我们接收键盘信息为空格停息时,我们调用该函数,该函数本质就是死循环睡眠。再次接收到空格时我们就恢复。
void pause()
{
while (1)
{
Sleep(300);
if (KEY_PRESS(VK_SPACE))
break;
}
}
蛇移动函数
我们根据蛇移动方向来判断下一步在哪,实在就是将下一个位置处创建结点当头结点。但是我们需要判断移动后头结点位置是食品,照旧墙,照旧正常位置。然后看是否需要释放最后一个节点。
void SnakeMove(pSnake ps)
{
//创建一个节点
pSnakeNode node = (pSnakeNode)malloc(sizeof(SnakeNode));
if (node == NULL)
{
perror("SnakeMove malloc");
exit(1);
}
node->next = NULL;
//通过switch case来将下一个节点坐标赋值
switch (ps->direction)
{
case UP:
node->x = ps->psnake->x;
node->y = ps->psnake->y - 1;
break;
case DOWN:
node->x = ps->psnake->x;
node->y = ps->psnake->y + 1;
break;
case RIGHT:
node->x = ps->psnake->x + 1;
node->y = ps->psnake->y;
break;
case LEFT:
node->x = ps->psnake->x - 1;
node->y = ps->psnake->y;
break;
}
pSnakeNode tempNode = node;
//下一个节点是食物
if (node->x == ps->pfood->x && node->y == ps->pfood->y)
{
EatFood(ps, node);
}
//不是食物
else
{
NoFood(ps,node);
}
//判断移动后是否死来
if (NextIsSelf (ps,tempNode)== 1)
{
ps->status = KILL_BY_SELF;
}
else if (tempNode->x == 0
|| tempNode->x == 79
|| tempNode->y == 0
|| tempNode->y == 27)
{
ps->status = KILL_BY_WALL;
}
}
下一个是食品
当下个是食品我们就不用对最后一个节点释放。吃掉之后释放掉在创建下一个食品。
void EatFood(pSnake ps,pSnakeNode node)
{
//头插
node->next = ps->psnake;
ps->psnake = node;
//打印蛇
pSnakeNode cur = ps->psnake;
ps->sorce += ps->food_wight;
while (cur)
{
SetPos(cur->x, cur->y);
wprintf(L"%c", BODY);
cur = cur->next;
}
//释放食物
free(ps->pfood);
IniteFood(ps);
}
下一个不是食品
下一个不是食品蛇往下一步走后需要释放掉最后一个节点。
void NoFood(pSnake ps, pSnakeNode node)
{
//头插
node->next = ps->psnake;
ps->psnake = node;
//打印蛇
pSnakeNode cur = ps->psnake;
while (cur)
{
SetPos(cur->x, cur->y);
wprintf(L"%c", BODY);
cur = cur->next;
}
cur = ps->psnake;
while (cur->next->next)
{
cur = cur->next;
}
pSnakeNode temp = cur->next;
SetPos(temp->x, temp->y);
printf(" ");
cur->next = NULL;
free(temp);
}
游戏结束
在游戏结束后通过蛇状态来打印死因,然后释放掉所有空间。
void GameOver(pSnake ps)
{
SetPos(40, 15);
if (ps->status == KILL_BY_SELF)
printf("你撞到自己死了");
else if (ps->status == KILL_BY_WALL)
printf("你撞到墙死了");
else if (ps->status == EXIT_NORMAL)
printf("你正常退出了");
pSnakeNode cur = ps->psnake;
while (cur)
{
ps->psnake = cur->next;
free(cur);
cur = ps->psnake;
}
free(ps->pfood);
}
源码
snake.h
#pragma once#include <windows.h>#include <stdbool.h>#include <stdlib.h>#include <stdio.h>#include<locale.h>#include<time.h>#define WALL L'█'#define POS_X 25#define POS_Y 5#define BODY L'○'#define FOOD L'♥'#define KEY_PRESS(VK) ((GetAsyncKeyState(VK)&1)?1:0)//方向上下左右enum DIRECTION{ UP = 1, DOWN, LEFT, RIGHT};//蛇的状态,正常,撞墙死,撞本身死,正常退出enum GAME_STATUS{ NORMAL, KILL_BY_WALL, KILL_BY_SELF, EXIT_NORMAL};//蛇的节点、坐标,指针
typedef struct SnakeNode
{
int x;
int y;//坐标信息
struct SnakeNode* next;//下一个节点
}SnakeNode;
typedef SnakeNode* pSnakeNode;
//贪吃蛇对象typedef struct Snake{ pSnakeNode psnake;//蛇头 pSnakeNode pfood;//食品 enum DIRECTION direction;//蛇的方向 enum GAME_STATUS status;//蛇的状态 int food_wight;//一个食品分数 int sorce;//总分数 int sleep_time;//苏息时间}Snake;typedef Snake* pSnake;//设置光标坐标void SetPos(short x, short y);//初始化游戏
void GameStart(pSnake ps);
//打印欢迎界面void WelcomeToGame();//绘制地图x 80, y 27void CreateMap();//5. 创建蛇void IniteSnake(pSnake ps);//6. 创建食品void IniteFood(pSnake ps);//游戏进行页面void GameRun(pSnake ps);//不是食品void NoFood(pSnake ps, pSnakeNode node);//吃食品void EatFood(pSnake ps, pSnakeNode node);//蛇移动void SnakeMove(pSnake ps);//游戏结束void GameOver(pSnake ps); snake.c
# define _CRT_SECURE_NO_WARNINGS 1;#include"snake.h"//设置光标坐标void SetPos(short x, short y)
{
//获取一个句柄
HANDLE hOutPut = GetStdHandle(STD_OUTPUT_HANDLE);
//坐标
COORD pos = { x,y };
//设置光标坐标
SetConsoleCursorPosition(hOutPut, pos);
}
//3. 打印情况界面和功能先容void WelcomeToGame()
{
SetPos(40, 14);
wprintf(L"欢迎来到贪吃蛇小游戏\n");
SetPos(42, 20);
system("pause");
system("cls");
SetPos(25, 14);
wprintf(L"用 ↑. ↓ . ← . → 来控制蛇的移动,按F3加速,F4减速\n");
SetPos(25, 15);
wprintf(L"加速能够得到更高的分数\n");
SetPos(42, 20);
system("pause");
system("cls");
}
//4. 绘制地图void CreateMap()
{
SetPos(0, 0);
int i = 0;
for (i = 0; i < 80; i++)
{
wprintf(L"%lc", WALL);
}
//下
SetPos(0, 26);
for (i = 0; i < 80; i++)
{
wprintf(L"%lc", WALL);
}
for ( i = 1; i <= 25; i++)
{
SetPos(0, i);
wprintf(L"%lc", WALL);
}
//右
for ( i = 1; i <= 25; i++)
{
SetPos(79, i);
wprintf(L"%lc", WALL);
}
system("pause");
}
//5. 创建蛇void IniteSnake(pSnake ps)
{
pSnakeNode node = NULL;
//初始蛇身有5个节点
for (int i = 0; i < 5; i++)
{
node = (pSnakeNode)malloc(sizeof(SnakeNode));
if (node == NULL)
{
perror("IniteSnake malloc");
exit(1);
}
//设置坐标
node->x = POS_X + i;
node->y = POS_Y;
node->next = NULL;
//头插
if (ps->psnake == NULL)
ps->psnake = node;
else
{
node->next = ps->psnake;
ps->psnake = node;
}
}
//打印蛇身
node = ps->psnake;
while (node)
{
SetPos(node->x, node->y);
wprintf(L"%c",BODY);
node = node->next;
}
//设置蛇状态
ps->direction = RIGHT;
ps->sleep_time = 300;
ps->status = NORMAL;
ps->sorce = 0;
ps->food_wight = 30;
}
//6. 创建食品void IniteFood(pSnake ps)
{
pSnakeNode food = (pSnakeNode)malloc(sizeof(SnakeNode));
if (food == NULL)
{
perror("IniteFood malloc");
exit(1);
}
//随机生成食物坐标,判断与蛇是否重合
int x = 0;
int y = 0;
again:
x = rand() % 78 + 1;
y = rand() % 23 + 1;
pSnakeNode cur = ps->psnake;
while (cur)
{
if (x == cur->x && y == cur->y)
{
goto again;
}
cur = cur->next;
}
food->x = x;
food->y = y;
food->next = NULL;
ps->pfood = food;
//打印食物
SetPos(food->x, food->y);
wprintf(L"%c", FOOD);
}
//初始化游戏void GameStart(pSnake ps)
{
//1.修改界面大小
system("mode con cols=150 lines=30");
system("title 贪吃蛇");
//2.隐藏光标
HANDLE hOutPut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO CursorInfo ;
GetConsoleCursorInfo(hOutPut, &CursorInfo);//获取控制台光标信息
CursorInfo.bVisible = false;
SetConsoleCursorInfo(hOutPut, &CursorInfo);//隐藏控制台光标信息
//3. 打印环境界面和功能介绍
WelcomeToGame();
//4. 绘制地图
CreateMap();
//5. 创建蛇
IniteSnake(ps);
//6. 创建食物
IniteFood(ps);
}
//打印帮助信息void PrintInfo(pSnake ps)
{
printf("当前总分:%d", ps->sorce);
SetPos(90, 11);
printf("当前一个食物分数: %2d", ps->food_wight);
SetPos(90, 12);
printf("不能穿墙,不能咬到自己");
SetPos(90, 13);
printf("空格键暂停,esc键退出");
SetPos(90, 14);
printf("用 ↑. ↓ . ← . → 来控制蛇的移动,按F3加速,F4减速");
SetPos(90, 15);
printf("加速减速最多10次,加速一次食物分数加2,减速一次食物分数减2");
}
//停息void pause()
{
while (1)
{
Sleep(300);
if (KEY_PRESS(VK_SPACE))
break;
}
}
//判断下一个是不是本身int NextIsSelf(pSnake ps, pSnakeNode node){ pSnakeNode cur = ps->psnake->next; while (cur) { if (cur->x == node->x && cur->y == node->y) return 1; cur = cur->next; } return 0;}//吃食品void EatFood(pSnake ps,pSnakeNode node)
{
//头插
node->next = ps->psnake;
ps->psnake = node;
//打印蛇
pSnakeNode cur = ps->psnake;
ps->sorce += ps->food_wight;
while (cur)
{
SetPos(cur->x, cur->y);
wprintf(L"%c", BODY);
cur = cur->next;
}
//释放食物
free(ps->pfood);
IniteFood(ps);
}
//不是食品void NoFood(pSnake ps, pSnakeNode node)
{
//头插
node->next = ps->psnake;
ps->psnake = node;
//打印蛇
pSnakeNode cur = ps->psnake;
while (cur)
{
SetPos(cur->x, cur->y);
wprintf(L"%c", BODY);
cur = cur->next;
}
cur = ps->psnake;
while (cur->next->next)
{
cur = cur->next;
}
pSnakeNode temp = cur->next;
SetPos(temp->x, temp->y);
printf(" ");
cur->next = NULL;
free(temp);
}
//蛇移动void SnakeMove(pSnake ps)
{
//创建一个节点
pSnakeNode node = (pSnakeNode)malloc(sizeof(SnakeNode));
if (node == NULL)
{
perror("SnakeMove malloc");
exit(1);
}
node->next = NULL;
//通过switch case来将下一个节点坐标赋值
switch (ps->direction)
{
case UP:
node->x = ps->psnake->x;
node->y = ps->psnake->y - 1;
break;
case DOWN:
node->x = ps->psnake->x;
node->y = ps->psnake->y + 1;
break;
case RIGHT:
node->x = ps->psnake->x + 1;
node->y = ps->psnake->y;
break;
case LEFT:
node->x = ps->psnake->x - 1;
node->y = ps->psnake->y;
break;
}
pSnakeNode tempNode = node;
//下一个节点是食物
if (node->x == ps->pfood->x && node->y == ps->pfood->y)
{
EatFood(ps, node);
}
//不是食物
else
{
NoFood(ps,node);
}
//判断移动后是否死来
if (NextIsSelf (ps,tempNode)== 1)
{
ps->status = KILL_BY_SELF;
}
else if (tempNode->x == 0
|| tempNode->x == 79
|| tempNode->y == 0
|| tempNode->y == 27)
{
ps->status = KILL_BY_WALL;
}
}
//游戏进行页面void GameRun(pSnake ps)
{
do {
SetPos(90, 10);
//右侧打印帮助信息
PrintInfo(ps);
//获取键盘信息
//移动方向
if (KEY_PRESS(VK_UP) && ps->direction != DOWN)
ps->direction = UP;
else if (KEY_PRESS(VK_DOWN) && ps->direction != UP)
ps->direction = DOWN;
else if (KEY_PRESS(VK_RIGHT) && ps->direction != LEFT)
ps->direction = RIGHT;
else if (KEY_PRESS(VK_LEFT) && ps->direction != RIGHT)
ps->direction = LEFT;
//加减速
else if (KEY_PRESS(VK_F3))
{
if (ps->food_wight <= 50 && ps->food_wight >= 10)
{
ps->sleep_time -= 10;
ps->food_wight += 2;
}
}
else if (KEY_PRESS(VK_F4))
{
if (ps->food_wight <= 50 && ps->food_wight >= 10)
{
ps->sleep_time += 10;
ps->food_wight -= 2;
}
}
//暂停
else if (KEY_PRESS(VK_SPACE))
pause();
//退出
else if (KEY_PRESS(VK_ESCAPE))
{
ps->status = EXIT_NORMAL;
break;
}
//判断分数超出限制调回
if (ps->food_wight > 50)
{
ps->food_wight = 50;
}
else if (ps->food_wight < 10)
{
ps->food_wight = 10;
}
//蛇移动
Sleep(ps->sleep_time);
SnakeMove(ps);
} while (ps->status == NORMAL);
}
//游戏结束void GameOver(pSnake ps)
{
SetPos(40, 15);
if (ps->status == KILL_BY_SELF)
printf("你撞到自己死了");
else if (ps->status == KILL_BY_WALL)
printf("你撞到墙死了");
else if (ps->status == EXIT_NORMAL)
printf("你正常退出了");
pSnakeNode cur = ps->psnake;
while (cur)
{
ps->psnake = cur->next;
free(cur);
cur = ps->psnake;
}
free(ps->pfood);
}
test.c
# define _CRT_SECURE_NO_WARNINGS 1;
#include"snake.h"
void test()
{
int n = 4;
srand((unsigned int)time(NULL));
do {
//创建贪吃蛇
Snake snake = { 0 };
//初始化游戏
GameStart(&snake);
//运行游戏
GameRun(&snake);
//结束游戏-善后工作
GameOver(&snake);
SetPos(10, 10);
//system("cls");
printf("是否再来一局Y/N: ");
n = getchar();
while (getchar() != '\n');
system("cls");
} while (n == 'Y' || n == 'y');
SetPos(0, 29);
}
int main()
{
//调节为本地状态
setlocale(LC_ALL, "");
test();
return 0;
}
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]