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 ...

Get Game Programming using Qt 5 Beginner's Guide - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.