0% found this document useful (0 votes)
63 views6 pages

Properties: Properties Provide A Way of Customizing Access To Instance Attributes

Properties allow customizing attribute access in classes. They are created using the property decorator on a method, so that when the attribute is accessed, the method is called instead. Properties can make attributes read-only or can include setter and getter methods. Classes represent game objects in a text adventure game. Examining an object returns its description. Combat is added by giving goblin objects a health attribute and hit method that reduces health, updating the description on damage.

Uploaded by

Arya Bhatt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views6 pages

Properties: Properties Provide A Way of Customizing Access To Instance Attributes

Properties allow customizing attribute access in classes. They are created using the property decorator on a method, so that when the attribute is accessed, the method is called instead. Properties can make attributes read-only or can include setter and getter methods. Classes represent game objects in a text adventure game. Examining an object returns its description. Combat is added by giving goblin objects a health attribute and hit method that reduces health, updating the description on damage.

Uploaded by

Arya Bhatt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Properties

Properties provide a way of customizing access to instance attributes.


They are created by putting the property decorator above a method, which means when the
instance attribute with the same name as the method is accessed, the method will be called
instead.
One common use of a property is to make an attribute read-only.
Example:
class Pizza:
def __init__(self, toppings):
self.toppings = toppings

@property
def pineapple_allowed(self):
return False

pizza = Pizza(["cheese", "tomato"])


print(pizza.pineapple_allowed)
pizza.pineapple_allowed = TrueTry It Yourself

Result: >>>
False

AttributeError: can't set attribute


>>>
Tap Try It Yourself to play around with the code!

Properties

Properties can also be set by defining setter/getter functions.


The setter function sets the corresponding property's value.
The getter gets the value.
To define a setter, you need to use a decorator of the same name as the property, followed by a
dot and the setter keyword.
The same applies to defining getter functions.

Example:
class Pizza:
def __init__(self, toppings):
self.toppings = toppings
self._pineapple_allowed = False

@property
def pineapple_allowed(self):
return self._pineapple_allowed

@pineapple_allowed.setter
def pineapple_allowed(self, value):
if value:
password = input("Enter the password: ")
if password == "Sw0rdf1sh!":
self._pineapple_allowed = value
else:
raise ValueError("Alert! Intruder!")

pizza = Pizza(["cheese", "tomato"])


print(pizza.pineapple_allowed)
pizza.pineapple_allowed = True
print(pizza.pineapple_allowed)Try It Yourself

Result:>>>
False
Enter the password: Sw0rdf1sh!
True

A Simple Game

Object-orientation is very useful when managing different objects and their relations. That is
especially useful when you are developing games with different characters and features.

Let's look at an example project that shows how classes are used in game development.
The game to be developed is an old fashioned text-based adventure game.
Below is the function handling input and simple parsing.def get_input():
command = input(": ").split()
verb_word = command[0]
if verb_word in verb_dict:
verb = verb_dict[verb_word]
else:
print("Unknown verb {}". format(verb_word))
return
if len(command) >= 2:
noun_word = command[1]
print (verb(noun_word))
else:
print(verb("nothing"))

def say(noun):
return 'You said "{}"'.format(noun)

verb_dict = {
"say": say,
}

while True:
get_input()
Result: >>>
: say Hello!
You said "Hello!"
: say Goodbye!
You said "Goodbye!"

: test
Unknown verb test
The code above takes input from the user, and tries to match the first word with a command in
verb_dict. If a match is found, the corresponding function is called.

A Simple Game

The next step is to use classes to represent game objects.class GameObject:


class_name = ""
desc = ""
objects = {}

def __init__(self, name):


self.name = name
GameObject.objects[self.class_name] = self

def get_desc(self):
return self.class_name + "\n" + self.desc

class Goblin(GameObject):
class_name = "goblin"
desc = "A foul creature"

goblin = Goblin("Gobbly")

def examine(noun):
if noun in GameObject.objects:
return GameObject.objects[noun].get_desc()
else:
return "There is no {} here.".format(noun)
We created a Goblin class, which inherits from the GameObjects class.
We also created a new function examine, which returns the objects description.
Now we can add a new "examine" verb to our dictionary and try it out! verb_dict = {
"say": say,
"examine": examine,
}
Combine this code with the one in our previous example, and run the program.>>>
: say Hello!
You said "Hello!"

: examine goblin
goblin
A foul creature

: examine elf
There is no elf here.
:
Combine this code with the one in our previous example, and run the program.

A Simple Game

This code adds more detail to the Goblin class and allows you to fight goblins.class
Goblin(GameObject):
def __init__(self, name):
self.class_name = "goblin"
self.health = 3
self._desc = " A foul creature"
super().__init__(name)

@property
def desc(self):
if self.health >=3:
return self._desc
elif self.health == 2:
health_line = "It has a wound on its knee."
elif self.health == 1:
health_line = "Its left arm has been cut off!"
elif self.health <= 0:
health_line = "It is dead."
return self._desc + "\n" + health_line

@desc.setter
def desc(self, value):
self._desc = value

def hit(noun):
if noun in GameObject.objects:
thing = GameObject.objects[noun]
if type(thing) == Goblin:
thing.health = thing.health - 1
if thing.health <= 0:
msg = "You killed the goblin!"
else:
msg = "You hit the {}".format(thing.class_name)
else:
msg ="There is no {} here.".format(noun)
return msg
Result:>>>
: hit goblin
You hit the goblin

: examine goblin
goblin
A foul creature
It has a wound on its knee.

: hit goblin
You hit the goblin

: hit goblin
You killed the goblin!

: examine goblin
A goblin

goblin
A foul creature
It is dead.
:
This was just a simple sample.
You could create different classes (e.g., elves, orcs, humans), fight them, make them fight each
other, and so on.

You might also like