使用Qt开发俄罗斯方块游戏

移动开发
本文讲解的是使用Qt开发俄罗斯方块游戏,不管是是手机,电视,电脑等一些电子类的产品,似乎都有俄罗斯方块这么一块游戏,那么作为编程者是否对他的研究更深刻呢?那么看本文章的讲述吧!

使用Qt开发俄罗斯方块游戏,可能大家都比较感兴趣吧。那么就快看下面的详细讲解吧!

其实在Qt Creator中已经有了俄罗斯方块的例子,大家可以在帮助中搜索Tetrix进行查看。其内容如下:

用Qt写俄罗斯方块游戏

但是对于初学者,这个例子并不是那么容易就能看懂。所以我结合这个例子和网上的一些资料,写了一个比较简单的方块游戏类。希望能帮助初学者更好的理解这个例子和写出自己的方块游戏。

我这里已经对所有的功能函数进行了整理,最简单的,你只需要定义一个对象就能让这个游戏运行起来。

写最简单的游戏

1. 新建Empty Qt4 Project,我们这里命名为myTetrix 。

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

3. 将myitem.cpp, myitem.h, gamearea.cpp, gamearea.h四个文件复制到工程文件夹下。

4. 将上面四个文件加入到工程中。

5. 将main.cpp的内容更改如下:

  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.可以看到,用这个类建立方块游戏是这么的简单。我们只是在主函数里新建了一个该类的对象,其中的参数为方块下移的速度,单位是毫秒,上面的500即0.5秒。

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

用Qt写俄罗斯方块游戏

在工程文件夹上点右键,弹出的菜单中Add New表示添加新文件,Add Existing Files表示添加工程文件夹中已有的文件。

#p#

功能展示

要想实现更强大的功能,我们就需要应用控制窗体,而让这个游戏区域只作为它的一个部件。为了更好的控制游戏,我们也需要自己建立定时器,而不再应用该类自带的定时器了。

核心功能:

(一)建立工程。

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的构造函数里添加语句。

  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);  

这里重新设定了主窗口大小,并在主窗口上新建了一个游戏区域对象。

5.这时运行程序效果如下。

用Qt写俄罗斯方块游戏

可以看到,因为使用了另一个构造函数,没有使用该类自带的定时器,所以只是显示了游戏区域,游戏并没有运行。

(二)添加定时器和开始按钮,让游戏可以运行。

1.在widget.h里的private中添加定时器对象和分数变量的声明。

  1. QTimer *timer;  
  2. int score; 

在public中添加显示分数函数的声明。

  1. void doScore(int); 

添加槽函数的声明。

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

2.在widget.cpp文件中的构造函数里添加下面的语句:

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

定义了定时器并进行了信号和槽函数的关联,初始化分数为0;

3.然后在下面定义两个函数。

  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; //清空分数  
  12. this->gameArea->init_Game(); //重新开始游戏  
  13. this->gameArea->gameStart();  
  14. this->timer->start(500);  
  15. }  
  16. else //如果是移动到底了  
  17. this->gameArea->nextItem(); //出现下一个图形  
  18. int num = this->gameArea->getFullRowNum(); //获得已满的行数  
  19. this->doScore(num); //显示分数  
  20. this->gameArea->gameStart(); //继续游戏  
  21. }  
  22. }  
  23. else //如果没有到底  
  24. {  
  25. this->gameArea->do_MoveNext(); //显示方块下移一步后的界面  
  26. }  
  27. }  
  28. void Widget::doScore(int num) //显示分数  
  29. {  
  30. score += num*100;  
  31. this->ui->label_2->setText(tr("%1").arg(score));  

4.在设计器中向主窗口上添加两个标签label和label_2,其中label写上“你的分数是:”,label_2写上“0”;然后再添加一个开始按钮。添加完后效果如下。

用Qt写俄罗斯方块游戏

5.然后右击“开始游戏”按钮,选择其单击事件的槽函数。更改如下。

  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(); //让游戏区域获得焦点,这样才能响应键盘  

6.现在游戏已经可以正常进行了。运行效果如下。

用Qt写俄罗斯方块游戏 

用Qt写俄罗斯方块游戏 

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

1.在主窗口上添加“暂停游戏”和“重新开始”两个按钮。在“暂停游戏”按钮的属性中将checkable选中。如下图所示。

用Qt写俄罗斯方块游戏 

用Qt写俄罗斯方块游戏

2.分别进入两个按钮的单击事件槽函数。修改如下。

  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()函数里添加QTextCodec::setCodecForTr(QTextCodec::codecForLocale());语句。

4.程序运行效果如下。

用Qt写俄罗斯方块游戏

高级功能的应用

(一)改变颜色和给方块添加图片。

1.添加“更改颜色”按钮和“方块贴图”按钮。如下图。

用Qt写俄罗斯方块游戏

2.更改其单击事件槽函数。如下。

  1. void Widget::on_pushButton_4_clicked() //改变颜色  
  2. {  
  3. this->gameArea->setGameAreaColor(QColor(255,255,0,qrand()%255));  
  4. //更改游戏区域背景颜色  
  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. //更新游戏区域  
  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. //更新显示区域  
  22. this->gameArea->setFocus();  

3.运行效果如下。

点击“改变背景”按钮后,游戏区域背景,方块的填充颜色和边框颜色都改变了。

用Qt写俄罗斯方块游戏

点击“方块贴图”按钮。注意,只有方块颜色的透明度不是255时,才能看见贴图。所以,如果开始游戏后直接按“方块贴图”按钮,是不能显示出背景图片的,我们需要先改变颜色。

用Qt写俄罗斯方块游戏 

#p#

(二)是否显示背景网格和下一个要出现的方块。

1.添加“网格显示”按钮和“方块提示”按钮。并将它们属性中的checkable选中。界面如下。

用Qt写俄罗斯方块游戏

2.修改它们的单击事件槽函数。

  1. void Widget::on_pushButton_6_clicked() //网格显示  
  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.修改其单击事件槽函数。

  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格式的。然后运行程序,测试一下效果。

(四)添加向下按键移动步数设置。

1.添加“是否坠落”按钮,并将其属性中的checkable选中。

用Qt写俄罗斯方块游戏

2.更改其单击事件槽函数。

  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.修改其单击事件槽函数。

  1. void Widget::on_pushButton_10_clicked() //添加方块  
  2. {  
  3. this->gameArea->init_Game();  
  4. //清空游戏区域  
  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写俄罗斯方块游戏

(六)设置旋转游戏区。

1.添加“旋转游戏”按钮。

用Qt写俄罗斯方块游戏

2.修改其单击事件槽函数。

  1. void Widget::on_pushButton_11_clicked() //旋转游戏  
  2. {  
  3. this->gameArea->setRotate(true);  
  4. //开启旋转  
  5. this->gameArea->setGameAreaPixOrigin(100,200);  
  6. //设置游戏区域新的坐标原点  
  7. this->gameArea->setGameAreaPix(-100,-200);  
  8. //设置游戏区域的位置  
  9. this->gameArea->setRotateAngle(qrand()%360);  
  10. //旋转度数  
  11. this->gameArea->draw_gameArea();  
  12. this->gameArea->setFocus();  

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

用Qt写俄罗斯方块游戏

第三部分:游戏分析

(一)可被外部调用的功能函数的原型。

 

  1. GameArea(QWidget *parent = 0); //不带定时器的构造函数  
  2. GameArea(int speed,QWidget *parent = 0); //带定时器的构造函数  
  3. //以下是核心功能控制函数  
  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. //以下是设置颜色函数  
  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. //以下是设置声音函数  
  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. //以下是设置游戏区域旋转函数  
  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. //以下是其他功能函数  
  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是方块类的定义文件,它用来提供随机产生的方块。

gamearea.cpp, gamearea.h是游戏区域类的定义文件,它实现了游戏的所有功能。

为了帮助大家更好的理解源码,我们这里描述一下游戏的实现流程。

用Qt写俄罗斯方块游戏 

 小结:似乎这个游戏是见怪不怪的了,不管怎么样,还是希望这篇文章能够帮到你!!!

【编辑推荐】

HTML 5游戏的重大变革

深度解析嵌入式QT开发环境搭建

Mominis:阐述手机游戏开发五大建议

浅谈自动化测试工具 QTP脚本的重用

游戏工作室高管:盗版也不完全是坏事

责任编辑:zhaolei 来源: 互联网
相关推荐

2015-01-22 15:36:46

游戏源码

2014-10-08 10:04:14

代码解释俄罗斯方块

2021-01-12 12:16:55

鸿蒙HarmonyOS游戏

2014-05-26 10:07:18

Javascript俄罗斯方块

2015-04-28 09:21:28

JSJS俄罗斯方块游戏帝国

2020-05-19 17:26:21

Python俄罗斯方块游戏开发

2020-02-27 13:43:14

Emacs俄罗斯方块应用

2021-12-29 11:56:16

Linux俄罗斯方块

2023-09-26 08:51:29

PygamePython语言

2023-09-25 12:35:27

Python

2020-12-11 12:45:04

鸿蒙Hi3861游戏

2016-06-13 10:21:49

二维码条形码二进制

2020-12-17 10:02:16

鸿蒙Hi3861开发板

2009-06-08 09:59:24

谷歌俄罗斯方块版权

2011-11-17 16:14:25

Jscex

2014-06-09 12:47:35

俄罗斯方块

2012-11-05 10:50:50

程序员万圣节俄罗斯方块

2023-10-17 10:20:53

VueReact

2022-11-29 16:35:02

Tetris鸿蒙

2011-07-06 10:12:26

Objective-CCSSJavaScript
点赞
收藏

51CTO技术栈公众号