c语言 qt 写小游戏,Qt实现Flappy Bird游戏

本文介绍了使用Qt框架实现Flappy Bird小游戏的过程,包括主界面控制、小鸟组件、游戏开始按钮、管道组件和地面的绘制。通过代码示例展示了游戏的关键功能,如小鸟飞行、碰撞检测和游戏结束状态。
摘要由CSDN通过智能技术生成

简述

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

效果图

edf0bb16ccde1e6209456a865ea5b762.gif

e02de05ef1239e52453d2f3df2c037df.gif

5826dc77eed67caca218226044be03f0.gif

代码

主界面控制

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("%1").arg(letterData[i].pix);

QGraphicsTextItem *letter = new QGraphicsTextItem();

letter->setHtml(htmlText);

letter->

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值