the first source
This commit is contained in:
100
mainwindow.cpp
Normal file
100
mainwindow.cpp
Normal file
@@ -0,0 +1,100 @@
|
||||
#include "mainwindow.h"
|
||||
#include "ui_mainwindow.h"
|
||||
|
||||
|
||||
MainWindow::MainWindow(QWidget *parent)
|
||||
: QMainWindow(parent), ui(new Ui::MainWindow)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
cellsize = 16;
|
||||
destx = this->width() / cellsize / 2;
|
||||
desty = this->height() / cellsize;
|
||||
QTimer *timer = new QTimer(this);
|
||||
adp = new adpainter(this->width(),this->height(),cellsize);
|
||||
adp->clear();
|
||||
adp->drawgrid();
|
||||
connect(timer, SIGNAL(timeout()), this, SLOT(ADrender()));
|
||||
aliens = new Aliens(destx, desty, this->width() / cellsize ,this->height() / cellsize, cellsize);
|
||||
aliens->AddAlien();
|
||||
this->setGeometry(100,100,this->width(),this->height());
|
||||
//qDebug() << "QT version" << qVersion();
|
||||
//AnimAlien = 0;
|
||||
timer->start(50);
|
||||
}
|
||||
|
||||
|
||||
void MainWindow::ADrender()
|
||||
{
|
||||
adp->drawAliens(aliens->curAliens[0].path, &aliens->curAliens[0].Position);
|
||||
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->x(),event->y());
|
||||
else SetCell(event->x(),event->y());
|
||||
}
|
||||
|
||||
|
||||
void MainWindow::mousePressEvent ( QMouseEvent * event )
|
||||
{
|
||||
if (event->button() == Qt::RightButton) {
|
||||
ClearCell(event->x(),event->y());
|
||||
mousebt = 1;
|
||||
} else {
|
||||
SetCell(event->x(),event->y());
|
||||
mousebt = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void MainWindow::SetCell(int x, int y)
|
||||
{
|
||||
QPoint cp;
|
||||
QRect wdrect(0, 0, (this->width() / cellsize) * cellsize, (this->height() / cellsize) * cellsize);
|
||||
if (wdrect.contains(x,y)) {
|
||||
cp.setX((int) x / cellsize);
|
||||
cp.setY((int) y / cellsize);
|
||||
//qDebug() << "click: " << cp.x() << ";" << cp.y();
|
||||
aliens->Cells[cp.x()][cp.y()] = - 1;
|
||||
adp->drawcell(cp);
|
||||
aliens->clearAliens();
|
||||
adp->StepAlien=0;
|
||||
adp->AnimAlien=0;
|
||||
if (!aliens->AddAlien()) ClearCell(x,y);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void MainWindow::ClearCell(int x, int y)
|
||||
{
|
||||
QPoint cp;
|
||||
QRect wdrect(0, 0, (this->width() / cellsize) * cellsize, (this->height() / cellsize) * cellsize);
|
||||
if (wdrect.contains(x,y)) {
|
||||
cp.setX((int) x / cellsize);
|
||||
cp.setY((int) y / cellsize);
|
||||
//qDebug() << "click: " << cp.x() << ";" << cp.y();
|
||||
aliens->Cells[cp.x()][cp.y()] = 0;
|
||||
adp->clearcell(cp);
|
||||
aliens->clearAliens();
|
||||
aliens->AddAlien();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
MainWindow::~MainWindow()
|
||||
{
|
||||
delete ui;
|
||||
delete aliens;
|
||||
delete adp;
|
||||
}
|
||||
Reference in New Issue
Block a user