#include "selectscene.h"
#include "ui_selectscene.h"
#include <QPushButton>
#include "startscene.h"
#include"mymainwindow.h"
#include"mypushbutton.h"
#include <QString>
#include <QMessageBox>
static int rx,ry,bx,by;
SelectScene::SelectScene(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::SelectScene)
{
ui->setupUi(this);
QPushButton *btnBack = new QPushButton("back",this);
btnBack->resize(100,40);
connect(btnBack,&QPushButton::clicked,this,&SelectScene::backBtnClicked);
btnBack->move((this->width()-btnBack->width()),
(this->height()-btnBack->height()));
scene=new QGraphicsScene(this);
view->resize(1123,543);
view->setScene(scene);
map=new QPixmapItem(":/res/map.png",scene);
scene->setSceneRect(0,0,1121,541);
view->show();
QPushButton *action= new QPushButton("推演结果",this);
action->resize(200,80);
connect(action,&QPushButton::clicked,this,&SelectScene::opendialog);
action->move((this->width()/2-100),
(this->height()*3/4-100));
QPushButton *allmove= new QPushButton("开战",this);
connect(allmove,&QPushButton::clicked,this,&SelectScene::itemsmove);
allmove->resize(200,80);
allmove->move((this->width()/2-100),
(this->height()*3/4));
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), SLOT(time_update()));
timer->start(1000); //1s执行一次,定时器
}
void SelectScene::mousePressEvent(QMouseEvent* event)//通过点击部署兵力
{
//红方
if(ui->comboBox->currentText()=="指挥部 1座" && h<1)
{
rx = event->x();
ry = event->y();//获取点击的坐标
redhome[h] = new QPixmapItem(":/res/house.png",this->scene);
redhome[h]->setPos(rx,ry);
h++;
}
if(ui->comboBox->currentText()=="飞机 6架" && p<6 && h!=0)
{
int x = event->x();
int y = event->y();//获取点击的坐标
planes[p] = new QPixmapItem(":/res/plane.png",this->scene);
planes[p]->setPos(x,y);
p++;
}
if(ui->comboBox->currentText()=="坦克 10辆" && t<10 && h!=0)
{
int x = event->x();
int y = event->y();//获取点击的坐标
tanks[t] = new QPixmapItem(":/res/tank.png",this->scene);
tanks[t]->setPos(x,y);
t++;
}
//蓝方
if(ui->comboBox_2->currentText()=="指挥部 1座" && bh<1)
{
bx = event->x();
by = event->y();//获取点击的坐标
bluehome[bh] = new QPixmapItem(":/res/bhome.png",this->scene);
bluehome[bh]->setPos(bx,by);
bh++;
}
if(ui->comboBox_2->currentText()=="飞机 6架" && bp<6 && bh!=0)
{
int x = event->x();
int y = event->y();//获取点击的坐标
bplanes[bp] = new QPixmapItem(":/res/bplane.png",this->scene);
bplanes[bp]->setPos(x,y);
bp++;
}
if(ui->comboBox_2->currentText()=="坦克 10辆" && bt<10 && bh!=0)
{
int x = event->x();
int y = event->y();//获取点击的坐标
btanks[bt] = new QPixmapItem(":/res/btank.png",this->scene);
btanks[bt]->setPos(x,y);
bt++;
}
}
void SelectScene::time_update() //显示系统时间的功能
{
QDateTime time = QDateTime::currentDateTime();
QString str = time.toString("yyyy-MM-dd hh:mm:ss ddd");
ui->time->setText(str);
}
void SelectScene::opendialog() //推演结果对话框
{
QMessageBox result;
if((p+t)>(bp+bt))
{
result.setText("经过激烈的交战,红方巧妙的利用了地形优势,获得胜利!");
result.exec();
}
if((p+t)<(bp+bt))
{
result.setText("经过激烈的交战,蓝方利用了自身兵力强大的优势,获得胜利!");
result.exec();
}
if((p+t)==(bp+bt))
{
result.setText("经过激烈的交战,双方打得难解难分,两败俱伤,平局");
result.exec();
}
}
void SelectScene::itemsmove()
{
for (int i=0;i<p ;i++ ) {
planes[i]->setPos(bx,by-i*10);
}
for (int i=0;i<t;i++ ) {
tanks[i]->setPos(bx-i*10,by);
}
for (int i=0;i<bp ;i++ ) {
bplanes[i]->setPos(rx,ry-10*i);
}
for (int i=0;i<bt;i++ ) {
btanks[i]->setPos(rx+i*10,ry);
}
SelectScene::Sleep(1000);
QSound *boomsound=new QSound(":/res/boom.wav",this);
boomsound->play();//爆炸音效
if((p+t)>(bp+bt))
{
beffect = new QPixmapItem(":/res/ExplosionEffect2.png",this->scene);
beffect->setPos(bx-80,by-80);
}
if((p+t)<(bp+bt))
{
reffect= new QPixmapItem(":/res/ExplosionEffect1.png",this->scene);
reffect->setPos(rx-80,ry-80);
}
if((p+t)==(bp+bt))
{
reffect= new QPixmapItem(":/res/ExplosionEffect1.png",this->scene);
reffect->setPos(rx-80,ry-80);
beffect = new QPixmapItem(":/res/ExplosionEffect2.png",this->scene);
beffect->setPos(bx-80,by-80);
}
}
void SelectScene::Sleep(int msec)
{
QTime dieTime = QTime::currentTime().addMSecs(msec);
while( QTime::currentTime() < dieTime )
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}
SelectScene::~SelectScene()
{
delete ui;
}
评论0