//
#include "gamewidget.h"
-#include <unistd.h> // for usleep()
+#include <unistd.h> // for usleep()
#include "gameboard.h"
-GameWidget::GameWidget(QWidget * parent/*= 0*/): QWidget(parent)//,
+GameWidget::GameWidget(QWidget * parent/*= 0*/): QWidget(parent),
+ level(1), gameBoard(new GameBoard(level)),
+ animating(false), boxMoving(false)
// score(new QLabel)
{
// score->setTextFormat(Qt::PlainText);
setFixedSize(580, 520);
- gameBoard = new GameBoard(1);
+// gameBoard = new GameBoard(1);
}
// QRect rcUpdate = dc.m_ps.rcPaint; // Update rect...
// QRect rcUpdate = QRect(0, 0, 580, 520); // Update rect?
- int maxLength = 60;
+ maxLength = 60;
int ptr = 0;
for(int y=0; y<gameBoard->height; y++)
}
else if (tile == GTBox)
{
- painter.setBrush(QBrush(Qt::red));
- painter.drawRect(x * maxLength, y * maxLength, maxLength, maxLength);
+ if (!(boxMoving && (movingBoxPositionX == x) && (movingBoxPositionY == y)))
+ {
+ painter.setBrush(QBrush(Qt::red));
+ painter.drawRect(x * maxLength, y * maxLength, maxLength, maxLength);
+ }
}
else if (tile == GTBoxSpot)
{
}
else if (tile == (GTBox | GTBoxSpot))
{
- painter.setBrush(QBrush(Qt::green));
- painter.drawRect(x * maxLength, y * maxLength, maxLength, maxLength);
+ if (!(boxMoving && (movingBoxPositionX == x) && (movingBoxPositionY == y)))
+ {
+ painter.setBrush(QBrush(Qt::green));
+ painter.drawRect(x * maxLength, y * maxLength, maxLength, maxLength);
+ }
+ else
+ {
+ painter.setBrush(QBrush(Qt::magenta));
+ painter.drawRect((x * maxLength) + 20, (y * maxLength) + 20, maxLength - 40, maxLength - 40);
+ }
}
- if ((gameBoard->playerX == x) && (gameBoard->playerY == y))
+ if ((gameBoard->playerX == x) && (gameBoard->playerY == y) && !animating)
{
painter.setBrush(QBrush(Qt::yellow));
painter.drawEllipse((x * maxLength) + 10, (y * maxLength) + 10, maxLength - 20, maxLength - 20);
-// painter.drawRect(x * maxLength, y * maxLength, maxLength, maxLength);
}
}
+
+ if (animating)
+ {
+ painter.setBrush(QBrush(Qt::yellow));
+ painter.drawEllipse(playerX + 10, playerY + 10, maxLength - 20, maxLength - 20);
+ }
+
+ if (boxMoving)
+ {
+ painter.setBrush(QBrush(Qt::red));
+ painter.drawRect(boxX, boxY, maxLength, maxLength);
+ }
}
void GameWidget::keyPressEvent(QKeyEvent * event)
{
int key = event->key();
+ float deltaX = 0, deltaY = 0;
+ float px = (float)(gameBoard->playerX * maxLength);
+ float py = (float)(gameBoard->playerY * maxLength);
+ float bx = px, by = py;
if (key == Qt::Key_Up)
{
- if (gameBoard->MovePlayerN() == PMInvalid)
+ boxMoving = gameBoard->IsBoxNOfPlayer();
+ int moveType = gameBoard->MovePlayerN();
+
+ if (moveType == PMInvalid)
return;
+
+ deltaY = -1.0;
}
else if (key == Qt::Key_Down)
{
+ boxMoving = gameBoard->IsBoxSOfPlayer();
+
if (gameBoard->MovePlayerS() == PMInvalid)
return;
+
+ deltaY = +1.0;
}
else if (key == Qt::Key_Left)
{
+ boxMoving = gameBoard->IsBoxWOfPlayer();
+
if (gameBoard->MovePlayerW() == PMInvalid)
return;
+
+ deltaX = -1.0;
}
else if (key == Qt::Key_Right)
{
+ boxMoving = gameBoard->IsBoxEOfPlayer();
+
if (gameBoard->MovePlayerE() == PMInvalid)
return;
+
+ deltaX = +1.0;
}
else
return;
+ animating = true;
+ update();
+
+ if (boxMoving)
+ {
+ movingBoxPositionX = gameBoard->playerX + (int)deltaX;
+ movingBoxPositionY = gameBoard->playerY + (int)deltaY;
+ bx += deltaX * (float)maxLength;
+ by += deltaY * (float)maxLength;
+ }
+
+ int steps = 15;
+ float stepSize = (float)maxLength / (float)steps;
+ deltaX *= stepSize, deltaY *= stepSize;
+
+ for(int i=0; i<steps; i++)
+ {
+ px += deltaX;
+ py += deltaY;
+ playerX = (int)px;
+ playerY = (int)py;
+ bx += deltaX;
+ by += deltaY;
+ boxX = (int)bx;
+ boxY = (int)by;
+ repaint();
+ Pause(3);
+ }
+
+ animating = boxMoving = false;
+
// Only update if a key we recognize has been pressed!
update();
m_nHiScore = m_nScore;
}
}
+#endif
//
; // Do nothing...
#endif
}
-#endif