掃雷游戲代碼_第1頁
掃雷游戲代碼_第2頁
掃雷游戲代碼_第3頁
掃雷游戲代碼_第4頁
掃雷游戲代碼_第5頁
已閱讀5頁,還剩33頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、精選文檔/*block.h*/#ifndef BLOCK_H_#define BLOCK_H_#include 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 ;voidsignals : turn_over( bool is_mine);

2、voidprotected mousePressEvent( QMouseEvent * event);privateboolmine_flag_;可編輯boolmark_flag_;精選文檔bool turn_over_flag_; int number_;#endif#include block.h#include #include #include #include Block:Block( bool mine_flag, QWidget * parent):QLabel (parent) mine_flag_=mine_flag;mark_flag_= false ;turn_over

3、_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 (:/images/mine.png ); elsesetPixmap( QPixmap (:/images/mine_ + QString (%1

4、 ).arg(numb er_)+ .png );update();bool Block:is_mine() constreturn mine_flag_;bool Block:is_turn_over() constreturn turn_over_flag_;可編輯精選文檔/* 鼠標(biāo)事件的實(shí)現(xiàn) */void Block:mousePressEvent( QMouseEvent * event)if (event-button()=Qt :LeftButton)if (!turn_over_flag_&!mark_flag_)turn_over_flag_= true ;if (mine_f

5、lag_= true )setPixmap( QPixmap (:/images/mine.png );update();emit turn_over( true );else setPixmap( QPixmap (:/images/mine_ + QString (%1 ).arg(numb er_)+ .png );update();emit turn_over( false );else if (event-button()= Qt :RightButton)if (!mark_flag_)if (!turn_over_flag_)可編輯精選文檔mark_flag_= true ;se

6、tPixmap( QPixmap (:/images/flag.png );else mark_flag_= false ; setPixmap( QPixmap (:/images/normal.png );update();QLabel :mousePressEvent(event);#ifndef BLOCK_AREA_H_#define BLOCK_AREA_H_#include block.h#include class QEvent ;class QGridLayout ;class QObject ;class BlockArea: public QWidgetQ_OBJECTp

7、ublic :BlockArea( int row, int column, int mine_number, QWidget * pare nt= 0);void set_block_area( int row, int column, int mine_number, int ini t_flag= false);signals :void game_over( bool is_win);protected :booleve ntFilter( QObject * watched, QEve nt * eve nt);private slots :void slot_turn_over(

8、bool is_mine);private :,y涓int calculatein es( int x,i nt y)c on st ;/ 璁$畻浠轟腑蹇幕兢涔?jié)?鏍煎唴鐨勯浄鏁void try_to_turn_over_more_blocks(int x,int y);private :QGridLayout * main Layout;int row_;int column_;int total_block_number_;int total_mine_number_;int turn_over_block_number_;bool game_over_flag_;#endif/*bloc

9、k_area.h*/#include block_area.h#include #include #include #include #include #include #include #include #include /*雷的隨機(jī)布置 */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 (pare

10、nt)set_block_area(row,column,mine_number, true );void BlockArea:set_block_area( int row, int column, int mine_number, int init_flag)if (!init_flag)for (int i= 0;irow_;i+)for (int j= 0;jcolumn_;j+)deletestatic_cast (mainLayout-itemAtPosition(i,j)-widget();delete mainLayout;row_=row;column_=column;tot

11、al_block_number_=row_*column_;total_mine_number_=mine_number; turn_over_block_number_= 0; game_over_flag_= false ; 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= QTi

12、me :currentTime(); qsrand(time.msec()+time.second()* 1000 ); std:random_shuffle(mine_flag,mine_flag+total_block_number_,p _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

13、+)Block*current_block= static_cast (mainLayout-itemAtPosition(i,j)-wi dget();current_block-set_number(calculate_mines(i,j);connect(current_block, SIGNAL (turn_over( bool ),this ,SLOT(slot_tu rn_over( bool );current_block-installEventFilter(this );bool BlockArea:eventFilter( QObject * watched, QEvent

14、 * 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

15、_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()= static_cast (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

16、i= 0 ;i 3 ;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 number;void BlockArea:try_to_turn_over_more_blocks( int x,int y)QQueue QPair queue;QPair pair; queue.enqueue(qMakePair(x,y);while (!queue.isEmpty

17、()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= 0) &(pair.first- 1+i= 0) &(pair.second- 1 +jcolumn_) )if (! static_cast (mainLayout-itemAtPosition(pair.first-+i,pair.second- 1 +j)-widget()-is_turn_over()1+i,pstatic_cast (mainLa

18、yout-itemAtPosition(pair.first-air.second- 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 MA

19、IN_WINDOW_H_#include block_area.h#include #include #include class QAction ;class QActionGroup ;class QCloseEvent ;class QMenu ;class QToolBar ;class QWidget ;class MainWindow: public QMainWindowQ_OBJECTpublic :MainWindow( QWidget * parent= 0);protectedvoid closeEvent( QCloseEvent * event);private sl

20、ots :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 c

21、reate_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_;i

22、nt current_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_acti

23、on;QAction * hard_standard_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 #include #include #include #include #include #include #include

24、 #include #include #include #include #include #include #include #include #include #include #include #include /* 關(guān)于游戲介紹的信息 */const QString g_software_name= Mine Sweeper ;const QString g_software_version= 1.2 ;const QString g_software_author= CHANGHUIZHEN ;/* 關(guān)于排行榜的信息 */const int g_no_record_time= 999

25、9 ;const QString g_no_record_name= ;MainWindow:MainWindow( QWidget * parent):QMainWindow (parent)area_= new BlockArea( 9,9,10);/ 一般設(shè)置connect(area_, SIGNAL (game_over( bool ),this ,SLOT (slot_game_o ver( bool );setCentralWidget(area_);create_actions();create_menus();create_game_toolBar();create_statu

26、sBar();QCoreApplication :setOrganizationName(g_software_author);QCoreApplication :setApplicationName(g_software_name);read_settings();layout()-setSizeConstraint( QLayout :SetFixedSize);setWindowTitle(g_software_name);setWindowIcon( QIcon (:/game.png );/* 等待最長時(shí)間,到時(shí)間就會(huì)提示游戲失敗 */timer.start( 1000 );conn

27、ect(&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();void MainWindow:slot_rank()QDialog d

28、ialog;dialog.setWindowTitle(tr(rank );QGridLayout * up_layout=new QGridLayout ;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-addWidget(newQLabel (tr( Easy ),1,0);up_layout-addWidget(newQLabel

29、 (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-addWidget( newQLabel (QString (%1 ).arg(middle_record_time_), 2,1);up_layout-addWidget(new QLabel (middle_record_name_), 2,2);up_layout-ad

30、dWidget(new QLabel (tr( Hard ),3,0);up_layout-addWidget( newQLabel (QString (%1 ).arg(hard_record_time_), 3,1);up_layout-addWidget( new QLabel (hard_record_name_), 3,2);QPushButton * recount_button= new QPushButton (tr( recount );QPushButton * close_button= new QPushButton (tr( close ); close_button

31、-setDefault( true );connect(recount_button, SIGNAL (clicked(),&dialog, SLOT(accept( );connect(close_button, SIGNAL (clicked(),&dialog, SLOT(reject();QHBoxLayout * bottom_layout= new QHBoxLayout ; bottom_layout-addStretch(); bottom_layout-addWidget(recount_button); bottom_layout-addWidget(close_butto

32、n);QVBoxLayout * main_layout= new QVBoxLayout (&dialog); main_layout-addLayout(up_layout); 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_

33、record_name;void MainWindow:slot_show_game_toolBar( bool show)if (show)game_toolBar-show();elsegame_toolBar-hide();void MainWindow:slot_show_statusBar( bool show)if (show)statusBar()-show();elsestatusBar()-hide();/* 游戲的設(shè)置容易、中等、困難及自定義 */void MainWindow:slot_standard( QAction * standard_action)if (sta

34、ndard_action=easy_standard_action)current_standard_= 0;row_= 9;column_= 9;mine_number_= 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_=

35、99;else if (standard_action=custom_standard_action)QDialog dialog;dialog.setWindowTitle(tr( set 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-setVal

36、ue(column_); QSpinBox * mine_spinBox= new QSpinBox ; mine_spinBox-setValue(mine_number_);QHBoxLayout * up_layout= new QHBoxLayout ; up_layout-addWidget(row_spinBox); up_layout-addWidget(column_spinBox); up_layout-addWidget(mine_spinBox);QDialogButtonBox * dialog_buttonBox= newQDialogButtonBox ;dialo

37、g_buttonBox-addButton( QDialogButtonBox :Ok);dialog_buttonBox-addButton( QDialogButtonBox :Cancel);connect(dialog_buttonBox, SIGNAL (accepted(),&dialog, SLOT(ac cept();connect(dialog_buttonBox, SIGNAL (rejected(),&dialog, SLOT (reje ct();QHBoxLayout * bottom_layout= new QHBoxLayout ; bottom_layout-a

38、ddStretch();bottom_layout-addWidget(dialog_buttonBox);QVBoxLayout * main_layout= newQVBoxLayout (&dialog);main_layout-addLayout(up_layout); main_layout-addLayout(bottom_layout);if (dialog.exec()= QDialog :Accepted)if (row_spinBox-value()*column_spinBox-value()mine_spinBo x-value()current_standard_=

39、3; row_=row_spinBox-value(); column_=column_spinBox-value(); mine_number_=mine_spinBox-value(); slot_new_game();/* 實(shí)現(xiàn)幫助菜單中的關(guān)于游戲,及功能 */void MainWindow:slot_about_game()QString introduction( +tr( About Mine Sweepr )+ + +tr( This game is played by revealing squares ofthe grid, typically by clicking the

40、m 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 surrounding squar

41、es 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 the square

42、 and indicated by a flag graphic). )+ + +tr( This program is free software; you canredistribute it and/or modify.it under the terms of the GNU GeneralPublic License as published by the Software Foundation; either version 3 of the License, or (at your option) any later version. )+ + +tr( Please see )

43、+ /licenses/gpl-3.0.html+tr( for an overview of GPLv3 licensing )+ + + +tr( Version: )+g_software_version+ +tr( Author: )+g_software_author+ );QMessageBoxmessageBox( QMessageBox :Information,tr( About Mine Sweeper ),introduction, QMessageBox :Ok);messageBox.exec();/* 游戲的判斷,及所給出的提示做出

44、判斷 */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()text().toInt();easy_record_name_=name;elseQMessageBox :information( this ,tr( Result ),tr( You win ); break ;case 1:if (time_label-text().toInt()text().

45、toInt();middle_record_name_=name;elseQMessageBox :information( this ,tr( Result ),tr( You win );break ;case 2:if (time_label-text().toInt()text().toInt();hard_record_name_=name;elseQMessageBox :information( this ,tr( Result ),tr( You win );break ;default :QMessageBox :information( this ,tr( Result )

46、,tr( Youwin );else QMessageBox :information( this ,tr( Result ),tr( Youlose );/*定時(shí)器的設(shè)置 */void MainWindow:slot_timer()time_label-setText( QString (%1 ).arg(time.elapsed()/ 1000 );/* 關(guān)于菜單欄的設(shè)置是否顯示游戲工具欄和狀態(tài)欄 */void MainWindow:read_settings()QSettings settings;settings.beginGroup( MainWindow );resize(sett

47、ings.value( size ).toSize();move(settings.value( pos ).toPoint();boolshow_game_toolBar=settings.value( 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_status

48、Bar_action-setChecked(show_statusBar);slot_show_statusBar(show_statusBar);settings.endGroup();settings.beginGroup( GameSetting ); current_standard_=settings.value( current_standard ).toInt(); switch (current_standard_)case 0:easy_standard_action-setChecked( true );break ;case1:middle_standard_action

49、-setChecked( true );break ;case 2:hard_standard_action-setChecked( true );break ;case3:custom_standard_action-setChecked( true );break ;default :;row_=settings.value( row ).toInt()= 0 ?9:settings.value( row ).to Int();column_=settings.value( column ).toInt()= 0 ?9 :settings.value( c olumn ).toInt();

50、mine_number_=settings.value( mine_number ).toInt()= 0 ?10 :se ttings.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_=setting

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論