QT(嵌入式开发)期末综合实验(打地鼠游戏)

QT(嵌入式开发)期末综合实验(打地鼠游戏),第1张

QT(嵌入式开发)期末综合实验(打地鼠游戏) 一、概述

主要介绍打滑稽游戏项目开发的目的和意义。
1.1项目意义
以Qt Creater设计平台作为背景,使用C++编程语言,开发一款休闲娱乐的小游戏,使用户达到轻松娱乐的目的。
1.2开发环境
windows10 *** 作系统,Qt Creater设计平台。

二、游戏玩法

打开游戏界面

点击开始游戏

用鼠标点击出现的滑稽脸
点到旁边的哆啦A梦五次,游戏失败
点击设置按键,可调整游戏难度

三、代码实现

不部分主要展示源文件包(Sources)下的代码实现(完整版源代码请见源代码文件)

dialog.cpp:
#include "dialog.h"
#include "ui_dialog.h"



Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
}

Dialog::~Dialog()
{

    delete ui;
}

void Dialog::mousePressEvent(QMouseEvent *e)
{

            if(maxforhuaji != ui->huajinumber->value())
                maxforhuaji = ui->huajinumber->value();

            if(timetonew != ui->newtime->value())
                timetonew=ui->newtime->value();

            //设置完成后,点击空间外任意地方,该函数生效
}

main.cpp:
#include "widget.h"
#include "startworld.h"
#include 
#include 

int main(int argc, char *argv[])
{

    QApplication a(argc, argv);
    Widget w;
    w.setWindowTitle("打滑稽");
    w.show();


    return a.exec();
}

startworld.cpp:
#include "startworld.h"
#include "ui_startworld.h"

int score=0;
int errortime=0;
double timetonew=1;
int maxforhuaji=3;

void startworld::setbutton(QPushButton * bt)
{
    bt->setStyleSheet("background: rgb(255, 255, 255)");
    bt->setFlat(1);
    // ui->stopButton->setIcon(QIcon(":/photo/picture/stopgame.jpg"));
    // ui->stopButton->setIconSize(QSize(ui->stopButton->width(),ui->stopButton->height()));
}

startworld::startworld(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::startworld)
{

    ui->setupUi(this);
    this->setWindowIcon(QIcon(":/photo/picture/Anmi.png"));
    QPalette palette;
    palette.setBrush(QPalette::Background,QBrush(QPixmap(":/photo/picture/Anmi.png")));
    this->setStyleSheet("background-image:url(:/photo/picture/Anmi.png)");
    this->setPalette(palette);
    /
}

void startworld::getscore()         //点对得分
{
    score= score +10;
    ui->getnumber->display(score);
}

void startworld::getmistake(){     //若点错
    errortime++;
    ui->losenumber->display(errortime);
    //音效添加
    QSoundEffect *login=new QSoundEffect(this);
    login->setSource(QUrl::fromLocalFile(":/listeb/music/huaq.wav"));

    login->play();
}

void startworld::lose()             //超过五个失误,游戏结束+d窗
{
    if(errortime>5){
        //****游戏结束,返回主菜单还是重新开始

        if( QMessageBox::question(this,
                                     tr("fail"),
                                     tr("you have losed,do you want to try again?"),
                                      QMessageBox::Yes, QMessageBox::No )
                           == QMessageBox::Yes){
                //e->accept();//不会将事件传递给组件的父组件
            //继续
            score =0;       //重置游戏计数
            errortime=-1;
        }else
        {
            //返回
            score =0;
            errortime=-1;
            this->close();
        }
    }
}

void startworld::on_stopButton_clicked()   //暂停
{
    m_pTimer->stop();
    play = 0;
}

void startworld::on_StartButton_clicked()   //继续
{
    m_pTimer->start(timetonew*1000);
    play = 1;
}

void startworld::on_ExitButton_clicked()    //退出
{
    if( QMessageBox::question(this,
                                 tr("Quit"),
                                 tr("Are you sure to quit this application?"),
                                  QMessageBox::Yes, QMessageBox::No )
                       == QMessageBox::Yes){
        this->close();
        delete this;
    }
    else{
    }

}

void startworld::keyPressEvent()    //更改鼠标样式
{
       //本来是要按键变换的所以叫keyPressEvent,后来懒得写了2333
       QCursor my(QPixmap(":/photo/picture/chanshizhe.png"));
       this->setCursor(my);
}

widget.cpp:
#include "widget.h"
#include "ui_widget.h"

//全局变量

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{

    ui->setupUi(this);

    //设置背景,一般就这三句,大多控件也适用
    QPalette palette;
    palette.setBrush(QPalette::Background,QBrush(QPixmap(":/photo/picture/firstbeijing.jpg")));
    this->setPalette(palette);

    //设置背景音乐
    QSoundEffect *login=new QSoundEffect(this);
    login->setSource(QUrl::fromLocalFile(":/listeb/music/Schnappi.wav"));
    login->setLoopCount(QSoundEffect::Infinite);    //参数-循环播放
    login->play();

        QImage Image;
        Image.load(":/photo/picture/shezhi.jpg");
        QPixmap pixmap = QPixmap::fromImage(Image);
        int with = ui->setbt->width();
        int height = ui->setbt->height();
        QPixmap fitpixmap = pixmap.scaled(with, height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);  // 饱满填充
        //QPixmap fitpixmap = pixmap.scaled(with, height, Qt::KeepAspectRatio, Qt::SmoothTransformation);  // 按比例缩放
        ui->setbt->setIcon(fitpixmap);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_startbt_clicked()   //开始游戏
{
    start=new startworld(this);
    start->show();


}

void Widget::on_exitbt_clicked()    //退出游戏
{
    exit(0);
}

void Widget::on_setbt_clicked()     //设置
{
    shezhi = new Dialog(this);
    shezhi->show();
}

完整源码请见:https://download.csdn.net/download/weixin_43372169/60723161

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5659471.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存