Qt_C++ 制作翻硬币游戏(2)---开始按钮的制作,并实现界面跳转

开始按钮的实现效果:

        1.制作按钮

        2.点击之后向下移动并返回原位置

        3.跳转到选择关卡界面

具体实现:

一.制作按钮

        思路:先创建按钮,在添加图片

        设置按钮大小

        设置按钮的不规则形状

        添加图片

        设置按钮上图片的大小

mypushbutton::mypushbutton(QString normalImg, QString pressImg)
{
    //设定按钮的大小,将图片给到按钮上
    QPixmap pix;
    bool ret=pix.load(normalImg);
    if(!ret)
    {
        qDebug()<<"图片错误";
    }
    //设置按钮大小
    this->setFixedSize(pix.width(),pix.height());
    //设置按钮为不规则形状
    this->setStyleSheet("QPushButton{border:0px;}");
    //设置图标
    this->setIcon(pix);
    //设置图标的大小
    this->setIconSize(QSize(pix.width(),pix.height()));
}

二.点击之后向下移动并返回原位置

        为了使得编码的可移植性,将此功能封装为mypushbutton(QString normalImg,QString pressImg="");normalImg为正常状态的图片,pressImg为按下之后的图片默认为空。

声明成员属性

//成员属性,便于有按下行为按钮功能的实现
    QString normalImgPath;
    QString pressImgPath;

实现按钮的按下和返回功能

 //按钮跳跃
    void zoomDone();
    void zoomUp();

接下来进行上述函数的具体实现:

        实现按下和返回的效果时,应该创建动态对象:

//创建动态对象.该对象设定以矩形框形式移动
QPropertyAnimation * animation=new QPropertyAnimation(this,"geometry");

//创建对象运动的时间间隔

//确定运动对象的起始位置

//确定运动对象的终止位置

//设置运动对象的运动方式

//运动对象开始执行

void mypushbutton::zoomDone()//按钮按下的动态行为
{
    //创建动态对象.该对象设定以矩形框形式移动
    QPropertyAnimation * animation=new QPropertyAnimation(this,"geometry");
    animation->setDuration(200);//设置运动时间
    //设置开始结束的位置
    animation->setStartValue(QRect(this->x(),this->y(),this->width(),this->height()));
    animation->setEndValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
    //设置动态对象的移动形式
    animation->setEasingCurve(QEasingCurve::OutBounce);
    //执行移动
    animation->start();

}
void mypushbutton::zoomUp()
{
    //设置动态对象
    QPropertyAnimation * animation=new QPropertyAnimation(this,"geometry");
    //设置运动时间
    animation->setDuration(200);
    //位置
    animation->setStartValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
    animation->setEndValue(QRect(this->x(),this->y(),this->width(),this->height()));
    //设置运动形式
    animation->setEasingCurve(QEasingCurve::OutBounce);
    //执行运动
    animation->start();

}

三.点击之后实现跳转界面

添加新的选择关卡文件chooseScence.cpp/.h,设置新窗口的大小,标题,背景图片

chooseScence::chooseScence(QWidget *parent)
    : QMainWindow{parent}
{
    this->setFixedSize(320,588);
    this->setWindowTitle("选择关卡");
    this->setWindowIcon(QIcon(":/res/Coin0001.png"));

    //设计菜单栏实现退出功能
    //创建菜单栏
    QMenuBar * bar=menuBar();
    setMenuBar(bar);
    //创建菜单
    QMenu * startMenu=bar->addMenu("开始");
    //创建菜单项
    QAction * quite=startMenu->addAction("退出");
    //实现退出功能
    connect(quite,&QAction::triggered,[=](){
        this->close();
    });



}

void chooseScence::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QPixmap pix;
    bool ret=pix.load(":/res/OtherSceneBg.png");
    if(!ret)
    {
        qDebug()<<"图片错误";
        return;
    }
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    //加载标题图片
    pix.load(":/res/Title.png");
    painter.drawPixmap( (this->width()-pix.width())*0.5,30,pix );

}

添加背景图片时,需要使用绘画事件进行实现

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值