ImageVerifierCode 换一换
格式:DOCX , 页数:25 ,大小:23.26KB ,
资源ID:5525377      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/5525377.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(扫雷游戏代码.docx)为本站会员(b****4)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

扫雷游戏代码.docx

1、扫雷游戏代码/*block.h*/#ifndef BLOCK_H_#define BLOCK_H_#includeclass QWidget;class Block:public QLabel Q_OBJECTpublic: explicit Block(bool mine_flag,QWidget* parent=0); void set_number(int number); void turn_over(); bool is_mine()const; bool is_turn_over()const; signals: void turn_over(bool is_mine); prot

2、ected: void mousePressEvent(QMouseEvent* event); private: bool mine_flag_; bool mark_flag_; bool turn_over_flag_; int number_;#endif#includeblock.h#include#include#include#includeBlock:Block(bool mine_flag,QWidget* parent) :QLabel(parent) mine_flag_=mine_flag; mark_flag_=false; turn_over_flag_=false

3、; number_=-1; setPixmap(QPixmap(:/images/normal.png);void Block:set_number(int number) number_=number;void Block:turn_over() if(!turn_over_flag_) turn_over_flag_=true; if(mine_flag_) setPixmap(QPixmap(:/images/mine.png); else setPixmap(QPixmap(:/images/mine_+QString(%1).arg(number_)+.png); update();

4、 bool Block:is_mine()const return mine_flag_;bool Block:is_turn_over()const return turn_over_flag_;/*鼠标事件的实现*/void Block:mousePressEvent(QMouseEvent* event) if(event-button()=Qt:LeftButton) if(!turn_over_flag_&!mark_flag_) turn_over_flag_=true; if(mine_flag_=true) setPixmap(QPixmap(:/images/mine.png

5、); update(); emit turn_over(true); else setPixmap(QPixmap(:/images/mine_+QString(%1).arg(number_)+.png); update(); emit turn_over(false); else if(event-button()=Qt:RightButton) if(!turn_over_flag_) if(!mark_flag_) mark_flag_=true; setPixmap(QPixmap(:/images/flag.png); else mark_flag_=false; setPixma

6、p(QPixmap(:/images/normal.png); update(); QLabel:mousePressEvent(event);#ifndef BLOCK_AREA_H_#define BLOCK_AREA_H_#includeblock.h#includeclass QEvent;class QGridLayout;class QObject;class BlockArea:public QWidget Q_OBJECTpublic: BlockArea(int row,int column,int mine_number,QWidget* parent=0); void s

7、et_block_area(int row,int column,int mine_number,int init_flag=false);signals: void game_over(bool is_win);protected: bool eventFilter(QObject* watched, QEvent* event); private slots: void slot_turn_over(bool is_mine); private: int calculate_mines(int x,int y)const; /璁畻浠 ,y涓轰腑蹇幂殑涔濆 镙煎唴镄勯浄鏁 void try_

8、to_turn_over_more_blocks(int x,int y); private: QGridLayout* mainLayout; int row_; int column_; int total_block_number_; int total_mine_number_; int turn_over_block_number_; bool game_over_flag_;#endif/*block_area.h*/#includeblock_area.h#include#include#include#include#include#include#include#includ

9、e#include/*雷的随机布置*/ptrdiff_t random(ptrdiff_t i) return qrand()%i;ptrdiff_t (*p_random)(ptrdiff_t)=random;BlockArea:BlockArea(int row,int column,int mine_number,QWidget* parent) :QWidget(parent) set_block_area(row,column,mine_number,true);void BlockArea:set_block_area(int row,int column,int mine_num

10、ber,int init_flag) if(!init_flag) for(int i=0;irow_;i+) for(int j=0;jcolumn_;j+) delete static_cast(mainLayout-itemAtPosition(i,j)-widget(); delete mainLayout; row_=row; column_=column; total_block_number_=row_*column_; total_mine_number_=mine_number; turn_over_block_number_=0; game_over_flag_=false

11、; bool mine_flagtotal_block_number_; for(int i=0;itotal_mine_number_;i+) mine_flagi=true; for(int i=total_mine_number_;itotal_block_number_;i+) mine_flagi=false; QTime time; time=QTime:currentTime(); qsrand(time.msec()+time.second()*1000); std:random_shuffle(mine_flag,mine_flag+total_block_number_,p

12、_random); mainLayout=new QGridLayout(this); for(int i=0;irow_;i+) for(int j=0;jaddWidget(new Block(mine_flagi*column_+j),i,j); for(int i=0;irow_;i+) for(int j=0;jcolumn_;j+) Block* current_block=static_cast(mainLayout-itemAtPosition(i,j)-widget(); current_block-set_number(calculate_mines(i,j); conne

13、ct(current_block,SIGNAL(turn_over(bool),this,SLOT(slot_turn_over(bool); current_block-installEventFilter(this); bool BlockArea:eventFilter(QObject* watched, QEvent* event) if(game_over_flag_) if(event-type()=QEvent:MouseButtonPress) return true; return QWidget:eventFilter(watched,event);void BlockAr

14、ea:slot_turn_over(bool is_mine) if(is_mine) game_over_flag_=true; emit game_over(false); else +turn_over_block_number_; if(turn_over_block_number_=total_block_number_-total_mine_number_) game_over_flag_=true; emit game_over(true); else for(int i=0;irow_;i+) for(int j=0;jcolumn_;j+) if(sender()=stati

15、c_cast(mainLayout-itemAtPosition(i,j)-widget() try_to_turn_over_more_blocks(i,j); int BlockArea:calculate_mines(int x,int y)const int number=0; for(int i=0;i3;i+) for(int j=0;j=0) & (x-1+i=0) & (y-1+jcolumn_) ) if(static_cast(mainLayout-itemAtPosition(x-1+i,y-1+j)-widget()-is_mine() +number; return

16、number;void BlockArea:try_to_turn_over_more_blocks(int x,int y) QQueueQPair queue; QPair pair; queue.enqueue(qMakePair(x,y); while(!queue.isEmpty() pair=queue.head(); queue.dequeue(); if(calculate_mines(pair.first,pair.second)=0) for(int i=0;i3;i+) for(int j=0;j=0) & (pair.first-1+i=0) & (pair.secon

17、d-1+jcolumn_) ) if(!static_cast(mainLayout-itemAtPosition(pair.first-1+i,pair.second-1+j)-widget()-is_turn_over() static_cast(mainLayout-itemAtPosition(pair.first-1+i,pair.second-1+j)-widget()-turn_over(); +turn_over_block_number_; queue.enqueue(qMakePair(pair.first-1+i,pair.second-1+j); if(turn_ove

18、r_block_number_=total_block_number_-total_mine_number_) game_over_flag_=true; emit game_over(true); #ifndef MAIN_WINDOW_H_#define MAIN_WINDOW_H_#includeblock_area.h#include#include#includeclass QAction;class QActionGroup;class QCloseEvent;class QMenu;class QToolBar;class QWidget;class MainWindow:pub

19、lic QMainWindow Q_OBJECTpublic: MainWindow(QWidget* parent=0);protected: void closeEvent(QCloseEvent* event); private slots: void slot_new_game(); void slot_rank(); void slot_show_game_toolBar(bool show); void slot_show_statusBar(bool show); void slot_standard(QAction* standard_action); void slot_ab

20、out_game(); void slot_game_over(bool is_win); void slot_timer();private: void read_settings(); void write_settings(); void create_actions(); void create_menus(); void create_game_toolBar(); void create_statusBar();private: BlockArea* area_; int row_; int column_; int mine_number_; int easy_record_ti

21、me_; int middle_record_time_; int hard_record_time_; QString easy_record_name_; QString middle_record_name_; QString hard_record_name_; int current_standard_; QMenu* game_menu; QMenu* setting_menu; QMenu* help_menu; QToolBar* game_toolBar; QAction* new_game_action; QAction* rank_action; QAction* exi

22、t_action; QAction* show_game_toolBar_action; QAction* show_statusBar_action; QAction* easy_standard_action; QAction* middle_standard_action; QAction* hard_standard_action; QAction* custom_standard_action; QActionGroup* standard_actionGroup; QAction* about_game_action; QAction* about_qt_action; QLabe

23、l* time_label; QTime time; QTimer timer;#endif#includemain_window.h#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include/*关于游戏介绍的信息*/const QString g_software_name=Mine Sweeper;const QString g_s

24、oftware_version=1.2;const QString g_software_author=CHANGHUIZHEN;/*关于排行榜的信息*/const int g_no_record_time=9999;const QString g_no_record_name=;MainWindow:MainWindow(QWidget* parent) :QMainWindow(parent) area_=new BlockArea(9,9,10);/一般设置 connect(area_,SIGNAL(game_over(bool),this,SLOT(slot_game_over(boo

25、l); setCentralWidget(area_); create_actions(); create_menus(); create_game_toolBar(); create_statusBar(); QCoreApplication:setOrganizationName(g_software_author); QCoreApplication:setApplicationName(g_software_name); read_settings(); layout()-setSizeConstraint(QLayout:SetFixedSize); setWindowTitle(g

26、_software_name); setWindowIcon(QIcon(:/game.png); /*等待最长时间,到时间就会提示游戏失败*/ timer.start(1000); connect(&timer,SIGNAL(timeout(),this,SLOT(slot_timer(); slot_new_game();void MainWindow:closeEvent(QCloseEvent*) write_settings();void MainWindow:slot_new_game() area_-set_block_area(row_,column_,mine_number_

27、); time_label-setText(0); time.restart(); timer.start();void MainWindow:slot_rank() QDialog dialog; dialog.setWindowTitle(tr(rank); QGridLayout* up_layout=new QGridLayout; up_layout-addWidget(new QLabel(tr(Standard),0,0); up_layout-addWidget(new QLabel(tr(Time),0,1); up_layout-addWidget(new QLabel(tr(Name),0,2); up_layout-a

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1