扫雷游戏代码.docx
- 文档编号:5525377
- 上传时间:2022-12-18
- 格式:DOCX
- 页数:25
- 大小:23.26KB
扫雷游戏代码.docx
《扫雷游戏代码.docx》由会员分享,可在线阅读,更多相关《扫雷游戏代码.docx(25页珍藏版)》请在冰豆网上搜索。
扫雷游戏代码
/*block.h*/
#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"block.h"
#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/normal.png"));
}
voidBlock:
:
set_number(intnumber)
{
number_=number;
}
voidBlock:
:
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();
}
}
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/mine.png"));
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/flag.png"));
}else{
mark_flag_=false;
setPixmap(QPixmap(":
/images/normal.png"));
}
update();
}
}
QLabel:
:
mousePressEvent(event);
}
#ifndefBLOCK_AREA_H_
#defineBLOCK_AREA_H_
#include"block.h"
#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;//璁$畻浠,y涓轰腑蹇幂殑涔濆镙煎唴镄勯浄鏁voidtry_to_turn_over_more_blocks(intx,inty);
private:
QGridLayout*mainLayout;
introw_;
intcolumn_;
inttotal_block_number_;
inttotal_mine_number_;
intturn_over_block_number_;
boolgame_over_flag_;
};
#endif
/*block_area.h*/
#include"block_area.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
/*雷的随机布置*/
ptrdiff_trandom(ptrdiff_ti)
{
returnqrand()%i;
}
ptrdiff_t(*p_random)(ptrdiff_t)=random;
BlockArea:
:
BlockArea(introw,intcolumn,intmine_number,QWidget*parent)
:
QWidget(parent)
{
set_block_area(row,column,mine_number,true);
}
voidBlockArea:
:
set_block_area(introw,intcolumn,intmine_number,intinit_flag)
{
if(!
init_flag){
for(inti=0;i for(intj=0;j deletestatic_cast deletemainLayout; } row_=row; column_=column; total_block_number_=row_*column_; total_mine_number_=mine_number; turn_over_block_number_=0; game_over_flag_=false; boolmine_flag[total_block_number_]; for(inti=0;i mine_flag[i]=true; for(inti=total_mine_number_;i mine_flag[i]=false; QTimetime; time=QTime: : currentTime(); qsrand(time.msec()+time.second()*1000); std: : random_shuffle(mine_flag,mine_flag+total_block_number_,p_random); mainLayout=newQGridLayout(this); for(inti=0;i for(intj=0;j mainLayout->addWidget(newBlock(mine_flag[i*column_+j]),i,j); for(inti=0;i for(intj=0;j Block*current_block=static_cast 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_over_flag_) if(event->type()==QEvent: : MouseButtonPress) returntrue; returnQWidget: : eventFilter(watched,event); } voidBlockArea: : slot_turn_over(boolis_mine) { if(is_mine){ game_over_flag_=true; emitgame_over(false); }else{ ++turn_over_block_number_; if(turn_over_block_number_==total_block_number_-total_mine_number_){ game_over_flag_=true; emitgame_over(true); }else{ for(inti=0;i for(intj=0;j if(sender()==static_cast try_to_turn_over_more_blocks(i,j); } } } intBlockArea: : calculate_mines(intx,inty)const { intnumber=0; for(inti=0;i<3;i++) for(intj=0;j<3;j++) if((x-1+i>=0)&&(x-1+i if(static_cast ++number; returnnumber; } voidBlockArea: : try_to_turn_over_more_blocks(intx,inty) { QQueue QPair queue.enqueue(qMakePair(x,y)); while(! queue.isEmpty()){ pair=queue.head(); queue.dequeue(); if(calculate_mines(pair.first,pair.second)==0){ for(inti=0;i<3;i++){ for(intj=0;j<3;j++){ if((pair.first-1+i>=0)&&(pair.first-1+i if(! static_cast static_cast ++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; emitgame_over(true); } } #ifndefMAIN_WINDOW_H_ #defineMAIN_WINDOW_H_ #include"block_area.h" #include #include #include classQAction; classQActionGroup; classQCloseEvent; classQMenu; classQToolBar; classQWidget; classMainWindow: publicQMainWindow { Q_OBJECT public: MainWindow(QWidget*parent=0); protected: voidcloseEvent(QCloseEvent*event); privateslots: voidslot_new_game(); voidslot_rank(); voidslot_show_game_toolBar(boolshow); voidslot_show_statusBar(boolshow); voidslot_standard(QAction*standard_action); voidslot_about_game(); voidslot_game_over(boolis_win); voidslot_timer(); private: voidread_settings(); voidwrite_settings(); voidcreate_actions(); voidcreate_menus(); voidcreate_game_toolBar(); voidcreate_statusBar(); private: BlockArea*area_; introw_; intcolumn_; intmine_number_; inteasy_record_time_; intmiddle_record_time_; inthard_record_time_; QStringeasy_record_name_; QStringmiddle_record_name_; QStringhard_record_name_; intcurrent_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_standard_action; QAction*custom_standard_action; QActionGroup*standard_actionGroup; QAction*about_game_action; QAction*about_qt_action; QLabel*time_label; QTimetime; QTimertimer; }; #endif #include"main_window.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /*关于游戏介绍的信息*/ constQStringg_software_name="MineSweeper"; constQStringg_software_version="1.2"; constQStringg_software_author="CHANGHUIZHEN"; /*关于排行榜的信息*/ constintg_no_record_time=9999; constQStringg_no_record_name=""; MainWindow: : MainWindow(QWidget*parent) : QMainWindow(parent) { area_=newBlockArea(9,9,10);//一般设置 connect(area_,SIGNAL(game_over(bool)),this,SLOT(slot_game_over(bool))); 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_software_name); setWindowIcon(QIcon(": /game.png")); /*等待最长时间,到时间就会提示游戏失败*/ timer.start(1000); connect(&timer,SIGNAL(timeout()),this,SLOT(slot_timer())); slot_new_game(); } voidMainWindow: : closeEvent(QCloseEvent*) { write_settings(); } voidMainWindow: : slot_new_game() { area_->set_block_area(row_,column_,mine_number_); time_label->setText("0"); time.restart(); timer.start(); } voidMainWindow: : slot_rank() { QDialogdialog; dialog.setWindowTitle(tr("rank")); QGridLayout*up_layout=newQGridLayout; up_layout->addWidget(newQLabel(tr("Standard")),0,0); up_layout->addWidget(newQLabel(tr("Time")),0,1); up_layout->addWidget(newQLabel(tr("Name")),0,2); up_layout->a
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 扫雷 游戏 代码