一.游戏场景搭建
1.自定义按钮类
MyPushButton::MyPushButton(QString nomalImg,QString changImg)
{
nomalImgPath=nomalImg;
changImgPath=changImg;
//设置按钮的图标
QPixmap pix;
pix.load(nomalImgPath);
//设置图片固定大小
this->setFixedSize(pix.width(),pix.height());
//设置不规则图片
this->setStyleSheet("QPushButton{border:0px};");
this->setIcon(pix);
this->setIconSize(QSize(pix.width(),pix.height()));
}
2.按钮弹跳动画的实现
使用 QPropertyAnimation 类
//动画方式:在矩形框中弹跳,间隔200毫秒
QPropertyAnimation * animation =new QPropertyAnimation(this,"geometry");
animation->setDuration(200);
//弹跳起始位置与结束位置
animation->setStartValue(QRect(this->x(),this->y(),this->width(),this->height()));
animation->setStartValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
//设置缓和曲线,QEasingCurve::OutBounce为弹跳效果
animation->setEasingCurve(QEasingCurve::OutBounce);
animation->start();
3.菜单栏的代码实现
QMenuBar *bar=menuBar();
setMenuBar(bar);
QMenu*menu=bar->addMenu("开始");
QAction* exit=menu->addAction("退出");
4.返回按钮实现
1.点击图片切换。重写鼠标按下、释放事件
void MyPushButton::mousePressEvent(QMouseEvent *e)
{
if(this->changImgPath !="")
{
QPixmap pix;
pix.load(this->changImgPath);
this->setFixedSize(pix.width(),pix.height());
this->setStyleSheet("QPushButton{border:0px};");
this->setIcon(QPixmap(changImgPath));
this->setIconSize(QSize(this->width(),this->height()));
}
return QPushButton::mousePressEvent(e);
}
2.返回键返回主场景 ,向主场景发射自定义信号,
signals:
void chooseSineBack();
connect(backbtn,&MyPushButton::clicked,[=](){
emit this->chooseSineBack();
});
主场景监听并实现返回功能
connect(chooseLevel,&chooseSence::chooseSineBack,[=](){
chooseLevel->close();
this->show();
});
5.选择场景的关卡按钮实现
效果图
一次循环创建矩阵方法: menubtn->move(25+i%4*70,130+i/4*70);
设置位置坐标。25,130为起始位置,70为间距位置
标签中字体的对齐方式,和设置鼠标穿透性 55号
//设置字体对齐方式
label1->setAlignment(Qt::AlignHCenter |Qt::AlignVCenter);
//让鼠标进行穿透 51号属性
label1->setAttribute(Qt::WA_TransparentForMouseEvents);
5.选择关卡标签
效果图
设置标签中字体字号
QFont font;
font.setFamily("华文新魏");
font.setPointSize(20);
设置标签位置及大小
label->setGeometry(30,this->height()-50,120,60);