125 lines
2.7 KiB
C++
125 lines
2.7 KiB
C++
#include "mainwindow.h"
|
|
#include "ui_mainwindow.h"
|
|
|
|
|
|
MainWindow::MainWindow(QWidget *parent)
|
|
: QMainWindow(parent), ui(new Ui::MainWindow)
|
|
{
|
|
qDebug() << "Load START!";
|
|
ui->setupUi(this);
|
|
//sndr = new UdpSender("127.0.0.1",10101);
|
|
//sndr->connectSend("hellow");
|
|
cellsize = 16;
|
|
destx = this->width() / cellsize;
|
|
desty = this->height() / cellsize / 2;
|
|
timer = new QTimer(this);
|
|
adp = new adpainter(this->width(),this->height(),cellsize);
|
|
adp->clear();
|
|
adp->drawgrid();
|
|
connect(timer, SIGNAL(timeout()), this, SLOT(ADrender()));
|
|
GameMain = new games(this->size(),QPoint(0,this->height()/2),QPoint(this->width()-cellsize,this->height()/2), cellsize);
|
|
aliens = new Aliens(GameMain);
|
|
aliens->AddAlien();
|
|
this->setGeometry(100,100,this->width(),this->height());
|
|
//qDebug() << "QT version" << qVersion();
|
|
//AnimAlien = 0;
|
|
timer->start(50);
|
|
qDebug() << "Load DONE!";
|
|
mousebt = 0;
|
|
}
|
|
|
|
|
|
void MainWindow::ADrender()
|
|
{
|
|
adp->drawAlien(aliens->alienAt(0));
|
|
this->repaint();
|
|
}
|
|
|
|
|
|
void MainWindow::paintEvent(QPaintEvent*)
|
|
{
|
|
QPainter painter;
|
|
painter.begin(this);
|
|
painter.drawPixmap(0,0,*adp->getPixmap());
|
|
painter.end();
|
|
}
|
|
|
|
|
|
void MainWindow::mouseMoveEvent (QMouseEvent *event)
|
|
{
|
|
if (mousebt == 1) ClearCell(event->pos());
|
|
if (mousebt == 2) SetCell(event->pos());
|
|
}
|
|
|
|
|
|
void MainWindow::mousePressEvent (QMouseEvent *event)
|
|
{
|
|
if (event->button() == Qt::RightButton) {
|
|
ClearCell(event->pos());
|
|
mousebt = 1;
|
|
return;
|
|
}
|
|
if(event->button() == Qt::LeftButton) {
|
|
SetCell(event->pos());
|
|
mousebt = 2;
|
|
return;
|
|
}
|
|
mousebt = 0;
|
|
}
|
|
|
|
|
|
void MainWindow::SetCell(QPoint pos)
|
|
{
|
|
QPoint cp;
|
|
QRect wdrect(0, 0, (this->width() / cellsize) * cellsize, (this->height() / cellsize) * cellsize);
|
|
if (wdrect.contains(pos)) {
|
|
cp.setX(pos.x()/cellsize);
|
|
cp.setY(pos.y()/cellsize);
|
|
//qDebug() << "click: " << cp.x() << ";" << cp.y();
|
|
GameMain->Cells[cp.x()][cp.y()] = - 1;
|
|
adp->drawcell(cp);
|
|
//aliens->clearAliens();
|
|
//adp->StepAlien=0;
|
|
//adp->AnimAlien=0;
|
|
if (!aliens->alienAt(0)->RecreatePath()) ClearCell(pos);
|
|
else adp->StepAlien=0;
|
|
}
|
|
}
|
|
|
|
|
|
void MainWindow::ClearCell(QPoint pos)
|
|
{
|
|
QPoint cp;
|
|
QRect wdrect(0, 0, (this->width() / cellsize) * cellsize, (this->height() / cellsize) * cellsize);
|
|
if (wdrect.contains(pos)) {
|
|
cp.setX(pos.x()/cellsize);
|
|
cp.setY(pos.y()/cellsize);
|
|
//qDebug() << "click: " << cp.x() << ";" << cp.y();
|
|
GameMain->Cells[cp.x()][cp.y()] = 0;
|
|
adp->clearcell(cp);
|
|
//aliens->clearAliens();
|
|
//aliens->AddAlien();
|
|
aliens->alienAt(0)->RecreatePath();
|
|
adp->StepAlien=0;
|
|
}
|
|
}
|
|
|
|
|
|
void MainWindow::keyPressEvent(QKeyEvent * e)
|
|
{
|
|
qDebug() << e->key();
|
|
switch(e->key()) {
|
|
case 69:
|
|
aliens->AddAlien();
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
MainWindow::~MainWindow()
|
|
{
|
|
delete ui;
|
|
delete aliens;
|
|
delete adp;
|
|
}
|