1 #include "timercontrolledtursas.h"
2 #include <QGraphicsScene>
6 TimerControlledTursas::TimerControlledTursas(QPixmap pixmap, int speed, QGraphicsItem* parent) :
7 QObject(), QGraphicsPixmapItem(pixmap, parent)
11 connect(&timer_,SIGNAL(timeout()),this,SLOT(move()));
14 void TimerControlledTursas::startMoving()
19 void TimerControlledTursas::stopMoving()
24 void TimerControlledTursas::setSpeed(int speed)
26 timer_.setInterval(1000/speed); //converts from pixels in second to milliseconds per pixel
29 void TimerControlledTursas::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 //If the new position would collide with anything, go back to the old position and change direction
93 QList<QGraphicsItem*> collidesList = collidingItems();
94 if (!collidesList.isEmpty())
104 void TimerControlledTursas::changeDirection()
106 qDebug () << "Supposed to change direction";
108 int direction = (qrand()%8);
109 qDebug() << direction;