#include "gamewindow.h"
#include "ui_gamewindow.h"
#include <QFile>
#include <QGraphicsView>
#include <QSequentialAnimationGroup>
#include <QPropertyAnimation>
#include <QStyleOption>
#include <buttonitem.h>
#include <QTimer>
#include <QKeyEvent>
#include "roaditem.h"
#include "pipeitem.h"
GameWindow::GameWindow(QWidget *parent) :
QWidget(parent),
m_startGame(false),
ui(new Ui::GameWindow)
{
ui->setupUi(this);
initControl();
initBackgroundMusic();
this->setWindowTitle("飞翔的小鸟");
}
GameWindow::~GameWindow()
{
delete ui;
}
void GameWindow::GameOver()
{
//“墙都不扶,就服你!”
const int nLetters = 9;
struct{
char const *pix;
qreal initX, initY;
qreal destX, destY;
}letterData[nLetters] = {
{"墙", -1000, -1000, 200, 160 },//第一行
{"都", -800, -1000, 250, 160 },
{"不", -600, -1000, 300, 160 },
{"扶", -400, -1000, 350, 160 },//第二行
{",", 1000, 2000, 400, 160 },
{"就", 800, 2000, 225, 220 },
{"服", 600, 2000, 275, 220 },
{"你", 400, 2000, 325, 220 },
{"!", 200, 2000, 375, 220 }
};
//game over 文字动画组
QParallelAnimationGroup* lettersGroupMoving = new QParallelAnimationGroup(this);
for(int i = 0; i < nLetters; ++i)
{
//文本图形项
QString htmlText = QString("<span style=\"font-family:'Berlin Sans FB';font-size:38px;font-weight:600;color:#194819;\">%1</span>").arg(letterData[i].pix);
QGraphicsTextItem *letter = new QGraphicsTextItem();
letter->setHtml(htmlText);
letter->setPos(letterData[i].initX,letterData[i].initY);
//文本项属性动画
QPropertyAnimation *moveAnimation = new QPropertyAnimation(letter,"pos",lettersGroupMoving);
moveAnimation->setEndValue(QPointF(letterData[i].destX,letterData[i].destY));//设置字符动画结束的位置
moveAnimation->setDuration(200);//动画时长200ms
moveAnimation->setEasingCurve(QEasingCurve::OutElastic);//动画曲线效果
//文本图形项添加到游戏场景中去
m_scene->addItem(letter);
}
lettersGroupMoving->start(QAbstractAnimation::DeleteWhenStopped);
//将小鸟图形项移除
m_scene->removeItem(m_birdItem);
//游戏结束的音效
m_welcomePlayer->stop();
m_welcomePlayerList->clear();
m_welcomePlayer->setMedia(QUrl("qrc:/BirdGame/Resources/sound/gameover.wav"));
m_welcomePlayer->setVolume(100);
m_welcomePlayer->play();
}
void GameWindow::initControl()
{
loadStyleSheet("GameWindow");
m_scene = new GameScene(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 GameWindow::initBackgroundMusic()
{
m_welcomePlayer = new QMediaPlayer(this);
//播放一次
// {
// m_welcomePlayer->setMedia(QUrl("qrc:/BirdGame/Resources/sound/welcome.mp3"));
// m_welcomePlayer->setVolume(88);
// m_welcomePlayer->play();
// }
m_welcomePlayerList = new QMediaPlaylist(this);
m_welcomePlayerList->addMedia(QUrl("qrc:/BirdGame/Resources/sound/welcome.mp3"));
m_welcomePlayerList->setPlaybackMode(QMediaPlaylist::CurrentItemInLoop);//循环播放
m_welcomePlayer->setPlaylist(m_welcomePlayerList);//将播放列表放置到播放器
m_welcomePlayer->play();
}
//加载样式
void GameWindow::loadStyleSheet(const QString &sheetName)
{
QFile file(":/BirdGame/Resources/qss/"+sheetName+".css");
if(file.open(QFile::ReadOnly))
{
QString strStyleSheet = this->styleSheet();//读取当前样式
strStyleSheet+=file.readAll();
this->setStyleSheet(strStyleSheet);
file.close();
}
}
void GameWindow::startWelcome()
{
m_birdItem = new BirdItem(m_scene);//小鸟对象
//游戏状态检测
m_checkGameStatusTimer = new QTimer(this);
//定时检测游戏状态
connect(m_checkGameStatusTimer,SIGNAL(timeout()),this,SLOT(onCheckGameStatus()));
//管道
PipeItem* pipe = new PipeItem(m_scene);
//道路
RoadItem* road = new RoadItem(m_scene);
//文字
const int nLetters = 15;
struct{
char const* pix;
qreal initx,inity;
qreal destx,desty;
} letterData[nLetters]= {
{"飞",-1000,-1000,250,100},
{"吧",-800,-1000,300,100},
{",",-6000,-1000,350,100},
{"像",-400,-1000,150,160},
{"风",1000,2000,200,160},
{"一",800,2000,250,160},
{"样",600,2000,300,160},
{"自",400,2000,350,160},
{"由",200,2000,400,160},
{",",0,2000,450,160},
{"无",0,2000,175,220},
{"法",0,2000,225,220},
{"挽",0,2000,275,220},
{"留",0,2000,325,220},
{"...",0,2000,375,220},
};
//连续动画组
QSequentialAnimationGroup* letterGroupMoving = new QSequentialAnimationGroup(this);
//并行动画组
m_letterGroupFading = new QParallelAnimationGroup(this);
for(int i=0;i<nLetters;i++)
{
//文本图形项
QString htmlText = QString("<span style=\"font-family:'Berlin Sans FB';font-size:38px;font-weight:600;color:#194819\">%1</span>").arg(letterData[i].pix);
QGraphicsTextItem* letter = new QGraphicsTextItem();
letter->setHtml(htmlText);
letter->setPos(letterData[i].initx,letterData[i].inity);//位置初始值
QPropertyAnimation* moveAnimation = new QPropertyAnimation(letter,"pos",letterGroupMoving);
moveAnimation->setEndValue(QPoint(letterData[i].destx,letterData[i].desty));//位置结束值
moveAnimation->setDuration(200);//动画时长
moveAnimation->setEasingCurve(QEasingCurve::OutElastic);//动画曲线效果
letterGroupMoving->addPause(50);
//渐渐消失动画
QPropertyAnimation* fadeAnimation = new QPropertyAnimation(letter,"opacity",m_letterGroupFading);
fadeAnimation->setDuration(1000);
fadeAnimation->setEndValue(0);
fadeAnimation->setEasingCurve(QEasingCurve::OutQuad);
m_scene->addItem(letter);//往游戏场景中添加字符项
}
//启动字符动画
letterGroupMoving->start(QAbstractAnimation::DeleteWhenStopped);
//按钮图形项目
QPixmap pix(":/BirdGame/Resources/image/startButton.png");
ButtonItem* btnItem = new ButtonItem(pix,m_scene);
btnItem->setPos(QPointF(220,340));
//按钮渐渐消失动画
QPropertyAnimation* fadeAnimation = new QPropertyAnimation(btnItem,"opacity",m_letterGroupFading);
fadeAnimation->setDuration(600);
fadeAnimation->setEndValue(0);//透明度为0,1为完全不透明
fadeAnimation->setEasingCurve(QEasingCurve::OutQuad);
connect(btnItem,SIGNAL(clicked()),this,SLOT(onStartBtnClicked()));//开始游戏
connect(fadeAnimation,&QPropertyAnimation::finished,[this](){
m_startGame = true;
m_checkGameStatusTimer->start(50);
m_birdItem->flyLandfallAnimation();
m_welcomePlayerList->clear();
m_welcomePlayerList->addMedia(QUrl("qrc:/BirdGame/Resources/sound/background.mp3"));
m_welcomePlayer->play();
});
}
void GameWindow::paintEvent(QPaintEvent *event)
{
//自定义paint事件
QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget,&opt,&p,this);
}
void GameWindow::keyPressEvent(QKeyEvent *event)
{
if(m_startGame)
{
m_birdItem-
评论0