Qt跑酷游戏开发,背景移动,人物跳跃

本文介绍如何使用Qt进行跑酷游戏开发,重点讲述如何实现背景的平滑移动以及游戏角色的跳跃功能,涵盖了Qt在游戏开发中的基本应用和技术要点。
摘要由CSDN通过智能技术生成

在这里插入图片描述

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QDialog>
#include <QKeyEvent>
#include <QMediaPlayer>
#include <QTimer>
#include "director.h"

namespace Ui {
class MainWindow;
}

/*
 *  MainWindow - 游戏场景
 *  SLOT:
 *  @nextFrame():  每个朋友会做什么
 *  @gameOver:  检查游戏是否结束
 *  @isPlaying:  检查游戏是否暂停
 *  @counter:  数一数朋友的数目
 *  @scene:  场景的所有组成部分
 *  @bgm: 游戏的背景音乐
 */
class MainWindow : public QDialog
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

public slots:
    void next_frame();//每个fream更新屏幕
    void pause_and_resume();//暂停并继续游戏
    void mute_and_play();//静音和取消静音效果
    void change_configuration();//应用配置更改
    void update_ui();//更新配置设置框的ui
    void save_configuration();//用输入的名称保存当前配置
    void load_configuration(); //加载配置文件
protected:
    void paintEvent(QPaintEvent *event);
    void keyPressEvent(QKeyEvent *event);

private:
    volatile bool gameOver = false;
    volatile bool isPlaying = true;
    volatile int counter = 0;

    Ui::MainWindow *ui;
    Scene* scene;//场景
    QMediaPlayer bgm;//音效
    QString exepath;//程序路径
    QTimer* timer;
};


#endif // MAINWINDOW_H

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QFile>
#include <QFileInfo>
#include <QMediaPlaylist>
#include <QApplication>
#include <iostream>

//the path of the configuration file


MainWindow::MainWindow(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::MainWindow)
{
    //set up the UI
    ui->setupUi(this);
    exepath = QApplication::applicationDirPath();
    qDebug()<<"exepath=="<<exepath;
    this->resize(FRAME_WIDTH,FRAME_HEIGHT);
    ui->EnterFileName->hide();
    ui->MenuBox->hide();
    ui->Configuration->hide();

    //set up the soundeffect to play
    QMediaPlaylist *playlist = new QMediaPlaylist();
    playlist->addMedia(QUrl::fromLocalFile(QFileInfo("exepath/Audio/Lucid_Dreamer1.mp3").absoluteFilePath()));
    playlist->setPlaybackMode(QMediaPlaylist::Loop);

    bgm.setPlaylist(playlist);
    bgm.setVolume(50);
    bgm.play();
    //创建一个计时器来更新每个fream
    QTimer* timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(next_frame()));
    timer->start(50);
    //load from the configuration file
    //从配置文件加载
    load_configuration();//创建场景器

    this->update();
}

MainWindow::~MainWindow()
{
    gameOver = true;
    delete ui;
    delete scene;
}

/* SLOT */
//update the screen each fream每个fream更新屏幕
void MainWindow::next_frame()
{
    update();
}

//pause and resume game暂停并继续游戏
void MainWindow::pause_and_resume()
{
    isPlaying = !isPlaying;
    mute_and_play();
}

//静音和取消静音效果
void MainWin
  • 4
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

vqt5_qt6

你的鼓励是我们创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值