Skip to content

Commit

Permalink
Awesomeness
Browse files Browse the repository at this point in the history
  • Loading branch information
gja committed Feb 2, 2009
1 parent 451c05b commit 2187a2e
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 8 deletions.
74 changes: 66 additions & 8 deletions game.cpp
Expand Up @@ -4,8 +4,11 @@
#include <QDebug>
#include <QMouseEvent>
#include <QGraphicsLineItem>
#include <QPixmap>
#include <QGraphicsPixmapItem>
#include <QList>

GoferSmash::GoferSmash(QWidget *parent): QGraphicsView(parent), scene(NULL), board(NULL), deleteQueue(NULL)
GoferSmash::GoferSmash(QWidget *parent): QGraphicsView(parent), scene(NULL), board(NULL), deleteQueue(NULL), smash(NULL), state(SmashHidden)
{
// Create A Blank Scene
scene = new QGraphicsScene();
Expand All @@ -17,13 +20,21 @@ GoferSmash::GoferSmash(QWidget *parent): QGraphicsView(parent), scene(NULL), boa
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

initializeGameBoard();

connect(&timer, SIGNAL(timeout()), this, SLOT(timeout()));
timer.start(500);
}

void GoferSmash::mousePressEvent(QMouseEvent *event)
{
Smash *smash = selectSmash(event->pos());
connect(smash, SIGNAL(deleteMe(Smash *)), this, SLOT(deleteOldSmash(Smash *)));
smash->start();
if (! smash->isUnderMouse() )
return;

state = SmashInProgress;

Smash *s = selectSmash(smash->pos().toPoint() + QPoint( width() / 6, height() / 6));
connect(s, SIGNAL(deleteMe(Smash *)), this, SLOT(deleteOldSmash(Smash *)));
s->start();
}

GoferSmash::~GoferSmash()
Expand All @@ -33,10 +44,25 @@ GoferSmash::~GoferSmash()

void GoferSmash::initializeGameBoard()
{
int x = size().width() / 3;
int y = size().height() / 3;
int w[3], h[3];
w[0] = 0;
w[1] = x + 1;
w[2] = 2*x + 2;
h[0] = 0;
h[1] = y + 1;
h[2] = 2*y + 2;

points.clear();
for (x = 0; x < 3; x++)
for (y = 0; y < 3; y++)
points<<QPoint(w[x], h[y]);

qDebug()<<"Initializing Board";

int x = size().width();
int y = size().height();
x = size().width();
y = size().height();

int x_3 = x / 3;
int y_3 = y / 3;
Expand All @@ -52,6 +78,15 @@ void GoferSmash::initializeGameBoard()
new QGraphicsLineItem( 0, 2 * y_3 + 2, x, 2 * y_3 + 2, board);
scene->addItem(board);

if (smash)
delete smash;

QPixmap t = QPixmap("data/smash.jpg").scaled(size() / 3);
smash = new QGraphicsPixmapItem(t);
smash->hide();
smash->setZValue(-1.0f);
scene->addItem(smash);

scene->update();
}

Expand All @@ -61,10 +96,33 @@ void GoferSmash::resizeEvent(QResizeEvent *event)
initializeGameBoard();
}

void GoferSmash::deleteOldSmash(Smash *smash)
void GoferSmash::deleteOldSmash(Smash *s)
{
if (deleteQueue != NULL)
delete deleteQueue;

deleteQueue = smash;
deleteQueue = s;

smash->hide();
state = SmashHidden;
}

void GoferSmash::timeout()
{
int x, y;
QPoint point;

switch (state) {
case SmashVisible:
smash->hide();
break;
case SmashHidden:
point = points[rand() % 9];
smash->setPos(point);
smash->show();
break;
case SmashInProgress:
break;
}

}
21 changes: 21 additions & 0 deletions game.h
Expand Up @@ -3,9 +3,12 @@

#include <QGraphicsView>
#include <QPoint>
#include <QList>
#include <QTimer>

class QGraphicsScene;
class QGraphicsItemGroup;
class QGraphicsPixmapItem;
class Smash;

class GoferSmash : public QGraphicsView {
Expand All @@ -27,6 +30,21 @@ class GoferSmash : public QGraphicsView {
// Selects a Smash, and Creates it
Smash *selectSmash(const QPoint &point);

// The Photo to Smash
QGraphicsPixmapItem *smash;

enum State {
SmashHidden = 0,
SmashVisible,
SmashInProgress
};
State state;

// List of all the Points
QList <QPoint> points;

QTimer timer;

public:
// Constructor/Destructor
GoferSmash(QWidget *parent = NULL);
Expand All @@ -40,6 +58,9 @@ class GoferSmash : public QGraphicsView {

// Deletes an old smash item
void deleteOldSmash(Smash *);

// Timeout, either show or hide
void timeout();
};

#endif

0 comments on commit 2187a2e

Please sign in to comment.