#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QTime>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
Init();
}
Widget::~Widget()
{
delete ui;
}
void Widget::Init()
{
randNumber = 0;
text.clear();
flag = 0;
count = 0;
qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
randNumber = rand()%100;
connect(ui->btn0,SIGNAL(clicked()),this,SLOT(doProcess()));
connect(ui->btn1,SIGNAL(clicked()),this,SLOT(doProcess()));
connect(ui->btn2,SIGNAL(clicked()),this,SLOT(doProcess()));
connect(ui->btn3,SIGNAL(clicked()),this,SLOT(doProcess()));
connect(ui->btn4,SIGNAL(clicked()),this,SLOT(doProcess()));
connect(ui->btn5,SIGNAL(clicked()),this,SLOT(doProcess()));
connect(ui->btn6,SIGNAL(clicked()),this,SLOT(doProcess()));
connect(ui->btn7,SIGNAL(clicked()),this,SLOT(doProcess()));
connect(ui->btn8,SIGNAL(clicked()),this,SLOT(doProcess()));
connect(ui->btn9,SIGNAL(clicked()),this,SLOT(doProcess()));
}
void Widget::doProcess()
{
QString num;
QPushButton * btn = (QPushButton *)this ->sender();
num = btn->text();
text += num;
ui->lcdNumber->display(text.toInt());
if(1 == flag)
{
ui->textEdit->clear();
count = 0;
flag = 0;
}
if(text.length() < 2)
{
}
else
{
if(text == QString::number(randNumber))
{
++count;
ui->textEdit->append("输入正确");
ui->lineEdit->setText(QString::number(count));
randNumber = rand()%100;
flag = 1;
}
else if(text < QString::number(randNumber))
{
++count;
ui->textEdit->append("输入小了");
ui->lineEdit->setText(QString::number(count));
}
else
{
++count;
ui->textEdit->append("输入大了");
ui->lineEdit->setText(QString::number(count));
}
text.clear();
}
}
Qt编写的猜数字小游戏
最新推荐文章于 2022-12-06 17:29:18 发布