扫雷游戏代码.docx
《扫雷游戏代码.docx》由会员分享,可在线阅读,更多相关《扫雷游戏代码.docx(15页珍藏版)》请在冰豆网上搜索。
![扫雷游戏代码.docx](https://file1.bdocx.com/fileroot1/2023-1/24/78e418a7-09c4-428c-b4c1-1984aa6bbc50/78e418a7-09c4-428c-b4c1-1984aa6bbc501.gif)
扫雷游戏代码
/**/
#ifndefBLOCK_H_
#defineBLOCK_H_
#include
classQWidget;
classBlock:
publicQLabel
{
Q_OBJECT
public:
explicitBlock(boolmine_flag,QWidget*parent=0);
voidset_number(intnumber);
voidturn_over();
boolis_mine()const;
boolis_turn_over()const;
signals:
voidturn_over(boolis_mine);
protected:
voidmousePressEvent(QMouseEvent*event);
private:
boolmine_flag_;
boolmark_flag_;
boolturn_over_flag_;
intnumber_;
};
#endif
#include""
#include
#include
#include
#include
Block:
:
Block(boolmine_flag,QWidget*parent)
:
QLabel(parent)
{
mine_flag_=mine_flag;
mark_flag_=false;
turn_over_flag_=false;
number_=-1;
setPixmap(QPixmap(":
/images/"));
}
voidBlock:
:
set_number(intnumber)
{
number_=number;
}
voidBlock:
:
turn_over()
{
if(!
turn_over_flag_){
turn_over_flag_=true;
if(mine_flag_)
setPixmap(QPixmap(":
/images/"));
else
setPixmap(QPixmap(":
/images/mine_"+QString("%1").arg(number_)+".png"));
update();
}
}
boolBlock:
:
is_mine()const
{
returnmine_flag_;
}
boolBlock:
:
is_turn_over()const
{
returnturn_over_flag_;
}
/*鼠标事件的实现*/
voidBlock:
:
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/"));
update();
emitturn_over(true);
}else{
setPixmap(QPixmap(":
/images/mine_"+QString("%1").arg(number_)+".png"));
update();
emitturn_over(false);
}
}
}elseif(event->button()==Qt:
:
RightButton){
if(!
turn_over_flag_){
if(!
mark_flag_){
mark_flag_=true;
setPixmap(QPixmap(":
/images/"));
}else{
mark_flag_=false;
setPixmap(QPixmap(":
/images/"));
}
update();
}
}
QLabel:
:
mousePressEvent(event);
}
#ifndefBLOCK_AREA_H_
#defineBLOCK_AREA_H_
#include""
#include
classQEvent;
classQGridLayout;
classQObject;
classBlockArea:
publicQWidget
{
Q_OBJECT
public:
BlockArea(introw,intcolumn,intmine_number,QWidget*parent=0);
voidset_block_area(introw,intcolumn,intmine_number,intinit_flag=false);
signals:
voidgame_over(boolis_win);
protected:
booleventFilter(QObject*watched,QEvent*event);
privateslots:
voidslot_turn_over(boolis_mine);
private:
intcalculate_mines(intx,inty)const;rg(easy_record_time_)),1,1);
up_layout->addWidget(newQLabel(easy_record_name_),1,2);
up_layout->addWidget(newQLabel(tr("Middle")),2,0);
up_layout->addWidget(newQLabel(QString("%1").arg(middle_record_time_)),2,1);
up_layout->addWidget(newQLabel(middle_record_name_),2,2);
up_layout->addWidget(newQLabel(tr("Hard")),3,0);
up_layout->addWidget(newQLabel(QString("%1").arg(hard_record_time_)),3,1);
up_layout->addWidget(newQLabel(hard_record_name_),3,2);
QPushButton*recount_button=newQPushButton(tr("recount"));
QPushButton*close_button=newQPushButton(tr("close"));
close_button->setDefault(true);
connect(recount_button,SIGNAL(clicked()),&dialog,SLOT(accept()));
connect(close_button,SIGNAL(clicked()),&dialog,SLOT(reject()));
QHBoxLayout*bottom_layout=newQHBoxLayout;
bottom_layout->addStretch();
bottom_layout->addWidget(recount_button);
bottom_layout->addWidget(close_button);
QVBoxLayout*main_layout=newQVBoxLayout(&dialog);
main_layout->addLayout(up_layout);
main_layout->addLayout(bottom_layout);
if()==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;
}
}
voidMainWindow:
:
slot_show_game_toolBar(boolshow)
{
if(show)
game_toolBar->show();
else
game_toolBar->hide();
}
voidMainWindow:
:
slot_show_statusBar(boolshow)
{
if(show)
statusBar()->show();
else
statusBar()->hide();
}
/*游戏的设置容易、中等、困难及自定义*/
voidMainWindow:
:
slot_standard(QAction*standard_action)
{
if(standard_action==easy_standard_action){
current_standard_=0;
row_=9;
column_=9;
mine_number_=10;
}elseif(standard_action==middle_standard_action){
current_standard_=1;
row_=16;
column_=16;
mine_number_=40;
}elseif(standard_action==hard_standard_action){
current_standard_=2;
row_=16;
column_=30;
mine_number_=99;
}elseif(standard_action==custom_standard_action){
QDialogdialog;
(tr("setstandard"));
QSpinBox*row_spinBox=newQSpinBox;
row_spinBox->setRange(5,50);
row_spinBox->setValue(row_);
QSpinBox*column_spinBox=newQSpinBox;
column_spinBox->setRange(5,50);
column_spinBox->setValue(column_);
QSpinBox*mine_spinBox=newQSpinBox;
mine_spinBox->setValue(mine_number_);
QHBoxLayout*up_layout=newQHBoxLayout;
up_layout->addWidget(row_spinBox);
up_layout->addWidget(column_spinBox);
up_layout->addWidget(mine_spinBox);
QDialogButtonBox*dialog_buttonBox=newQDialogButtonBox;
dialog_buttonBox->addButton(QDialogButtonBox:
:
Ok);
dialog_buttonBox->addButton(QDialogButtonBox:
:
Cancel);
connect(dialog_buttonBox,SIGNAL(accepted()),&dialog,SLOT(accept()));
connect(dialog_buttonBox,SIGNAL(rejected()),&dialog,SLOT(reject()));
QHBoxLayout*bottom_layout=newQHBoxLayout;
bottom_layout->addStretch();
bottom_layout->addWidget(dialog_buttonBox);
QVBoxLayout*main_layout=newQVBoxLayout(&dialog);
main_layout->addLayout(up_layout);
main_layout->addLayout(bottom_layout);
if()==QDialog:
:
Accepted)
if(row_spinBox->value()*column_spinBox->value()>mine_spinBox->value()){
current_standard_=3;
row_=row_spinBox->value();
column_=column_spinBox->value();
mine_number_=mine_spinBox->value();
}
}
slot_new_game();
}
/*实现帮助菜单中的关于游戏,及功能*/
voidMainWindow:
:
slot_about_game()
{
QStringintroduction(
"
"+tr("AboutMineSweepr")+"
"
+"
"+tr("Thisgameisplayedbyrevealingsquaresofthegrid,typicallybyclickingthemwithamouse.Ifasquarecontainingamineisrevealed,theplayerlosesthegame.Otherwise,adigitisrevealedinthesquare,indicatingthenumberofadjacentsquares(outofthepossibleeight)thatcontainthisnumberiszerothenthesquareappearsblank,andthesurroundingsquaresareautomaticallyalsorevealed.Byusinglogic,theplayercaninmanyinstancesusethisinformationtodeducethatcertainothersquaresaremine-free,inwhichcasetheymaybesafelyrevealed,ormine-filled,inwhichtheycanbemarkedassuch(whichiseffectedbyright-clickingthesquareandindicatedbyaflaggraphic).")+"
"
+"
"+tr("Thisprogramisfreesoftware;youcanredistributeitand/orunderthetermsoftheGNUGeneralPublicLicenseaspublishedbytheSoftwareFoundation;eitherversion3oftheLicense,or(atyouroption)anylaterversion.")+"
"
+"
"+tr("Pleasesee")+""
+"
"+tr("Version:
")+g_software_version+""
+"
"+tr("Author:
")+g_software_author+""
);
QMessageBoxmessageBox(QMessageBox:
:
Information,tr("AboutMineSweeper"),introduction,QMessageBox:
:
Ok);
();
}
/*游戏的判断,及所给出的提示做出判断*/
voidMainWindow:
:
slot_game_over(boolis_win)
{
();
QStringname;
if(is_win){
switch(current_standard_){
case0:
if(time_label->text().toInt()name=QInputDialog:
:
getText(this,tr("Pleaseenteryourname"),tr("Youcreatearecord.Pleaseenteryourname"));
if(!
()){
easy_record_time_=time_label->text().toInt();
easy_record_name_=name;
}
}else
QMessageBox:
:
information(this,tr("Result"),tr("Youwin"));
break;
case1:
if(time_label->text().toInt()name=QInputDialog:
:
getText(this,tr("Pleaseenteryourname"),tr("Youcreatearecord.Pleaseenteryourname"));
if(!
()){
middle_record_time_=time_label->text().toInt();
middle_record_name_=name;
}
}else
QMessageBox:
:
information(this,tr("Result"),tr("Youwin"));
break;
case2:
if(time_label->text().toInt()name=QInputDialog:
:
getText(this,tr("Pleaseenteryourname"),tr("Youcreatearecord.Pleaseenteryourname"));
if(!
()){
hard_record_time_=time_label->text().toInt();
hard_record_name_=name;
}
}else
QMessageBox:
:
information(this,tr("Result"),tr("Youwin"));
break;
default:
QMessageBox:
:
information(this,tr("Result"),tr("Youwin"));
}
}else{
QMessageBox:
:
information(this,tr("Result"),tr("Youlose"));
}
}
/*定时器的设置*/
voidMainWindow:
:
slot_timer()
{
time_label->setText(QString("%1").arg()/1000));
}
/*关于菜单栏的设置是否显示游戏工具栏和状态栏*/
voidMainWindow:
:
read_settings()
{
QSettingssettings;
("MainWindow");
resize("size").toSize());
move("pos").toPoint());
boolshow_game_toolBar=("showGameToolBar").toBool();
show_game_toolBar_action->setChecked(show_game_toolBar);
slot_show_game_toolBar(show_game_toolBar);
boolshow_statusBar=("showStatusBar").toBool();
show_statusBar_action->setChecked(show_statusBar);
slot_show_statusBar(show_statusBar);
();
("GameSetting");
current_standard_=("current_standard").toInt();
switch(current_standard_){
case0:
easy_standard_action->setChecked(true);break;
case1:
middle_standard_action->setChecked(true);break;
case2:
hard_standard_action->setChecked(true);break;
case3:
custom_standard_action->setChecked(true);break;
default:
;
}
row_=("row").toInt()==09:
("row").toInt();
column_=("column").toInt()==09:
("column").toInt();
mine_number_=("mine_number").toInt()==010:
("mine_number").toInt();
();
("Rank");
easy_record_time_=("easy_record_time").toInt()==0g_no_record_time:
("easy_record_time").toInt();
middle_record_time_=("middle_record_time").toInt()==0g_no_record_time:
("middle_record_time").toInt();
hard_record_time_=("h