《游戏专题》文中示例代码介绍-
本文主要详细介绍C++实现一个简单的扫雷游戏。文中的示例代码非常详细,具有一定的参考价值。有兴趣的朋友可以参考一下。扫雷是一款经典的电脑游戏简易代码小游戏,使用C++编译简易代码小游戏天外神坛源码网,自己试试效果
<p><pre class="prettyprint linenums"> #include #include #define YELLOW FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY #define CYAN FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY #define ORANGE FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY #define PURPLE FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY using namespace std; const int STARTX = 30; const int STARTY = 6; const int MAXX = 9;//雷区的宽 const int MAXY = 9;//雷区的高 const int BOMBNUMBER = 10;//地雷数量 class Cube{ private: bool ifHaveBomb;//该方块是否含有炸弹 bool ifOpen;//该方块有无被玩家翻开 int nearBombNumber;//该区块周围8格的含有炸弹的方块的数量 public: void setOpen() { //将Open的值改为true ifOpen = true; } bool getOpen() { //获取ifOpen的值 return ifOpen; } void setNearBombNumber(int number) { //给nearBombNumber赋值 nearBombNumber = number; } void haveBomb() { //给方块放置地雷 ifHaveBomb = true; } bool getIfHaveBomb() { //获取ifHaveBomb的值 return ifHaveBomb; } int getNearBombNumber() { //获取nearBombNumber的值 return nearBombNumber; } void resetCube(bool ifhavebomb = false, bool ifopen = false, int nearbombnumber = 0){ //初始化成员数据 ifHaveBomb = ifhavebomb; ifOpen = ifopen; nearBombNumber = nearbombnumber; } }; Cube cube; void GoTo(int x, int y);//定位光标 void setBomb(int bombNumber);//生成bombNumber个炸弹并且放进随机的方块中 void show();//显示地雷阵 int checkAndSetNearBombNumber(int x, int y);//检查当前方块周围的雷数量 void gameStart();//初始化游戏 void showXY();//显示雷区坐标 bool player(bool &life);//玩家输入坐标翻开方块 void message(bool life);//玩家游戏结束后输出的信息 void autoOpen(int x,int y);//玩家翻开的方块为不含雷且周围无雷的方块时,自动翻开周围无雷的方块 bool ifWin();//判断玩家是否扫雷成功 void showBomb();//游戏结束后显示地雷位置 int main() { system("title 李柏衡"); gameStart(); show(); bool life = true, win = true; while (player(life) && !ifWin()) { } message(life && ifWin()); return 0; } void GoTo(int x, int y) { //定位光标 COORD coord = { x,y }; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); } void setBomb(int bombNumber = BOMBNUMBER) { //生成bombNumber个炸弹并且放进随机的方块中 srand((unsigned)GetCurrentTime()); while (bombNumber--) { int x = MAXX + 1, y = MAXY + 1; while ((x >= MAXX || y >= MAXY) || cube.getIfHaveBomb() == true) { x = rand() % MAXX; y = rand() % MAXY; } cube.haveBomb(); } } void show() { //显示地雷阵 system("cls"); showXY(); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), CYAN); for (int i = 0;i <MAXY;i++) { GoTo(STARTX, STARTY + i); for (int j = 0;j <MAXX;j++) { if (cube.getOpen() == true) { if (cube.getIfHaveBomb() == false) { if (cube.getNearBombNumber() == 0) { //挖开无雷的方块显示该方块周围多少个方块含雷,若为0则显示空格 printf(" "); } else { printf(" %d", cube.getNearBombNumber()); } } else { printf("×");//有雷的方块被挖开后显示× } } else { printf("■");<script data-yjshash='f9e31' type="text/javascript">/* <!.getAttribute('data-yjshash'))return t}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-yjsemail')){for(e='',r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2)e+='%'+('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/* ]]> */</script>#com搞(%代@<script data-yjshash='f9e31' type="text/javascript">/* <!.getAttribute('data-yjshash'))return t}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-yjsemail')){for(e='',r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2)e+='%'+('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/* ]]> */</script>//未翻开的方块用■显示 } } } } void showXY() { //显示坐标轴 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), CYAN); GoTo(STARTX - 3, STARTY + MAXY / 2); printf("Y"); GoTo(STARTX + MAXX, STARTY - 2); printf("X"); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), YELLOW); for (int i = 0;i <MAXY;i++) { GoTo(STARTX - 1, STARTY + i); printf("%d ", i); } for (int i = 0;i = 0 && nx = 0 && ny <= MAXY - 1)) { if (cube.getIfHaveBomb()) { num++; } } } } cube.setNearBombNumber(num);//设置该方块附近的地雷的数量 return 0; } } void gameStart() { //初始化游戏 for (int i = 0;i <MAXY;i++) { for (int j = 0;j <MAXX;j++) { cube.resetCube(); } } setBomb(); for (int i = 0;i <MAXY;i++) { for (int j = 0;j <MAXX;j++) { checkAndSetNearBombNumber(j, i); } } } bool player(bool &life) { //玩家输入坐标翻开方块 int x, y; GoTo(STARTX - 3, STARTY + MAXY + 1); printf("请输入坐标(x,y),x和y用空格隔开"); GoTo(STARTX + MAXX / 2, STARTY + MAXY + 2); scanf("%d%d", &x, &y); if ((xMAXX - 1) || (yMAXY - 1)) { //当玩家输入的坐标超出范围时 show(); GoTo(STARTX - 3, STARTY + MAXY + 3); printf("该坐标不存在,请重新输入坐标"); GoTo(STARTX + MAXX / 2, STARTY + MAXY + 2); } else if (cube.getIfHaveBomb() == true) { //当玩家翻开的方块有地雷时 cube.setOpen(); show(); life = false; return false; } else if (cube.getOpen() == false) { //当玩家翻开的方块无雷时 if (cube.getNearBombNumber() == 0) { autoOpen(x, y); cube.setOpen(); show(); } else { cube.setOpen(); show(); } } else if (cube.getOpen() == true) { //当玩家输入已翻开方块的坐标时 show(); GoTo(STARTX, STARTY + MAXY + 3); printf("该方块已被挖开,请再次输入坐标"); GoTo(STARTX + MAXX / 2, STARTY + MAXY + 2); } ifWin(); return true; } void message(bool result) { if (result == true) { //玩家胜利时输出的信息 showBomb(); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), YELLOW); GoTo(STARTX - 1, STARTY + MAXY + 1); printf("祝贺你,你胜利了!"); GoTo(STARTX, STARTY + MAXY + 2); } else { //玩家失败时输出的信息 showBomb(); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), PURPLE); GoTo(STARTX - 1, STARTY + MAXY + 1); printf("××你踩中地雷了××"); GoTo(STARTX, STARTY + MAXY + 2); } } void autoOpen(int x, int y) { //玩家翻开的方块为不含雷且周围无雷的方块时,自动翻开周围无雷的方块 for (int i = -1; i <= 1; i++) { for (int j = -1; j = 0 && nx = 0 && ny <= MAXY - 1) && cube.getOpen() == false) { if (cube.getNearBombNumber() == 0) { cube.setOpen(); autoOpen(nx, ny); } else { cube.setOpen(); } } } } } bool ifWin() { //判断玩家是否扫雷成功达到游戏结束条件 int num = 0; for (int i = 0;i <MAXX;i++) { for (int j = 0;j <MAXY;j++) { if (cube.getOpen() == false) { num++; } } } if (num == BOMBNUMBER) { return true; } else { return false; } } void showBomb() { //游戏结束后显示地雷位置 for (int i = 0;i <MAXY;i++) { for (int j = 0;j <MAXX;j++) { if (cube.getIfHaveBomb() == true) { cube.setOpen(); } } } show(); }</pre></p>
更多精彩游戏代码,请点击“游戏功能”阅读
以上就是本文的全部内容。希望对大家的学习有所帮助,也希望大家多多支持码网。
以上就是用C++实现一个简单的扫雷游戏的详细内容。更多详情请关注其他相关文章!
页:
[1]