黄金点游戏
本周在第一次开发的基础上,添加了游戏的UI界面,为用户提供了图形化的输入、显示界面,以提高玩家的游戏体验。同时,解决了上一次开发留下的一些问题,如判断用户输入是否合法,循环进行游戏,能够输入中文姓名等。
游戏UI设计
开发环境:QT 5.9.8(MSVC 2017 64-bit)
界面设计:
游戏启动界面:
信息输入界面:
结果显示界面:
UI界面设计源代码
//输入界面源代码
#include "mainwindow2.h"
#include "ui_mainwindow2.h"
#include<QDebug>
MainWindow2::MainWindow2(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow2)
{
ui->setupUi(this);
this->setWindowTitle("GoldPoint Game");
connect(this,&MainWindow2::inputFinish_2,this,&MainWindow2::handleResult);//接收到输入完成信号后,处理结果
connect(this,SIGNAL(showWd3(Referee)),&wd3,SLOT(showResult(Referee)));
}
MainWindow2::~MainWindow2()
{
delete ui;
}
//每按下一次按钮,判断输入正确性,如果正确则记录每个玩家输入的名字和数字
void MainWindow2::on_pushButton_clicked()
{
if(ui->lineEdit_name->text().isEmpty()||ui->lineEdit_num->text().isEmpty()){
QMessageBox::warning(NULL,"waring","输入的姓名或者数字为空,请重新输入!",QMessageBox::Yes|QMessageBox::No,QMessageBox::Yes);
}
else{
QString temp_name=ui->lineEdit_name->text();
QString temp_num=ui->lineEdit_num->text();
int num=temp_num.toInt();
if(num<0||num>100){
QMessageBox::warning(NULL,"waring","请输入0-100之间的数字!",QMessageBox::Yes|QMessageBox::No,QMessageBox::Yes);
}
else{
system.setPlayerInfo(temp_name,temp_num,playerCount);
ui->lineEdit_name->clear();
ui->lineEdit_num->clear();
playerCount++;
if(playerCount==PlayerNum){
emit(inputFinish_2());
}
}
}
}
//得到本轮游戏结果并进行记录,跳转到显示结果界面
void MainWindow2::handleResult(){
system.calGoldpoint();
system.getResult();
system.allotScore();
system.record();
emit(showWd3(system));
this->close();
}
//新一轮游戏开始,进行初始化
void MainWindow2::wd2Show(){
playerCount=0; //游戏人数初始为0;
system.gameInit();
this->show();
}
//结果显示界面源代码
#include "mainwindow3.h"
#include "ui_mainwindow3.h"
MainWindow3::MainWindow3(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow3)
{
ui->setupUi(this);
this->setWindowTitle("GoldPoint Game");
connect(this,SIGNAL(gameContinue(Referee)),&wd4,SLOT(wd4Show(Referee)));
connect(&wd4,SIGNAL(showWd3(Referee)),this,SLOT(showResult(Referee)));
connect(this,SIGNAL(showRecord(int)),&wd5,SLOT(wd5Init(int)));
connect(&wd5,SIGNAL(gameContinue()),this,SLOT(on_pushButton_continue_clicked()));
}
MainWindow3::~MainWindow3()
{
delete ui;
}
//显示本轮游戏结果
void MainWindow3::showResult(Referee s){
this->show();
system=s;
QString str,str_playerName,str_num,str_winnerName,str_loserName,str_score;
int num;
double gp;
for(int i=0;i<PlayerNum;i++){
str=system.getName(i);
str_playerName.append(str);
str_playerName.append('\t');
num=system.getNum(i);
str=QString::number(num);
str_num.append(str);
str_num.append('\t');
num=system.getScore(i);
str=QString::number(num);
str_score.append(str);
str_score.append('\t');
}
gp=system.getGoldPoint();
str=QString::number(gp,10,3);
str_winnerName=system.getWinnerName();
str_loserName=system.getLoserName();
ui->label_playerName->setText(str_playerName);
ui->label_num->setText(str_num);
ui->label_GP->setText(str);
ui->label_winnerName->setText(str_winnerName);
ui->label_loserName->setText(str_loserName);
ui->label_score->setText(str_score);
}
//如果点击继续按钮,开始新一轮游戏
void MainWindow3::on_pushButton_continue_clicked()
{
emit(gameContinue(system));
this->hide();
wd4.show();
}
void MainWindow3::on_pushButton_quit_clicked()
{
this->close();
}
void MainWindow3::on_pushButton_record_clicked()
{
emit (showRecord(Referee::round));
this->hide();
}
后续开发目标
- 添加查询和显示历史游戏结果的功能。
- 连接数据库,将用户信息及游戏结果存放在数据库中。
- 增加人机对战模式,可由一名玩家单机与系统控制的多名玩家进行游戏。