Qt实现Flappy Bird游戏

简述

最近浏览网站的时候,忘记在哪里看的这个FlappyBird了,这个小游戏在之前小火了一段时间。今天用QT简单的实现了一把,然后在网上找了一些相关的切图,便进行了制作。难度不是很大,只是通过写这篇博客,能有点启发以及大家共同学习。

效果图

代码

主界面控制

MainWindow::MainWindow(QWidget *parent)
 : BasicWindow(parent)
 , m_startGame(false)
{
 ui.setupUi(this);
 setAttribute(Qt::WA_TranslucentBackground);
 initControl();
}

void MainWindow::initControl()
{
 loadStyleSheet("MainWindow");
 m_scene = new MainGraphicsScene(this, rect());
 QGraphicsView* view = new QGraphicsView(m_scene, this);

 view->setScene(m_scene);
 view->setStyleSheet("border:none; background:transparent;");
 view->setCacheMode(QGraphicsView::CacheBackground);
 startWelcome();
}

void MainWindow::startWelcome()
{
 //道路
 GraphicsRoadItem *roadItem = new GraphicsRoadItem(m_scene);

 //小鸟
 m_bird = new FlappyBird(m_scene);

 //管道
 GraphicsPipeitem *pipeItem = new GraphicsPipeitem(m_scene);

 //游戏状态检测,开启定时器,50ms检测一次
 m_checkGameStatus = new QTimer(this);
 connect(m_checkGameStatus, SIGNAL(timeout()), this, SLOT(onCheckGameStatus()));

 //flappybird字母
 static const int nLetters = 10;
 static struct {
  char const *pix;
  qreal initX, initY;
  qreal destX, destY;
 } letterData[nLetters] = {
  { "F", -1000, -1000, 150, 100 },
  { "L", -800, -1000, 200, 100 },
  { "A", -600, -1000, 250, 100 },
  { "P", -400, -1000, 300, 100 },
  { "P", 1000, 2000, 350, 100 },
  { "Y", 800, 2000, 400, 100 },
  { "B", 600, 2000, 260, 160 },
  { "I", 400, 2000, 310, 160 },
  { "R", 200, 2000, 360, 160 },
  { "D", 0, 2000, 410, 160 } };

 QSequentialAnimationGroup * lettersGroupMoving = new QSequentialAnimationGroup(this);
 m_lettersGroupFading = new QParallelAnimationGroup(this);

 for (int i = 0; i < nLetters; ++i) {
  QString& htmlText = QString("<span style=\"font-family:'Berlin Sans FB';font-size:48px;font-weight:600;color:#194819;\">%1</span>").arg(letterData[i].pix);
  QGraphicsTextItem *letter = new QGraphicsTextItem();
  letter->setHtml(htmlText);
  letter->setPos(letterData[i].initX, letterData[i].initY);

  QPropertyAnimation *moveAnim = new QPropertyAnimation(letter, "pos", lettersGroupMoving);
  moveAnim->setEndValue(QPointF(letterData[i].destX, letterData[i].destY));
  moveAnim->setDuration(200);
  moveAnim->setEasingCurve(QEasingCurve::OutElastic);
  lettersGroupMoving->addPause(50);

  QPropertyAnimation *fadeAnim = new QPropertyAnimation(letter, "opacity", m_lettersGroupFading);
  fadeAnim->setDuration(1000);
  fadeAnim->setEndValue(0);
  fadeAnim->setEasingCurve(QEasingCurve::OutQuad);

  m_scene->addItem(letter);
 }
 lettersGroupMoving->start(QAbstractAnimation::DeleteWhenStopped);

 //游戏开始按钮
 QPixmap&& pix = QPixmap(":/FlappyBird/Resources/texture/startButton.png").scaled(QSize(160, 48), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
 GraphicsButtonItem* btnItem = new GraphicsButtonItem(pix, m_scene);
 btnItem->setPos(QPointF(220, 340));
 QPropertyAnimation *fadeAnim = new QPropertyAnimation(btnItem, "opacity", m_lettersGroupFading);
 fadeAnim->setDuration(600);
 fadeAnim->setEndValue(0);
 fadeAnim->setEasingCurve(QEasingCurve::OutQuad);
 connect(btnItem, SIGNAL(clicked()), this, SLOT(onStartBtnClicked()));
 connect(fadeAnim, &QPropertyAnimation::finished, [this](){
  m_startGame = true;
  m_checkGameStatus->start(50);
  m_bird->flyLandfallAnimation();
 });
}

void MainWindow::onCheckGameStatus()
{
 //检测小鸟是否与地面和管道发生碰撞
 if (m_bird->checkIsCollided())
 {
  GameOver();
 }
}

void MainWindow::GameOver()
{
 static const int nLetters = 8;
 static struct {
  char const *pix;
  qreal initX, initY;
  qreal destX, destY;
 } letterData[nLetters] = {
  { "G", -1000, -1000, 150, 100 },
  { "A", -800, -1000, 200, 100 },
  { "M", -600, -1000, 250, 100 },
  { "E", -400, -1000, 300, 100 },
  { "O", 600, 2000, 260, 160 },
  { "V", 400, 2000, 310, 160 },
  { "E", 200, 2000, 360, 160 },
  { "R", 0, 2000, 410, 160 } };

 QParallelAnimationGroup * lettersGroupMoving = new QParallelAnimationGroup(this);

 for (int i = 0; i < nLetters; ++i) {
  QString& htmlText = QString("<span style=\"font-family:'Berlin Sans FB';font-size:48px;font-weight:600;color:#194819;\">%1</span>").arg(letterData[i].pix);
  QGraphicsTextItem *letter = new QGraphicsTextItem();
  letter->setHtml(htmlText);
  letter->setPos(letterData[i].initX, letterData[i].initY);

  QPropertyAnimation *moveAnim = new QPropertyAnimation(letter, "pos", lettersGroupMoving);
  moveAnim->setEndValue(QPointF(letterData[i].destX, letterData[i].destY));
  moveAnim->setDuration(200);
  moveAnim->setEasingCurve(QEasingCurve::OutElastic);
  m_scene->addItem(letter);
 }
 lettersGroupMoving->start(QAbstractAnimation::DeleteWhenStopped);

 m_scene->removeItem(m_bird);
}

void MainWindow::onStartBtnClicked()
{
 m_lettersGroupFading->start(QAbstractAnimation::DeleteWhenStopped);
}

void MainWindow::keyPressEvent(QKeyEvent *event)
{
 if (m_startGame)
 {
  m_bird->keyPressEvent(event);
 }
}

小鸟组件绘制

FlappyBird::FlappyBird(QGraphicsScene *scene): m_curflyStatus(0)
   , m_IsLandFall(true)
   , m_IsRaise(true)
{
 scene->addItem(this);
 m_scene = scene;
 m_birdRefreashTime = new QTimer(this);
 connect(m_birdRefreashTime, SIGNAL(timeout()), this, SLOT(onRefreashBird()));
 m_birdRefreashTime->start(12);

 m_flyAnimation = new QPropertyAnimation(this, "pos");
}

void FlappyBird::onRefreashBird()
{
 update();
}

QRectF FlappyBird::boundingRect() const
{
 return QRectF(60, FLY_BIRD_SIZE * 5 , FLY_BIRD_SIZE, FLY_BIRD_SIZE);
}

void FlappyBird::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
 painter->save();
 if (m_curflyStatus < 10)
 {
  m_curflyStatus++;
  painter->drawImage(boundingRect(), QImage(":/FlappyBird/Resources/texture/bird1.png"));
 }
 else if (m_curflyStatus < 20)
 {
  m_curflyStatus++;
  painter->drawImage(boundingRect(), QImage(":/FlappyBird/Resources/texture/bird2.png"));
 }
 else if ( m_curflyStatus < 30)
 {
  m_curflyStatus++;
  painter->drawImage(boundingRect(), QImage(":/FlappyBird/Resources/texture/bird3.png"));
 }
 else
 {
  m_curflyStatus = 0;
 }
 painter->restore();
}

void FlappyBird::flyRaiseAnimation()
{
 if (m_IsRaise)
 {
  m_IsLandFall = false;
  m_IsRaise = false;
  m_flyAnimation->stop();

  if (pos().y() > -180)
  {
   m_flyAnimation->setDuration(300);
   m_flyAnimation->setEndValue(QPoint(pos().x(), pos().y() - FLY_BIRD_SIZE));
  }
  else
  {
   m_flyAnimation->setDuration(300);
   m_flyAnimation->setEndValue(pos());
  }
  m_flyAnimation->setEasingCurve(QEasingCurve::OutQuad);
  m_flyAnimation->start();
  connect(m_flyAnimation, SIGNAL(finished()), this, SLOT(onFlyRaiseAnimationFinished()));
 }
}

void FlappyBird::onFlyRaiseAnimationFinished()
{
 m_flyAnimation->disconnect(SIGNAL(finished()));
 m_IsRaise = true;
 m_IsLandFall = true;
 flyLandfallAnimation();
}

void FlappyBird::flyLandfallAnimation()
{
 if (m_birdRefreashTime->isActive())
 {
  m_birdRefreashTime->stop();
 }

 if (m_IsLandFall)
 {
  m_flyAnimation->stop();
  int fallHeight = m_scene->height() - pos().y();
  int time = 1000 * fallHeight / m_scene->height();
  m_flyAnimation->setDuration(time);
  m_flyAnimation->setEndValue(QPoint(pos().x(), pos().y() + fallHeight));
  m_flyAnimation->setEasingCurve(QEasingCurve::InQuad);
  m_flyAnimation->start();
  m_IsLandFall = false;
 }
}

bool FlappyBird::checkIsCollided()
{
 if (!collidingItems().isEmpty())
  return true;
 else
  return false;
}

void FlappyBird::keyPressEvent(QKeyEvent *event)
{
 if (event->key() == Qt::Key_Space)
 {
  flyRaiseAnimation();
 }
}

游戏开始按钮

GraphicsButtonItem::GraphicsButtonItem(const QPixmap &pixmap, QGraphicsScene *scene) : pix(pixmap)
{
 scene->addItem(this);
 setCursor(QCursor(Qt::PointingHandCursor));
}

void GraphicsButtonItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
 if (event->button() == Qt::LeftButton)
 {
  emit clicked();
 }
 __super::mousePressEvent(event);
}

QSizeF GraphicsButtonItem::size() const
{
 return pix.size();
}

QRectF GraphicsButtonItem::boundingRect() const
{
 return QRectF(QPointF(0, 0), pix.size());
}

void GraphicsButtonItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
 painter->drawPixmap(0, 0, pix);
}

管道组件绘制

#define PIPE_WIDTH 60

GraphicsPipeitem::GraphicsPipeitem(QGraphicsScene *scene)
{
 m_scene = scene;
 m_scene->addItem(this);

 createPipeHeight();
 startMove();
}
void GraphicsPipeitem::createPipeHeight()
{
 m_upPipeHeight = qrand() % 100 + 80;
 m_downPipeHeight = m_scene->height() - m_upPipeHeight - 178;
}

QRectF GraphicsPipeitem::boundingRect() const
{
 return QRectF(m_scene->width(), 0, PIPE_WIDTH, m_scene->height());
}

QPainterPath GraphicsPipeitem::shape() const
{
 QPainterPath path;
 path.addRect(QRectF(m_scene->width(), 0, PIPE_WIDTH, m_upPipeHeight));
 path.addRect(QRectF(m_scene->width(), m_upPipeHeight + 140, PIPE_WIDTH, m_downPipeHeight));
 return path;
}

void GraphicsPipeitem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
 painter->save();
 painter->drawImage(QRectF(m_scene->width(), 0, PIPE_WIDTH, m_upPipeHeight), QImage(":/FlappyBird/Resources/texture/tubeup.png").scaled(PIPE_WIDTH, m_upPipeHeight));
 painter->drawImage(QRectF(m_scene->width(), m_upPipeHeight + 140, PIPE_WIDTH, m_downPipeHeight), QImage(":/FlappyBird/Resources/texture/tubedown.png").scaled(PIPE_WIDTH, m_downPipeHeight));
 painter->restore();
}

void GraphicsPipeitem::startMove()
{
 QPropertyAnimation* moveAnimation = new QPropertyAnimation(this, "pos");
 moveAnimation->setLoopCount(-1);
 moveAnimation->setDuration(3000);
 moveAnimation->setStartValue(QPoint(0, pos().y()));
 moveAnimation->setEndValue(QPoint(-1 * m_scene->width() - PIPE_WIDTH, pos().y()));
 moveAnimation->start();
 connect(moveAnimation, &QPropertyAnimation::currentLoopChanged, [this](int loopCount){
  createPipeHeight();
 });
}

地面绘制

#define ROAD_ITEM_HEIGHT 38

GraphicsRoadItem::GraphicsRoadItem(QGraphicsScene *scene) :m_scene(scene)
{
 scene->addItem(this);
 startMove();
}

QRectF GraphicsRoadItem::boundingRect() const
{
 return QRectF(0, m_scene->height() - ROAD_ITEM_HEIGHT, m_scene->width() * 2, ROAD_ITEM_HEIGHT);
}

void GraphicsRoadItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
 painter->drawImage(QRectF(0, m_scene->height() - ROAD_ITEM_HEIGHT, m_scene->width(), ROAD_ITEM_HEIGHT), QImage(":/FlappyBird/Resources/texture/road.png"));
 painter->drawImage(QRectF(m_scene->width(), m_scene->height() - ROAD_ITEM_HEIGHT, m_scene->width(), ROAD_ITEM_HEIGHT), QImage(":/FlappyBird/Resources/texture/road.png"));
}

void GraphicsRoadItem::startMove()
{
 QPropertyAnimation* moveAnimation = new QPropertyAnimation(this, "pos");
 moveAnimation->setLoopCount(-1);
 moveAnimation->setDuration(6000);
 moveAnimation->setStartValue(QPoint(0, pos().y()));
 moveAnimation->setEndValue(QPoint(0 - m_scene->width(), pos().y()));
 moveAnimation->setEasingCurve(QEasingCurve::Linear);
 moveAnimation->start();
}

结尾

全部代码,都在上面了,希望对大伙有所点启发和帮助,只为记录,只为分享! 愿所写能对你有所帮助。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • java实现flappy Bird小游戏

    本文实例为大家分享了java实现flappy Bird游戏的具体代码,供大家参考,具体内容如下 整个游戏由3个类构成.Bird类,Pipe类,stage类 第一步:首先写一个Bird类 //鸟类 public class Bird { private int flyHeight;//飞行高度 private int xpos;//距离y轴(窗口左边缘)的位置, public static int Up=1;//向上飞 public static int Down=-1;//向下飞 public

  • C语言实现flappy bird游戏

    本文实例为大家分享了C语言实现flappy bird的具体代码,供大家参考,具体内容如下 #include<stdio.h> #include<conio.h> #include<windows.h> //定义全局变量 int high,width; //边界 int bird_x,bird_y; //小鸟坐标 int bar_y; //挡板坐标 int bar_xTop,bar_xDown; //挡板开口上下坐标 int score; //得分 void HideCu

  • C语言简易版flappy bird小游戏

    假期在家无聊,想随便码点东西,故有此简陋的小游戏诞生.觉着可能对初学C语言的小伙伴练习有点帮助,故写此博客.游戏界面如下: 首先,先画出整个小游戏实现的流程图,如下: 思路很简单,整个游戏界面是由一个大的char类型数组构成,更新数组的值然后不停的打印出来就形成了动态效果. 由上图看,大循环是保证游戏一直不断的进行下去,小循环是让小鸟的速度大于游戏界面里背景(由#构成的柱子)的速度(小鸟动四下柱子才动一下). 下面是具体代码(水平有限大家多多见谅,但是效果还是有的!) Bird.c文件 #inc

  • java实现Flappy Bird游戏源代码

    本文实例为大家分享了java实现Flappy Bird游戏的具体代码,供大家参考,具体内容如下 /* 2017/7/23 */ import java.awt.Graphics; //import java.util.Timer; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseListener; import java.awt.event.Mo

  • Java实现Flappy Bird游戏源码

    本文实例为大家分享了Java实现Flappy Bird游戏的具体代码,供大家参考,具体内容如下 1.首先在mainActivity.xml中放置一个View,ID为viewDraw 2.开始编程,程序中自定义一个View类的子类,与viewDraw关联,程序除了放置如一个view控件,没有其他控件,程序上面的所有图片都是通过控制canvas画图实现 3.游戏是依据flappybird的游戏过程重新写的算法,功能与原版游戏相似,可能有些地方不足,完全是学习练习编程而已,做的不好见谅 4.原图片大小

  • C语言实现Flappy Bird小游戏

    本文实例为大家分享了C语言实现Flappy Bird小游戏的具体代码,供大家参考,具体内容如下 #include<stdio.h> #include<stdlib.h> #include<conio.h> #include<time.h> #include<Windows.h> /********函数变量声明********/ #define PR_Box printf("■") #define PR_Gold printf(

  • python实现flappy bird游戏

    flappy bird最近火遍大江南北,教你用python写游戏的第一课就向它开刀了. 这个课程的基础是假定你有比较不错的编程功底,对python有一点点的基础. 一.准备工作 1.用python写游戏需要什么呢? 1)当然是python本身了,我用的是python2.7,不同版本大同小异. 2)pygame,这个非常重要,所有的核心都是基于这个lib的. 2.分析游戏 flappy bird这个游戏很简单,大致可以分为4个部分: 1)背景.背景分为两个,一个是bg,一个是land.bg就是那张

  • C++版本简易Flappy bird

    大一,上学期学完了C,写了几个控制台游戏 这学期自学C++,由于学校课程第七周才有C++ 边学边写了这个小游戏,SDL 图形库完成的图形绘画 时间匆忙,BUG也有,代码效率比较低 和原作品还是很大的差别, 源代码在附件游戏文件夹中 演示图 #include <stdlib.h> #include<windows.h> #include <time.h> #include<conio.h> #include <iostream> #include

  • 纯JavaScript 实现flappy bird小游戏实例代码

    前言: <flappy bird>是一款由来自越南的独立游戏开发者Dong Nguyen所开发的作品,游戏于2013年5月24日上线,并在2014年2月突然暴红.2014年2月,<Flappy Bird>被开发者本人从苹果及谷歌应用商店撤下.2014年8月份正式回归APP STORE,正式加入Flappy迷们期待已久的多人对战模式.游戏中玩家必须控制一只小鸟,跨越由各种不同长度水管所组成的障碍. 正文: 接下来就是一步一步来实现它 步骤1:页面布局,这儿就不多说了,页面内容如下:

  • Unity实现Flappy Bird游戏开发实战

    本文实例为大家分享了Unity实现Flappy Bird游戏的具体代码,供大家参考,具体内容如下 参考:腾讯课程(零基础制作像素鸟) 环境:Unity2017.2.0f3 主界面(Main)的制作 没有什么技巧性 注意点: 1.写好Button的点击效果,并在UI上添加效果 2.切换界面的实现不需要通过load,直接设置SetActive()true or false 来的更快更效率 // 比如:当点击打开解释说明的按钮时候 public void clickOpenExplainScene()

随机推荐