QT 接水果小游戏

利用QT实现了一个简单的接水果游戏,以一个水果为例,水果下落速度可调节。效果如下:

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

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setFixedSize(900,800);

    gameWidget = new QWidget(this);
    gameWidget->move(0, 100);
    gameWidget->resize(900, 700);
    gameWidget->setStyleSheet("background-color:white;");

    basket = new QLabel(gameWidget);
    basket->setGeometry(0, 500, 100, 50);
    basket->setStyleSheet("border-image: url(:/basket.jpg);");

    //水果出现位置随机
    qsrand(time(NULL));
    a = (0 + rand() % 9000)/double(10);
    b = 0 + rand() % 150;
    fruit1 = new QLabel(gameWidget);
    fruit1->setGeometry(a,b,50, 50);
    fruit1->setStyleSheet("border-image: url(:/fruit1.jpg);");

    QString button_style="QPushButton{background-color:rgb(152,188,228);\
color: black;border-radius:20px;border: 2px groove gray;\
border-style: outset;}"
"QPushButton:hover{background-color:white; color: black;}"
"QPushButton:pressed{background-color:#4682B4;\
border-style: inset; }";

    startBtn = new QPushButton(this);
    startBtn->setMaximumWidth(120);
    startBtn->setText("开始");
    startBtn->setStyleSheet(button_style);
    startBtn->setFont(QFont("黑体", 18));

    acBtn=new QPushButton(this);
    acBtn->setMaximumWidth(120);
    acBtn->setText("加速");
    acBtn->setStyleSheet(button_style);
    acBtn->setFont(QFont("黑体", 18));

    dcBtn = new QPushButton(this);
    dcBtn->setMaximumWidth(120);
    dcBtn->setText("减速");
    dcBtn->setStyleSheet(button_style);
    dcBtn->setFont(QFont("黑体", 18));

    stopBtn=new QPushButton(this);
    stopBtn->setMaximumWidth(120);
    stopBtn->setText("停止");
    stopBtn->setStyleSheet(button_style);
    stopBtn->setFont(QFont("黑体", 18));

    scorelb=new QLabel(this);
    scorelb->setGeometry(740,35,70,30);
    scorelb->setText("得分:");
    scorelb->setFont(QFont("黑体",18));


    score=new QLabel(this);
    score->setGeometry(820,35,50,30);
    score->setText("0");
    score->setFont(QFont("黑体",18));

    QWidget *layoutwindow = new QWidget(this);
    layoutwindow->setGeometry(20,10,700,80);
    QHBoxLayout *layout= new QHBoxLayout;
    layout->addWidget(startBtn);
    layout->addWidget(acBtn);
    layout->addWidget(dcBtn);
    layout->addWidget(stopBtn);
    layoutwindow->setLayout(layout);

    timer=new QTimer();
    //打开鼠标位置追踪
    this->setMouseTracking(true);
    gameWidget->setMouseTracking(true);
    gameWidget->installEventFilter(this);

    connect(startBtn, &QPushButton::clicked, this, &MainWindow::startgame);
    connect(stopBtn, &QPushButton::clicked, this, &MainWindow::stopgame);
    connect(acBtn,&QPushButton::clicked,this,&MainWindow::acgame);
    connect(dcBtn, &QPushButton::clicked, this, &MainWindow::dcgame);
    connect(timer, SIGNAL(timeout()), this, SLOT(basketmove()));

    iniscore=0;
    delay=10;
    db=2;
}
//固定果篮纵向位置
void MainWindow::resizeEvent(QResizeEvent * dragwindow)
{
    if (dragwindow) {
        basket->move(basket->x(), gameWidget->height() - 50);
    }
}

void MainWindow::startgame()
{
    timer->start(10);
}

void MainWindow::acgame()
{
    delay = delay - 2;
    timer->setInterval(delay);
    timer->start();
}

void MainWindow::dcgame()
{
    delay = delay + 2;
    timer->setInterval(delay);
    timer->start();
}

void MainWindow::stopgame()
{
    timer->stop();
}

//碰撞检测
void MainWindow::basketmove()
{
    if (fruit1->pos().x()>basket->pos().x()-fruit1->width() && (fruit1->pos().x())<(basket->pos().x()+basket->width()) \
            && (fruit1->pos().y()+fruit1->height())>basket->pos().y())
    {
        iniscore++;
        score->setText(QString("%1").arg(iniscore));
        //随机水果位置,保留1位小数
        a = (0 + rand() % 9000)/double(10);
        b = 0 + rand() % 150;
        fruit1->move(a,b);
    }
    else if (fruit1->pos().y()>gameWidget->height())
    {
        a = (0 + rand() % 9000)/double(10);
        b = 0 + rand() % 150;
        fruit1->move(a,b);
    }

    b += db;
    fruit1->move(a,b);
}

//鼠标移动事件
void MainWindow::mouseMoveEvent(QMouseEvent * e)
{
    e->accept();
    if (e->pos().x()>0&& e->pos().x()<800)
    {
        basket->move(e->pos().x(), basket->pos().y());
    }
    else if (e->pos().x() >= 850)
    {
        basket->move(800, basket->pos().y());
    }
}

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

猜你喜欢

转载自blog.csdn.net/L1114187703/article/details/107999997