Time for action – creating an item for Benjamin

First we need a custom item class for Benjamin. We call the class Player and choose QGraphicsPixmapItem as the base class because Benjamin is a PNG image. In the item's Player class, we further create a property of integer type and call it m_direction. Its value signifies in which direction Benjamin walks—left or right—or if he stands still. Of course, we use a getter and setter function for this property. Since the header file is simple, let's have a look at the implementation right away (you will find the whole source code at the end of this book):

Player::Player(QGraphicsItem *parent) : QGraphicsPixmapItem(parent), m_direction(0) { setPixmap(QPixmap(":/elephant")); setTransformOriginPoint(boundingRect().center()); ...

Get Game Programming Using Qt 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.