The Item class
The object classes will all be kept in a single file called game_classes.py. Each class will be added to the bottom of the file and tested / demonstrated in the "if name == 'main':" block.
The Item class is the simplest class to design and will be used for the coin, the picnic, and the key. This is the class diagram for the Item class.
Item class |
---|
|
|
Here is the Python code for the item class.
Note that another property, collectable, has been added. This can be used to stop the player from taking objects that are not meant to be collected. E.g. the vending machine.
To test the code we add this at the bottom of the file:
Note that it is not un-Pythonic to access the class properties directly. This tutorial will only use class methods where necessary or to maintain consistency. For some classes the description will change depending on the state of the class instance. For example the Admin office and the Teacher's office have different descriptions if they are locked or unlocked so a get_desc() method will be required. To save trying to remember which classes have a get_desc() method and which do not, we will implement a get_desc() for all classes.