目录
引入:
本片文章目的:
整个游戏的实现流程图如下:
游戏实现
GameRun
PrintHelpInfo
Pause
NextIsFood
printSnake
EatFood
NoFood
KillByWall
KillBySelf
GameRun
GameEnd
总代码:
(1)snack.h
(2)snack.c
(3)test.c
结语:
引入:
在上篇我们介绍了win32API和宽字符的打印还有控制台操作。通过引用GetStdHandle,GetConsoleCursorInfo,CONSOLE_CURSOR_INFO ,SetConsoleCursorInfo,SetConsoleCursorPosition,SetPos,GetAsyncKeyState(VK)的用法来帮助大家掌握这些系统提供的函数并实现了游戏开始-GameStart。
本片文章目的:
实现游戏运行-GameRun和游戏结束-GameEnd还有将这三个函数在main函数中引用并实现多次输入。
整个游戏的实现流程图如下:
游戏实现
GameRun
为了使程序整洁GameRun里面又包含了几个函数如下:
PrintHelpInfo | 打印游戏信息 |
Pause | 按空格暂停 |
NextIsFood | 判断蛇头到达的坐标处是否是食物 |
printSnake | 打印蛇身 |
EatFood | 吃食物的情况 |
NoFood | 没吃食物的情况 |
KillByWall | 撞到墙 |
KillBySelf | 撞到自己 |
SnakeMove | 蛇的移动 |
PrintHelpInfo
既然是游戏那么必须要提示玩家游戏规则。
SetPos在上篇已经介绍过并实现故这里不在过多描述。
void PrintHelpInfo()
{SetPos(70, 13);printf("游戏规则:");SetPos(64, 15);printf("1.不能撞墙,不能咬到自己");SetPos(64, 16);printf("2.使用 ↑.↓.←.→ 分别控制蛇的移动");SetPos(64, 17);printf("3.A加速,B减速");SetPos(64, 18);printf("4.ESC-退出, 空格-暂停游戏");SetPos(64, 20);printf("HXL");
}
效果如下:
Pause
按下空格游戏暂停。
Sleep睡眠函数,要包含头文件time.h里面的时间表示睡眠多少毫秒,KEY_PRESS(VK_SPACE)在上篇文章GetAsyncKeyState的那个模块里面有解释,这里如果不想深入了解的话把它看成一个识别函数里面的参数是否被按过。VK_SPACE是空格的虚拟键。
键盘虚拟键的参考文本
void Pause()
{while (1){Sleep(100);if (KEY_PRESS(VK_SPACE)){break;}}
}
NextIsFood
判断蛇的头结点的x,y坐标是否和食物的一样。
int NextIsFood(pSnake ps, pSnakeNode pnext)
{if (ps->_pFood->x == pnext->x && ps->_pFood->y == pnext->y){return 1;}else{return 0;}
}
printSnake
打印蛇身。
利用单链表的特性遍历整个链表。
void printSnake(pSnake ps)
{pSnakeNode cur = ps->_pSnake;while (cur){SetPos(cur->x, cur->y);wprintf(L"%lc", BODY);cur = cur->next;}
}
EatFood
这里要分为吃食物和不吃食物两种情况,因为吃到食物就是蛇加一个结点尾结点不用释放,没吃到头结点加一个尾结点删去来实现蛇的移动。用pnext来找到下一个结点,蛇的移动其实就是链接新的头节点,删去尾结点。
void EatFood(pSnake ps, pSnakeNode pnext)
{//头插pnext->next = ps->_pSnake;ps->_pSnake = pnext;//打印蛇printSnake(ps);free(ps->_pFood);ps->_Score += ps->_FoodWeight;CreateFood(ps);//新创建食物
}
NoFood
没有吃到食物的情况,那就要把尾结点删去,头结点插入新的结点。
特别注意:有的朋友可能会疑惑cur->next->next这个条件会使倒数第二个结点打印不出来,其实这个第二个结点可以不用打印,真正需要打印的只是新的头和删去的尾,因为尾在前面已经打印出来即使把尾结点释放掉,屏幕上也不会消失故我们需要打印两个空格(宽字符)来把他覆盖掉。
void NoFood(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
判断头结点的坐标是否和墙壁重合即可,只要把蛇的状态改为KILL_BY_WALL即可。
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;
}
KillBySelf
将cur保存头结点以后的结点并遍历和头结点比较如果坐标重合的话把蛇的状态设为KILL_BY_SELF。
void 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;}cur = cur->next;}
}
SnakeMove
蛇的移动先将下一个结点的坐标求出,再把它分为吃食物和不吃食物两种情况,最后通过KillByWall(ps);蛇是否撞墙。KillBySelf(ps);蛇是否自杀。来实现游戏功能。
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{//不吃食物NoFood(ps, pNext);}//蛇是否撞墙KillByWall(ps);//蛇是否自杀KillBySelf(ps);
}
GameRun
将上面几个函数引用。
void GameRun(pSnake ps)
{PrintHelpInfo();//getchar();do{SetPos(64, 10);printf("得分:%5d", ps->_Score);SetPos(64, 11);printf("每个食物的分数:%2d", 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_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 = END_NOMAL;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->_SleepTime < 320){ps->_SleepTime += 30;ps->_FoodWeight -= 2;}}Sleep(ps->_SleepTime);SnakeMove(ps);} while (ps->_Status == OK);
}
GameEnd
最后游戏的善后工作,用SetPos设置控制台位置根据退出的状态打印对应的信息,由于是使用链表那么用完必须要释放空间。
void GameEnd(pSnake ps)
{SetPos(20, 12);switch (ps->_Status){case END_NOMAL:printf("退出游戏成功\n");break;case KILL_BY_SELF:printf("自杀了游戏结束\n");break;case KILL_BY_WALL:printf("撞墙了游戏结束\n");break;}//释放蛇身的结点pSnakeNode cur = ps->_pSnake;while (cur){pSnakeNode del = cur;cur = cur->next;free(del);}ps->_pSnake = NULL;
}
运行结果
总代码:
代码分为3个模块
(1)snack.h
(2)snack.c
(3)test.c
(1)snack.h
#pragma once
#pragma once
#pragma once
#include <locale.h>
#include <stdlib.h>
#include <windows.h>
#include <stdbool.h>
#include <stdio.h>
#include <time.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 )
enum DIRECTION//方向
{UP = 1,DOWN,LEFT,RIGHT
};
enum GAME_STATUS//状态
{OK,KILL_BY_WALL,KILL_BY_SELF,END_NOMAL
};
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 _Score;int _FoodWeight;int _SleepTime;
}Snake, * pSnake;
void SetPos(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 PrintHelpInfo();
void Pause();
void SnakeMove(pSnake ps);
int NextIsFood(pSnake ps, pSnakeNode pnext);
void EatFood(pSnake ps, pSnakeNode pnext);
void printSnake(pSnake ps);
void NoFood(pSnake ps, pSnakeNode pnext);
void KillByWall(pSnake ps);
void KillBySelf(pSnake ps);
void GameEnd(pSnake ps);
运行结构:
(2)snack.c
#define _CRT_SECURE_NO_WARNINGS 1
#define _CRT_SECURE_NO_WARNINGS 1
#define _CRT_SECURE_NO_WARNINGS 1
#include "snack.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, 14);printf("欢迎来到贪吃蛇小游戏\n");SetPos(40, 25);system("pause");system("cls");SetPos(20, 14);printf("使用 ↑ . ↓ . ← . → . 分别控制蛇的移动, F1是加速(Fn+F1),F2是减速(Fn+F2)");SetPos(40, 25);system("pause");system("cls");//char ch = getchar();
}
void CreateMap()
{SetPos(0, 0);int i = 0;for (i = 0; i <= 56; i += 2){wprintf(L"%c", WALL);}SetPos(0, 26);for (i = 0; i <= 56; i += 2){wprintf(L"%c", WALL);}for (i = 1; i <= 25; i++){SetPos(0, i);wprintf(L"%c", WALL);}for (i = 1; i <= 25; i++){SetPos(56, i);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 0;}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"%c", BODY);cur = cur->next;}ps->_Status = OK;ps->_Score = 0;ps->_SleepTime = 200;ps->_pFood = NULL;ps->_FoodWeight = 10;ps->_Dir = RIGHT;}
void CreateFood(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->_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;}pFood->x = x;pFood->y = y;ps->_pFood = pFood;SetPos(x, y);wprintf(L"%c", FOOD);//getchar();
}
void GameStart(pSnake ps)
{//控制台窗口设置system("mode con cols=100 lines=30");system("title 贪吃蛇");//隐藏屏幕光标HANDLE hOutput = NULL;hOutput = GetStdHandle(STD_OUTPUT_HANDLE);CONSOLE_CURSOR_INFO CursorInfo;GetConsoleCursorInfo(hOutput, &CursorInfo);//获取控制台光标信息CursorInfo.bVisible = false;//光标隐藏SetConsoleCursorInfo(hOutput, &CursorInfo);//设置控制台光标状态//打印欢迎界面WelcomeToGame();//创建地图CreateMap();//初始化蛇身InitSnake(ps);//创建食物CreateFood(ps);
}
void PrintHelpInfo()
{SetPos(70, 13);printf("游戏规则:");SetPos(64, 15);printf("1.不能撞墙,不能咬到自己");SetPos(64, 16);printf("2.使用 ↑.↓.←.→ 分别控制蛇的移动");SetPos(64, 17);printf("3.F1是加速(Fn+F1),F2是减速(Fn+F2)");SetPos(64, 18);printf("4.ESC-退出, 空格-暂停游戏");SetPos(64, 20);printf("HXL");
}
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;}else{return 0;}
}
void printSnake(pSnake ps)
{pSnakeNode cur = ps->_pSnake;while (cur){SetPos(cur->x, cur->y);wprintf(L"%lc", BODY);cur = cur->next;}
}
void EatFood(pSnake ps, pSnakeNode pnext)
{//头插pnext->next = ps->_pSnake;ps->_pSnake = pnext;//打印蛇printSnake(ps);free(ps->_pFood);ps->_Score += ps->_FoodWeight;CreateFood(ps);//新创建食物
}
void NoFood(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 == 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 (ps->_pSnake->x == cur->x && ps->_pSnake->y == cur->y){ps->_Status = KILL_BY_SELF;}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{//不吃食物NoFood(ps, pNext);}//蛇是否撞墙KillByWall(ps);//蛇是否自杀KillBySelf(ps);
}
void GameRun(pSnake ps)
{PrintHelpInfo();//getchar();do{SetPos(64, 10);printf("得分:%5d", ps->_Score);SetPos(64, 11);printf("每个食物的分数:%2d", 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_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 = END_NOMAL;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->_SleepTime < 320){ps->_SleepTime += 30;ps->_FoodWeight -= 2;}}Sleep(ps->_SleepTime);SnakeMove(ps);} while (ps->_Status == OK);
}
void GameEnd(pSnake ps)
{SetPos(20, 12);switch (ps->_Status){case END_NOMAL:printf("退出游戏成功\n");break;case KILL_BY_SELF:printf("自杀了游戏结束\n");break;case KILL_BY_WALL:printf("撞墙了游戏结束\n");break;}//释放蛇身的结点pSnakeNode cur = ps->_pSnake;while (cur){pSnakeNode del = cur;cur = cur->next;free(del);}ps->_pSnake = NULL;
}
运行结构:
(3)test.c
#define _CRT_SECURE_NO_WARNINGS 1
#define _CRT_SECURE_NO_WARNINGS 1
#define _CRT_SECURE_NO_WARNINGS 1
#include "snack.h"
void test()
{char ch = 0;do{Snake snake = { 0 };//1.游戏开始--初始化游戏GameStart(&snake);//2.游戏运行--游戏正常运行过程GameRun(&snake);//3.游戏结束--游戏善后GameEnd(&snake);SetPos(20, 18);printf("再来一局吗?(Y/N):");ch = getchar();getchar();// 清理掉\n} while (ch == 'Y' || ch == 'y');SetPos(0, 27);
}
int main()
{setlocale(LC_ALL, "");srand((unsigned int)time(NULL));test();return 0;
}
运行结果:
结语:
其实写博客不仅仅是为了教大家,同时这也有利于我巩固自己的知识点,和一个学习的总结,由于作者水平有限,对文章有任何问题的还请指出,接受大家的批评,让我改进,如果大家有所收获的话还请不要吝啬你们的点赞收藏和关注,这可以激励我写出更加优秀的文章。