下面是游戏整体制作框架:
目录
演示:
原蛇
一,GameStart:
1.设置控制台窗口大小
设置之前需要了解一下Win32 API:
Windows这个多作业系统除了协调应用程序的执行、分配内存、管理资源之外,它同时也是一个很大的服务中心,调用这个服务中心的各种服务(每一种服务就是一个函数),可以帮应用程序达到开启视窗、描绘图形、使用周边设备等目的,由于这些函数服务的对象是应用程序(Application),所以便称之为Application Programming Interface,简称API函数。WIN32 API也就是Microsoft Windows32位平台的应用程序编程接口。
平时我们直接在编译器运行起来的窗口就是控制台程序,在实现贪吃蛇之前,我们要先将终端属性设置一下(如果已经是设置好的就不用设置了)
接着设置窗口大小,只需要在控制台输入命令就可以了:
Win+R,输入cmd调出控制台输入:
mode con cols=150 lines=50
接着回车就成功了,使用代码进行上述操作需要用到system函数:
system("mode con cols=150 lines=50");
运行时走到这一步就设置成功了。
2.设置控制台窗口名字
同样使用命令来设置:
system("title 原蛇");
3.隐藏屏幕光标
HANDLE GetStdHandle(DWORD nStdHandle);
使用:
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleCursorInfo函数:检索有关指定控制台屏幕缓冲区的光标大小和可见性的信息
原型:
BOOL WINAPI GetConsoleCursorInfo(
HANDLE hConsoleOutput,
PCONSOLE_CURSOR_INFO lpConsoleCursorInfo
);
CONSOLE_CURSOR_INFO 是一个结构体,控制控制台光标的信息
typedef struct _CONSOLE_CURSOR_INFO {
DWORD dwSize;
BOOL bVisible;
} CONSOLE_CURSOR_INFO, *PCONSOLE_CURSOR_INFO;
SetConsoleCursorInfo函数:设置指定控制台屏幕缓冲区的光标的大小和可见性。
原型:
BOOL WINAPI SetConsoleCursorInfo(
HANDLE hConsoleOutput,
const CONSOLE_CURSOR_INFO *lpConsoleCursorInfo
);
所以要隐藏光标流程为:获得句柄->获取控制台信息->隐藏光标->设置光标状态:
//设置光标隐藏
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO CursorInfo;
GetConsoleCursorInfo(handle, &CursorInfo);
CursorInfo.bVisible = false;//注意顺序
SetConsoleCursorInfo(handle, &CursorInfo);
还有一个设置光标位置的操作:
原型:
BOOL WINAPI SetConsoleCursorPosition(
HANDLE hConsoleOutput,
COORD pos
);
COORD是API定义的一个结构体,表示一个字符在控制台屏幕上的坐标
typedef struct _COORD {
SHORT X;
SHORT Y;
} COORD, *PCOORD;
设置:
COORD pos = { 1, 2 };
所以设置光标位置流程为:获得句柄->COORD设置->SetConsoleCursorPosition :
把它封装成一个函数:
void SetPos(short x, short y)
{
COORD pos = { x, y };
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
//设置标准输出上光标的位置为pos
SetConsoleCursorPosition(handle, pos);
}
4.打印欢迎界面
控制台的坐标(为正):
打印上面的界面:
//欢迎界面
void Welcome_Game()
{
SetPos(68, 24);
printf("欢迎来到原蛇");
SetPos(66, 40);
system("pause");
system("cls");
SetPos(45, 16);
printf("使用↑ . ↓ . ← . → 分别控制蛇的移动, F3为加速,F4为减速\n");
SetPos(62, 20);
printf("加速将能得到更高的分数。\n");
SetPos(65, 40);
system("pause");
system("cls");
}
5.初始化地图
上面的字符都为宽字符,占两个字节,普通字符占一个字节,宽度也是普通字符的两倍,高度一样,使用宽字符前需要本地化,需要setlocale函数,包含于<locale.h>。
//本地化
setlocale(LC_ALL, "");
地图初始化:
//绘制地图
void Initmap()
{
SetPos(0, 0);
for (int i = 0; i <= 78; i += 2)
{
wprintf(L"%c", WALL);
}
for (int i = 1; i <= 38; i++)
{
SetPos(0, i);
wprintf(L"%c", WALL);
}
for (int i = 1; i <= 38; i++)
{
SetPos(78, i);
wprintf(L"%c", WALL);
}
for (int i = 0; i <= 78; i += 2)
{
SetPos(i, 39);
wprintf(L"%c", WALL);
}
}
由于宽字符宽度是普通字符的两倍,所以x轴上要注意坐标。
6.初始化蛇身
蛇身由节点组成:
//蛇身节点
typedef struct SnakeNode
{
int x;
int y;
struct SnakeNode* next;
}SnakeNode, * pSnakeNode;
先定义一个结构体管理原蛇:
typedef struct Snake
{
pSnakeNode _pSnake;//维护整条蛇的指针
pSnakeNode _pFood;//维护食物的指针
enum DIRECTION _Dir;//蛇头的方向默认是向右
enum GAME_STATUS _Status;//游戏状态
int _Socre;//当前获得分数
int _Add;//默认每个食物10分
int _SleepTime;//每走一步休眠时间
}Snake, * pSnake;
初始化:
//初始化蛇
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("InitSnake()::malloc()");
return;
}
//设置坐标
cur->x = POS_X + i * 2;
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"%c", BODY);
cur = cur->next;
}
//初始化贪吃蛇数据
ps->_SleepTime = 200;
ps->_Socre = 0;
ps->_Status = OK;
ps->_Dir = RIGHT;
ps->_Add = 10;
}
7.随机创建食物
食物也是一个节点:
//创建正常食物
void CreateFood(pSnake ps)
{
int x = 0;
int y = 0;
again:
//产生的x坐标应该是2的倍数,这样才可能和蛇头坐标对齐。
do
{
x = rand() % 75 + 2;
y = rand() % 38 + 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("CreateFood::malloc()");
return;
}
else
{
pFood->x = x;
pFood->y = y;
SetPos(pFood->x, pFood->y);
wprintf(L"%c", FOOD);
ps->_pFood = pFood;
}
}
二,GameRun:
1.帮助信息
//打印帮助信息
void PrintHelpInfo()
{
//打印提示信息
SetPos(100, 15);
printf("不能穿墙,不能咬到自己\n");
SetPos(100, 17);
printf("用↑.↓.←.→分别控制蛇的移动.");
SetPos(100, 19);
printf("F3 为加速,F4 为减速\n");
SetPos(100, 21);
printf("ESC :退出游戏 space:暂停游戏");
SetPos(100, 23);
printf("cookies_s_s@版权");
}
2.当前食物分数和获得分数
3.获取按键情况
#define KEY_PRESS(VK) ((GetAsyncKeyState(VK)&0x1) ? 1 : 0)
4.蛇的移动:
4.1计算蛇头的坐标和方向
//创建下一个节点
pSnakeNode pNextNode = (pSnakeNode)malloc(sizeof(SnakeNode));
if (pNextNode == NULL)
{
perror("SnakeMove()::malloc()");
return;
}
//确定下一个节点的坐标,下一个节点的坐标根据,蛇头的坐标和方向确定
switch (ps->_Dir)
{
case UP:
{
pNextNode->x = ps->_pSnake->x;
pNextNode->y = ps->_pSnake->y - 1;
}
break;
case DOWN:
{
pNextNode->x = ps->_pSnake->x;
pNextNode->y = ps->_pSnake->y + 1;
}
break;
case LEFT:
{
pNextNode->x = ps->_pSnake->x - 2;
pNextNode->y = ps->_pSnake->y;
}
break;
case RIGHT:
{
pNextNode->x = ps->_pSnake->x + 2;
pNextNode->y = ps->_pSnake->y;
}
break;
}
4.2判断下一个状态(食物与非食物)
//下一个是食物
int NextIsFood(pSnakeNode pnext, pSnake ps)
{
return (pnext->x == ps->_pFood->x) && (pnext->y == ps->_pFood->y);
}
4.3吃掉食物(蛇节点增加)
//吃掉食物
void EatFood(pSnakeNode pnext, pSnake ps)
{
//头插法
pnext->next = ps->_pSnake;
ps->_pSnake = pnext;
pSnakeNode cur = ps->_pSnake;
//打印蛇
while (cur)
{
SetPos(cur->x, cur->y);
wprintf(L"%c", BODY);
cur = cur->next;
}
ps->_Socre += ps->_Add;
free(ps->_pFood);
CreateFood(ps);
}
4.4正常空格(正常移动)
//下一个不是食物
void NoFood(pSnakeNode pnext, pSnake ps)
{
//头插法
pnext->next = ps->_pSnake;
ps->_pSnake = pnext;
pSnakeNode cur = ps->_pSnake;
//打印蛇
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;
}
4.5判断是否与墙冲突
//蛇头与墙冲突
int KillByWall(pSnake ps)
{
if (ps->_pSnake->x == 0 || ps->_pSnake->x == 78 || ps->_pSnake->y == 0 || ps->_pSnake->y == 39)
{
ps->_Status = KILL_BY_WALL;
return 1;
}
return 0;
}
4.6判断是否与蛇身冲突
//紫砂
int KillBySelf(pSnake ps)
{
pSnakeNode cur = ps->_pSnake->next;
while (cur)
{
if ((ps->_pSnake->x == cur->x)
&& (ps->_pSnake->y == cur->y))
{
ps->_Status = KILL_BY_SELF;
return 1;
}
cur = cur->next;
}
return 0;
}
三,GameEnd:
1.提醒游戏状态
枚举状态:
//游戏状态
enum GAME_STATUS
{
OK,//正常运行
KILL_BY_WALL,//撞墙
KILL_BY_SELF,//咬到自己
END//正常结束
};
检测状态:
switch (ps->_Status)
{
case END:
printf("正在退出游戏......\n");
exit(0);
break;
case KILL_BY_SELF:
printf("您撞到了自己,游戏结束!\n");
break;
case KILL_BY_WALL:
printf("墙把您撞了,游戏结束!\n");
break;
}
2.释放节点空间
释放空间:
//释放蛇节点空间
while (cur)
{
pSnakeNode del = cur;
cur = cur->next;
free(del);
}
四,代码
//Snake.h
#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include <windows.h>
#include <time.h>
#include <stdio.h>
#include <stdbool.h>
#define KEY_PRESS(VK) ((GetAsyncKeyState(VK)&0x1) ? 1 : 0)
//方向
enum DIRECTION
{
UP = 1,
DOWN,
LEFT,
RIGHT
};
//游戏状态
enum GAME_STATUS
{
OK,//正常运行
KILL_BY_WALL,//撞墙
KILL_BY_SELF,//咬到自己
END//正常结束
};
#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 _pSnake;//维护整条蛇的指针
pSnakeNode _pFood;//维护食物的指针
enum DIRECTION _Dir;//蛇头的方向默认是向右
enum GAME_STATUS _Status;//游戏状态
int _Socre;//当前获得分数
int _Add;//默认每个食物10分
int _SleepTime;//每走一步休眠时间
}Snake, * pSnake;
//游戏开始前的初始化
void GameStart(pSnake ps);
//游戏运行过程
void GameRun(pSnake ps);
//游戏结束
void GameEnd(pSnake ps);
//设置光标的坐标
void SetPos(short x, short y);
//欢迎界面
void Welcome_Game();
//打印帮助信息
void PrintHelpInfo();
//创建地图
void Initmap();
//初始化蛇
void InitSnake(pSnake ps);
//创建食物
void CreateFood(pSnake ps);
//暂停响应
void pause();
//下一个节点是食物
int NextIsFood(pSnakeNode psn, pSnake ps);
//吃食物
void EatFood(pSnakeNode psn, pSnake ps);
//不吃食物
void NoFood(pSnakeNode psn, pSnake ps);
//撞墙检测
int KillByWall(pSnake ps);
//撞自身检测
int KillBySelf(pSnake ps);
//蛇的移动
void SnakeMove(pSnake ps);
//游戏初始化
void GameStart(pSnake ps);
//游戏运行
void GameRun(pSnake ps);
//游戏结束
void GameEnd(pSnake ps);
//Snake.c
#include "Snake.h"
//设置坐标
void SetPos(short x, short y)
{
COORD pos = { x, y };
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
//设置标准输出上光标的位置为pos
SetConsoleCursorPosition(handle, pos);
}
//欢迎界面
void Welcome_Game()
{
SetPos(68, 24);
printf("欢迎来到原蛇");
SetPos(66, 40);
system("pause");
system("cls");
SetPos(45, 16);
printf("使用↑ . ↓ . ← . → 分别控制蛇的移动, F3为加速,F4为减速\n");
SetPos(62, 20);
printf("加速将能得到更高的分数。\n");
SetPos(65, 40);
system("pause");
system("cls");
}
//绘制地图
void Initmap()
{
SetPos(0, 0);
for (int i = 0; i <= 78; i += 2)
{
wprintf(L"%c", WALL);
}
for (int i = 1; i <= 38; i++)
{
SetPos(0, i);
wprintf(L"%c", WALL);
}
for (int i = 1; i <= 38; i++)
{
SetPos(78, i);
wprintf(L"%c", WALL);
}
for (int i = 0; i <= 78; i += 2)
{
SetPos(i, 39);
wprintf(L"%c", WALL);
}
}
//初始化蛇
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("InitSnake()::malloc()");
return;
}
//设置坐标
cur->x = POS_X + i * 2;
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"%c", BODY);
cur = cur->next;
}
//初始化贪吃蛇数据
ps->_SleepTime = 200;
ps->_Socre = 0;
ps->_Status = OK;
ps->_Dir = RIGHT;
ps->_Add = 10;
}
//创建正常食物
void CreateFood(pSnake ps)
{
int x = 0;
int y = 0;
again:
//产生的x坐标应该是2的倍数,这样才可能和蛇头坐标对齐。
do
{
x = rand() % 75 + 2;
y = rand() % 38 + 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("CreateFood::malloc()");
return;
}
else
{
pFood->x = x;
pFood->y = y;
SetPos(pFood->x, pFood->y);
wprintf(L"%c", FOOD);
ps->_pFood = pFood;
}
}
//打印帮助信息
void PrintHelpInfo()
{
//打印提示信息
SetPos(100, 15);
printf("不能穿墙,不能咬到自己\n");
SetPos(100, 17);
printf("用↑.↓.←.→分别控制蛇的移动.");
SetPos(100, 19);
printf("F3 为加速,F4 为减速\n");
SetPos(100, 21);
printf("ESC :退出游戏 space:暂停游戏");
SetPos(100, 23);
printf("cookies_s_s@版权");
}
//暂停
void pause()
{
while (1)
{
Sleep(300);
if (KEY_PRESS(VK_SPACE))
{
break;
}
}
}
//下一个是食物
int NextIsFood(pSnakeNode pnext, pSnake ps)
{
return (pnext->x == ps->_pFood->x) && (pnext->y == ps->_pFood->y);
}
//吃掉食物
void EatFood(pSnakeNode pnext, pSnake ps)
{
//头插法
pnext->next = ps->_pSnake;
ps->_pSnake = pnext;
pSnakeNode cur = ps->_pSnake;
//打印蛇
while (cur)
{
SetPos(cur->x, cur->y);
wprintf(L"%c", BODY);
cur = cur->next;
}
ps->_Socre += ps->_Add;
free(ps->_pFood);
CreateFood(ps);
}
//下一个不是食物
void NoFood(pSnakeNode pnext, pSnake ps)
{
//头插法
pnext->next = ps->_pSnake;
ps->_pSnake = pnext;
pSnakeNode cur = ps->_pSnake;
//打印蛇
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->_pSnake->x == 0 || ps->_pSnake->x == 78 || ps->_pSnake->y == 0 || ps->_pSnake->y == 39)
{
ps->_Status = KILL_BY_WALL;
return 1;
}
return 0;
}
//紫砂
int KillBySelf(pSnake ps)
{
pSnakeNode cur = ps->_pSnake->next;
while (cur)
{
if ((ps->_pSnake->x == cur->x)
&& (ps->_pSnake->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("SnakeMove()::malloc()");
return;
}
//确定下一个节点的坐标,下一个节点的坐标根据,蛇头的坐标和方向确定
switch (ps->_Dir)
{
case UP:
{
pNextNode->x = ps->_pSnake->x;
pNextNode->y = ps->_pSnake->y - 1;
}
break;
case DOWN:
{
pNextNode->x = ps->_pSnake->x;
pNextNode->y = ps->_pSnake->y + 1;
}
break;
case LEFT:
{
pNextNode->x = ps->_pSnake->x - 2;
pNextNode->y = ps->_pSnake->y;
}
break;
case RIGHT:
{
pNextNode->x = ps->_pSnake->x + 2;
pNextNode->y = ps->_pSnake->y;
}
break;
}
//如果下一个位置就是食物
if (NextIsFood(pNextNode, ps))
{
EatFood(pNextNode, ps);
}
else//如果没有食物
{
NoFood(pNextNode, ps);
}
KillByWall(ps);
KillBySelf(ps);
}
//游戏前期工作
void GameStart(pSnake ps)
{
//窗口设置
system("mode con cols=150 lines=50");
system("title 原蛇");
//设置光标隐藏
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO CursorInfo;
GetConsoleCursorInfo(handle, &CursorInfo);
CursorInfo.bVisible = false;//注意顺序
SetConsoleCursorInfo(handle, &CursorInfo);
//欢迎界面
Welcome_Game();
//绘制地图
Initmap();
//帮助信息
PrintHelpInfo();
//初始化蛇
InitSnake(ps);
//创造第⼀个⻝物
CreateFood(ps);
}
//游戏运行
void GameRun(pSnake ps)
{
//打印右侧帮助信息
PrintHelpInfo();
do
{
SetPos(100, 10);
printf("得分:%d ", ps->_Socre);
printf("每个食物得分:%2d分", ps->_Add);
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_SPACE))
{
pause();
}
else if (KEY_PRESS(VK_ESCAPE))
{
ps->_Status = END;
break;
}
else if (KEY_PRESS(VK_F3))
{
if (ps->_SleepTime >= 50)
{
ps->_SleepTime -= 30;
ps->_Add += 2;
}
}
else if (KEY_PRESS(VK_F4))
{
if (ps->_SleepTime < 350)
{
ps->_SleepTime += 30;
ps->_Add -= 2;
if (ps->_SleepTime == 350)
{
ps->_Add = 1;
}
}
}
//蛇每次一定之间要休眠的时间,时间短,蛇移动速度就快
Sleep(ps->_SleepTime);
SnakeMove(ps);
} while (ps->_Status == OK);
}
//游戏结束后期工作
void GameEnd(pSnake ps)
{
pSnakeNode cur = ps->_pSnake;
SetPos(28, 15);
switch (ps->_Status)
{
case END:
printf("正在退出游戏......\n");
exit(0);
break;
case KILL_BY_SELF:
printf("您撞到了自己,游戏结束!\n");
break;
case KILL_BY_WALL:
printf("墙把您撞了,游戏结束!\n");
break;
}
//释放蛇节点空间
while (cur)
{
pSnakeNode del = cur;
cur = cur->next;
free(del);
}
}
//Test.c
#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(28, 17);
printf("再来一局吗?(Y/N):");
ch = getchar();
getchar();//清理\n
} while (ch == 'Y');
SetPos(0, 27);
}
int main()
{
//本地化
setlocale(LC_ALL, "");
test();
return 0;
}