版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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_
2、over(bool is_mine);protected: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,QW
3、idget* parent):QLabel(parent)mine_flag_=mine_flag;mark_flag_=false;turn_over_flag_=false;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("
4、:/images/mine.png");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-
5、>button()=Qt:LeftButton)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();
6、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");elsemark_flag_=false;setPixmap(QPixmap(":/images/normal.png");update();QLabel:mousePressEvent(event);#ifndef BLOCK_AREA_H_#def
7、ine BLOCK_AREA_H_#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);sig
8、nals:void game_over(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 c
9、olumn_;int total_block_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
10、>#include<QQueue>#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);vo
11、id BlockArea:set_block_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_;
12、total_mine_number_=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()+tim
13、e.second()*1000);std: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* curren
14、t_block=static_cast<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, QEven
15、t* event)if(game_over_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_-t
16、otal_mine_number_)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(
17、int i=0;i<3;i+)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_bl
18、ocks(int x,int y)QQueue<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) && (pa
19、ir.first-1+i<row_) && (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.sec
20、ond-1+j)->widget()->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&qu
21、ot;#include<QMainWindow>#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 s
22、lots: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_about_game();void slot_game_over(bool is_win);void slot_timer();private:void read_settings();void write_settings();void create_
23、actions();void create_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 curr
24、ent_standard_;QMenu* 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_s
25、tandard_action;QAction* 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>#incl
26、ude<QDialog>#include<QDialogButtonBox>#include<QHBoxLayout>#include<QIcon>#include<QInputDialog>#include<QLayout>#include<QMainWindow>#include<QMenu>#include<QMenuBar>#include<QMessageBox>#include<QPushButton>#include<QSettings>
27、#include<QSpinBox>#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"/*关于排
28、行榜的信息*/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(bool);setCentralWidget(area_);create_actions();create_menus();cr
29、eate_game_toolBar();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"); /*等待最长
30、时间,到时间就会提示游戏失败*/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_);time_label->setText("0");time.restart(
31、);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
32、(tr("Name"),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_
33、layout->addWidget(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_la
34、yout->addWidget(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_
35、button,SIGNAL(clicked(),&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)
36、;main_layout->addLayout(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
37、();elsegame_toolBar->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_
38、=10;else if(standard_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("s
39、et standard");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_)
40、;QHBoxLayout* up_layout=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(QDialo
41、gButtonBox:Cancel);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_layou
42、t=new QVBoxLayout(&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->
43、value();mine_number_=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
44、, typically by clicking 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
45、, and the surrounding 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 b
46、y right-clicking the 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
47、 3 of the License, 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&q
48、uot;)+"</p>"+"<br>"+tr("Version:")+g_software_version+"</br>"+"<br>"+tr("Author: ")+g_software_author+"</br>");QMessageBox messageBox(QMessageBox:Information,tr("About Mine Sweeper"),introdu
49、ction,QMessageBox: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(&
50、quot;You create a 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=Q
51、InputDialog:getText(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");bre
52、ak;case 2:if(time_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:i
53、nformation(this,tr("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(QStri
54、ng("%1").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(&qu
55、ot;showGameToolBar").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); set
56、tings.endGroup();settings.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-&g
57、t;setChecked(true);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.
58、value("mine_number").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").toInt();hard_record_time_=settings.value(&qu
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 花卉养护教学课程设计
- 福建师范大学《班级管理含班主任工作与综合育人》2022-2023学年第一学期期末试卷
- 英语教师教育实习总结(3篇)
- 初中七年级主题班会:创新思维的培养(课件)
- 楼顶防水工程简单版合同范本(3篇)
- 收费员个人述职报告
- 清明假期安全教育策划书(3篇)
- Kavain-Standard-生命科学试剂-MCE
- Isopentyl-3-4-methoxyphenyl-acrylate-Standard-生命科学试剂-MCE
- xxx民办学校教师管理方案
- 《中国传统建筑》课件-中国民居建筑
- 《铁路货运组织》课件-项目2 整车、零担货物运输过程
- 山东省高等学校教师岗前培训考试暨教师资格笔试题库及完整答案(易错题)
- 新制定《公平竞争审查条例》学习课件
- DZ/T 0452.3-2023 稀土矿石化学分析方法 第3部分:锂、铍、钪、锰、钴、镍、铜、锌、镓、铷、铌、钼、铟、铯、钽、钨、铊、铅、铋、钍、铀及15个稀土元素含量的测定 ICP-MS法(正式版)
- 少数民族朝鲜族民俗介绍
- 2024年环磷酰胺原料药项目申请报告
- 鸟的天堂(课件)五年级上册语文
- 安全防护用品采购管理制度
- 人教版《烛之武退秦师》课件(共42张)
- 中医定向透药治疗在临床上的应用试题及答案
评论
0/150
提交评论