版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、/*block.h*/#ifndef BLOCK_H_#define BLOCK_H_#include<QLabel>class QWidget;class Block:public QLabelQ_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);prote
2、cted:void mousePressEvent(QMouseEvent* event);private:bool mine_flag_;bool mark_flag_;bool turn_over_flag_;int number_;#endif#include"block.h"#include<QLabel>#include<QMouseEvent>#include<QPixmap>#include<QWidget>Block:Block(bool mine_flag,QWidget* parent):QLabel(pa
3、rent)mine_flag_=mine_flag;mark_flag_=false;turn_over_flag_=false;number_=-1;setPixmap(QPixmap(":/images/normal.png");1 / 22void 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&
4、quot;);elsesetPixmap(QPixmap(":/images/mine_"+QString("%1").arg(number_)+".png");update();bool Block:is_mine()constreturn mine_flag_;bool Block:is_turn_over()constreturn turn_over_flag_;/*鼠标事件的实现*/void Block:mousePressEvent(QMouseEvent* event)if(event->button()=Qt:Le
5、ftButton)if(!turn_over_flag_&&!mark_flag_)turn_over_flag_=true;if(mine_flag_=true)setPixmap(QPixmap(":/images/mine.png");update();emit turn_over(true);elsesetPixmap(QPixmap(":/images/mine_"+QString("%1").arg(number_)+".png");update();emit turn_over(fal
6、se);else if(event->button()=Qt:RightButton)if(!turn_over_flag_)if(!mark_flag_)mark_flag_=true;setPixmap(QPixmap(":/images/flag.png");elsemark_flag_=false;setPixmap(QPixmap(":/images/normal.png");update();QLabel:mousePressEvent(event);#ifndef BLOCK_AREA_H_#define BLOCK_AREA_H_#
7、include"block.h"#include<QWidget>class QEvent;class QGridLayout;class QObject;class BlockArea:public QWidgetQ_OBJECTpublic:BlockArea(int row,int column,int mine_number,QWidget* parent=0);void set_block_area(int row,int column,int mine_number,int init_flag=false);signals:void game_ove
8、r(bool is_win);protected:booleventFilter(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_to_turn_over_more_blocks(int x,int y);private:QGridLayout* mainLayout;int row_;int column_;int total_b
9、lock_number_;int total_mine_number_;int turn_over_block_number_;bool game_over_flag_;#endif/*block_area.h*/#include"block_area.h"#include<algorithm>#include<QEvent>#include<QGridLayout>#include<QLayout>#include<QMouseEvent>#include<QObject>#include<QQ
10、ueue>#include<QTime>#include<QWidget>/*雷的随机布置*/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_b
11、lock_area(int row,int column,int mine_number,int init_flag)if(!init_flag)for(int i=0;i<row_;i+)for(int j=0;j<column_;j+)delete static_cast<Block*>(mainLayout->itemAtPosition(i,j)->widget();delete mainLayout;row_=row;column_=column;total_block_number_=row_*column_;total_mine_number_
12、=mine_number;turn_over_block_number_=0;game_over_flag_=false;bool mine_flagtotal_block_number_;for(int i=0;i<total_mine_number_;i+)mine_flagi=true;for(int i=total_mine_number_;i<total_block_number_;i+)mine_flagi=false;QTime time;time=QTime:currentTime();qsrand(time.msec()+time.second()*1000);s
13、td:random_shuffle(mine_flag,mine_flag+total_block_number_,p_random);mainLayout=new QGridLayout(this);for(int i=0;i<row_;i+)for(int j=0;j<column_;j+)mainLayout->addWidget(new Block(mine_flagi*column_+j),i,j);for(int i=0;i<row_;i+)for(int j=0;j<column_;j+)Block* current_block=static_cas
14、t<Block*>(mainLayout->itemAtPosition(i,j)->widget();current_block->set_number(calculate_mines(i,j);connect(current_block,SIGNAL(turn_over(bool),this,SLOT(slot_turn_over(bool);current_block->installEventFilter(this);boolBlockArea:eventFilter(QObject* watched, QEvent* event)if(game_o
15、ver_flag_)if(event->type()=QEvent:MouseButtonPress)return true;return QWidget:eventFilter(watched,event);void BlockArea: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_)
16、game_over_flag_=true;emit game_over(true);elsefor(int i=0;i<row_;i+)for(int j=0;j<column_;j+)if(sender()=static_cast<Block*>(mainLayout->itemAtPosition(i,j)->widget()try_to_turn_over_more_blocks(i,j);int BlockArea:calculate_mines(int x,int y)constint number=0;for(int i=0;i<3;i+)
17、for(int j=0;j<3;j+)if( (x-1+i>=0) && (x-1+i<row_) && (y-1+j>=0) && (y-1+j<column_) )if(static_cast<Block*>(mainLayout->itemAtPosition(x-1+i,y-1+j)->widget()->is_mine()+number;return number;void BlockArea:try_to_turn_over_more_blocks(int x,int y)Q
18、Queue<QPair<int,int> > queue;QPair<int,int> 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;i<3;i+)for(int j=0;j<3;j+)if( (pair.first-1+i>=0) && (pair.first-1+i<ro
19、w_) && (pair.second-1+j>=0) && (pair.second-1+j<column_) )if(!static_cast<Block*>(mainLayout->itemAtPosition(pair.first-1+i,pair.second-1+j)->widget()->is_turn_over()static_cast<Block*>(mainLayout->itemAtPosition(pair.first-1+i,pair.second-1+j)->widge
20、t()->turn_over();+turn_over_block_number_;queue.enqueue(qMakePair(pair.first-1+i,pair.second-1+j);if(turn_over_block_number_=total_block_number_-total_mine_number_)game_over_flag_=true;emit game_over(true);#ifndef MAIN_WINDOW_H_#define MAIN_WINDOW_H_#include"block_area.h"#include<QMa
21、inWindow>#include<QTime>#include<QTimer>class QAction;class QActionGroup;class QCloseEvent;class QMenu;class QToolBar;class QWidget;class MainWindow:public QMainWindowQ_OBJECTpublic:MainWindow(QWidget* parent=0);protected:void closeEvent(QCloseEvent* event);private slots:void slot_new
22、_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_about_game();void slot_game_over(bool is_win);void slot_timer();private:void read_settings();void write_settings();void create_actions();void cre
23、ate_menus();void create_game_toolBar();void create_statusBar();private:BlockArea* area_;int row_;int column_;int mine_number_;int easy_record_time_;int middle_record_time_;int hard_record_time_;QString easy_record_name_;QString middle_record_name_;QString hard_record_name_;int current_standard_;QMen
24、u* game_menu;QMenu* setting_menu;QMenu* help_menu;QToolBar* game_toolBar;QAction* new_game_action;QAction* rank_action;QAction* exit_action;QAction* show_game_toolBar_action;QAction* show_statusBar_action;QAction* easy_standard_action;QAction* middle_standard_action;QAction* hard_standard_action;QAc
25、tion* custom_standard_action;QActionGroup* standard_actionGroup;QAction* about_game_action;QAction* about_qt_action;QLabel* time_label;QTime time;QTimer timer;#endif#include"main_window.h"#include<QAction>#include<QActionGroup>#include<QApplication>#include<QDialog>
26、#include<QDialogButtonBox>#include<QHBoxLayout>#include<QIcon>#include<QInputDialog>#include<QLayout>#include<QMainWindow>#include<QMenu>#include<QMenuBar>#include<QMessageBox>#include<QPushButton>#include<QSettings>#include<QSpinB
27、ox>#include<QStatusBar>#include<QToolBar>#include<QVBoxLayout>#include<QWidget>/*关于游戏介绍的信息*/const QString g_software_name="Mine Sweeper"const QString g_software_version="1.2"const QString g_software_author="CHANGHUIZHEN"/*关于排行榜的信息*/const int g
28、_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(bool);setCentralWidget(area_);create_actions();create_menus();create_game_toolBar(
29、);create_statusBar();QCoreApplication:setOrganizationName(g_software_author);QCoreApplication:setApplicationName(g_software_name);read_settings();layout()->setSizeConstraint(QLayout:SetFixedSize);setWindowTitle(g_software_name);setWindowIcon(QIcon(":/game.png"); /*等待最长时间,到时间就会提示游戏失败*/ti
30、mer.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_);time_label->setText("0");time.restart();timer.start();vo
31、id 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&quo
32、t;),0,2);up_layout->addWidget(new QLabel(tr("Easy"),1,0);up_layout->addWidget(new QLabel(QString("%1").arg(easy_record_time_),1,1);up_layout->addWidget(new QLabel(easy_record_name_),1,2);up_layout->addWidget(new QLabel(tr("Middle"),2,0);up_layout->addWidg
33、et(new QLabel(QString("%1").arg(middle_record_time_),2,1);up_layout->addWidget(new QLabel(middle_record_name_),2,2);up_layout->addWidget(new QLabel(tr("Hard"),3,0);up_layout->addWidget(new QLabel(QString("%1").arg(hard_record_time_),3,1);up_layout->addWidget
34、(new QLabel(hard_record_name_),3,2);QPushButton* recount_button=new QPushButton(tr("recount");QPushButton* close_button=new QPushButton(tr("close");close_button->setDefault(true);connect(recount_button,SIGNAL(clicked(),&dialog,SLOT(accept();connect(close_button,SIGNAL(clic
35、ked(),&dialog,SLOT(reject();QHBoxLayout* bottom_layout=new QHBoxLayout;bottom_layout->addStretch();bottom_layout->addWidget(recount_button);bottom_layout->addWidget(close_button);QVBoxLayout* main_layout=new QVBoxLayout(&dialog);main_layout->addLayout(up_layout);main_layout->a
36、ddLayout(bottom_layout);if(dialog.exec()=QDialog:Accepted)easy_record_time_=middle_record_time_=hard_record_time_=g_no_record_time;easy_record_name_=middle_record_name_=hard_record_name_=g_no_record_name;void MainWindow:slot_show_game_toolBar(bool show)if(show)game_toolBar->show();elsegame_toolBa
37、r->hide();void MainWindow:slot_show_statusBar(bool show)if(show)statusBar()->show();elsestatusBar()->hide();/*游戏的设置容易、中等、困难及自定义*/void MainWindow:slot_standard(QAction* standard_action)if(standard_action=easy_standard_action)current_standard_=0;row_=9;column_=9;mine_number_=10;else if(standa
38、rd_action=middle_standard_action)current_standard_=1;row_=16;column_=16;mine_number_=40;else if(standard_action=hard_standard_action)current_standard_=2;row_=16;column_=30;mine_number_=99;else if(standard_action=custom_standard_action)QDialog dialog;dialog.setWindowTitle(tr("set standard")
39、;QSpinBox* row_spinBox=new QSpinBox;row_spinBox->setRange(5,50);row_spinBox->setValue(row_);QSpinBox* column_spinBox=new QSpinBox;column_spinBox->setRange(5,50);column_spinBox->setValue(column_);QSpinBox* mine_spinBox=new QSpinBox;mine_spinBox->setValue(mine_number_);QHBoxLayout* up_l
40、ayout=new QHBoxLayout;up_layout->addWidget(row_spinBox);up_layout->addWidget(column_spinBox);up_layout->addWidget(mine_spinBox);QDialogButtonBox* dialog_buttonBox=new QDialogButtonBox;dialog_buttonBox->addButton(QDialogButtonBox:Ok);dialog_buttonBox->addButton(QDialogButtonBox:Cancel)
41、;connect(dialog_buttonBox,SIGNAL(accepted(),&dialog,SLOT(accept();connect(dialog_buttonBox,SIGNAL(rejected(),&dialog,SLOT(reject();QHBoxLayout* bottom_layout=new QHBoxLayout;bottom_layout->addStretch();bottom_layout->addWidget(dialog_buttonBox);QVBoxLayout* main_layout=new QVBoxLayout(
42、&dialog);main_layout->addLayout(up_layout);main_layout->addLayout(bottom_layout);if(dialog.exec()=QDialog:Accepted)if(row_spinBox->value()*column_spinBox->value()>mine_spinBox->value()current_standard_=3;row_=row_spinBox->value();column_=column_spinBox->value();mine_numbe
43、r_=mine_spinBox->value();slot_new_game();/*实现帮助菜单中的关于游戏,及功能*/void MainWindow:slot_about_game()QString introduction("<h2>"+tr("About Mine Sweepr")+"</h2>"+"<p>"+tr("This game is played by revealing squares of the grid, typically by cli
44、cking them with a mouse. If a square containing a mine is revealed, the player loses the game. Otherwise, a digit is revealed in the square, indicating the number of adjacent squares (out of the possible eight) that contain mines.if this number is zero then the square appears blank, and the surround
45、ing squares are automatically also revealed. By using logic, the player can in many instances use this information to deduce that certain other squares are mine-free, in which case they may be safely revealed, or mine-filled, in which they can be marked as such (which is effected by right-clicking t
46、he square and indicated by a flag graphic).")+"</p>"+"<p>"+tr("This program is free software; you can redistribute it and/or modify.it under the terms of the GNU General Public License as published by the Software Foundation; either version 3 of the License,
47、 or (at your option) any later version.")+"</p>"+"<p>"+tr("Please see ")+"<a href=/licenses/gpl-3.0.html>/licenses/gpl-3.0.html</a>"+tr(" for an overview of GPLv3 licensing")+"</p
48、>"+"<br>"+tr("Version:")+g_software_version+"</br>"+"<br>"+tr("Author: ")+g_software_author+"</br>");QMessageBox messageBox(QMessageBox:Information,tr("About Mine Sweeper"),introduction,QMessageBox:
49、Ok);messageBox.exec();/*游戏的判断,及所给出的提示做出判断*/void MainWindow:slot_game_over(bool is_win)timer.stop();QString name;if(is_win)switch(current_standard_)case 0:if(time_label->text().toInt()<easy_record_time_)name=QInputDialog:getText(this,tr("Please enter your name"),tr("You create a
50、record. Please enter your name");if(!name.isEmpty()easy_record_time_=time_label->text().toInt();easy_record_name_=name;elseQMessageBox:information(this,tr("Result"),tr("You win");break;case 1:if(time_label->text().toInt()<middle_record_time_)name=QInputDialog:getTex
51、t(this,tr("Please enter your name"),tr("You create a record. Please enter your name");if(!name.isEmpty()middle_record_time_=time_label->text().toInt();middle_record_name_=name;elseQMessageBox:information(this,tr("Result"),tr("You win");break;case 2:if(time_
52、label->text().toInt()<hard_record_time_)name=QInputDialog:getText(this,tr("Please enter your name"),tr("You create a record. Please enter your name");if(!name.isEmpty()hard_record_time_=time_label->text().toInt();hard_record_name_=name;elseQMessageBox:information(this,tr
53、("Result"),tr("You win");break;default:QMessageBox:information(this,tr("Result"),tr("You win");elseQMessageBox:information(this,tr("Result"),tr("You lose");/*定时器的设置*/void MainWindow:slot_timer()time_label->setText(QString("%1")
54、.arg(time.elapsed()/1000);/*关于菜单栏的设置是否显示游戏工具栏和状态栏*/void MainWindow:read_settings()QSettings settings;settings.beginGroup("MainWindow");resize(settings.value("size").toSize();move(settings.value("pos").toPoint();bool show_game_toolBar=settings.value("showGameToolBar
55、").toBool();show_game_toolBar_action->setChecked(show_game_toolBar);slot_show_game_toolBar(show_game_toolBar);bool show_statusBar=settings.value("showStatusBar").toBool();show_statusBar_action->setChecked(show_statusBar);slot_show_statusBar(show_statusBar); settings.endGroup();s
56、ettings.beginGroup("GameSetting");current_standard_=settings.value("current_standard").toInt();switch(current_standard_)case 0:easy_standard_action->setChecked(true);break;case 1:middle_standard_action->setChecked(true);break;case 2:hard_standard_action->setChecked(true)
57、;break;case 3:custom_standard_action->setChecked(true);break;default:;row_=settings.value("row").toInt()=0?9:settings.value("row").toInt();column_=settings.value("column").toInt()=0?9:settings.value("column").toInt();mine_number_=settings.value("mine_n
58、umber").toInt()=0?10:settings.value("mine_number").toInt();settings.endGroup();settings.beginGroup("Rank");easy_record_time_=settings.value("easy_record_time").toInt()=0?g_no_record_time:settings.value("easy_record_time").toInt();middle_record_time_=settings.value("middle_record_time").toInt()=0?g_no_record_time:settings.value("middle_record_time").toIn
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024年服务行业专属保密协议范本版
- 2024年多路水温测量仪合作协议书
- 2024年专业版凯悦酒店管理条款协议样本版
- 2024年企业内兼职讲师协议3篇
- 2024年合作协议:软件开发3篇
- 2024工程项目安全生产监管协议
- 2024年常用协议公证格式样本版
- 2024年度产品销售协议格式样本版A版
- 2024专项商标权联合使用协议
- 2024年博物馆展区装修协议3篇
- 农药安全科学使用技术
- 低压电工实用技术-常用电工工具的使用
- 哈尔滨工业大学高等数学期末考试试题和答案
- 钢筋笼吊装作业安全监理细则
- 酒店布草管理与核算
- 翁敏-孕期营养与膳食指导
- 人教版鄂教版六年级科学上册知识要点
- 实验报告 配合物的生成、性质和应用
- 雅思阅读必看完整课件
- 钢丝绳在卷筒上旋向的判断
- 西班牙授权书委托书格式委托书
评论
0/150
提交评论