From f8775657da3dd7f37ecd3bd4f8c53a17cee0aebe Mon Sep 17 00:00:00 2001 From: =?utf8?q?Heli=20Hyv=C3=A4ttinen?= Date: Sun, 5 Jun 2011 22:56:20 +0300 Subject: [PATCH] SeaScene with randomized ghost positions --- mainwindow.cpp | 10 +++++--- mainwindow.h | 3 ++- rotationcontrol2.pro | 14 +++++++++-- seascene.cpp | 59 +++++++++++++++++++++++++++++++++++++++++++++ seascene.h | 36 +++++++++++++++++++++++++++ timercontrolledtursas.cpp | 1 + 6 files changed, 116 insertions(+), 7 deletions(-) create mode 100644 seascene.cpp create mode 100644 seascene.h diff --git a/mainwindow.cpp b/mainwindow.cpp index c508710..d292082 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -13,13 +13,13 @@ MainWindow::MainWindow(QWidget *parent) { paused_ = false; - pScene_ = new QGraphicsScene (); + pScene_ = new SeaScene (); pView_ = new QGraphicsView (); QPixmap waves (":/pix/meri.png"); pScene_->setBackgroundBrush(QBrush(waves)); - pTursas_ = new OrientationControlledGraphicsPixmapObject(QPixmap(":/pix/tursas.png")); + pTursas_ = new OrientationControlledGraphicsPixmapObject(QPixmap(":/pix/laiva.png")); pScene_->addItem(pTursas_); pView_->setScene(pScene_); @@ -37,8 +37,8 @@ MainWindow::MainWindow(QWidget *parent) connect(pPauseAction,SIGNAL(triggered(bool)),this,SLOT(pause(bool))); menuBar()->addAction(pPauseAction); - QGraphicsPixmapItem * pGhost = pScene_->addPixmap(QPixmap(":/pix/aave.png")); - pGhost->setData(0,"ghost"); +// QGraphicsPixmapItem * pGhost = pScene_->addPixmap(QPixmap(":/pix/aave.png")); +// pGhost->setData(0,"ghost"); QGraphicsPixmapItem * pRock = pScene_->addPixmap(QPixmap(":/pix/kari.png")); QGraphicsPixmapItem * pRock2 = pScene_->addPixmap(QPixmap(":/pix/kari.png")); pRock->moveBy(40,40); @@ -77,6 +77,8 @@ void MainWindow::initializeBoundaries() pTursas_->startMoving(); qDebug() << "Initialized boundaries" << rectangle.left() << rectangle.right() << pView_->width(); + + pScene_->setupMap(5,5,5); } void MainWindow::pause(bool paused) diff --git a/mainwindow.h b/mainwindow.h index 58d2a4e..6a708e7 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -4,6 +4,7 @@ #include #include #include "orientationcontrolledgraphicspixmapobject.h" +#include "seascene.h" class MainWindow : public QMainWindow { @@ -19,7 +20,7 @@ public slots: private: -QGraphicsScene * pScene_; +SeaScene * pScene_; QGraphicsView * pView_; OrientationControlledGraphicsPixmapObject * pTursas_; bool paused_; diff --git a/rotationcontrol2.pro b/rotationcontrol2.pro index 4dac436..68072d7 100644 --- a/rotationcontrol2.pro +++ b/rotationcontrol2.pro @@ -13,11 +13,15 @@ TEMPLATE = app SOURCES += main.cpp\ mainwindow.cpp \ orientationcontrolledgraphicspixmapobject.cpp \ - timercontrolledtursas.cpp + timercontrolledtursas.cpp \ + seascene.cpp \ + seaview.cpp HEADERS += mainwindow.h \ orientationcontrolledgraphicspixmapobject.h \ - timercontrolledtursas.h + timercontrolledtursas.h \ + seascene.h \ + seaview.h CONFIG += mobility MOBILITY = sensors @@ -40,3 +44,9 @@ unix:!symbian { RESOURCES += \ orientationcontrol2pix.qrc + +maemo5 { + desktopfile.files = $${TARGET}.desktop + desktopfile.path = /usr/share/applications/hildon + INSTALLS += desktopfile +} diff --git a/seascene.cpp b/seascene.cpp new file mode 100644 index 0000000..eaaef92 --- /dev/null +++ b/seascene.cpp @@ -0,0 +1,59 @@ +#include "seascene.h" +#include +#include + +const QString ghostImageFilename_ = ":/pix/aave.png"; +const QString rockImageFilename_ =":/pix/kari.png"; +const QString octopusImageFilename_= ":/pix/tursas.png"; + + +SeaScene::SeaScene(QObject *parent) : + QGraphicsScene(parent) +{ + + +} + +void SeaScene::setupMap(int ghosts, int rocks, int octopuses) +{ + //empty the list of free slots + freeTiles_.clear(); + + //fill the list of free slots + + int numberOfXTiles = width() / 40; + int numberOfYTiles = height() /40; + + qDebug() << numberOfXTiles << " slots in x direction"; + qDebug() << numberOfYTiles << " slots in y rirection"; + + for (int i = 0; i < numberOfXTiles; i++ ) + { + for (int j = 0; j < numberOfYTiles; j++) + { + freeTiles_.append(QPointF(i*40,j*40)); + } + } + spreadGhosts(ghosts); +} + +void SeaScene::spreadGhosts(int ghosts) +{ + for (int i=0; i < ghosts; i++) + { + QPointF position = findRandomFreeSlot(); + + QGraphicsPixmapItem * pGhost = addPixmap(QPixmap(":/pix/aave.png")); + pGhost->setData(0,"ghost"); + pGhost->setPos(position); + } +} + +QPointF SeaScene::findRandomFreeSlot() +{ + int index = qrand()%freeTiles_.size(); + + qDebug() << index << " index"; + return freeTiles_.takeAt(index); + +} diff --git a/seascene.h b/seascene.h new file mode 100644 index 0000000..bfeb5cb --- /dev/null +++ b/seascene.h @@ -0,0 +1,36 @@ +#ifndef SEASCENE_H +#define SEASCENE_H + +#include + +class SeaScene : public QGraphicsScene +{ + Q_OBJECT +public: + explicit SeaScene(QObject *parent = 0); + +signals: + +public slots: + + void setupMap(int ghosts, int rocks, int octopuses); + + void spreadGhosts(int ghosts); + +protected: + + QPointF findRandomFreeSlot(); + + const QString ghostImageFilename_; + const QString rockImageFilename_; + const QString octopusImageFilename_; + const QStringList shipImageFilenames_; + + QList freeTiles_; + + + + +}; + +#endif // SEASCENE_H diff --git a/timercontrolledtursas.cpp b/timercontrolledtursas.cpp index 520b94c..317bab9 100644 --- a/timercontrolledtursas.cpp +++ b/timercontrolledtursas.cpp @@ -61,6 +61,7 @@ void TimerControlledTursas::move() //Bound the item into the scene and change direction if hitting a boundary + //Only works if the old position is inside the boundaries if (!scene()) //no movement if this item does not belong to a scene return; -- 1.7.9.5