最终效果呈现:
实现的基本功能:
•
贪吃蛇地图绘制
•
蛇吃食物的功能 (上、下、左、右方向键控制蛇的动作)
•
蛇撞墙死亡
•
蛇撞自己死亡
•
计算得分
•
蛇身加速、减速
•
暂停游戏
游戏的主逻辑:
程序开始就设置程序⽀持本地模式,然后进⼊游戏的主逻辑。
主逻辑分为3个过程:
•
游戏开始(GameStart)完成游戏的初始化
•
游戏运行(GameRun)完成游戏运行逻辑的实现
•
游戏结束(GameEnd)完成游戏结束的说明,实现资源释放
为了养成模块化好习惯,我们把代码分开来写
snake.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<time.h>
#include<windows.h>
#define POS_X 24
#define POS_Y 5
#define WALL L'□'
#define BODY L'●'
#define FOOD L'★'
//类型的声明
//蛇身的节点类型
typedef struct SnakeNode
{
//坐标
int x;
int y;
struct SnakeNode* next;
}SnakeNode,*pSnakeNode;
//typedef struct SnakeNode* pSnakeNode;
enum DIRECTION//蛇的方向
{
UP = 1,
DOWN,
LEFT,
RIGHT
};
enum GAME_STATUS//蛇的状态
{
OK,
KILL_BY_WALL,
KILL_BY_SELF,
END_NORMAL
};
//贪吃蛇
typedef struct Snake
{
pSnakeNode _pSnake;//指向蛇头的指针
pSnakeNode _pFood;//指向蛇头的指针
enum DIRECTION _dir;//方向
enum GAME_STATUS _status;//状态
int _food_weight;//一个食物的分数
int _score;//总成绩
int _sleep_time;//休息时间
}Snake,*pSnake;
//函数的声明
void set_pos(short x, short y);//设置光标位置
void GameStart(pSnake ps);//游戏初始化
void WelcomeToGame();//欢迎界面的打印
void CreateMap();//创建地图
void InitSnake(pSnake ps);//初始化蛇身
void CreateFood(pSnake ps);//创建食物
void GameRun(pSnake ps);//游戏运行的逻辑
void SnakeMove(pSnake ps);//蛇的移动
int NextIsFood(pSnakeNode pn,pSnake ps);//判断下一个坐标是否是食物
void EatFood(pSnakeNode pn, pSnake ps);//是---吃掉食物
void NoFood(pSnakeNode pn, pSnake ps);//不是食物
void KillByWall(pSnake ps);//检测蛇是否撞墙
void KillBySelf(pSnake ps);//检测蛇是否撞到自己
void GameEnd(pSnake ps);//游戏善后工作
snake.c
#include"snake.h"
void SetPos(short x, short y)//设置光标位置
{
HANDLE houtput = NULL;//获得标准输出设备句柄
houtput = GetStdHandle(STD_OUTPUT_HANDLE);
//定位光标的位置
COORD pos = { x,y };
SetConsoleCursorPosition(houtput, pos);
}
void WelcomeToGame()
{
SetPos(38, 12);
wprintf(L"!欢迎来到贪吃蛇小游戏!\n");
SetPos(38, 13);
wprintf(L" 熊大王版本\n");
SetPos(42, 20);
system("pause");
system("cls");
SetPos(35, 12);
wprintf(L"用↑.↓.←.→来控制蛇的移动\n");
SetPos(35, 13);
wprintf(L"按F3加速,F4减速\n");
SetPos(35, 14);
wprintf(L"加速能够得到更高的分数\n");
SetPos(42, 20);
system("pause");
system("cls");
}
void CreateMap()
{
//上
int i = 0;
for (i = 0; i < 29; i++)
{
wprintf(L"%lc", WALL);
}
//下
SetPos(0, 26);
for (i = 0; i < 29; 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(56, i);
wprintf(L"%lc", WALL);
}
}
void InitSnake(pSnake ps)
{
int i = 0;
pSnakeNode cur = NULL;
for (i = 0; i < 5; i++)
{
cur = (pSnakeNode)malloc(sizeof(SnakeNode));
if (cur == NULL)
{
perror("InitSnake()::malloc()");
return;
}
cur->next = NULL;
cur->x = POS_X + 2 * i;
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"%lc", BODY);
cur = cur->next;
}
//设置贪吃蛇的属性
ps->_dir = RIGHT;//默认向右
ps->_score = 0;
ps->_food_weight = 10;
ps->_sleep_time = 200;//ms
ps->_status = OK;
}
void CreateFood(pSnake ps)
{
int x = 0;
int y = 0;
//生成x是2的倍数
//x:2-54
//y:1-25
again:
do
{
x = rand() % 53 + 2;
y = rand() % 25 + 1;
} while (x % 2 != 0);
//x与y的坐标不能与蛇的身体坐标冲突
pSnakeNode cur = ps->_pSnake;
while (cur)
{
if (x == cur->x && y == cur->y)
{
goto again;
}
cur = cur->next;
}
//创建食物节点
pSnakeNode pFood = (pSnakeNode)malloc(sizeof(SnakeNode));
if (pFood == NULL)
{
perror("CreateFood()::malloc()");
return;
}
pFood->x = x;
pFood->y = y;
pFood->next = NULL;
SetPos(x, y);//定位位置
wprintf(L"%lc", FOOD);
ps->_pFood = pFood;
}
void GameStart(pSnake ps)
{
//1.设置窗口大小
system("mode con cols=100 lines=30");
system("title 贪吃蛇");
HANDLE houtput = GetStdHandle(STD_OUTPUT_HANDLE);//输出句柄
//2.光标隐藏
CONSOLE_CURSOR_INFO CursorInfo;//定义结构体
GetConsoleCursorInfo(houtput, &CursorInfo);//获取控制台光标信息
CursorInfo.bVisible = false;//可见性隐藏
SetConsoleCursorInfo(houtput, &CursorInfo);//设置光标
int input = 0;
//3.打印欢迎界面
WelcomeToGame();
//难度选择
//int op_input();
//4.绘制地图
CreateMap();
//5.绘制蛇
InitSnake(ps);
//6.创建食物
CreateFood(ps);
}
void PrintHelpInfo()
{
SetPos(64, 14);
wprintf(L"%ls", L"-不能穿墙,不能咬到自己");
SetPos(64, 15);
wprintf(L"%ls", L"-用↑.↓.←.→来控制蛇的移动");
SetPos(64, 16);
wprintf(L"%ls", L"-按F3加速,F4减速");
SetPos(64, 17);
wprintf(L"%ls", L"-按ESC退出游戏,按空格暂停游戏");
SetPos(64, 19);
wprintf(L"%ls", L" 熊大王 制作完成");
}
#define KEY_PRESS(vk)((GetAsyncKeyState(vk)&1)?1:0)
void Pause()
{
while (1)
{
//int t=op_input();
Sleep(1);
if (KEY_PRESS(VK_SPACE))
{
break;
}
}
}
int NextIsFood(pSnakeNode pn, pSnake ps)
{
return(ps->_pFood->x == pn->x && ps->_pFood->y == pn->y);
}
void EatFood(pSnakeNode pn, pSnake ps)
{
//头插法
ps->_pFood->next = ps->_pSnake;
ps->_pSnake = ps->_pFood;
//释放下一个位置的节点
free(pn);
pn = NULL;
pSnakeNode cur = ps->_pSnake;
//打印蛇
while (cur)
{
SetPos(cur->x, cur->y);
wprintf(L"%lc", BODY);
cur = cur->next;
}
ps->_score += ps->_food_weight;
//重新创建食物
CreateFood(ps);
}
void NoFood(pSnakeNode pn, pSnake ps)
{
//头插法
pn->next = ps->_pSnake;
ps->_pSnake = pn;
pSnakeNode cur = ps->_pSnake;
while (cur->next->next != NULL)
{
SetPos(cur->x, cur->y);
wprintf(L"%lc", BODY);
cur = cur->next;
}
//把最后一个节点打印成空格
SetPos(cur->next->x, cur->next->y);
printf(" ");
//释放最后一个节点
free(cur->next);
//把倒数第二个节点的地址置为NULL
cur->next = NULL;
}
void KillByWall(pSnake ps)
{
if (ps->_pSnake->x == 0 || ps->_pSnake->x == 56 ||
ps->_pSnake->y == 0 || ps->_pSnake->y == 26)
{
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;
break;
}
cur = cur->next;
}
}
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 GameRun(pSnake ps,int t)
{
//打印帮助信息
PrintHelpInfo();
do
{
//打印总分数和食物的分值
SetPos(64, 10);
printf("总分数:%d\n", ps->_score);
SetPos(64, 11);
printf("当前食物的分数:%2d\n", ps->_food_weight);
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;
}
else if (KEY_PRESS(VK_F3))
{
//加速
if (ps->_sleep_time > 80)
{
ps->_sleep_time -= 30;
ps->_food_weight += 2;
}
}
else if (KEY_PRESS(VK_F4))
{
//减速
if (ps->_food_weight > 2)
{
ps->_sleep_time += 30;
ps->_food_weight -= 2;
}
}
SnakeMove(ps);
Sleep(ps->_sleep_time);
} while (ps->_status == OK);
}
void GameEnd(pSnake ps)
{
SetPos(24, 12);
switch (ps->_status)
{
case END_NORMAL:
wprintf(L"您主动结束游戏\n");
break;
case KILL_BY_WALL:
wprintf(L"您撞到墙上,死掉了,游戏结束\n");
break;
case KILL_BY_SELF:
wprintf(L"您撞到了自己,死掉了,游戏结束\n");
break;
}
//释放蛇身的链表
pSnakeNode cur = ps->_pSnake;
while (cur)
{
pSnakeNode del = cur;
cur = cur->next;
free(del);
}
}
test.c
#include"snake.h"
#include<locale.h>
void test()
{
int ch = 0;
do
{
system("cls");
Snake snake = { 0 };
//初始化游戏
GameStart(&snake);
//运行游戏
GameRun(&snake);
//结束游戏
GameEnd(&snake);
SetPos(20, 15);
printf("还想再来一局吗?(Y/N):");
while (getchar() != '\n');
} while (ch == 'Y' || ch == 'y');
SetPos(0, 27);
}
int main()
{
//设置适配本地环境
setlocale(LC_ALL, "");
srand((unsigned int)time(NULL));
test();
return 0;
}
分享到这里,感谢观看,希望对你有所帮助^.^