1、说明:
简易实现了走迷宫小游戏。
2、基本原理:
(1)用二维数组构建地图
(2)初始化函数确定玩家坐标位置并绘制玩家
(3) 绘制函数根据二维数组的遍历不断更新玩家位置和判断是否到达终点
(4)更新函数通过键盘输入改变玩家位置不断把玩家坐标进行更新
(5)主函数利用死循环不断实现更新函数与绘制函数的执行
3、运行结果展示:
4、代码展示:
#include<graphics.h>#include<conio.h>struct Player {int i;int j;};Player player;char map[25][26] = {//绘制地图"wwwwwwwwwwwwwwwwwwwwwwwww","mpppppppppppppppwwwwwpppw","wpwwwwwpwwwwwwwpwpppwpwpw","wpwpppwpwpppppppwpwpwpwpw","wpwpwwwpwpwwwwwwwwwpwpwpw","wpwpppppwpppwpppppppwpwpw","wpwpwwwpwwwpwpwwwwwwwpwpw","wpwpwpwpwpppwpppppppppwpw","wpwpwpwpwpwwwpwpwwwpwwwpw","wwwpwpwpwpppwpwpwpwpppwpw","wpwpppwpwwwpwpwpwpwpwpwpw","wpwwwwwpwpppwpwpwpwpwpwww","wpwpppwpwpwwwpwpwpwpwpwpw","wpwpwpwpwpppwpwpwpwpwpwpw","wpppppwpwwwpwpwpppwpwpwpw","wpwwwwwpwwwwwpwwwwwpwpwpw","wpwpppppppppppppppwpwpwpw","wwwpwpwpwpwwwwpwwpwpwpppw","wpwpwpwpwppppwpwwpwpwwwww","wpwpwpwpwwpwpwpwppwpppppn","wpwpwpwpwppwpwpwpwwwwwwww","wpwpwpwpwpwwpppwppppppppw","wpwwwpwpwwwwwwwwwwwwwwwpw","wpppppwpppppppppppppppppw","wwwwwwwwwwwwwwwwwwwwwwwww" , };void startup(){int i, j;initgraph(640, 640);//setbkcolor(YELLOW);BeginBatchDraw();for (i = 0; i < 25; i++)for (j = 0; j < 25; j++)if (map[i][j] == 'm'){//找到玩家,将坐标赋值给全局变量playerplayer.i = i;player.j = j;map[i][j] = 'p';}}void show(){cleardevice();int i, j;for (i = 0; i < 25; i++){for (j = 0; j < 25; j++){if (map[i][j] == 'w'){setfillcolor(RED);setlinecolor(RED);fillrectangle(j * 20, i * 20, (j + 1) * 20, (i + 1) * 20);}else if (map[i][j] == 'p'){setfillcolor(YELLOW);setlinecolor(YELLOW);fillrectangle(j * 20, i * 20, (j + 1) * 20, (i + 1) * 20);}else if (map[i][j] == 'n'){setfillcolor(YELLOW);setlinecolor(YELLOW);fillrectangle(j * 20, i * 20, (j + 1) * 20, (i + 1) * 20);}else if (map[i][j] == 'm'){setfillcolor(GREEN);setlinecolor(GREEN);fillrectangle(j * 20, i * 20, (j + 1) * 20, (i + 1) * 20);}if (player.i == 19 && player.j == 24){setbkmode(TRANSPARENT);//显示透明文字settextcolor(GREEN);//设置字体颜色settextstyle(50, 0, _T("宋体"));//设置字体大小,字体outtextxy(320, 320, _T("游戏胜利"));//输出字体}}}//绘制玩家图像i = player.i;j = player.j;setfillcolor(GREEN);setlinecolor(GREEN);fillrectangle(j * 20, i * 20, (j + 1) * 20, (i + 1) * 20);FlushBatchDraw();}void update(){char input = _getch();if (input == 'a' || input == 's' || input == 'd' || input == 'w'){//有效按键int goal_i = player.i;int goal_j = player.j;int goalNext_i = goal_i;int goalNext_j = goal_j;if (input == 'a'){goal_j = player.j - 1;goalNext_j = player.j - 1;}else if (input == 'd'){goal_j = player.j + 1;goalNext_j = player.j + 1;}else if (input == 'w'){goal_i = player.i - 1;goalNext_i = player.i - 1;}else if (input == 's'){goal_i = player.i + 1;goalNext_i = player.i + 1;}if (map[goal_i][goal_j] == 'p' || map[goalNext_i][goalNext_j] == 'p'){//如果玩家的目标位置是path,目标的前一个位置也是path,那么player.i = goal_i;//玩家移动到目标位置player.j = goal_j;}if (map[goal_i][goal_j] == 'n'){//如果玩家的目标位置是n那么player.i = goal_i;//玩家移动到目标位置player.j = goal_j;}}else//可能出现的情况都考虑到,其他情况就不做处理return;}int main(){startup();while (1){show();update();}return 0;}
(本专栏将会展示部分代码骑士的初期学习编程的小游戏作品用于学习和纪念,将会不定期更新,敬请期待……)
如果觉得《C++游戏编程--基于EasyX图形库--实现走迷宫小游戏》对你有帮助,请点赞、收藏,并留下你的观点哦!