在写代码之前,要先理清逻辑,首先要有进入页面,开始游戏,游戏结束,
分布来进行设计
如图可以看到在控制台中他的长度和宽度都是设定好的,这个可以在控制台中修改
而控制台的名字也同样变化了,这个可以用title命令进行修改
我们看到界面中间出现字体,说明光标不是从开头进行的,我们可以通过代码修改光标出现的位置
接着通过代码屏蔽掉界面显示的光标
HANDLE handle =GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO CursorInfo;
GetConsoleCursorInfo(handle, &CursorInfo);
CursorInfo.bVisible = false;
SetConsoleCursorInfo(handle, &CursorInfo);
按任意键进行直接用pause()函数;
接着我们进入下个界面 和上述同理
可以发现图中出现了方格 圆圈和型号图案 这些图案看大小肯定不是只占一个宽度 而是占两个而此时就需要用wprintf(L“%lc”,’图案‘);进行输出
而右面的字体同理 改变光标的位置进行输出
这些过程制作完毕后 游戏进入界面就完成了 之后就是开始游戏
首先还是进行逻辑分析 先将我们以后可能用到的数据写入结构体中
//贪吃蛇
typedef struct Snake
{
pSnakeNode pSnake;//维护整条蛇的指针,是指向蛇头
pSnakeNode pFood;//指向食物的指针
int Score;//当前累积的分数
int FoodWeight;//一个食物的分数
int SleepTime;//蛇休眠的时间,休眠的时间越短,蛇的速度越快,休眠的时间越长,蛇的速度越慢
enum GAME_STATUS status;//游戏当前的状态
enum DIRECTION dir;//蛇当前走的方向
//...
}Snake, * pSnake;
开始之前我们要先将数据初始化 而图中的贪吃蛇最适合的数据就是顺序链表
因此我们先创建一个长度为5的顺序链表
//蛇身结点的定义
typedef struct SnakeNode
{
int x;
int y;
struct SnakeNode* next;
}SnakeNode, * pSnakeNode;
void InitSnake(pSnake ps)
{
//创建5个蛇身的结点
pSnakeNode cur = NULL;
int i = 0;
for (i = 0; i < 5; i++)
{
cur = (pSnakeNode)malloc(sizeof(SnakeNode));
if (cur == NULL)
{
perror("InitSnake():malloc()");
return;
}
cur->x = POS_X + 2 * i;
cur->y = POS_Y;
cur->next = NULL;
//头插法
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"%lc", BODY);
cur = cur->next;
}
//贪吃蛇的其他信息初始化
ps->dir = RIGHT;
ps->FoodWeight = 10;
ps->pFood = NULL;
ps->Score = 0;
ps->SleepTime = 200;
ps->status = OK;
}
接着要创建食物,同样通过创建链表的形式
void CreateFood(pSnake ps)
{
int x = 0;
int y = 0;
again:
do
{
x = rand() % 53 + 2;
y = rand() % 24 + 1;
} while (x % 2 != 0);
pSnakeNode cur1 = NULL;
cur1 = ps->pSnake;
while (cur1==NULL)
{
if (x == cur1->x && y == cur1->y)
{
goto again;
}
}
pSnakeNode pFood=(pSnakeNode)malloc(sizeof(SnakeNode));
pFood->x = x;
pFood->y = y;
ps->pFood = pFood;
SetPos(x, y);
wprintf(L"%lc", FOOD);
}
创建完后就可以让蛇在图案中行动了,按左键向左走,按右键向右走,要让代码识别键盘上的信息
#define KEY_PRESS(VK) ( (GetAsyncKeyState(VK) & 0x1) ? 1 : 0 )
SnakeMove(ps);
do
{
//当前的分数情况
SetPos(62, 10);
printf("总分:%5d\n", ps->Score);
SetPos(62, 11);
printf("食物的分值:%02d\n", ps->FoodWeight);
//检测按键
//上、下、左、右、ESC、空格、F3、F4
if (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_F3))
{
if (ps->SleepTime >= 80)
{
ps->SleepTime -= 30;
ps->FoodWeight += 2;
}
}
else if (KEY_PRESS(VK_F4))
{
if (ps->FoodWeight > 2)
{
ps->SleepTime += 30;
ps->FoodWeight -= 2;
}
}
//走一步
SnakeMove(ps);
Sleep(ps->SleepTime);
} while (ps->status == OK);
}
void SnakeMove(pSnake ps)
{
pSnakeNode pNext = (pSnakeNode)malloc(sizeof(SnakeNode));
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);
}
int NextIsFood(pSnake ps, pSnakeNode pNext)
{
if (pNext->x == ps->pFood->x && pNext->y == ps->pFood->y)
{
return 1;
}
else
return 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 KillByWall(pSnake ps)
{
if (ps->pSnake->x == 56 || ps->pSnake->x == 0 ||
ps->pSnake->y == 25 || ps->pSnake->y == 0)
{
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 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 GameEnd(pSnake ps)
{
SetPos(15, 12);
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;
pSnakeNode del = NULL;
while (cur)
{
del = cur;
cur = cur->next;
free(del);
}
free(ps->pFood);
ps = NULL;
}