您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
本篇内容主要讲解“怎么用C++制作扫雷游戏”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么用C++制作扫雷游戏”吧!
具体代码如下
#ifndef SAOLEI_H #define SAOLEI_H class Block { friend class Saoleigame; public: Block(); bool isShown(); void setnum(int); int getnum(); bool isbomb(); protected: int num; bool flag_show; int x; int y; }; class Saoleigame { public: Saoleigame(); ~Saoleigame(); void _init_(); void gameStart(); void reflash(); void check(int x, int y); void click(int x, int y); void gameOver(); private: Block juzheng[100]; bool flag; int b[10]; unsigned int score; }; #endif
以上是编写的头文件
#include<iostream> #include<cstdlib> #include<ctime> #include"Saolei.h" using namespace std; Saoleigame::Saoleigame() { _init_(); flag = true; score = 0; } Saoleigame::~Saoleigame() { } void Saoleigame::_init_() { srand(time(NULL)); for( int i = 0; i < 10; i++ ) { b[i] = -1; } for(int i = 0; i < 10; i ++ ) { bool temp_flag = false; do { int temp = (unsigned int)rand()%100; for( int j = 0; j < i; j ++) { if(temp == b[i]) { temp_flag = true; } } if(!temp_flag) { b[i] = temp; } }while(temp_flag); } for(int i = 0; i < 10; i++ ) { juzheng[b[i]].setnum(-1); } for( int i = 0; i < 10; i ++ ) { for( int j = 0; j < 10;j++) { juzheng[i*10+j].x = i+1; juzheng[i*10+j].y = j+1; } } for(int m = 0; m < 10; m ++ ) { for( int n = 0; n < 10 ; n++ ) { check(m + 1, n + 1); } } } void Saoleigame::check(int x, int y) { if(juzheng[(x - 1)*10 + (y - 1)].num == -1)return; int trans = (x - 1)*10 + (y - 1); int number = 0; for( int i = -1; i < 2; i ++ ) { for(int j = -1; j < 2; j ++) { if(!(x + i<= 0 && x + i >= 10 || j + y <= 0 && y + j >= 10)) { if(juzheng[(x + i - 1)*10 + (y + j - 1)].num == -1) number ++; } } } juzheng[(x - 1)*10 + (y - 1)].setnum(number); } void Saoleigame::click(int x, int y) { if(juzheng[(x- 1)*10 + (y - 1)].num == 0) { for( int i = -1; i < 2; i ++ ) { for(int j = -1; j < 2; j ++) { if(!((x + i<= 0 || x + i > 10 )|| (j + y <= 0 || y + j > 10)) && !(i == 0&& j ==0) && !juzheng[(x+i- 1)*10 + (y +j- 1)].flag_show){ juzheng[(x+i- 1)*10 + (y +j- 1)].flag_show = true; click(x + i, y + j); } } } } juzheng[(x- 1)*10 + (y - 1)].flag_show = true; return; } void Saoleigame::gameStart() { do { reflash(); int x, y; cout<<"input the position: "; cin >> x>> y; if(juzheng[(x-1)*10 + (y-1)].isbomb()) { gameOver(); return; } else { click(x , y); } }while(flag); } void Saoleigame::reflash() { system("cls"); score = 0; cout<<" 扫雷"<<endl; cout<<" 1 2 3 4 5 6 7 8 9 10"<<endl; cout<<" -------------------"<<endl; for(int i = 0; i < 100; i ++ ) { if(i%10 == 0) { if(i /10 + 1 == 10)cout<<10<<"|"; else cout<<i /10 + 1<<" |"; } if(juzheng[i].isShown()) { if(juzheng[i].isShown() && (juzheng[i].getnum())!=-1) { score ++; } if((juzheng[i].getnum())==-1)cout <<"*"<<"|"; else cout <<juzheng[i].getnum()<<"|"; } else { cout<<" |"; } if((i+1)%10 == 0)cout<<endl; } cout<<" -------------------"<<endl; cout<<"score:"<<(score*100)/95<<endl; } void Saoleigame::gameOver() { for(int i = 0 ; i < 10 ; i++ ) { juzheng[b[i]].flag_show = true; } reflash(); cout<<"Game Over"<<endl<<endl; flag = false; } Block::Block() { flag_show = false; num = 0; } bool Block::isShown() { return flag_show; } void Block::setnum(int _num) { num = _num; } int Block::getnum() { return num; } bool Block::isbomb() { return num == -1; }
到此,相信大家对“怎么用C++制作扫雷游戏”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。