山东三强建设咨询有限公司网站,购买域名,wordpress分类信息插件,网络托管公司有哪些⭐大家好#xff0c;我是Dark Falme Masker,学习了动画制作及键盘交互之后#xff0c;我们就可以开动利用图形库写一个简单的贪吃蛇小游戏#xff0c;增加学习乐趣。 ⭐专栏#xff1a;EasyX部分小游戏实现详细讲解 最终效果如下 首先包含头文件 #includestdio.h #… ⭐大家好我是Dark Falme Masker,学习了动画制作及键盘交互之后我们就可以开动利用图形库写一个简单的贪吃蛇小游戏增加学习乐趣。 ⭐专栏EasyX部分小游戏实现详细讲解 最终效果如下 首先包含头文件 #includestdio.h #includeeasyx.h #includeconio.h #includetime.h ✅我们将会使用rand函数生成随机数来控制食物的随机生成设置时间戳srand(time(NULL)),所以要包含time.h int main()
{initgraph(800, 600);setbkcolor(RGB(164, 225, 202));cleardevice();closegraph();return 0;
}
创建窗体更换背景颜色。
将窗体划分为各个边长为40的正方形小格方便后续表示蛇和食物。
为了方便后续可以修改每个小格的边长可以将其定义一下 #define NodeWidth 40 然后划分分界线让后续绘制出蛇和食物位置更加清晰。
void paintGrid()
{for (int y 0; y 600; y NodeWidth){line(0, y, 800, y);}for (int x 0; x 800; x NodeWidth){line(x, 0, x, 600);}
}
运行代码观察是否存在问题 接下来绘制蛇蛇的起始长度设置为5蛇是由五个连续的正方形小块构成食物由一个正方形小块构成。我们可以盛情一个结构体变量存放每个正方形小格子的左上角的坐标。 typedef struct { int x; int y; }Node; 创建一个结构体数组装着蛇的坐标信息在第七行绘制出蛇的各个节点如果数组第一个元素作为头节点那么其余节点的x坐标是递减NodeWidth的直接传格数在绘制时乘以小方块宽度即可。 Node snake[100] { {5,7},{4,7},{3,7},{2,7},{1,7} }; 绘制蛇的函数
void paintSnake(Node* snake, int n)
{int left, top, right, bottom;for (int i 0; i n; i){left snake[i].x * NodeWidth;top snake[i].y * NodeWidth;right (snake[i].x 1) * NodeWidth;bottom (snake[i].y 1) * NodeWidth;solidrectangle(left, top, right, bottom);}
}
传入结构体指针及社的节点个数即可在画面中绘制出一个长度为5格的蛇。
但是蛇是会移动的所以要加上键盘交互功能控制蛇的方向移动。
创建枚举 enum direction { eUp, eDown, eLeft, eRight }; 一共有三种状况在蛇移动时判断键盘是否控制蛇的移位通过控制结构体数组内x,y的值就可以实现蛇的移动。
默认direction为右 enum direction d eRight; 判断玩家按下键盘的函数传入枚举指针改变枚举值从而根据改变后的值改变更新蛇的位置的x,y坐标再次绘制时设就可以转向。
void changeDirection(enum direction* pD)
{if (_kbhit() ! 0){char c _getch();switch (c){casew:if (*pD ! eDown)*pD eUp;break;cases:if (*pD ! eUp)*pD eDown;break;casea:if (*pD ! eRight)*pD eLeft;break;cased:if (*pD ! eLeft)*pD eRight;break;}}
}
改变direction后就可以更改蛇节点的参数要注意的是蛇头不可以向正在移动的方向的相反方向移动这里要进行判断。
在移动时后续节点覆盖前边的节点的位置根据蛇头位置及移动方向生成新的节点位置作为蛇头设置这个函数的返回值为NODE类型记录蛇尾节点。
✨返回蛇尾节点利用一个结构体变量接收在判断蛇头吃掉食物结点之后就可以将返回的节点续上并且蛇的长度。
Node snakeMove(Node* snake, int length, int direction)
{Node tail snake[length - 1];for (int i length - 1; i 0; i--){snake[i] snake[i - 1];}Node NewHead;NewHead snake[0];if (direction eUp){NewHead.y--;}else if (direction eDown){NewHead.y;}else if (direction eLeft){NewHead.x--;}else if (direction eRight){NewHead.x;}snake[0] NewHead;return tail;
}
创建食物
创建食物是随机生成坐标不可以超出创建的窗体大小而且不可以生成到蛇的身体上可以使用三子棋的思路设置死循环直到生成满足我们需要的位置然后break。
Node createFood(Node* snake, int length)
{Node food;while (1){food.x rand() % (800 / NodeWidth);food.y rand() % (600 / NodeWidth);int i;for (i 0; i length; i){if ((food.x snake[i].x) (food.y snake[i].y)){break;}}if (i length)continue;elsebreak;}return food;
}
生成完成后返回创建出的食物的结构体变量
然后绘制出食物利用不同颜色作为区分。
void paintFood(Node food)
{int left, right, top, bottom;left food.x * NodeWidth;top food.y * NodeWidth;right (food.x 1) * NodeWidth;bottom (food.y 1) * NodeWidth;setfillcolor(YELLOW);solidrectangle(left, top, right, bottom);setfillcolor(WHITE);
}
我们绘制出的蛇的身体是白色的如果在这里更改了填充颜色再次绘制蛇就变成和食物一样的颜色所以在绘制食物后还要将填充颜色还原为白色。
判断是否吃掉食物及节点的续接。 Node lastTail snakeMove(snake, length, d); if (snake[0].x food.x snake[0].y food.y) { if (length 100) { snake[length] lastTail; length; } food createFood(snake, length); } 在循环中不断判断吃掉食物后就创建出新的食物。
☁️判断游戏结束
如果蛇吃掉自己的身体或者蛇头越界了就表示游戏失败
bool IsGameover(Node* snake, int length)
{if (snake[0].x0 ||snake[0].x(800 / NodeWidth))return true;if (snake[0].y0 || snake[0].y(600 / NodeWidth))return true;for (int i 1; i length; i)//0改为1{if (snake[0].x snake[i].x snake[0].y snake[i].y)return true;}return false;
}
如果没有碰到墙或者遍历过程中蛇头没有和某蛇节点重合才返回false否则返回true表示游戏失败。
判断失败后重置蛇的节点重新生成新的食物。
讲解到这里就结束啦
代码如下大家可以运行看一看效果用于使格子更加明显的线可以画也可以不画 #includestdio.h
#includeeasyx.h
#includeconio.h
#includetime.h
#define NodeWidth 40
typedef struct {int x;int y;
}Node;void paintGrid()
{for (int y 0; y 600; y NodeWidth){line(0, y, 800, y);}for (int x 0; x 800; x NodeWidth){line(x, 0, x, 600);}
}
void paintSnake(Node* snake, int n)
{int left, top, right, bottom;for (int i 0; i n; i){left snake[i].x * NodeWidth;top snake[i].y * NodeWidth;right (snake[i].x 1) * NodeWidth;bottom (snake[i].y 1) * NodeWidth;solidrectangle(left, top, right, bottom);}
}
enum direction
{eUp,eDown,eLeft,eRight
};
Node snakeMove(Node* snake, int length, int direction)
{Node tail snake[length - 1];for (int i length - 1; i 0; i--){snake[i] snake[i - 1];}Node NewHead;NewHead snake[0];if (direction eUp){NewHead.y--;}else if (direction eDown){NewHead.y;}else if (direction eLeft){NewHead.x--;}else if (direction eRight){NewHead.x;}snake[0] NewHead;return tail;
}
void changeDirection(enum direction* pD)
{if (_kbhit() ! 0){char c _getch();switch (c){casew:if (*pD ! eDown)*pD eUp;break;cases:if (*pD ! eUp)*pD eDown;break;casea:if (*pD ! eRight)*pD eLeft;break;cased:if (*pD ! eLeft)*pD eRight;break;}}
}Node createFood(Node* snake, int length)
{Node food;while (1){food.x rand() % (800 / NodeWidth);food.y rand() % (600 / NodeWidth);int i;for (i 0; i length; i){if ((food.x snake[i].x) (food.y snake[i].y)){break;}}if (i length)continue;elsebreak;}return food;
}void paintFood(Node food)
{int left, right, top, bottom;left food.x * NodeWidth;top food.y * NodeWidth;right (food.x 1) * NodeWidth;bottom (food.y 1) * NodeWidth;setfillcolor(YELLOW);solidrectangle(left, top, right, bottom);setfillcolor(WHITE);
}
bool IsGameover(Node* snake, int length)
{if (snake[0].x0 ||snake[0].x(800 / NodeWidth))return true;if (snake[0].y0 || snake[0].y(600 / NodeWidth))return true;for (int i 1; i length; i)//0改为1{if (snake[0].x snake[i].x snake[0].y snake[i].y)return true;}return false;
}
void reset(Node* snake, int* length, enum direction* d)
{snake[0] Node{ 5,7 };snake[1] Node{ 4,7 };snake[2] Node{ 3,7 };snake[3] Node{ 2,7 };snake[4] Node{ 1,7 };*length 5;*d eRight;
}int main()
{initgraph(800, 600);setbkcolor(RGB(164, 25, 202));cleardevice();paintGrid();Node snake[100] { {5,7},{4,7},{3,7},{2,7},{1,7} };int length 5;enum direction d eRight;srand(unsigned int(time(NULL)));Node food createFood(snake, length);while (1){cleardevice();paintGrid();paintSnake(snake, length);paintFood(food);Sleep(500);changeDirection(d);Node lastTail snakeMove(snake, length, d);if (snake[0].x food.x snake[0].y food.y){if (length 100){snake[length] lastTail;length;}food createFood(snake, length);}if (IsGameover(snake, length) true){reset(snake, length, d);food createFood(snake, length);}}closegraph();return 0;
}
文章转载自: http://www.morning.sgrwd.cn.gov.cn.sgrwd.cn http://www.morning.thrcj.cn.gov.cn.thrcj.cn http://www.morning.cknsx.cn.gov.cn.cknsx.cn http://www.morning.rlzxr.cn.gov.cn.rlzxr.cn http://www.morning.mgkb.cn.gov.cn.mgkb.cn http://www.morning.bfwk.cn.gov.cn.bfwk.cn http://www.morning.qctsd.cn.gov.cn.qctsd.cn http://www.morning.qyfqx.cn.gov.cn.qyfqx.cn http://www.morning.lxqkt.cn.gov.cn.lxqkt.cn http://www.morning.lgtcg.cn.gov.cn.lgtcg.cn http://www.morning.ghccq.cn.gov.cn.ghccq.cn http://www.morning.yknsr.cn.gov.cn.yknsr.cn http://www.morning.xrmwc.cn.gov.cn.xrmwc.cn http://www.morning.rgxcd.cn.gov.cn.rgxcd.cn http://www.morning.skksz.cn.gov.cn.skksz.cn http://www.morning.lizpw.com.gov.cn.lizpw.com http://www.morning.qpntn.cn.gov.cn.qpntn.cn http://www.morning.ygwyt.cn.gov.cn.ygwyt.cn http://www.morning.jwcmq.cn.gov.cn.jwcmq.cn http://www.morning.srrzb.cn.gov.cn.srrzb.cn http://www.morning.msbpb.cn.gov.cn.msbpb.cn http://www.morning.cbynh.cn.gov.cn.cbynh.cn http://www.morning.tjndb.cn.gov.cn.tjndb.cn http://www.morning.gmyhq.cn.gov.cn.gmyhq.cn http://www.morning.jzgxp.cn.gov.cn.jzgxp.cn http://www.morning.c7498.cn.gov.cn.c7498.cn http://www.morning.kntsd.cn.gov.cn.kntsd.cn http://www.morning.pqkrh.cn.gov.cn.pqkrh.cn http://www.morning.qyglt.cn.gov.cn.qyglt.cn http://www.morning.dhmll.cn.gov.cn.dhmll.cn http://www.morning.rhsr.cn.gov.cn.rhsr.cn http://www.morning.cgstn.cn.gov.cn.cgstn.cn http://www.morning.ygkq.cn.gov.cn.ygkq.cn http://www.morning.ztnmc.cn.gov.cn.ztnmc.cn http://www.morning.cylbs.cn.gov.cn.cylbs.cn http://www.morning.pmdlk.cn.gov.cn.pmdlk.cn http://www.morning.httzf.cn.gov.cn.httzf.cn http://www.morning.bnbtp.cn.gov.cn.bnbtp.cn http://www.morning.rdnpg.cn.gov.cn.rdnpg.cn http://www.morning.rdlfk.cn.gov.cn.rdlfk.cn http://www.morning.cykqg.cn.gov.cn.cykqg.cn http://www.morning.gklxm.cn.gov.cn.gklxm.cn http://www.morning.rfhmb.cn.gov.cn.rfhmb.cn http://www.morning.clnmf.cn.gov.cn.clnmf.cn http://www.morning.pqfbk.cn.gov.cn.pqfbk.cn http://www.morning.mlwpr.cn.gov.cn.mlwpr.cn http://www.morning.zrlms.cn.gov.cn.zrlms.cn http://www.morning.rltsx.cn.gov.cn.rltsx.cn http://www.morning.rshs.cn.gov.cn.rshs.cn http://www.morning.qjbxt.cn.gov.cn.qjbxt.cn http://www.morning.gfqj.cn.gov.cn.gfqj.cn http://www.morning.fstdf.cn.gov.cn.fstdf.cn http://www.morning.grzpc.cn.gov.cn.grzpc.cn http://www.morning.rtbj.cn.gov.cn.rtbj.cn http://www.morning.kxyqy.cn.gov.cn.kxyqy.cn http://www.morning.tkyxl.cn.gov.cn.tkyxl.cn http://www.morning.nqcwz.cn.gov.cn.nqcwz.cn http://www.morning.nflpk.cn.gov.cn.nflpk.cn http://www.morning.hwljx.cn.gov.cn.hwljx.cn http://www.morning.1000sh.com.gov.cn.1000sh.com http://www.morning.nydtt.cn.gov.cn.nydtt.cn http://www.morning.gfpyy.cn.gov.cn.gfpyy.cn http://www.morning.xqcbz.cn.gov.cn.xqcbz.cn http://www.morning.jcfdk.cn.gov.cn.jcfdk.cn http://www.morning.cxnyg.cn.gov.cn.cxnyg.cn http://www.morning.ywzqk.cn.gov.cn.ywzqk.cn http://www.morning.zdmlt.cn.gov.cn.zdmlt.cn http://www.morning.mbbgk.com.gov.cn.mbbgk.com http://www.morning.hgscb.cn.gov.cn.hgscb.cn http://www.morning.djxnn.cn.gov.cn.djxnn.cn http://www.morning.ybshj.cn.gov.cn.ybshj.cn http://www.morning.sbqrm.cn.gov.cn.sbqrm.cn http://www.morning.kngqd.cn.gov.cn.kngqd.cn http://www.morning.qtryb.cn.gov.cn.qtryb.cn http://www.morning.mknxd.cn.gov.cn.mknxd.cn http://www.morning.mgskc.cn.gov.cn.mgskc.cn http://www.morning.sjsks.cn.gov.cn.sjsks.cn http://www.morning.ghxtk.cn.gov.cn.ghxtk.cn http://www.morning.xqjh.cn.gov.cn.xqjh.cn http://www.morning.lsfzq.cn.gov.cn.lsfzq.cn