目录
1.建议先把源码拿去VS中测试一下了解这个贪吃蛇是什么样的
1.头文件代码
2.源代码
3.测试代码
4.代码详解
1.头文件的解析
2.源代码的解析
1.光标的位置封装函数
2.打印欢迎界面
3.打印整体的一个地图
4.蛇的初始化 (重要)
5.打印边栏信息
6.设置用来暂停的函数
7. 3个函数,关于走的下一步是不是食物
8.关于蛇是怎么死的
9.蛇的移动(重点)
10.游戏的运行(游戏的逻辑)
11.关于整体游戏的测试代码
#pragma once
#include
#include
#include
#include
#define KEY_PRESS(VK) ((GetAsyncKeyState(VK)&0x1) ? 1 : 0)//键盘按键的判断
#define WALL L'□'
#define BODY L'○' //★○●◇◆□■
#define FOOD L'★' //★○●◇◆□■
#define POS_X 24
#define POS_Y 5
typedef struct SnakeNode//蛇身
{
int x;//蛇身的坐标
int y;//蛇身的坐标
struct SnakeNode* next;//下一个蛇身的节点
}SnakeNode ,* pSnakeNode;
typedef struct Snake//贪吃蛇整体
{
pSnakeNode _pbody;//蛇身的指针
pSnakeNode _pfood;//果实的指针
enum DIRECTION _dir;//蛇的方向
enum GAME_STATUS _status;//目前游戏的状态
int _score;//当前的分数
int _foodweight;//每个食物的得分
int _sleeptime;//游戏的速度
}Snake,* pSnake;
enum DIRECTION//snake direction
{
UP = 1,
DOWN,
LEFT,
RIGHT
};
enum GAME_STATUS//the game.s status
{
OK,
KILL_BY_WALL,
KILL_BY_SELF,
END_NORMAL
};
//游戏的整体函数
void GameStart(pSnake ps);
void GameRun(pSnake ps);
void GameEnd(pSnake ps);
void SetPos(short x, short y);
void WelcomeToGame();
void PrintHelpInfo();//print some helpinfo
void CreatMap();
void InitSnake(pSnake ps);
void CreatFood(pSnake ps);
void pause();//game stop
int NextIsFood(pSnakeNode psn, pSnake ps);
void EatFood(pSnakeNode psn, pSnake ps);
void NotFood(pSnakeNode psn, pSnake ps);
int KillByWall(pSnake ps);
int KillBySelf(pSnake ps);
void SnakeMove(pSnake ps);
#include"tcs.h"
void SetPos(short x, short y)
{
COORD pos = { x,y };
HANDLE houtput = NULL;
houtput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(houtput, pos);
}
void WelcomeToGame()
{
SetPos(40, 15);
printf("欢迎来到贪吃蛇\n");
SetPos(40, 25);
system("pause");
system("cls");
SetPos(25, 12);
wprintf(L"⽤ ↑ . ↓ . ← . → 分别控制蛇的移动, F3为加速,F4为减速\n");
SetPos(25, 13);
printf("加速获得的分数更高\n");
SetPos(40, 25);
system("pause");
system("cls");
}
void CreatMap()
{
SetPos(0, 0);
for (int i = 0; i < 58; i = i + 2)
{
wprintf(L"%c", WALL);
}
SetPos(0, 26);
for (int i = 0; i < 58; i= i + 2)
{
wprintf(L"%c", WALL);
}
for (int i = 1; i < 26; i++)
{
SetPos(0, i);
wprintf(L"%c", WALL);
}
for (int i = 1; i < 26; i++)
{
SetPos(56, i);
wprintf(L"%c", WALL);
}
}
void InitSnake(pSnake ps)
{
pSnakeNode cur = NULL;
for (int i = 0; i < 5; i++)
{
cur = (pSnakeNode)malloc(sizeof(SnakeNode));
if (cur == NULL)
{
perror("snakenode don't get:");
return;
}
cur->next = NULL;
cur->x = POS_X + i * 2;
cur->y = POS_Y;
if (ps->_pbody == NULL)
{
ps->_pbody = cur;
}
else
{
cur->next = ps->_pbody;
ps->_pbody = cur;
}
}
//print snake
cur = ps->_pbody;
while (cur)
{
SetPos(cur->x, cur->y);
wprintf(L"%c", BODY);
cur = cur->next;
}
//init snake data
ps->_dir = RIGHT;
ps->_foodweight = 100;
ps->_score = 0;
ps->_sleeptime = 200;
ps->_status = OK;
}
void CreatFood(pSnake ps)
{
int x = 0;
int y = 0;
again:
do
{
x = rand() % 53+2;
y = rand() % 25+1;
} while (x % 2 != 0);
pSnakeNode cur = ps->_pbody;
while (cur)
{
if (cur->x == x && cur->y == y)
{
goto again;
}
cur = cur->next;
}
pSnakeNode pFood = (pSnakeNode)malloc(sizeof(SnakeNode));
if (pFood == NULL)
{
perror("creat pfood failed,fool!\n");
return;
}
else
{
pFood->x = x;
pFood->y = y;
SetPos(pFood->x, pFood->y);
wprintf(L"%c", FOOD);
ps->_pfood = pFood;
}
}
void PrintHelpInfo()
{
SetPos(64, 15);
printf("不能创墙,也不能创自己");
SetPos(64, 16);
printf("用↑.↓.←.→分别控制蛇的移动.");
SetPos(64, 17);
printf("F3 为加速,F4 为减速\n");
SetPos(64, 18);
printf("ESC :退出游戏.space:暂停游戏.");
SetPos(64, 20);
printf("江子龙@版权");
}
void pause()
{
while (1)
{
Sleep(300);
if (KEY_PRESS(VK_SPACE))
{
break;
}
}
}
int NextIsFood(pSnakeNode psn, pSnake ps)
{
return(psn->x == ps->_pfood->x) && (psn->y == ps->_pfood->y);
}
void EatFood(pSnakeNode psn, pSnake ps)
{
psn->next = ps->_pbody;
ps->_pbody = psn;
pSnakeNode cur = ps->_pbody;
while (cur)
{
SetPos(cur->x, cur->y);
wprintf(L"%c", BODY);
cur = cur->next;
}
ps->_score += ps->_foodweight;
free(ps->_pfood);
CreatFood(ps);
}
void NotFood(pSnakeNode psn, pSnake ps)
{
psn->next = ps->_pbody;
ps->_pbody = psn;
pSnakeNode cur = ps->_pbody;
while (cur->next->next)
{
SetPos(cur->x, cur->y);
wprintf(L"%c", BODY);
cur = cur->next;
}
SetPos(cur->next->x, cur->next->y);
printf(" ");
free(cur->next);
cur->next = NULL;
}
int KillByWall(pSnake ps)
{
if ((ps->_pbody->x == 0) || (ps->_pbody->x == 56) || (ps->_pbody->y == 0) || (ps->_pbody->y == 26))
{
ps->_status = KILL_BY_WALL;
return 1;
}
return 0;
}
int KillBySelf(pSnake ps)
{
pSnakeNode cur = ps->_pbody->next;
while (cur)
{
if ((ps->_pbody->x == cur->x) && (ps->_pbody->y == cur->y))
{
ps->_status = KILL_BY_SELF;
return 1;
}
cur = cur->next;
}
return 0;
}
void SnakeMove(pSnake ps)
{
pSnakeNode pNextNode = (pSnakeNode)malloc(sizeof(SnakeNode));
if (pNextNode == NULL)
{
perror("snake move open falied");
return;
}
switch (ps->_dir)
{
case UP:
{
pNextNode->y = ps->_pbody->y - 1;
pNextNode->x = ps->_pbody->x;
}
break;
case DOWN:
{
pNextNode->y = ps->_pbody->y + 1;
pNextNode->x = ps->_pbody->x;
}
break;
case LEFT:
{
pNextNode->y = ps->_pbody->y ;
pNextNode->x = ps->_pbody->x - 2;
}
break;
case RIGHT:
{
pNextNode->y = ps->_pbody->y;
pNextNode->x = ps->_pbody->x + 2;
}
break;
default:
break;
}
if (NextIsFood(pNextNode, ps))
{
EatFood(pNextNode, ps);
}
else
{
NotFood(pNextNode, ps);
}
KillBySelf(ps);
KillByWall(ps);
}
void GameStart(pSnake ps)
{
system("mode con cols=100 lines=30");
system("title 贪吃蛇");
HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO CursorInfo;
GetConsoleCursorInfo(hOutput, &CursorInfo);
CursorInfo.bVisible = false;
SetConsoleCursorInfo(hOutput, &CursorInfo);
WelcomeToGame();
CreatMap();
InitSnake(ps);
CreatFood(ps);
}
void GameRun(pSnake ps)
{
PrintHelpInfo();
do
{
SetPos(64, 10);
printf("得分:%d ", ps->_score);
printf("每个食物得分:%d", ps->_foodweight);
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_RIGHT) && ps->_dir != LEFT)
{
ps->_dir = RIGHT;
}
else if (KEY_PRESS(VK_LEFT) && ps->_dir != RIGHT)
{
ps->_dir = LEFT;
}
else if (KEY_PRESS(VK_SPACE))
{
pause();
}
else if (KEY_PRESS(VK_ESCAPE))
{
ps->_status = END_NORMAL;
break;
}
else if (KEY_PRESS(VK_F3))
{
if (ps->_sleeptime >= 50)
{
ps->_sleeptime -= 30;
ps->_foodweight += 20;
}
}
else if (KEY_PRESS(VK_F4))
{
if (ps->_sleeptime < 350)
{
ps->_sleeptime += 30;
ps->_foodweight -= 20;
if (ps->_sleeptime == 350)
{
ps->_foodweight = 1;
}
}
}
Sleep(ps->_sleeptime);
SnakeMove(ps);
} while (ps->_status == OK);
}
void GameEnd(pSnake ps)
{
pSnakeNode cur = ps->_pbody;
SetPos(24, 12);
switch (ps->_status)
{
case KILL_BY_SELF:
printf("你创了自己!,游戏结束\n");
break;
case KILL_BY_WALL:
printf("你创了墙!game over\n");
break;
case END_NORMAL:
printf("游戏正常结束!\n");
break;
}
while (cur)
{
pSnakeNode del = cur;
cur = cur->next;
free(del);
}
}
#include
#include"tcs.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;
}
首先这一块是取库函数,和定义宏。
KEY_PRESS(VK)封装的是一个关于键盘是否被按下的函数。
这个函数的使用方法就是判断一个键盘按键是否被按下,如果按下它的返回值就是1不是就是0.所以用& 来判断是1还是0就能判断是否按下按键。
这一块就是设置整体游戏函数的结构体,和定义一些参数。当然不习惯用enum定义,也可以用#define 来定义下面的方向以及游戏状态。 用typedef将 蛇身和整体的类型名缩写。
这一块就是等会需要实现的函数。
首先先说下COORD,他是一个结构体在WINDOWS API中定义的,里面存的变量是用来更改光标位置,为什么要改变光标的位置呢?改变光标位置你就可以在任何位置打印数据 。
第二个HANDLE就是一个变量,这个变量可以当做一个控制器,你可以给这个控制器变成任何一种工具,这里就获取了(GetStdHandle),标准输出(STD_OUTPUT_HANDLE),就是上面这个命令提示符。然后设置了此处的光标位置SetConsoleCursorPosition(houtput, pos)
GetStdHandle是获取一个控制器,下面的setconsolecursorposition根据字面意思就是设置光标位置。
void WelcomeToGame()
{
SetPos(40, 15);
printf("欢迎来到贪吃蛇\n");
SetPos(40, 25);
system("pause");
system("cls");
SetPos(25, 12);
wprintf(L"⽤ ↑ . ↓ . ← . → 分别控制蛇的移动, F3为加速,F4为减速\n");
SetPos(25, 13);
printf("加速获得的分数更高\n");
SetPos(40, 25);
system("pause");
system("cls");
}
这一步就是用来打印欢迎界面以及介绍界面的
system("pause");这一步可以用来暂停,按一个键可以继续运行。
system("cls");这一步是用来清屏的
void CreatMap()
{
SetPos(0, 0);
for (int i = 0; i < 58; i = i + 2)
{
wprintf(L"%c", WALL);
}
SetPos(0, 26);
for (int i = 0; i < 58; i= i + 2)
{
wprintf(L"%c", WALL);
}
for (int i = 1; i < 26; i++)
{
SetPos(0, i);
wprintf(L"%c", WALL);
}
for (int i = 1; i < 26; i++)
{
SetPos(56, i);
wprintf(L"%c", WALL);
}
}
首先游戏大小代码里设置的是58*26的就是58列26行。
然后setpos就是第一个函数,用来设定光标的位置,然后
关于wprintf因为在设计墙体蛇身这些图形时一个字符的空间是不够的,所以要用到wprintf,用来打印图形等双字符。且使用wprintf后面要在双引号前加上L表示打印宽字符。头文件是
#include
void InitSnake(pSnake ps)
{
pSnakeNode cur = NULL;
for (int i = 0; i < 5; i++)
{
cur = (pSnakeNode)malloc(sizeof(SnakeNode));
if (cur == NULL)
{
perror("snakenode don't get:");
return;
}
cur->next = NULL;
cur->x = POS_X + i * 2;
cur->y = POS_Y;
if (ps->_pbody == NULL)
{
ps->_pbody = cur;
}
else
{
cur->next = ps->_pbody;
ps->_pbody = cur;
}
}
//print snake
cur = ps->_pbody;
while (cur)
{
SetPos(cur->x, cur->y);
wprintf(L"%c", BODY);
cur = cur->next;
}
//init snake data
ps->_dir = RIGHT;
ps->_foodweight = 100;
ps->_score = 0;
ps->_sleeptime = 200;
ps->_status = OK;
}
关于初始化蛇,就是把snake 结构体的各项都初始化,还有把蛇身整体先打印出来。
关于蛇身的打印,可以根据你的想法打印出具体个数,for循环里更改就好。
首先关于这个蛇的实现我们用的是链表的知识,不知道的同学可以先去补一补链表。
对每个节点都设定好后,就是到while循环了,循环设定好的节点,把它打印在屏幕上。
这里用到setpos来找到需要打印的位置。
后面把snake结构体中的变量都初始化(根据自己喜欢调节就好)就可以进行下一步了。(这一步的蛇只是打印在屏幕上不能动。)
void CreatFood(pSnake ps)
{
int x = 0;
int y = 0;
again:
do
{
x = rand() % 53+2;
y = rand() % 25+1;
} while (x % 2 != 0);
pSnakeNode cur = ps->_pbody;
while (cur)
{
if (cur->x == x && cur->y == y)
{
goto again;
}
cur = cur->next;
}
pSnakeNode pFood = (pSnakeNode)malloc(sizeof(SnakeNode));
if (pFood == NULL)
{
perror("creat pfood failed,fool!\n");
return;
}
else
{
pFood->x = x;
pFood->y = y;
SetPos(pFood->x, pFood->y);
wprintf(L"%c", FOOD);
ps->_pfood = pFood;
}
}
因为玩的是贪吃蛇,我们现在初始化好蛇了,那接下来就是蛇的食物了。
用到rand()随机数。随机生成食物。
goto语句就是找到后面的名称的位置,跳到那个位置执行,(可以做到如果结果错误,重新打印食物)。其他的一看就懂了。
void PrintHelpInfo()
{
SetPos(64, 15);
printf("不能创墙,也不能创自己");
SetPos(64, 16);
printf("用↑.↓.←.→分别控制蛇的移动.");
SetPos(64, 17);
printf("F3 为加速,F4 为减速\n");
SetPos(64, 18);
printf("ESC :退出游戏.space:暂停游戏.");
SetPos(64, 20);
printf("江子龙@版权");
}
void pause()
{
while (1)
{
Sleep(300);
if (KEY_PRESS(VK_SPACE))
{
break;
}
}
}
这个函数主要用来,当你按下space的时候,给计算机一个sleep的死循环。
int NextIsFood(pSnakeNode psn, pSnake ps)
{
return(psn->x == ps->_pfood->x) && (psn->y == ps->_pfood->y);
}
void EatFood(pSnakeNode psn, pSnake ps)
{
psn->next = ps->_pbody;
ps->_pbody = psn;
pSnakeNode cur = ps->_pbody;
while (cur)
{
SetPos(cur->x, cur->y);
wprintf(L"%c", BODY);
cur = cur->next;
}
ps->_score += ps->_foodweight;
free(ps->_pfood);
CreatFood(ps);
}
void NotFood(pSnakeNode psn, pSnake ps)
{
psn->next = ps->_pbody;
ps->_pbody = psn;
pSnakeNode cur = ps->_pbody;
while (cur->next->next)
{
SetPos(cur->x, cur->y);
wprintf(L"%c", BODY);
cur = cur->next;
}
SetPos(cur->next->x, cur->next->y);
printf(" ");
free(cur->next);
cur->next = NULL;
}
第一个返回的是int 是为了等会在主体函数中判断真假,如果为真为1则执行EatFood,为假为0则执行NotFood。 (贪吃蛇变长的条件)
第二个EatFood 如果下一个点是食物的话,贪吃蛇就变长一格,用的是头插。
第三个NotFood 如果下一点不是食物的话,贪吃蛇就整个向前走一格,就是头插下一个空白格,然后free掉尾巴最后一格。
int KillByWall(pSnake ps)
{
if ((ps->_pbody->x == 0) || (ps->_pbody->x == 56) || (ps->_pbody->y == 0) || (ps->_pbody->y == 26))
{
ps->_status = KILL_BY_WALL;
return 1;
}
return 0;
}
int KillBySelf(pSnake ps)
{
pSnakeNode cur = ps->_pbody->next;
while (cur)
{
if ((ps->_pbody->x == cur->x) && (ps->_pbody->y == cur->y))
{
ps->_status = KILL_BY_SELF;
return 1;
}
cur = cur->next;
}
return 0;
}
接下来的这两个代码就是用来判断,你的蛇是怎么死的。
第一个代码判断出来的就是如果 蛇的头与墙的位置出现在了一起,那就把状态改变蛇就死了
第二个代码判断的是如果蛇和自己的身体的某一个节点的位置相同,那蛇的状态就是死了。
void SnakeMove(pSnake ps)
{
pSnakeNode pNextNode = (pSnakeNode)malloc(sizeof(SnakeNode));
if (pNextNode == NULL)
{
perror("snake move open falied");
return;
}
switch (ps->_dir)
{
case UP:
{
pNextNode->y = ps->_pbody->y - 1;
pNextNode->x = ps->_pbody->x;
}
break;
case DOWN:
{
pNextNode->y = ps->_pbody->y + 1;
pNextNode->x = ps->_pbody->x;
}
break;
case LEFT:
{
pNextNode->y = ps->_pbody->y ;
pNextNode->x = ps->_pbody->x - 2;
}
break;
case RIGHT:
{
pNextNode->y = ps->_pbody->y;
pNextNode->x = ps->_pbody->x + 2;
}
break;
default:
break;
}
if (NextIsFood(pNextNode, ps))
{
EatFood(pNextNode, ps);
}
else
{
NotFood(pNextNode, ps);
}
KillBySelf(ps);
KillByWall(ps);
}
蛇的移动代码量偏长,首先定义好蛇的移动方向上下左右,是怎么改变的。
因为蛇的节点的宽字符,所以在横坐标改变时是两倍改变,而纵坐标为单倍改变即可。
然后在进行判断蛇移动的下一个位置是否是食物,是食物则执行前面的三个函数。
最后判断蛇是否死亡。
void GameStart(pSnake ps)
{
system("mode con cols=100 lines=30");
system("title 贪吃蛇");
HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO CursorInfo;
GetConsoleCursorInfo(hOutput, &CursorInfo);
CursorInfo.bVisible = false;
SetConsoleCursorInfo(hOutput, &CursorInfo);
WelcomeToGame();
CreatMap();
InitSnake(ps);
CreatFood(ps);
}
void GameRun(pSnake ps)
{
PrintHelpInfo();
do
{
SetPos(64, 10);
printf("得分:%d ", ps->_score);
printf("每个食物得分:%d", ps->_foodweight);
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_RIGHT) && ps->_dir != LEFT)
{
ps->_dir = RIGHT;
}
else if (KEY_PRESS(VK_LEFT) && ps->_dir != RIGHT)
{
ps->_dir = LEFT;
}
else if (KEY_PRESS(VK_SPACE))
{
pause();
}
else if (KEY_PRESS(VK_ESCAPE))
{
ps->_status = END_NORMAL;
break;
}
else if (KEY_PRESS(VK_F3))
{
if (ps->_sleeptime >= 50)
{
ps->_sleeptime -= 30;
ps->_foodweight += 20;
}
}
else if (KEY_PRESS(VK_F4))
{
if (ps->_sleeptime < 350)
{
ps->_sleeptime += 30;
ps->_foodweight -= 20;
if (ps->_sleeptime == 350)
{
ps->_foodweight = 1;
}
}
}
Sleep(ps->_sleeptime);
SnakeMove(ps);
} while (ps->_status == OK);
}
void GameEnd(pSnake ps)
{
pSnakeNode cur = ps->_pbody;
SetPos(24, 12);
switch (ps->_status)
{
case KILL_BY_SELF:
printf("你创了自己!,游戏结束\n");
break;
case KILL_BY_WALL:
printf("你创了墙!game over\n");
break;
case END_NORMAL:
printf("游戏正常结束!\n");
break;
}
while (cur)
{
pSnakeNode del = cur;
cur = cur->next;
free(del);
}
}
每个游戏的运行都分为三步,开始,玩,结束。
这里也一样,游戏的开始就是用上面的函数进行游戏的初始化,菜单的打印,以及初始化蛇之类的开始函数。
第二步玩,这一步要用到之前封装的宏 KEY_PRESS( VK ),用来判断这个按键是否被按下,被按下就执行这个逻辑。同时贪吃蛇的一部分按键是不能重合的,像上和下是不能同时进行的,所以要进行判断。最后加上snakemove等函数,蛇就能动起来。
第三步结束,这一步就是判断你的游戏时因为什么结束的。然后再把蛇的节点全部free,以备开启下一把
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;
}
setlocale 是用来适应当地环境的。(用来打印宽字符)
srand函数,是为了让rand 函数的值不是伪随机值,而是随着时间更改。