1 #include "timercontrolledgraphicspixmapobject.h"
2 #include <QGraphicsScene>
6 TimerControlledGraphicsPixmapObject::TimerControlledGraphicsPixmapObject(QPixmap pixmap, int speed, QGraphicsItem* parent) :
7 QObject(), QGraphicsPixmapItem(pixmap, parent)
11 connect(&timer_,SIGNAL(timeout()),this,SLOT(move()));
14 void TimerControlledGraphicsPixmapObject::startMoving()
19 void TimerControlledGraphicsPixmapObject::stopMoving()
24 void TimerControlledGraphicsPixmapObject::setSpeed(int speed)
26 timer_.setInterval(1000/speed); //converts from pixels in second to milliseconds per pixel
29 void TimerControlledGraphicsPixmapObject::move()
39 //calculate the new position
41 if (direction_ == E || direction_ == SE || direction_ == NE)
46 if (direction_ == W || direction_ == SW || direction_ == NW)
51 if (direction_ == S || direction_ == SE || direction_ == SW)
56 if (direction_ == N || direction_ == NE || direction_ == NW)
63 //Bound the item into the scene and change direction if hitting a boundary
64 //Only works if the old position is inside the boundaries
66 if (!scene()) //no movement if this item does not belong to a scene
69 QRect sceneRectangle = scene()->sceneRect().toRect();
71 if (newx < sceneRectangle.left() || newx > sceneRectangle.right()-40)
78 if (newy < sceneRectangle.top() || newy > sceneRectangle.bottom()-40)
81 return; //the old x and y values remain intact
85 //Set the new position
91 //check for collisions and handle them (up to subclassess to implement)
92 //return to the old position if requested
94 if (handleCollisions() == false)
103 void TimerControlledGraphicsPixmapObject::changeDirection()
105 qDebug () << "Supposed to change direction";
107 int direction = (qrand()%8);
108 qDebug() << direction;
149 bool TimerControlledGraphicsPixmapObject::handleCollisions()