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

Finishing the game

The last part we have to discuss is the scene's initialization. Set the initial values for all fields and the constructor, create the initPlayField() function that will set up all the items, and call that function in the constructor. First, we initialize the sky, trees, ground, and player item:

void MyScene::initPlayField(){    setSceneRect(0, 0, 500, 340);    m_sky = new BackgroundItem(QPixmap(":/sky"));    addItem(m_sky);    BackgroundItem *ground = new BackgroundItem(QPixmap(":/ground"));    addItem(ground);    ground->setPos(0, m_groundLevel);    m_trees = new BackgroundItem(QPixmap(":/trees"));    m_trees->setPos(0, m_groundLevel - m_trees->boundingRect().height());    addItem(m_trees);    m_grass = new BackgroundItem(QPixmap(":/grass")); m_grass->setPos(0,m_groundLevel ...

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