自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

使用Qt開發(fā)俄羅斯方塊游戲

移動開發(fā)
本文講解的是使用Qt開發(fā)俄羅斯方塊游戲,不管是是手機,電視,電腦等一些電子類的產(chǎn)品,似乎都有俄羅斯方塊這么一塊游戲,那么作為編程者是否對他的研究更深刻呢?那么看本文章的講述吧!

使用Qt開發(fā)俄羅斯方塊游戲,可能大家都比較感興趣吧。那么就快看下面的詳細講解吧!

其實在Qt Creator中已經(jīng)有了俄羅斯方塊的例子,大家可以在幫助中搜索Tetrix進行查看。其內(nèi)容如下:

用Qt寫俄羅斯方塊游戲

但是對于初學者,這個例子并不是那么容易就能看懂。所以我結合這個例子和網(wǎng)上的一些資料,寫了一個比較簡單的方塊游戲類。希望能幫助初學者更好的理解這個例子和寫出自己的方塊游戲。

我這里已經(jīng)對所有的功能函數(shù)進行了整理,最簡單的,你只需要定義一個對象就能讓這個游戲運行起來。

寫最簡單的游戲

1. 新建Empty Qt4 Project,我們這里命名為myTetrix 。

2. 向工程里添加新的普通文本文件,命名為main.cpp 。

3. 將myitem.cpp, myitem.h, gamearea.cpp, gamearea.h四個文件復制到工程文件夾下。

4. 將上面四個文件加入到工程中。

5. 將main.cpp的內(nèi)容更改如下:

  1. #include  
  2. #include "gamearea.cpp"  
  3. int main(int argc,char* argv[])  
  4. {  
  5. QApplication app(argc,argv);  
  6. GameArea box(500);  
  7. box.show();  
  8. return app.exec();  

6.然后運行程序。效果如下圖。

用Qt寫俄羅斯方塊游戲

當游戲結束時會彈出提示框,確定后游戲將重新開始。

如下圖所示。

用Qt寫俄羅斯方塊游戲

7.可以看到,用這個類建立方塊游戲是這么的簡單。我們只是在主函數(shù)里新建了一個該類的對象,其中的參數(shù)為方塊下移的速度,單位是毫秒,上面的500即0.5秒。

提示:(如何向工程中添加文件)

用Qt寫俄羅斯方塊游戲

在工程文件夾上點右鍵,彈出的菜單中Add New表示添加新文件,Add Existing Files表示添加工程文件夾中已有的文件。

#p#

功能展示

要想實現(xiàn)更強大的功能,我們就需要應用控制窗體,而讓這個游戲區(qū)域只作為它的一個部件。為了更好的控制游戲,我們也需要自己建立定時器,而不再應用該類自帶的定時器了。

核心功能:

(一)建立工程。

1.首先建立工程Qt4 Gui Application,這里命名為Tetris,選用QWidget作為Base class 。

2.然后將myitem.cpp, myitem.h ,gamearea.cpp, gamearea.h四個文件復制到工程文件夾下并添加到工程中。

3.在widget.h中添加#include "gamearea.h"的頭文件包含。并在下面的private中聲明一個游戲類對象GameArea *gameArea;

4.在widget.cpp的構造函數(shù)里添加語句。

  1. Widget::Widget(QWidget *parent) :  
  2. QWidget(parent),  
  3. ui(new Ui::Widget)  
  4. {  
  5. ui->setupUi(this);  
  6. this->resize(800,500);  
  7. this->gameArea = new GameArea(this);  

這里重新設定了主窗口大小,并在主窗口上新建了一個游戲區(qū)域對象。

5.這時運行程序效果如下。

用Qt寫俄羅斯方塊游戲

可以看到,因為使用了另一個構造函數(shù),沒有使用該類自帶的定時器,所以只是顯示了游戲區(qū)域,游戲并沒有運行。

(二)添加定時器和開始按鈕,讓游戲可以運行。

1.在widget.h里的private中添加定時器對象和分數(shù)變量的聲明。

  1. QTimer *timer;  
  2. int score; 

在public中添加顯示分數(shù)函數(shù)的聲明。

  1. void doScore(int); 

添加槽函數(shù)的聲明。

  1. private slots:  
  2. void timer_upDate(); 

2.在widget.cpp文件中的構造函數(shù)里添加下面的語句:

  1. this->timer = new QTimer(this);  
  2. connect(this->timer,SIGNAL(timeout()),this,SLOT(timer_upDate()));  
  3. score =0

定義了定時器并進行了信號和槽函數(shù)的關聯(lián),初始化分數(shù)為0;

3.然后在下面定義兩個函數(shù)。

  1. void Widget::timer_upDate() //定時器溢出處理  
  2. {  
  3. this->gameArea->moveOneStep(); //先移動一步,這時并沒有顯示出來  
  4. if(this->gameArea->isMoveEnd()) //如果無法移動,到底了或結束了  
  5. {  
  6. if(this->gameArea->isGame_Over()) //如果是結束了  
  7. {  
  8. this->timer->stop(); //停止計時  
  9. QMessageBox::warning(this,tr("warning"),tr("Game Over!"),QMessageBox::Yes);  
  10. //彈出對話框  
  11. this->score =0; //清空分數(shù)  
  12. this->gameArea->init_Game(); //重新開始游戲  
  13. this->gameArea->gameStart();  
  14. this->timer->start(500);  
  15. }  
  16. else //如果是移動到底了  
  17. this->gameArea->nextItem(); //出現(xiàn)下一個圖形  
  18. int num = this->gameArea->getFullRowNum(); //獲得已滿的行數(shù)  
  19. this->doScore(num); //顯示分數(shù)  
  20. this->gameArea->gameStart(); //繼續(xù)游戲  
  21. }  
  22. }  
  23. else //如果沒有到底  
  24. {  
  25. this->gameArea->do_MoveNext(); //顯示方塊下移一步后的界面  
  26. }  
  27. }  
  28. void Widget::doScore(int num) //顯示分數(shù)  
  29. {  
  30. score += num*100;  
  31. this->ui->label_2->setText(tr("%1").arg(score));  

4.在設計器中向主窗口上添加兩個標簽label和label_2,其中l(wèi)abel寫上“你的分數(shù)是:”,label_2寫上“0”;然后再添加一個開始按鈕。添加完后效果如下。

用Qt寫俄羅斯方塊游戲

5.然后右擊“開始游戲”按鈕,選擇其單擊事件的槽函數(shù)。更改如下。

  1. void Widget::on_pushButton_clicked() //開始按鈕  
  2. {  
  3. this->gameArea->init_Game(); //第一次進入游戲時進行的初始化  
  4. this->gameArea->gameStart(); //開始游戲  
  5. this->timer->start(500); //開啟定時器  
  6. this->gameArea->setFocus(); //讓游戲區(qū)域獲得焦點,這樣才能響應鍵盤  

6.現(xiàn)在游戲已經(jīng)可以正常進行了。運行效果如下。

用Qt寫俄羅斯方塊游戲 

用Qt寫俄羅斯方塊游戲 

#p#
(三)添加暫停和重新開始按鈕,完成基本的控制功能。

1.在主窗口上添加“暫停游戲”和“重新開始”兩個按鈕。在“暫停游戲”按鈕的屬性中將checkable選中。如下圖所示。

用Qt寫俄羅斯方塊游戲 

用Qt寫俄羅斯方塊游戲

2.分別進入兩個按鈕的單擊事件槽函數(shù)。修改如下。

  1. void Widget::on_pushButton_2_clicked() //暫停按鈕  
  2. {  
  3. if(this->ui->pushButton_2->isChecked())  
  4. {  
  5. this->timer->stop();  
  6. this->ui->pushButton_2->setText(tr("取消暫停"));  
  7. }  
  8. else  
  9. {  
  10. this->timer->start(500);  
  11. this->ui->pushButton_2->setText(tr("暫停游戲"));  
  12. this->gameArea->setFocus();  
  13. }  
  14. }  
  15. void Widget::on_pushButton_3_clicked() //重新開始  
  16. {  
  17. this->timer->stop();  
  18. this->on_pushButton_clicked();  

3.在main.cpp中添加語句,讓程序中可以使用中文。

添加#include 的頭文件包含。

在main()函數(shù)里添加QTextCodec::setCodecForTr(QTextCodec::codecForLocale());語句。

4.程序運行效果如下。

用Qt寫俄羅斯方塊游戲

高級功能的應用

(一)改變顏色和給方塊添加圖片。

1.添加“更改顏色”按鈕和“方塊貼圖”按鈕。如下圖。

用Qt寫俄羅斯方塊游戲

2.更改其單擊事件槽函數(shù)。如下。

  1. void Widget::on_pushButton_4_clicked() //改變顏色  
  2. {  
  3. this->gameArea->setGameAreaColor(QColor(255,255,0,qrand()%255));  
  4. //更改游戲區(qū)域背景顏色  
  5. this->gameArea->setBoxBrushColor(QColor(0,255,0,qrand()%255));  
  6. //更改小方塊背景顏色  
  7. this->gameArea->setBoxPenColor(QColor(0,0,0,qrand()%255));  
  8. //更改小方塊邊框顏色  
  9. this->gameArea->draw_gameArea();  
  10. //更新游戲區(qū)域  
  11. this->gameArea->setFocus();  
  12. http://fund.eastmoney.com/dwjz.html  
  13. }  
  14. void Widget::on_pushButton_5_clicked() //方塊貼圖  
  15. {  
  16. this->gameArea->set_draw_box_picture(true);  
  17. //確認使用方塊背景圖片  
  18. this->gameArea->setBoxPicture("box.gif");  
  19. //添加方塊背景圖片  
  20. this->gameArea->draw_gameArea();  
  21. //更新顯示區(qū)域  
  22. this->gameArea->setFocus();  

3.運行效果如下。

點擊“改變背景”按鈕后,游戲區(qū)域背景,方塊的填充顏色和邊框顏色都改變了。

用Qt寫俄羅斯方塊游戲

點擊“方塊貼圖”按鈕。注意,只有方塊顏色的透明度不是255時,才能看見貼圖。所以,如果開始游戲后直接按“方塊貼圖”按鈕,是不能顯示出背景圖片的,我們需要先改變顏色。

用Qt寫俄羅斯方塊游戲 

#p#

(二)是否顯示背景網(wǎng)格和下一個要出現(xiàn)的方塊。

1.添加“網(wǎng)格顯示”按鈕和“方塊提示”按鈕。并將它們屬性中的checkable選中。界面如下。

用Qt寫俄羅斯方塊游戲

2.修改它們的單擊事件槽函數(shù)。

  1. void Widget::on_pushButton_6_clicked() //網(wǎng)格顯示  
  2. {  
  3. if(this->ui->pushButton_6->isChecked())  
  4. {  
  5. this->gameArea->setDrawGrid(false);  
  6. }  
  7. else  
  8. {  
  9. this->gameArea->setDrawGrid(true);  
  10. }  
  11. this->gameArea->draw_gameArea();  
  12. this->gameArea->setFocus();  
  13. }  
  14. void Widget::on_pushButton_7_clicked() //方塊提示  
  15. {  
  16. if(this->ui->pushButton_7->isChecked())  
  17. {  
  18. this->gameArea->setDrawNextItem(false);  
  19. }  
  20. else  
  21. {  
  22. this->gameArea->setDrawNextItem(true);  
  23. }  
  24. this->gameArea->draw_gameArea();  
  25. this->gameArea->setFocus();  

3.運行效果如下。

用Qt寫俄羅斯方塊游戲 

用Qt寫俄羅斯方塊游戲

(三)添加方塊移動的聲音。

1.添加“打開聲音”按鈕,并將其屬性中的checkable選中。

用Qt寫俄羅斯方塊游戲

2.修改其單擊事件槽函數(shù)。

  1. void Widget::on_pushButton_8_clicked() //聲音開關  
  2. {  
  3. if(this->ui->pushButton_8->isChecked())  
  4. {  
  5. this->gameArea->setPlaySound_itemChange("changeItem.wav",true);  
  6. this->gameArea->setPlaySound_moveDown("moveDown.wav",true);  
  7. this->gameArea->setPlaySound_moveLeft("moveLeft.wav",true);  
  8. this->gameArea->setPlaySound_moveRight("moveLeft.wav",true);  
  9. this->ui->pushButton_8->setText(tr("關閉聲音"));  
  10. }  
  11. else  
  12. {  
  13. this->gameArea->setPlaySound(false); //關閉音樂  
  14. this->ui->pushButton_8->setText(tr("打開聲音"));  
  15. }  
  16. this->gameArea->setFocus();  

3.我們把需要的聲音文件放到工程文件夾下的debug文件夾下。注意:只能是wav格式的。然后運行程序,測試一下效果。

(四)添加向下按鍵移動步數(shù)設置。

1.添加“是否墜落”按鈕,并將其屬性中的checkable選中。

用Qt寫俄羅斯方塊游戲

2.更改其單擊事件槽函數(shù)。

  1. void Widget::on_pushButton_9_clicked() //是否墜落  
  2. {  
  3. if(this->ui->pushButton_9->isChecked())  
  4. {  
  5. this->gameArea->setKey_Down_Move_oneStep(true);  
  6. //按一下向下方向鍵,下移一步  
  7. }  
  8. else  
  9. {  
  10. this->gameArea->setKey_Down_Move_oneStep(false);  
  11. //按一下向下方向鍵,移動到底  
  12. }  
  13. this->gameArea->setFocus();  

3.運行程序,測試一下效果。

 

(五)自己添加方塊。

1.添加“添加方塊”按鈕。

用Qt寫俄羅斯方塊游戲

2.修改其單擊事件槽函數(shù)。

  1. void Widget::on_pushButton_10_clicked() //添加方塊  
  2. {  
  3. this->gameArea->init_Game();  
  4. //清空游戲區(qū)域  
  5. this->gameArea->setbox(10,4);  
  6. this->gameArea->setbox(10,5);  
  7. this->gameArea->setbox(10,6);  
  8. //在第10行第4,5,6列添加三個方塊  
  9. this->gameArea->gameStart();  
  10. //重新開始游戲  
  11. this->gameArea->draw_gameArea();  
  12. this->gameArea->setFocus();  

3.運行程序,效果如下。

用Qt寫俄羅斯方塊游戲

(六)設置旋轉游戲區(qū)。

1.添加“旋轉游戲”按鈕。

用Qt寫俄羅斯方塊游戲

2.修改其單擊事件槽函數(shù)。

  1. void Widget::on_pushButton_11_clicked() //旋轉游戲  
  2. {  
  3. this->gameArea->setRotate(true);  
  4. //開啟旋轉  
  5. this->gameArea->setGameAreaPixOrigin(100,200);  
  6. //設置游戲區(qū)域新的坐標原點  
  7. this->gameArea->setGameAreaPix(-100,-200);  
  8. //設置游戲區(qū)域的位置  
  9. this->gameArea->setRotateAngle(qrand()%360);  
  10. //旋轉度數(shù)  
  11. this->gameArea->draw_gameArea();  
  12. this->gameArea->setFocus();  

3.運行程序,效果如下。

用Qt寫俄羅斯方塊游戲

第三部分:游戲分析

(一)可被外部調用的功能函數(shù)的原型。

 

  1. GameArea(QWidget *parent = 0); //不帶定時器的構造函數(shù)  
  2. GameArea(int speed,QWidget *parent = 0); //帶定時器的構造函數(shù)  
  3. //以下是核心功能控制函數(shù)  
  4. void init_gameArea(int X,int Y,int frame_width,int frame_height,int width,int height,int boxStep,int start_x,int start_y);  
  5. void moveOneStep();  
  6. bool isMoveEnd();  
  7. bool isGame_Over();  
  8. void init_Game();  
  9. void gameStart();  
  10. void nextItem();  
  11. int getFullRowNum();  
  12. void do_MoveNext();  
  13. void draw_gameArea();  
  14. //以下是設置顏色函數(shù)  
  15. void setGameAreaColor(QColor color=Qt::white);  
  16. void setBoxBrushColor(QColor color=Qt::green);  
  17. void setBoxPenColor(QColor color=Qt::black);  
  18. void set_draw_box_picture(bool Bool=false);  
  19. void setBoxPicture(QString fileName);  
  20. //以下是設置聲音函數(shù)  
  21. void setPlaySound_moveLeft(QString fileName,bool Bool=false);  
  22. void setPlaySound_moveRight(QString fileName,bool Bool=false);  
  23. void setPlaySound_moveDown(QString fileName,bool Bool=false);  
  24. void setPlaySound_itemChange(QString fileName,bool Bool=false);  
  25. void setPlaySound(bool Bool=false);  
  26. //以下是設置游戲區(qū)域旋轉函數(shù)  
  27. void setRotate(bool Bool=false);  
  28. void setGameAreaPixOrigin(int x,int y);  
  29. void setGameAreaPix(int x,int y);  
  30. void setRotateAngle(int angle);  
  31. //以下是其他功能函數(shù)  
  32. void setKey_Down_Move_oneStep(bool Bool = false);  
  33. void setDrawGrid(bool Bool = true);  
  34. void setDrawNextItem(bool Bool =true);  
  35. void setbox(int row,int col); 

(二)游戲流程分析。

這里共有四個文件myitem.cpp ,myitem.h, gamearea.cpp, gamearea.h

其中myitem.cpp, myitem.h是方塊類的定義文件,它用來提供隨機產(chǎn)生的方塊。

gamearea.cpp, gamearea.h是游戲區(qū)域類的定義文件,它實現(xiàn)了游戲的所有功能。

為了幫助大家更好的理解源碼,我們這里描述一下游戲的實現(xiàn)流程。

用Qt寫俄羅斯方塊游戲 

 小結:似乎這個游戲是見怪不怪的了,不管怎么樣,還是希望這篇文章能夠幫到你?。?!

【編輯推薦】

HTML 5游戲的重大變革

深度解析嵌入式QT開發(fā)環(huán)境搭建

Mominis:闡述手機游戲開發(fā)五大建議

淺談自動化測試工具 QTP腳本的重用

游戲工作室高管:盜版也不完全是壞事

責任編輯:zhaolei 來源: 互聯(lián)網(wǎng)
相關推薦

2014-10-08 10:04:14

代碼解釋俄羅斯方塊

2015-01-22 15:36:46

游戲源碼

2021-01-12 12:16:55

鴻蒙HarmonyOS游戲

2014-05-26 10:07:18

Javascript俄羅斯方塊

2020-05-19 17:26:21

Python俄羅斯方塊游戲開發(fā)

2015-04-28 09:21:28

JSJS俄羅斯方塊游戲帝國

2020-02-27 13:43:14

Emacs俄羅斯方塊應用

2021-12-29 11:56:16

Linux俄羅斯方塊

2023-09-25 12:35:27

Python

2023-09-26 08:51:29

PygamePython語言

2020-12-11 12:45:04

鴻蒙Hi3861游戲

2016-06-13 10:21:49

二維碼條形碼二進制

2020-12-17 10:02:16

鴻蒙Hi3861開發(fā)板

2011-11-17 16:14:25

Jscex

2014-06-09 12:47:35

俄羅斯方塊

2009-06-08 09:59:24

谷歌俄羅斯方塊版權

2012-11-05 10:50:50

程序員萬圣節(jié)俄羅斯方塊

2023-10-17 10:20:53

VueReact

2022-11-29 16:35:02

Tetris鴻蒙

2011-07-06 10:12:26

Objective-CCSSJavaScript
點贊
收藏

51CTO技術棧公眾號