建立文件
首先先建立3个文件。
Snake.h 函数的声明
Snake.c 函数的定义
Test.c 贪吃蛇的测试
分析项目
我们分析这整个项目
建立节点
首先在我们实现游戏开始的部分之前,我们要先创建贪吃蛇的节点,再由此创建整个贪吃蛇所包含的一些信息:
#pragma once//蛇身节点
typedef struct SnakeNode
{int x;int y;struct SnakeNode* next;
}SnakeNode,* pSnakeNode;
//SnakeNode 是struct SnakeNode的重命名
//pSnakeNode 是struct SnakeNode *的重命名//我们再根据此来创建整条蛇
typedef struct Snake
{pSnakeNode pSnake;//维护整条蛇的指针pSnakeNode pFood; //维护食物的指针int score;//当前的分数int foodWeight;//食物的权重int SleepTime;//休眠时间(其实就是你肉眼能看到的一眨一眨的)enum DIRECTION Dir;//蛇重生默认方向enum GAMESTATUS Status;//游戏状态
}Snake,*pSnake;
建立贪吃蛇所包含的一些信息
我们枚举一下这个贪吃蛇中所有的一些状态:
//方向
enum DIRECTION
{UP=1,DOWN,LEFT,RIGHT
};//游戏状态
enum GAME_STATUS
{OK=1,//正常运行ESC, //按了ESC键退出,正常退出KILL_BY_WALL,//撞墙KILL_BY_SELF //撞到自身
};
写Test.c函数
然后我们就能书写测试函数中的内容了
根据这张图来写
#define _CRT_SECURE_NO_WARNINGS 1
#include "Snake.h"
#include <locale.h>
void test()
{int ch=0;srand((unsigned int)time(NULL));do{//先创建一条贪吃蛇(不是物理上的,是物理上乃至整个游戏的地基)Snake snake = { 0 };//GameStart(&snake);//游戏开始//GameRun(&snake);//游戏中//GameEnd(&snake);//游戏结束SetPos(20, 15);printf("本局结束,是否再来一局?(Y/N)--");ch = getchar();getchar();} while (ch == 'Y' || ch == 'y');SetPos(0, 27);//打印程序退出以及代码所在文件地址,这里设置一下是为了美观
}int main()
{//修改当前地区模式,支持中文地区的打印setlocale(LC_ALL, "");//测试贪吃蛇test();return 0;
SetPos
1.其中的SetPos:封装⼀个设置光标位置的函数,他的头文件为#include <window.h>
//设置光标的坐标
void SetPos(short x, short y)
{COORD pos = { x,y };HANDLE hOutput = NULL;//获取标准输出的句柄(⽤来标识不同设备的数值)hOutput = GetStdHandle(STD_OUTPUT_HANDLE);//设置标准输出上光标的位置为posSetConsoleCursorPosition(hOutput, pos);
}
setlocale
2.其中的setlocale为:
<locale.h>提供的函数⽤于控制C标准库中对于不同的地区会产⽣不⼀样⾏为的部分。setlocale 函数⽤于修改当前地区,可以针对⼀个类项修改,也可以针对所有类项。setlocale 的第⼀个参数可以是前⾯说明的类项中的⼀个,那么每次只会影响⼀个类项,如果第⼀个参 数是LC_ALL,就会影响所有的类项。C标准给第⼆个参数仅定义了2种可能取值:"C"和" "。当地区设置为"C"时,库函数按正常⽅式执⾏,⼩数点是⼀个点。当程序运⾏起来后想改变地区,就只能显⽰调⽤setlocale函数。⽤" "作为第2个参数,调⽤setlocale 函数就可以切换到本地模式,这种模式下程序会适应本地环境。⽐如:切换到我们的本地模式后就⽀ 持宽字符(汉字)的输出等。
游戏开始:GameStart 函数
接下来我们实现GameStart函数
void GameStart(pSnake ps)
{//设置控制台窗口的大小,50行,150列system("mode con cols=150 lines=50");//设置窗口名称system("title 贪吃蛇大作战");//获取标准输出的句柄(⽤来标识不同设备的数值)HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);//隐藏光标操作CONSOLE_CURSOR_INFO CursorInfo;GetConsoleCursorInfo(hOutput, &CursorInfo);//获取控制台光标信息CursorInfo.bVisible = false; //隐藏控制台光标SetConsoleCursorInfo(hOutput, &CursorInfo);//设置控制台光标状态//打印欢迎界面WelcomTOGame();//打印地图CreateMap();//初始化蛇InitSnkae(ps);//创造第一个食物CreateFood(ps);
}
我们再实现GameStart的
//打印欢迎界面
WelcomTOGame();
//打印地图
CreateMap();
//初始化蛇
InitSnkae(ps);
//创造第一个食物
CreateFood(ps);
欢迎界面 WelcomTOGame();
void WelcomTOGame()
{SetPos(80, 30);printf("欢迎来到贪吃蛇大作战");SetPos(80, 50);system("pause");system("cls");SetPos(50, 25);printf("⽤ ↑.↓.←.→ 分别控制蛇的移动, F1为加速,F2为减速\n");SetPos(50, 26);printf("加速是给勇士的奖励.");SetPos(40, 25);system("pause");system("cls");getchar();//此时加一个getchar()是为了让他在这里停下,否则我们看不到这个过程,只是为了更//好的测试
}
我们这时候运行一下程序看下结果
这里的 ↑.↓.←.→是用的搜狗输入法符号大全里的
打印地图:CreateMap();
void CreateMap()
{int i = 0;//上下左右SetPos(0, 0);for (i = 0; i < 114; i += 2){wprintf(L"%c", WALL);}SetPos(0, 52);for (i = 0; i < 114; i += 2){wprintf(L"%c", WALL);}for(i = 1; i < 52; i++){SetPos(0, i);wprintf(L"%c", WALL);}for (i = 1; i < 52; i++){SetPos(112, i);wprintf(L"%c", WALL);}//打印提⽰信息SetPos(128, 30);printf("不能穿墙,不能咬自己\n");SetPos(128, 32);printf("利用↑.↓.←.→分别控制蛇的移动.");SetPos(128, 34);printf("F1 为加速,F2 为减速\n");SetPos(128, 36);printf("ESC :退出游戏.space:暂停游戏.");SetPos(128, 40);printf("@你小子别偷懒了");}
这里的WALL在.h文件中用宏定义说明一下
#define WALL L'◇'
这个时候我们再次运行一次代码
跟上次不同的是:这次多了一个规则的地图
初始化蛇 : InitSnkae(ps);
void InitSnake(pSnake ps)
{//创建一个单独的节点,这是为了创建蛇的身体pSnakeNode cur = NULL;int i = 0;//创建蛇身节点,并初始化坐标,利用头插法for (i = 0; i < 5; i++){cur = (pSnakeNode)malloc(sizeof(SnakeNode));if (cur == NULL){perror("malloc fail");return;}//如果cur不为空,就设置坐标cur->next = NULL;cur->x = POS_X + i * 2;cur->y = POS_Y;//然后给本游戏的蛇进行设置if (ps->pSnake == NULL){ps->pSnake = cur;}else{cur->next = ps->pSnake;ps->pSnake = cur;}}//打印蛇cur = ps->pSnake;while (cur){SetPos(cur->x, cur->y);wprintf(L"%c", BODY);cur = cur->next;}ps->SleepTime = 200;ps->score = 0;ps->status = OK;ps->dir = RIGHT;ps->foodWeight = 10;getchar();
}
我们再次运行:此时就有蛇的存在了
创造第一个食物:CreateFood(ps);
到这里我们还差创建食物
void CreateFood(pSnake ps)
{int x = 0;int y = 0;again://x坐标为2的倍数,如果他一直不为2的倍数就一直算do{x = rand() % 109 + 2;y = rand() % 52 + 1;} while (x % 2 != 0);pSnakeNode cur = ps->pSnake;//获取指向蛇头的指针//食物不能和蛇身冲突while (cur){if (cur->x == x && cur->y == y){goto again;}cur = cur->next;}pSnakeNode pFood = (pSnakeNode)malloc(sizeof(SnakeNode));//创建食物if (pFood == NULL){perror("malloc fail");return;}pFood->x = x;pFood->y = y;ps->pFood = pFood;SetPos(x, y);wprintf(L"%lc", FOOD);
}
这个时候我们再次运行代码
食物出现了,也就是说我们游戏开始的过程完成了,接下来就是游戏中的代码了。
游戏中:GameRun 函数
void GameRun(pSnake ps)
{do{//当前的分数情况SetPos(124, 20);printf("总分:%5d\n", ps->score);SetPos(124, 22);printf("食物的分值:%02d\n", ps->FoodWeight);//检测按键//上、下、左、右、ESC、空格、F1、F2if (KEY_PRESS(VK_UP) && ps->dir != DOWN){ps->dir = UP;}else if (KEY_PRESS(VK_DOWN) && ps->dir != UP){ps->dir = DOWN;}else if (KEY_PRESS(VK_LEFT) && ps->dir != RIGHT){ps->dir = LEFT;}else if (KEY_PRESS(VK_RIGHT) && ps->dir != LEFT){ps->dir = RIGHT;}else if (KEY_PRESS(VK_ESCAPE)){ps->status = ESC;break;}else if (KEY_PRESS(VK_SPACE)){//游戏要暂定pause();//暂定和回复暂定}else if (KEY_PRESS(VK_F1)){if (ps->SleepTime >= 80){ps->SleepTime -= 30;ps->FoodWeight += 2;}}else if (KEY_PRESS(VK_F2)){if (ps->FoodWeight > 2){ps->SleepTime += 30;ps->FoodWeight -= 2;}}//走一步SnakeMove(ps);//睡眠一下Sleep(ps->SleepTime);} while (ps->status == OK);
}
上述代码有pause函数和虚拟按键,和SnakeMove函数没有介绍到
pause函数
void pause()//暂停
{while (1){Sleep(300);if (KEY_PRESS(VK_SPACE)){break;}}
}
虚拟按键
在程序中需要添加宏定义:#define KEY_PRESS(VK) ( (GetAsyncKeyState(VK) & 0x1) ? 1 : 0 )
SnakeMove函数
void SnakeMove(pSnake ps)
{pSnakeNode pNext = (pSnakeNode)malloc(sizeof(SnakeNode));if (pNext == NULL){perror("SnakeMove()::malloc()");return;}pNext->next = NULL;switch (ps->dir){case UP:pNext->x = ps->pSnake->x;pNext->y = ps->pSnake->y - 1;break;case DOWN:pNext->x = ps->pSnake->x;pNext->y = ps->pSnake->y + 1;break;case LEFT:pNext->x = ps->pSnake->x - 2;pNext->y = ps->pSnake->y;break;case RIGHT:pNext->x = ps->pSnake->x + 2;pNext->y = ps->pSnake->y;break;}//下一个坐标处是否是食物if (NextIsFood(ps, pNext)){//是食物就吃掉EatFood(ps, pNext);}else{//不是食物就正常一步NotEatFood(ps, pNext);}//检测撞墙KillByWall(ps);//检测撞到自己KillBySelf(ps);
}
这里面又有5个函数不知道:NextIsFood EatFood NotEatFood KillByWall KillBySelf
NextIsFood
//pSnakNode psn 是下一个节点的地址
//pSnake ps 维护蛇的指针
int NextIsFood(pSnake ps, pSnakeNode pNext)
{if (ps->pFood->x == pNext->x && ps->pFood->y == pNext->y)return 1;//下一个坐标处是食物elsereturn 0;
}
EatFood
void EatFood(pSnake ps, pSnakeNode pNext)
{pNext->next = ps->pSnake;ps->pSnake = pNext;//打印蛇pSnakeNode cur = ps->pSnake;while (cur){SetPos(cur->x, cur->y);wprintf(L"%lc", BODY);cur = cur->next;}ps->score += ps->FoodWeight;//释放旧的食物free(ps->pFood);//新建食物CreateFood(ps);
}
NotEatFood
void NotEatFood(pSnake ps, pSnakeNode pNext)
{//头插法pNext->next = ps->pSnake;ps->pSnake = pNext;//释放尾结点pSnakeNode cur = ps->pSnake;while (cur->next->next){SetPos(cur->x, cur->y);wprintf(L"%lc", BODY);cur = cur->next;}//将尾节点的位置打印成空白字符SetPos(cur->next->x, cur->next->y);printf(" ");free(cur->next);cur->next = NULL;//易错
}
KillByWall
//检测是否撞墙
void KillByWall(pSnake ps)
{if (ps->pSnake->x == 0 ||ps->pSnake->x == 112 ||ps->pSnake->y == 0 ||ps->pSnake->y == 52){ps->status = KILL_BY_WALL;}
}
KillBySelf
//检测是否撞自己
void KillBySelf(pSnake ps)
{pSnakeNode cur = ps->pSnake->next;//从第二个节点开始while (cur){if (cur->x == ps->pSnake->x && cur->y == ps->pSnake->y){ps->status = KILL_BY_SELF;return;}cur = cur->next;}
}
游戏结束:GameEnd函数
此时程序总体还差一个GameEnd函数
void GameEnd(pSnake ps)
{SetPos(30, 24);switch (ps->status){case ESC:printf("主动退出游戏,正常退出\n");break;case KILL_BY_WALL:printf("很遗憾,撞墙了,游戏结束\n");break;case KILL_BY_SELF:printf("很遗憾,咬到自己了,游戏结束\n");break;}pSnakeNode cur = ps->pSnake;//释放蛇身的节点while (cur){pSnakeNode del = cur;cur = cur->next;free(del);}
}
最后我们给上最终代码
最终代码
Snake.h
#pragma once
#include <locale.h>
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
#include <stdbool.h>#define WALL L'□'
#define BODY L'●'
#define FOOD L'★'//蛇的初始位置
#define POS_X 24
#define POS_Y 5
// #define KEY_PRESS(VK) ( (GetAsyncKeyState(VK) & 0x1) ? 1 : 0 )
#define KEY_PRESS(VK) ( (GetAsyncKeyState(VK) & 0x1) ? 1 : 0 )//方向
enum DIRECTION
{UP=1,DOWN,LEFT,RIGHT
};//游戏状态
enum GAME_STATUS
{OK=1,//正常运行ESC, //按了ESC键退出,正常退出KILL_BY_WALL,//撞墙KILL_BY_SELF //撞到自身
};//蛇身节点
typedef struct SnakeNode
{int x;int y;struct SnakeNode* next;
}SnakeNode,* pSnakeNode;
//SnakeNode 是struct SnakeNode的重命名
//pSnakeNode 是struct SnakeNode *的重命名//我们再根据此来创建整条蛇
typedef struct Snake
{pSnakeNode pSnake;//维护整条蛇的指针pSnakeNode pFood; //维护食物的指针int score;//当前的分数int FoodWeight;//食物的权重int SleepTime;//休眠时间(其实就是你肉眼能看到的一眨一眨的)enum DIRECTION dir;//蛇重生默认方向enum GAMESTATUS status;//游戏状态
}Snake,* pSnake;//游戏开始前的初始化
void GameStart(pSnake ps);
//游戏运⾏过程
void GameRun(pSnake ps);
//游戏结束
void GameEnd(pSnake ps);
//设置光标的坐标
void SetPos(short x, short y);
//欢迎界⾯
void WelcomToGame();
//创建地图
void CreateMap();
//初始化蛇
void InitSnake(pSnake ps);
//创建⻝物
void CreateFood(pSnake ps);
//暂停响应
void pause();
//下⼀个节点是⻝物
int NextIsFood(pSnake ps, pSnakeNode pNext);//下一步要走的位置处就是食物,就吃掉食物
void EatFood(pSnake ps, pSnakeNode pNext);
//下一步要走的位置处不是食物,不吃食物
void NotEatFood(pSnake ps, pSnakeNode pNext);
//撞墙检测
void KillByWall(pSnake ps);
//撞⾃⾝检测
void KillBySelf(pSnake ps);
//蛇的移动
void SnakeMove(pSnake ps);
//游戏初始化
void GameStart(pSnake ps);
//游戏运⾏
void GameRun(pSnake ps);
//游戏结束
void GameEnd(pSnake ps);
Snake.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "Snake.h"//设置光标的坐标
void SetPos(int x, int y)
{COORD pos = { x,y };HANDLE hOutput = NULL;//获取标准输出的句柄(⽤来标识不同设备的数值)hOutput = GetStdHandle(STD_OUTPUT_HANDLE);//设置标准输出上光标的位置为posSetConsoleCursorPosition(hOutput, pos);
}
void CreateMap()
{int i = 0;//上下左右SetPos(0, 0);for (i = 0; i < 114; i += 2){wprintf(L"%c", WALL);}SetPos(0, 52);for (i = 0; i < 114; i += 2){wprintf(L"%c", WALL);}for(i = 1; i < 52; i++){SetPos(0, i);wprintf(L"%c", WALL);}for (i = 1; i < 52; i++){SetPos(112, i);wprintf(L"%c", WALL);}//打印提⽰信息SetPos(128, 30);printf("不能穿墙,不能咬自己\n");SetPos(128, 32);printf("利用↑.↓.←.→分别控制蛇的移动.");SetPos(128, 34);printf("F1 为加速,F2 为减速\n");SetPos(128, 36);printf("ESC :退出游戏.space:暂停游戏.");SetPos(128, 40);printf("@你小子别偷懒了");}
void WelcomToGame()
{SetPos(80, 30);printf("欢迎来到贪吃蛇大作战");SetPos(80, 50);system("pause");system("cls");SetPos(50, 25);printf("⽤ ↑.↓.←.→ 分别控制蛇的移动, F1为加速,F2为减速\n");SetPos(50, 26);printf("加速是给勇士的奖励.");SetPos(40, 25);system("pause");system("cls");
}
void InitSnake(pSnake ps)
{//创建一个单独的节点,这是为了创建蛇的身体pSnakeNode cur = NULL;int i = 0;//创建蛇身节点,并初始化坐标,利用头插法for (i = 0; i < 5; i++){cur = (pSnakeNode)malloc(sizeof(SnakeNode));if (cur == NULL){perror("malloc fail");return;}//如果cur不为空,就设置坐标cur->next = NULL;cur->x = POS_X + i * 2;cur->y = POS_Y;//然后给本游戏的蛇进行设置if (ps->pSnake == NULL){ps->pSnake = cur;}else{cur->next = ps->pSnake;ps->pSnake = cur;}}//打印蛇cur = ps->pSnake;while (cur){SetPos(cur->x, cur->y);wprintf(L"%c", BODY);cur = cur->next;}ps->SleepTime = 200;ps->score = 0;ps->status = OK;ps->dir = RIGHT;ps->FoodWeight = 10;ps->pFood = NULL;
}
void CreateFood(pSnake ps)
{int x = 0;int y = 0;again://x坐标为2的倍数,如果他一直不为2的倍数就一直算do{x = rand() % 109 + 2;y = rand() % 52 + 1;} while (x % 2 != 0);pSnakeNode cur = ps->pSnake;//获取指向蛇头的指针//食物不能和蛇身冲突while (cur){if (cur->x == x && cur->y == y){goto again;}cur = cur->next;}pSnakeNode pFood = (pSnakeNode)malloc(sizeof(SnakeNode));//创建食物if (pFood == NULL){perror("malloc fail");return;}pFood->x = x;pFood->y = y;ps->pFood = pFood;SetPos(x, y);wprintf(L"%lc", FOOD);}
void GameStart(pSnake ps)
{//设置控制台窗口的大小,60行,200列system("mode con cols=200 lines=60");//设置窗口名称system("title 贪吃蛇大作战");//获取标准输出的句柄(⽤来标识不同设备的数值)HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);//隐藏光标操作CONSOLE_CURSOR_INFO CursorInfo;GetConsoleCursorInfo(hOutput, &CursorInfo);//获取控制台光标信息CursorInfo.bVisible = false; //隐藏控制台光标SetConsoleCursorInfo(hOutput, &CursorInfo);//设置控制台光标状态//打印欢迎界面WelcomToGame();//打印地图CreateMap();//初始化蛇InitSnake(ps);//创造第一个食物CreateFood(ps);
}
void pause()
{while (1){Sleep(100);if (KEY_PRESS(VK_SPACE)){break;}}
}int NextIsFood(pSnake ps, pSnakeNode pNext)
{if (ps->pFood->x == pNext->x && ps->pFood->y == pNext->y)return 1;//下一个坐标处是食物elsereturn 0;
}void EatFood(pSnake ps, pSnakeNode pNext)
{pNext->next = ps->pSnake;ps->pSnake = pNext;//打印蛇pSnakeNode cur = ps->pSnake;while (cur){SetPos(cur->x, cur->y);wprintf(L"%lc", BODY);cur = cur->next;}ps->score += ps->FoodWeight;//释放旧的食物free(ps->pFood);//新建食物CreateFood(ps);
}void NotEatFood(pSnake ps, pSnakeNode pNext)
{//头插法pNext->next = ps->pSnake;ps->pSnake = pNext;//释放尾结点pSnakeNode cur = ps->pSnake;while (cur->next->next){SetPos(cur->x, cur->y);wprintf(L"%lc", BODY);cur = cur->next;}//将尾节点的位置打印成空白字符SetPos(cur->next->x, cur->next->y);printf(" ");free(cur->next);cur->next = NULL;//易错
}//检测是否撞墙
void KillByWall(pSnake ps)
{if (ps->pSnake->x == 0 ||ps->pSnake->x == 112 ||ps->pSnake->y == 0 ||ps->pSnake->y == 52){ps->status = KILL_BY_WALL;}
}//检测是否撞自己
void KillBySelf(pSnake ps)
{pSnakeNode cur = ps->pSnake->next;//从第二个节点开始while (cur){if (cur->x == ps->pSnake->x && cur->y == ps->pSnake->y){ps->status = KILL_BY_SELF;return;}cur = cur->next;}
}void SnakeMove(pSnake ps)
{pSnakeNode pNext = (pSnakeNode)malloc(sizeof(SnakeNode));if (pNext == NULL){perror("SnakeMove()::malloc()");return;}pNext->next = NULL;switch (ps->dir){case UP:pNext->x = ps->pSnake->x;pNext->y = ps->pSnake->y - 1;break;case DOWN:pNext->x = ps->pSnake->x;pNext->y = ps->pSnake->y + 1;break;case LEFT:pNext->x = ps->pSnake->x - 2;pNext->y = ps->pSnake->y;break;case RIGHT:pNext->x = ps->pSnake->x + 2;pNext->y = ps->pSnake->y;break;}//下一个坐标处是否是食物if (NextIsFood(ps, pNext)){//是食物就吃掉EatFood(ps, pNext);}else{//不是食物就正常一步NotEatFood(ps, pNext);}//检测撞墙KillByWall(ps);//检测撞到自己KillBySelf(ps);
}void GameRun(pSnake ps)
{do{//当前的分数情况SetPos(124, 20);printf("总分:%5d\n", ps->score);SetPos(124, 22);printf("食物的分值:%02d\n", ps->FoodWeight);//检测按键//上、下、左、右、ESC、空格、F1、F2if (KEY_PRESS(VK_UP) && ps->dir != DOWN){ps->dir = UP;}else if (KEY_PRESS(VK_DOWN) && ps->dir != UP){ps->dir = DOWN;}else if (KEY_PRESS(VK_LEFT) && ps->dir != RIGHT){ps->dir = LEFT;}else if (KEY_PRESS(VK_RIGHT) && ps->dir != LEFT){ps->dir = RIGHT;}else if (KEY_PRESS(VK_ESCAPE)){ps->status = ESC;break;}else if (KEY_PRESS(VK_SPACE)){//游戏要暂定pause();//暂定和回复暂定}else if (KEY_PRESS(VK_F1)){if (ps->SleepTime >= 80){ps->SleepTime -= 30;ps->FoodWeight += 2;}}else if (KEY_PRESS(VK_F2)){if (ps->FoodWeight > 2){ps->SleepTime += 30;ps->FoodWeight -= 2;}}//走一步SnakeMove(ps);//睡眠一下Sleep(ps->SleepTime);} while (ps->status == OK);
}void GameEnd(pSnake ps)
{SetPos(30, 24);switch (ps->status){case ESC:printf("主动退出游戏,正常退出\n");break;case KILL_BY_WALL:printf("很遗憾,撞墙了,游戏结束\n");break;case KILL_BY_SELF:printf("很遗憾,咬到自己了,游戏结束\n");break;}pSnakeNode cur = ps->pSnake;//释放蛇身的节点while (cur){pSnakeNode del = cur;cur = cur->next;free(del);}
}
Test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "Snake.h"void test()
{int ch=0;srand((unsigned int)time(NULL));do{//先创建一条贪吃蛇(不是物理上的,是物理上乃至整个游戏的地基)Snake snake = { 0 };GameStart(&snake);//游戏开始GameRun(&snake);//游戏中GameEnd(&snake);//游戏结束SetPos(20, 15);printf("本局结束,是否再来一局?(Y/N)--");ch = getchar();getchar();} while (ch == 'Y' || ch == 'y');}int main()
{//修改当前地区模式,支持中文地区的打印setlocale(LC_ALL, "");//测试贪吃蛇test();SetPos(0, 27);//打印程序退出以及代码所在文件地址,这里设置一下是为了美观return 0;
}
贪吃蛇视频演示
贪吃蛇展示