O'Reilly logo

Game Programming using Qt 5 Beginner's Guide - Second Edition by Lorenz Haas, Witold Wysota, Pavel Strakhov

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action - Moving the background

The scene will create a graphics item for each part of the background and store pointers to them in the m_skym_grass, and m_trees private fields. Now the question is how to move them at different speeds. The solution is quite simple—the slowest one, the sky, is the smallest image. The fastest background, the ground and the grass, are the largest images. Now when we take a look at the end of the movePlayer() function's slot, we see this:

qreal ratio = qreal(m_worldShift) / maxWorldShift;
applyParallax(ratio, m_sky);
applyParallax(ratio, m_grass);
applyParallax(ratio, m_trees);

The applyParallax() helper method contains the following code:

void MyScene::applyParallax(qreal ratio, QGraphicsItem* item) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required