Python Development Course
Chapter
>
Level
Classes and Objects
Class members
Objective
Grab materials off the field and use them to create roads.
The roads leaning to the old shed are bare, grab the materials in the field and build up the roads. There are two types of roads, those made of “wood” and those made of “stone”, you can use a class to create objects to be used for building the roads. Each road requires 4 items of it’s respective type in order to build.
class road:
number = 4
def __init__(self, material):
self.material = material
The above code creates a class specifically made to create roads, the function _init_() is an internal class function designed to allow values to be passed directly into the class. For this function, you are able to set what type of material the road is when creating an object, like this: wood_road = road("wood") .
Grab all the materials in the field, and create a class named road , follow it up by creating objects detailing the type of road you wish to craft. Walk to the X marks and complete the roads by using the build() function and building the created object like this player.build(wood_road).