最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++實現掃雷、排雷小游戲

 更新時間:2020年05月19日 09:23:36   作者:xinger_28  
這篇文章主要為大家詳細介紹了C++實現掃雷、排雷小游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了C++實現掃雷、排雷小游戲的具體代碼,供大家參考,具體內容如下

界面:

游戲思想: 

掃雷游戲:

1.隨機給定雷點坐標

2.判斷每個點的雷情況

3.由用戶根據上下左右鍵到達指定位置,點擊enter,翻開該點
        如果該點是雷點,此時翻開所有雷點,告知游戲結束
        非雷點,翻開該點坐標

代碼:

#include<iostream>
#include<vector>
#include<sstream>
#include<algorithm>
#include<graphics.h>
#include<ctime>
#include<conio.h>
 
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<Point> 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 < storage.size(); i++) {
 if (tem == storage[i]) {
  flag = true;
  break;
 }
 }
 if (flag==false) {
 //說明沒有重復
 storage.push_back(tem);
 count++;
 }
 }
 }
 vector<Point>& 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<vector<int>> nearbythunder;
 vector<vector<bool>> isopen;
 ThunderPoint thunder;
 Point currposition;
 Point preposition;
 vector<COLORREF> 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<bool>(WIDTH / SIDE, false));
  nearbythunder.resize(HEIGHT/SIDE,vector<int>(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) < WIDTH/SIDE) && (nearbythunder[x + SIDE/SIDE][y - SIDE/SIDE] != 9)) {
 nearbythunder[x + SIDE/SIDE][y - SIDE/SIDE]++;
 }
 if (((x - SIDE/SIDE) >= 0) && (nearbythunder[x - SIDE/SIDE][y] != 9)) {
 nearbythunder[x - SIDE/SIDE][y]++;
 }
 if (((x + SIDE/SIDE) < WIDTH/SIDE) && (nearbythunder[x + SIDE/SIDE][y] != 9)) {
 nearbythunder[x + SIDE/SIDE][y]++;
 }
 if (((y + SIDE/SIDE) < HEIGHT/SIDE) && (nearbythunder[x][y + SIDE/SIDE] != 9)) {
 nearbythunder[x][y + SIDE/SIDE]++;
 }
 if (((y + SIDE/SIDE) < HEIGHT/SIDE) && ((x - SIDE/SIDE) >= 0) && (nearbythunder[x - SIDE/SIDE][y + SIDE/SIDE] != 9)) {
 nearbythunder[x - SIDE/SIDE][y + SIDE/SIDE]++;
 }
 if (((y + SIDE / SIDE) < HEIGHT / SIDE) && ((x + SIDE / SIDE) < WIDTH / SIDE) && (nearbythunder[x + SIDE / SIDE][y + SIDE / SIDE] != 9)) {
 nearbythunder[x + SIDE / SIDE][y + SIDE / SIDE]++;
 }
 ++it;
 }
 for (int i = 0; i < HEIGHT; i = i + SIDE) {
 setlinecolor(YELLOW);
 line(0, i, WIDTH, i);
 line(i, 0, i, HEIGHT);
 }
 }
 void keyDown()
 {
 char userKey = _getch();
 if (userKey == -32) // 表明這是方向鍵
 userKey = -_getch(); // 獲取具體方向,并避免與其他字母的 ASCII 沖突
 setfillcolor(GREEN);
 preposition = currposition;
 switch (userKey)
 {
 case 'w':
 case 'W':
 case -72: //上
 {
 if (currposition.y - SIDE >= 0) {
 currposition.y -= SIDE;
 }
 }
 break;
 case 's':
 case 'S':
 case -80://下
 {
 if (currposition.y + SIDE < HEIGHT)
 currposition.y += SIDE;
 }
 break;
 case 'a':
 case 'A':
 case -75://左
 {
 if (currposition.x - SIDE >= 0)
 currposition.x -= SIDE;
 }
 break;
 case 'd':
 case 'D':
 case -77://右
 {
 if (currposition.x + SIDE < WIDTH) {
 currposition.x += SIDE;
 }
 }
 break;
 case '\r':
 {
 score -= EVERY_OF_DESC;
 settextstyle(10, 10, "楷體");
 settextcolor(GREEN);
 RECT rect;
 rect.left = WIDTH-100; rect.top = 100; rect.right = WIDTH; rect.bottom = 200;
 string ll = to_string(score);
 const char* str = ll.c_str();
 //drawtext(str, &rect, 1);
 
 //現在開始翻
 openOne(currposition);
 }
 break;
 }
 setlinecolor(BLACK);
 setlinestyle(1, 0);
 line(preposition.x, preposition.y, preposition.x + SIDE, preposition.y);
 setfillcolor(GREEN);
 setlinecolor(WHITE);
 setlinestyle(1, 2);
 line(currposition.x, currposition.y, currposition.x + SIDE, currposition.y );
 }
private:
 bool pred(bool x) {
 return x == true;
 }
 void openOne(const Point& cur) {
 //說明此時翻一個
 isopen[cur.x / SIDE][cur.y / SIDE] = true;
 //此時應該著色
 settextcolor(color[nearbythunder[cur.x/SIDE][cur.y/SIDE]]);
 RECT rect;
 settextstyle(SIDE, SIDE, "楷體");
 rect.left = cur.x; rect.top = cur.y; rect.right = cur.x + SIDE; rect.bottom = cur.y + SIDE;
 drawtext((to_string(nearbythunder[cur.x / SIDE][cur.y / SIDE])).c_str(), &rect, 1);
 int count = 0;
 for (int i = 0; i < HEIGHT / SIDE; ++i) {
 for (int j = 0; j < WIDTH / SIDE; ++j) {
 if (isopen[i][j] == true) {
  count++;
 }
 }
 }
 cout << count << endl;
 if (nearbythunder[cur.x / SIDE][cur.y / SIDE] == 9 || count==((WIDTH/SIDE)*(HEIGHT/SIDE)- THUNDERNUM)) {
 //說明游戲結束
 setfillcolor(RED);
 for (int i = 0; i < thunder.getThunderPoint().size(); i++) {
 fillcircle((thunder.getThunderPoint()[i]).x + SIDE / 2, (thunder.getThunderPoint()[i]).y + SIDE / 2, SIDE / 2);
 }
 settextcolor(WHITE);
 settextstyle(SIDE, SIDE, "楷體");
 rect.left = 0; rect.top = 4*SIDE; rect.right = WIDTH; rect.bottom = HEIGHT;
 drawtext("GAMEOVER!", &rect, 2);
 isOver = true;
 return;
 }
 }
};
 
int main() {
 initgraph(WIDTH, HEIGHT);//初始化界面
 srand((unsigned)time(NULL));//設置隨機數據種子,以當前的時間戳為種子
 setbkcolor(RED);
 srand((unsigned)time(NULL));
 settextcolor(BLUE);
 setbkmode(TRANSPARENT); // 設置文字輸出模式為透明c
 setbkcolor(HSLtoRGB(100, 0.3, 7.5f));
 Grid gr;
 while (gr.isOver==false) {
 gr.keyDown();
 }
 system("pause");
 clearcliprgn();
}

代碼解讀:

1.類之間的聯系

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • C++回溯與分支限界算法分別解決背包問題詳解

    C++回溯與分支限界算法分別解決背包問題詳解

    給定n種物品和一背包。物品i的重量是wi,其價值為vi,背包的容量為C。問應如何選擇裝入背包的物品,使得裝入背包中物品的總價值最大?下面我們分別用回溯與分支限界方法解決
    2022-06-06
  • OpenCV實現簡單錄屏功能

    OpenCV實現簡單錄屏功能

    這篇文章主要為大家詳細介紹了OpenCV實現簡單錄屏功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • C++11中std::packaged_task的使用詳解

    C++11中std::packaged_task的使用詳解

    這篇文章主要介紹了C++11中std::packaged_task的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-02-02
  • 基于C++泛型編程職工管理系統(tǒng)

    基于C++泛型編程職工管理系統(tǒng)

    這篇文章主要介紹了基于C++泛型編程職工管理系統(tǒng),前面介紹到了C++的泛型編程,并實現了萬能容器,不過那使用的是數組,今天呢咱帶大家實踐一下使用泛型技術,結合單鏈表實現一個職工管理系統(tǒng),需要的朋友可以參考一下
    2022-02-02
  • C++類成員函數后面加const問題

    C++類成員函數后面加const問題

    這篇文章主要介紹了C++類成員函數后面加const問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • C++九種排序具體實現代碼

    C++九種排序具體實現代碼

    這篇文章主要介紹了C++九種排序具體實現代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-04-04
  • C++中std::allocator的使用案例詳解

    C++中std::allocator的使用案例詳解

    這篇文章主要介紹了C++中std::allocator的使用案例詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下
    2021-09-09
  • C語言實現新生入學登記系統(tǒng)

    C語言實現新生入學登記系統(tǒng)

    這篇文章主要為大家詳細介紹了C語言實現新生入學登記系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • C++三體星戰(zhàn)小游戲源代碼

    C++三體星戰(zhàn)小游戲源代碼

    這篇文章主要給大家介紹了關于C++三體星戰(zhàn)小游戲的相關資料,文中給出了詳細完整的代碼示例,對大家的學習或者工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-08-08
  • Linux系統(tǒng)中C語言編程創(chuàng)建函數fork()執(zhí)行解析

    Linux系統(tǒng)中C語言編程創(chuàng)建函數fork()執(zhí)行解析

    最近在看進程間的通信,看到了fork()函數,雖然以前用過,這次經過思考加深了理解?,F總結如下
    2013-04-04

最新評論

二连浩特市| 亚东县| 天祝| 彰化市| 彭州市| 临泉县| 合水县| 库伦旗| 城固县| 满洲里市| 和平区| 宁津县| 壶关县| 含山县| 凌云县| 类乌齐县| 津南区| 鹿泉市| 抚顺县| 高安市| 甘南县| 醴陵市| 汕头市| 遂川县| 准格尔旗| 瓦房店市| 淮安市| 长海县| 高阳县| 临沧市| 屯昌县| 禹城市| 互助| 罗平县| 泌阳县| 镇赉县| 昌都县| 滕州市| 墨玉县| 白水县| 南江县|