qt 实现翻金币游戏

本文介绍了如何使用Qt框架开发一款翻金币游戏,包括游戏玩法、界面设计及代码实现。游戏共有20关,玩家通过翻转金币达到全金状态即获胜。文章详细讲解了开始界面按钮的弹跳效果、选关界面的布局以及游戏界面中金币的翻转逻辑,涉及自定义按钮、QPropertyAnimation动画和标志位控制等技术。
摘要由CSDN通过智能技术生成

游戏玩法介绍:

游戏设置关卡二十关,通过选关界面可以选择进入到对应的关卡中。 进入对应关卡之后,点击任意金币,可以使该硬币以及周边(上,下,左, 右)金边翻转。如果硬币都翻转为金币,则游戏胜利。

游戏界面设置:

开始界面:
在这里插入图片描述
开始场景中需要自定义一个按钮,点击开始按钮可以实现一个动图弹跳的显示效果。
同时弹出选关窗口,关闭当前窗口。
对于我这个新手来说,这一部分实现的难点是按钮的弹跳效果的实现。

对应代码:

#include "mainScene.h"
#include "mybutton.h"
#include <QPainter>
#include <QPixmap>
#include <QDebug>
#include <QTimer>
#include <QSound>

MainScene::MainScene(QWidget *parent)
    : QMainWindow(parent)
{
    this->setFixedSize(320, 588);
    menu_bar = this->menuBar();
    startMenu = new QMenu("开始");
    menu_bar->addMenu(startMenu);
    exitAction = new QAction("退出");
    startMenu->addAction(exitAction);
    this->setWindowIcon(QPixmap(":/res/image/Coin0001.png"));
    this->setWindowTitle("翻金币");

    MyButton * btn = new MyButton(":/res/image/MenuSceneStartButton.png");
    btn->setParent(this);
    btn->move(this->width()*0.5-btn->width()*0.5, this->height()*0.7);
    secondScene = new selectScene();
    connect(exitAction, &QAction::triggered, [=](){
        this->close();
    });

    connect(btn, &MyButton::clicked, [=](){
        QSound::play(":/res/voice/TapButtonSound.wav");
        btn->sink();
        btn->rise();
        QTimer::singleShot(300, this, [=](){
            this->hide();
            secondScene->show();
            secondScene->setGeometry(this->geometry());
        });
    });

    connect(secondScene, &selectScene::backSignanl, [=](){
        QSound::play(":/res/voice/TapButtonSound.wav");
        QTimer::singleShot(300, this, [=](){
            this->show();
            setGeometry(secondScene->geometry());
            secondScene->hide();
        });
    });
}

MainScene::~MainScene()
{
}

void MainScene::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/image/MenuSceneBg.png");
    painter.drawPixmap(0, 0, this->width(), this->height(), pix);

    pix.load(":/res/image/Title.png");
    pix = pix.scaled(pix.width()*0.5, pix.height()*0.5);
    painter.drawPixmap(30, 30, pix);

}

在初始界面需要构建一个自定义按钮,用于开始按钮,关卡按钮和返回按钮。
开始按钮需要增加弹跳效果,按钮类中使用QPropertyAnimation增加动态效果。
返回按钮则需要点击时切换图片状态,通过重写按钮按下,释放时间实现。

代码实现:

#include "mybutton.h"
#include <QPainter>
#include <QtDebug>
#include <QPropertyAnimation>
#include <QMouseEvent>

MyButton::MyButton(QString orignIcon, QString switchIcon)
{
    this->myOrignIcon = orignIcon;
    this->mySwitchIcon = switchIcon;
    QPixmap *pix = new QPixmap();
    bool res = pix->load(myOrignIcon);
    if(!res)
    {
        qDebug() << QString("路径错误%1").arg(orignIcon) << endl;
    }
    this->setFixedSize(pix->width(), pix->height());
    this->setStyleSheet("QPushButton{border:0px}");
    this->setIcon(QIcon(orignIcon));
    this->setIconSize(QSize(pix->width(), pix->height()));
}

void MyButton::rise()
{
    QPropertyAnimation *animatio
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值