案例预期目的
分别是 开始界面 、 选关界面、游戏界面、结算界面。
开始界面
需要设置 基础配置、菜单栏、背景图、标题、start
基础配置
固定大小 、窗口图标、窗口名称 、工具栏
#include "mainscene.h"
#include "ui_mainscene.h"
MainScene::MainScene(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainScene)
{
ui->setupUi(this);
//设置固定大小
this->setFixedSize(320,588);
//设置 点击GameOver就退出游戏
connect(ui->actionGameOver,&QAction::triggered,[=](){
//进行一个延时处理
QTimer::singleShot(100,this,[=](){
this->close();
});
});
}
MainScene::~MainScene()
{
delete ui;
}
背景图
利用重写paintEvent(QPaintEvent *e) 进行添加背景图
//设置背景图
void MainScene::paintEvent(QPaintEvent *e){
//设置背景图
QPainter *painter=new QPainter(this);
painter->drawPixmap(QRect(0,0,this->width(),this->height()),QPixmap(":/res/OtherSceneBg.png"));
//设置标题图片
QPixmap pix;
bool res=pix.load(":/res/Title.png");
if(!res){
qDebug()<<"MainScene中标题图片传入失败";
return ;
}
pix=pix.scaled(pix.width()*0.7,pix.height()*0.7); //重置标题图片大小
painter->drawPixmap(10,50,pix);
}
都是利用 QPainter 来把图片画上去
start 开始按钮
#ifndef MYPUSHBUTTON_H
#define MYPUSHBUTTON_H
#include <QWidget>
#include"QPushButton"
#include"QPropertyAnimation"
class MyPushButton : public QPushButton
{
Q_OBJECT
public:
// explicit MyPushButton(QWidget *parent = nullptr);
MyPushButton(QString first_image,QString second_image="");
QString first_path;//第一个图片路径
QString second_path;//第二个图片路径
//设置start动画特效
void zoom1();
void zoom2();
signals:
};
#endif // MYPUSHBUTTON_H
添加一个mypushbutton的class文件(父类为QPushButton)
mypushbutton所需要的成员函数和成员变量。
重写构造函数传入图的路径
MyPushButton::MyPushButton(QString first_image,QString second_image){
this->first_path=first_image;
this->second_path=second_image;
QPixmap pix_mybtn;
bool res=pix_mybtn.load(first_path);
if(!res){
qDebug()<<"mypushbutton中first_image传入失败";
return ;
}
this->setFixedSize(QSize(pix_mybtn.width(),pix_mybtn.height()));
this->setStyleSheet("QPushButton{border:0Px}");
this->setIcon(pix_mybtn);
this->setIconSize(QSize(pix_mybtn.width(),pix_mybtn.height()));
}
this->setFixedSize(QSize(pix_mybtn.width(),pix_mybtn.height()));
this->setStyleSheet("QPushButton{border:0Px}"); //使图片更美观
this->setIcon(pix_mybtn);
this->setIconSize(QSize(pix_mybtn.width(),pix_mybtn.height()));通过这四行代码可以令mypushbutton的实例化对象出现在窗口
使statr开始按钮具有动画特效
void MyPushButton::zoom1(){
QPropertyAnimation *ani =new QPropertyAnimation(this,"geometry");
ani->setDuration(200);//设置延时200ms
//以下是设置开始位置 结束位置 动画特效方式 动画执行
ani->setStartValue(QRect(this->x(),this->y(),this->width(),this->height()));
ani->setEndValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
ani->setEasingCurve(QEasingCurve::OutInElastic);
ani->start();
}
void MyPushButton::zoom2(){
QPropertyAnimation *ani =new QPropertyAnimation(this,"geometry");
ani->setDuration(200);
ani->setStartValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
ani->setEndValue(QRect(this->x(),this->y(),this->width(),this->height()));
ani->setEasingCurve(QEasingCurve::OutInElastic);
ani->start();
}
//以下是设置 开始位置 结束位置 动画特效方式 动画执行
ani->setStartValue(QRect(this->x(),this->y(),this->width(),this->height())); //开始位置
ani->setEndValue(QRect(this->x(),this->y()+10,this->width(),this->height())); //结束位置
ani->setEasingCurve(QEasingCurve::OutInElastic); //动画特效方式
ani->start(); //动画执行
选择关卡界面
由开始界面进入选择关卡界面
创建名为ChooseScene的class文件
connect(start_but,&QPushButton::clicked,[=](){
qDebug()<<"点击了Start";
QTimer::singleShot(200,this,[=](){
start_but->zoom1();
start_but->zoom2();
});
choose = new ChooseScene;
this->hide();
choose->show();
});
点击start键那么就由开始界面进入选择关卡界面。
choose = new ChooseScene;
this->hide(); //隐藏开始界面
choo