Qt学习记录(十)项目:数字炸弹/猜数字游戏
立即下载
资源介绍:
Qt学习记录(十)项目:数字炸弹/猜数字游戏
新手项目,佬们多多包含
#include "mywidget.h"
#include "ui_mywidget.h"
#include
#include //退出游戏
#include
#include //进度条
#include
#include //设置时间
#include
#include //输入数据
#include
#include
#include
#include
#include
#include
static int t=0; //时间
static int randomInt ;//随机数
static int i=0; //页数
static int num_1=0; //显示的范围min
static int num_2=100; //显示的范围max
MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::MyWidget)
{
//初始状态
ui->setupUi(this);
//创建定时器
timer = new QTimer(this);
//文本框不超过2位数
ui->lineEdit->setValidator(new QRegExpValidator(QRegExp("[0-9]{2}")));
//提示隐藏
ui->label_6->hide();
ui->label_7->hide();
ui->label_7->clear();
//计时器结束
connect (timer,&QTimer::timeout, this,&MyWidget::updateCountdowm);
//数字按键0-9
connect (ui->play_0,&QPushButton::pressed,this,&MyWidget::play_0_9);
connect (ui->play_1,&QPushButton::pressed,this,&MyWidget::play_0_9);
connect (ui->play_2,&QPushButton::pressed,this,&MyWidget::play_0_9);
connect (ui->play_3,&QPushButton::pressed,this,&MyWidget::play_0_9);
connect (ui->play_4,&QPushButton::pressed,this,&MyWidget::play_0_9);
connect (ui->play_5,&QPushButton::pressed,this,&MyWidget::play_0_9);
connect (ui->play_6,&QPushButton::pressed,this,&MyWidget::play_0_9);
connect (ui->play_7,&QPushButton::pressed,this,&MyWidget::play_0_9);
connect (ui->play_8,&QPushButton::pressed,this,&MyWidget::play_0_9);
connect (ui->play_9,&QPushButton::pressed,this,&MyWidget::play_0_9);
//清除和确认
connect (ui->play_x,&QPushButton::pressed,ui->lineEdit,&QLineEdit::clear);
connect (ui->play_e,&QPushButton::pressed,this,&MyWidget::on_lineEdit_editingFinished);
//样式表
this->setWindowFlags(Qt::FramelessWindowHint); //隐藏最大最小化等按键
setAttribute(Qt::WA_TranslucentBackground); //设置窗口透明化
ui->lineEdit->setAlignment(Qt::AlignHCenter); //输入框文本居中
ui->comboBox->setStyleSheet("QComboBox{"
"border-width: 2px;"
"border-style: solid;"
"border-radius:10px;"
"border-color: rgba(176, 100, 213,0);"
"background-color: rgba(176, 100, 213,0.5);"
"color: rgb(255, 255, 255);"
"}"
"QComboBox::drop-down{"
"border-width:0px;"
"border-style: solid;"
"border-radius:5px;"
"border-color: rgba(176, 100, 213,0);"
"background-color: rgba(176, 100, 213,0.5);"
"color: rgb(255, 255, 255);"
"}"
"QComboBox QAbstractItemView{"
"border-width:0px;"
"border-style: solid;"
"border-radius:5px;"
"border-color: rgba(176, 100, 213,0);"
"background-color: rgb(195,142,215);"
"color: rgb(255, 255, 255);"
"}");
ui->progressBar->setStyleSheet("QProgressBar{"
"font:9pt;"
"border-radius:5px;"
"text-align:center;"
"border:1px solid #E8EDF2;"
"background-color: rgb(179,222,255);"
"border-color: rgb(180, 180, 180);"
"}"
"QProgressBar:chunk{"
"border-radius:5px;"
"background-color:rgb(16,116,191);"
"}");
}
MyWidget::~MyWidget()
{
delete ui;
}
//右上角X,关闭游戏
void MyWidget ::closeEvent (QCloseEvent *ev)
{
int ret = QMessageBox::question(this,"提示","是否关闭窗口");
if(ret == QMessageBox::Yes) this->close();
else ev->ignore();
}
//page
//“开始游戏”从page转跳到page_2
void MyWidget::on_pushButton_clicked()
{
if(t==0)//未选择时间
{
QMessageBox::StandardButton reply;//警告
reply = QMessageBox::warning (this, "警告!", "未选择时间");
}
else//跳转page2
{
num_1=0;num_2=100; //范围初始值
ui->stackedWidget->setCurrentIndex(i=1); //跳转page2
randomInt = QRandomGenerator::global()->bounded(0,101);//获取一个随机数字作为炸弹
timer->start(1000); //计时器启动,1s为单位
ui->lineEdit->setFocus(); //一转跳,光标就在文本框上面
ui->label_4->setText(QString("%1-%2").arg(num_1).arg(num_2));//显示数字范围“0-100”
ui->progressBar->setValue(t); //进度条的初始值
//提示隐藏
ui->label_6->hide();
ui->label_7->hide();
ui->label_7->clear();
}
}
//退出游戏
void MyWidget::on_pushButton_2_clicked()
{
int ret = QMessageBox::question(this,"提示","是否退出游戏");
if(ret == QMessageBox::Yes) QApplication::quit();//立即终止应用程序
}
//设置时间
void MyWidget::on_comboBox_activated(const QString &arg1)
{
int value =arg1.toInt();//获取选择选项,并转型
t = value;
ui->progressBar->setRange(0,t);//进度条的范围
ui->progressBar->setValue(0); //进度条的初始值
}
//时间到了
void MyWidget::updateCountdowm()
{
int value = ui->progressBar->value(); //获取进度条的长度
if (value > 0) ui->progressBar->setValue(value - 1); //进度条不断减少
else
{ //倒计时结束
timer->stop(); //时间停止
QMessageBox::StandardButton reply; //提示
reply = QMessageBox::question (this, "时间已到", "是否重开一局游戏");
if (reply == QMessageBox::Yes) on_pushButton_clicked();//调用按钮点击事件,恢复初始值
else ui->stackedWidget->setCurrentIndex(i=0); //返回主页
}
}
//page_2
//返回主界面
void MyWidget::on_pushButton_16_clicked()
{
ui->stackedWidget->setCurrentIndex(i=0); //page2->page
timer->stop(); //停止倒计时
}
//按下数字按键
void MyWidget::play_0_9()
{
QObject*mySender=this->sender(); //获取所点击的按钮
QPushButton *Button=(QPushButton*)mySender; //mySender指针->QPushButton指针,赋值给 Button
if(Button)
{
QString Text=Button->text(); //获取按钮中的内容
ui->lineEdit->setText(ui->lineEdit->text()+Text);//将按钮文本内容追加到文本框中
if (Text.size()>2) //超出范围,直接清空并提示
{
ui->lineEdit->clear(); //清除文本框
//提示
ui->label_6->show();
u