Dear forum, Im writing a pong game that uses a timer to call advance on the graphicsscene to show the animation on the screen, somehow it doesnt work. I would really appreciate your help. here the code...
main.CPP :
QTimer timer;
QObject::connect(&timer, SIGNAL(timeout()), &scene, SLOT(advance()));
timer.start(1000 / 33);
return app.exec();
gameworld.H :
protected:
void drawBackground(QPainter *painter, const QRectF &rect);
virtual void keyPressEvent(QKeyEvent *event);
virtual void keyReleaseEvent(QKeyEvent *event);
void advance(int phase);
GameWorld.CPP :
void GameWorld::advance(int phase) {
printf("hello");
QPointF direction = ball.getDirection();
if (phase == 0) {
QRectF rect = sceneRect();
QPointF point = ball.pos();
//if (!collidingItems(Qt::IntersectsItemBoundingRect).i sEmpty())
// ball.setDirection(-direction.x(), direction.y());
if (point.y() <= rect.top() + 20) {
ball.setDirection(direction.x(), -direction.y());
}
if (point.y() >= rect.bottom() - 20) {
ball.setDirection(direction.x(), -direction.y());
}
if (point.x() <= rect.left() + 20) {
ball.setDirection(-direction.x(), direction.y());
}
if (point.x() >= rect.right() - 20) {
ball.setDirection(-direction.x(), direction.y());
}
} else
ball.moveBy(direction.x(), direction.y());
}
Bookmarks