加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
mainwindow.cpp 8.38 KB
一键复制 编辑 原始数据 按行查看 历史
mahaobo 提交于 2023-05-10 16:09 . 修改注释,优化逻辑
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QIcon>
#include <QKeyEvent>
#include <ctime>
#include <QFontDatabase>
#include <QString>
#include "config.h"
#include "nahida.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
initWindow(); //初始化窗口
}
MainWindow::~MainWindow()
{
delete ui;
}
//初始化窗口
void MainWindow::initWindow(){
setFixedSize(GAME_WIDTH,GAME_HEIGHT); //大小
setWindowTitle(GAME_TITLE); //标题
setWindowIcon(QIcon(GAME_ICON)); //图标
background.load(BACKGROUND_PATH); //加载背景
update();
int fontId=QFontDatabase::addApplicationFont(QStringLiteral(":/res/hk4e_zh-cn.ttf")); //导入字体文件
QStringList fontFamilies=QFontDatabase::applicationFontFamilies(fontId);
QFont font;
font.setFamily(fontFamilies[0]);
ui->start->setFont(font);
ui->intro->setFont(font);
ui->restart->setFont(font);
ui->return_main->setFont(font);
ui->score->setFont(font);
ui->distance->setFont(font);
ui->over_score->setFont(font);
ui->over_distance->setFont(font);
ui->game_over->setFont(font);
ui->start->setGeometry(390,140,ui->start->width(),ui->start->height()); //大小
ui->intro->setGeometry(390,260,ui->intro->width(),ui->intro->height());
ui->start->setFocusPolicy(Qt::NoFocus);
ui->intro->setFocusPolicy(Qt::NoFocus);
ui->restart->setFocusPolicy(Qt::NoFocus);
ui->return_main->setFocusPolicy(Qt::NoFocus);
ui->score->hide();
ui->distance->hide();
ui->groupBox->hide();
m_Timer.setInterval(GAME_RATE); //主定时器设置
sprint_Timer.setInterval(SPRINT_DURATION); //冲刺定时器设置
sprint_Timer.setSingleShot(true);
sprint_interval_Timer.setInterval(SPRINT_INTERVAL);
sprint_interval_Timer.setSingleShot(true);
add_Barrier_interval_Timer.setInterval(ADD_BARRIER_INTERVAL); //障碍物计时器设置
add_veget_intervai_Timer.setInterval(ADD_VEGET_INTERVAL); //兰纳罗计时器设置
add_veget_intervai_Timer.setSingleShot(true);
protected_Timer.setInterval(PROTECTED_DURATION);
protected_Timer.setSingleShot(true);
nahida.y=NAHIDA_ON_GROUNG_POS_Y;
connect(&m_Timer,&QTimer::timeout,[=](){
updatePosition(); //更新坐标
collisionDetection(); //碰撞检测
ui->score->setText("Score: "+QString::number(score)); //更新分数
ui->distance->setText("Distance: "+QString::number(grounds.distance)+" m");
update(); //刷新屏幕
});
connect(&add_Barrier_interval_Timer,&QTimer::timeout,[=](){
addBarrier();
});
score=0;
grounds.distance=0;
ui->score->setText("Score: "+QString::number(score));
ui->distance->setText("Distance: "+QString::number(grounds.distance)+" m");
sprint_once=false;
sprint_twice=false;
}
void MainWindow::paintEvent(QPaintEvent *event){ ////////paintEvent //////////Event
QPainter painter(this);
painter.drawPixmap(0,0,background); //绘制背景
for(int i=0;i<9;i++){ //绘制地面
painter.drawPixmap(grounds.grounds[i].ground_posX,393,grounds.grounds[i].ground);
}
painter.drawPixmap(nahida.x,nahida.y,nahida.getImg());
for(auto& barr:barriers) //绘制障碍物
{
if(protected_Timer.isActive()){
painter.drawPixmap(barr->x,barr->y,barr->getImg(1));
}
else{
painter.drawPixmap(barr->x,barr->y,barr->getImg(0));
}
}
}
void MainWindow::keyPressEvent(QKeyEvent *event){ /////////keyPressEvent
QKeyEvent *key=(QKeyEvent*) event;
if(key->key()==Qt::Key_Space){ //空格键跳跃
nahida.jump();
}
if(key->key()==Qt::Key_Shift){ //shift键冲刺
sprint();
}
}
void MainWindow::mousePressEvent(QMouseEvent *event){ /////////mousePressEvent
if(event->button()==Qt::RightButton){
sprint();
}
}
void MainWindow::playgame(){ /////////playgame & gameover
score=0;
grounds.distance=0;
nahida.current_run_img=0;
ui->score->show();
ui->distance->show();
m_Timer.start(); //启动计时器
nahida.run_Timer.start();
add_Barrier_interval_Timer.start();
}
void MainWindow::gameover(){
m_Timer.stop();
sprint_Timer.stop();
sprint_interval_Timer.stop();
add_Barrier_interval_Timer.stop();
add_veget_intervai_Timer.stop();
nahida.run_Timer.stop();
protected_Timer.stop();
ui->groupBox->setGeometry(330,0,ui->groupBox->width(),ui->groupBox->height());
ui->over_score->setText("Score: "+QString::number(score));
ui->over_distance->setText("Distance: "+QString::number(grounds.distance)+" m");
ui->groupBox->show();
}
void MainWindow::updatePosition(){ //更新对象坐标
grounds.calculatePositions();
for(it=barriers.begin();it!=barriers.end();){
if((*it)->isOut()){
it=barriers.erase(it);
}
else{
(*it)->updatePosition();
(*it)->updatePiaofulingPosY(); //对象为飘浮灵,则更新Y坐标;对象不是飘浮灵,则执行空函数
it++;
}
}
nahida.updatePositionY();
if(this->sprint_Timer.isActive()){
grounds.calculatePositions();
grounds.calculatePositions();
for(auto& barr:barriers)
{
barr->updatePosition();
barr->updatePosition();
}
}
}
void MainWindow::collisionDetection(){
int i=0;
for(it=barriers.begin();it!=barriers.end();){
i=(*it)->collisionDetection(nahida.nahida_Rect);
switch (i) {
case 0: //无碰撞
it++;
break;
case 1:{ //障碍物
it++;
if(!protected_Timer.isActive()&&!sprint_Timer.isActive()){
gameover();
}
break;
}
case 2:{ //兰纳罗
protected_Timer.start();
it=barriers.erase(it);
score+=20;
break;
}
case 3:{ //苹果
it=barriers.erase(it);
score+=2;
break;
}
default:
break;
}
}
}
void MainWindow::sprint(){
if(!sprint_interval_Timer.isActive()){
sprint_once=false;
sprint_twice=false;
}
if(sprint_twice||(sprint_Timer.remainingTime()>90&&sprint_Timer.isActive())){
return;
}
if(!sprint_once){
sprint_once=true;
sprint_Timer.start();
sprint_interval_Timer.start();
}
else if(!sprint_twice){
sprint_twice=true;
sprint_Timer.start();
}
}
void MainWindow::addBarrier(){ //生成障碍物
srand((unsigned int)time(NULL));
i=rand()%4;
switch (i) {
case 0:
barriers.emplace_back(new Shilaimu);
barriers.emplace_back(new Apple(1));
break;
case 1:
barriers.emplace_back(new Piaofuling);
break;
case 2:
barriers.emplace_back(new Yucha);
barriers.emplace_back(new Apple(2));
break;
case 3:
if(add_veget_intervai_Timer.isActive()){
barriers.emplace_back(new Apple(2));
return;
}
add_veget_intervai_Timer.start();
barriers.emplace_back(new Veget);
break;
default:
break;
}
}
void MainWindow::on_start_clicked() //开始游戏按键 /////QButton
{
ui->start->hide();
ui->intro->hide();
playgame();
}
void MainWindow::on_intro_clicked() //游戏介绍按键
{
intro=new Introduction;
intro->setWindowFlags(Qt::WindowStaysOnTopHint);
intro->show();
}
void MainWindow::on_restart_clicked()
{
ui->groupBox->hide();
barriers.clear();
nahida.y=NAHIDA_ON_GROUNG_POS_Y;
playgame();
}
void MainWindow::on_return_main_clicked()
{
ui->groupBox->hide();
ui->score->hide();
ui->distance->hide();
ui->start->show();
ui->intro->show();
barriers.clear();
nahida.y=NAHIDA_ON_GROUNG_POS_Y;
nahida.current_run_img=0;
update();
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化