当前位置:  首页  >  PHP资讯  >  业界资讯

C++实现扫雷、排雷小游戏

这篇文章主要为大家详细介绍了C++实现扫雷、排雷小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C++实现扫雷、排雷小游戏的具体代码,供大家参考,具体内容如下

界面:

游戏思想: 

扫雷游戏:

1.随机给定雷点坐标

2.判断每个点的雷情况

3.由用户根据上下左右键到达指定位置,点击enter,翻开该点
        如果该点是雷点,此时翻开所有雷点,告知游戏结束
        非雷点,翻开该点坐标

代码:

 #include #include #include #include #include #include #include using namespace std; #define WIDTH 500 #define HEIGHT 500 #define SIDE 50 #define THUNDERNUM 10 #define TOTALSCORE 100 #define EVERY_OF_DESC 5 int score = TOTALSCORE; class Point { public: int x; int y; Point() {} Point(int _x, int _y) :x(_x), y(_y) {} void setPoint(int x, int y) { this->x = x; this->y = y; } Point(const Point& xy) :x(xy.x), y(xy.y) {} bool operator<(const Point& xy)const { if (x <= xy.x) return true; return y <= xy.y; } Point& operator=(const Point& xy) { if (this != &xy) { x = xy.x; y = xy.y; } return *this; } bool operator==(const Point& xy) { return ((x == xy.x) && (y = xy.y)); } bool operator!=(const Point& xy) { return !(*this == xy); } }; class ThunderPoint { private: vector storage; int num; public: ThunderPoint(int _num = THUNDERNUM) :num(_num) { //初始化雷点位置 int count = 0; while (count != num) { int x = (rand() % (WIDTH / SIDE)) * SIDE;//随机生成数据,使之在图形界面之中 int y = (rand() % (HEIGHT / SIDE)) * SIDE; Point tem(x, y); int i = -1; bool flag = false; for (i = 0; i & getThunderPoint() { //获得雷点位置 return storage; } void drawThunderPoint() { auto it = storage.begin(); while (it != storage.end()) { setfillcolor(RED); fillcircle((*it).x, (*it).y, SIDE / 2); ++it; } } }; class Grid { private: vector> nearbythunder; vector> isopen; ThunderPoint thunder; Point currposition; Point preposition; vector color; public: bool isOver; public: Grid() :thunder(10),currposition() { preposition.setPoint(0, 0); for (int i = 0; i <10; ++i) { color.push_back(RGB(rand() % 256, rand() % 256, rand() % 256)); } isOver = false; isopen.resize(HEIGHT / SIDE, vector(WIDTH / SIDE, false)); nearbythunder.resize(HEIGHT/SIDE,vector(WIDTH/SIDE,0)); currposition.setPoint(0, 0); //先将雷点的位置标出来,标为数字 9 // 任何一个点,他附近的雷点最多8个 auto it = thunder.getThunderPoint().begin(); while (it != thunder.getThunderPoint().end()) { int x = ((*it).x)/SIDE; int y = ((*it).y)/SIDE; nearbythunder[x][y] = 9; if (((y - SIDE/SIDE) >= 0) && (nearbythunder[x][y - SIDE/SIDE] != 9)) { nearbythunder[x][y - SIDE/SIDE]++; } if (((y - SIDE/SIDE) >= 0) && ((x - SIDE/SIDE) >= 0) && (nearbythunder[x - SIDE/SIDE][y - SIDE/SIDE] != 9)) { nearbythunder[x - SIDE/SIDE][y - SIDE/SIDE]++; } if (((y - SIDE/SIDE) >= 0) && ((x + SIDE/SIDE) = 0) && (nearbythunder[x - SIDE/SIDE][y] != 9)) { nearbythunder[x - SIDE/SIDE][y]++; } if (((x + SIDE/SIDE) = 0) && (nearbythunder[x - SIDE/SIDE][y + SIDE/SIDE] != 9)) { nearbythunder[x - SIDE/SIDE][y + SIDE/SIDE]++; } if (((y + SIDE / SIDE) = 0) { currposition.y -= SIDE; } } break; case 's': case 'S': case -80://下 { if (currposition.y + SIDE = 0) currposition.x -= SIDE; } break; case 'd': case 'D': case -77://右 { if (currposition.x + SIDE  

代码解读:

1.类之间的联系

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

内容推荐:免费高清PNG素材下载
吐了个 "CAO" !
扫码关注 PHP1 官方微信号
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved PHP1.CN 第一PHP社区 版权所有 京ICP备19059560号-4