QT实现 倒计时猜数字小游戏 QT5.12.3环境 C++实现
代码:
game.h
#ifndef GAME_H
#define GAME_H
#include <QWidget>
#include <QtWidgets>
namespace Ui {
class game;
}
class game : public QWidget
{
Q_OBJECT
public:
explicit game(int time,QWidget *parent = nullptr);
~game();
QTimer *timer;
game *gameui;
int answer; // 答案
int gametime; // 倒计时
QString result; // 输入的结果
void timerEvent(QTimerEvent *event);
public slots:
void timeout_slot();
private slots:
void on_button_hint_clicked();
void on_button1_clicked();
void on_button_2_clicked();
void on_button_3_clicked();
void on_button_4_clicked();
void on_button_5_clicked();
void on_button_6_clicked();
void on_button_7_clicked();
void on_button_8_clicked();
void on_button_9_clicked();
void on_button_0_clicked();
void on_button_enter_clicked();
private:
Ui::game *ui;
};
#endif // GAME_H
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QtWidgets>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void on_exit_button_clicked();
void on_start_button_clicked();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
game.cpp
#include "game.h"
#include "ui_game.h"
game::game(int time,QWidget *parent) :
QWidget(parent),
ui(new Ui::game)
{
ui->setupUi(this);
timer = new QTimer(this);
answer = rand() % 9000 + 1000; // 随机数
result.clear();
gametime = time;
// 设置进度条
ui->progressBar->setMinimum(0);//最小值
ui->progressBar->setMaximum(gametime);//最大值
ui->progressBar->setValue(gametime); //当前值
connect(timer,&QTimer::timeout,this,&game::timeout_slot);
timer->start(1000); // 1秒
}
game::~game()
{
delete ui;
}
void game::timerEvent(QTimerEvent *event){}
// timeout_slot()
void game::timeout_slot()
{
gametime--;
if(gametime <= 0){
timer->stop();
QMessageBox::warning(this, "OVER","倒计时结束,游戏结束");
this->close();
}
ui->progressBar->setValue(gametime);
}
// 提示button
void game::on_button_hint_clicked()
{
QString str = "答案为:" + QString::number(answer);
ui->textBrowser->setText(str); // 显示
result.clear();
}
// button_1
void game::on_button1_clicked()
{
result += "1"; // 加到result后面
ui->textBrowser->setText(result);
}
// button_2
void game::on_button_2_clicked()
{
result += "2"; // 加到result后面
ui->textBrowser->setText(result);
}
// button_3
void game::on_button_3_clicked()
{
result += "3"; // 加到result后面
ui->textBrowser->setText(result);
}
// button_4
void game::on_button_4_clicked()
{
result += "4"; // 加到result后面
ui->textBrowser->setText(result);
}
// button_5
void game::on_button_5_clicked()
{
result += "5"; // 加到result后面
ui->textBrowser->setText(result);
}
// button_6
void game::on_button_6_clicked()
{
result += "6"; // 加到result后面
ui->textBrowser->setText(result);
}
// button_7
void game::on_button_7_clicked()
{
result += "7"; // 加到result后面
ui->textBrowser->setText(result);
}
// button_8
void game::on_button_8_clicked()
{
result += "8"; // 加到result后面
ui->textBrowser->setText(result);
}
// button_9
void game::on_button_9_clicked()
{
result += "9"; // 加到result后面
ui->textBrowser->setText(result);
}
// button_0
void game::on_button_0_clicked()
{
if(result == "") return ;
result += "0"; // 加到result后面
ui->textBrowser->setText(result);
}
// 回车button
void game::on_button_enter_clicked()
{
if(result == "") return ;
int resultint = result.toInt();
if(resultint == answer){
ui->textBrowser->clear();
QMessageBox::information(this, "闯关成功","回答正确!!!");
this->close();
}
else if(resultint > answer){
ui->textBrowser->setText("比答案大");
result.clear();
}
else{
ui->textBrowser->setText("比答案小");
result.clear();
}
}
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include "game.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_exit_button_clicked()
{
this->close();
}
void Widget::on_start_button_clicked()
{
int time = ui->time_box->value(); // 获取倒计时
game *gameui = new game(time); // 游戏界面
this->close();
gameui->show();
}
game.ui
widget.ui