Graphical representation of crops and animals

One of the questions our investigation list generated was:

  • How can we combine the functionality of a crop or animal with a graphical representation?

There are two possible solutions to this problem:

  1. Composition
  2. Multiple inheritance

Both of these solutions would result in a satisfactory outcome i.e. we would have a single class that could be used to instantiate objects that have functionality of our crops (or animals) and the functionality of a QGraphicsPixmapItem, which we can place in a QGraphicsScene.

In this situation we are going to use composition as multiple inheritance would add unnecessary complexity to our solution.

Representing the class structure

The diagram below represents the classes necessary to give us the required functionality for our graphical crops and animals:

Class diagram showing necessary composition

Field item class and its sub-classes

The above diagram shows the FieldItemGaphicsPixmapItem class which inherits from QGraphicsPixmapItem. The field item class is necessary as a base class which sets some general properties for the graphical representation of either a crop or animal. This means that these properties are inherited by the sub-classes CropGraphicsPixmapItem and AnimalGraphicsPixmapItem and if we need to change these properties in future we only need to do so in one place rather than several.

The sub-classes CropGraphicsPixmapItem and AnimalGraphicsPixmapItem are required as each has a different implementation of the update_status() method which depends specifically status of the class relating to a crop or an animal.

The video below demonstrates how to create these classes.


Task 1

Use the above video to create the graphical crop and animal item classes.