forked from Sean-Bradley/Design-Patterns-In-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_character.py
57 lines (46 loc) · 1.52 KB
/
game_character.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"The Game Character whose state changes"
from memento import Memento
class GameCharacter():
"The Game Character whose state changes"
def __init__(self):
self._score = 0
self._inventory = set()
self._level = 0
self._location = {"x": 0, "y": 0, "z": 0}
@property
def score(self):
"A `getter` for the objects score"
return self._score
def register_kill(self):
"The character kills its enemies as it progesses"
self._score += 100
def add_inventory(self, item):
"The character finds objects in the game"
self._inventory.add(item)
def progress_to_next_level(self):
"The characer progresses to the next level"
self._level += 1
def move_forward(self, amount):
"The character moves around the environment"
self._location["z"] += amount
def __str__(self):
return(
f"Score: {self._score}, "
f"Level: {self._level}, "
f"Location: {self._location}\n"
f"Inventory: {self._inventory}\n"
)
@ property
def memento(self):
"A `getter` for the characters attributes as a Memento"
return Memento(
self._score,
self._inventory.copy(),
self._level,
self._location.copy())
@ memento.setter
def memento(self, memento):
self._score = memento.score
self._inventory = memento.inventory
self._level = memento.level
self._location = memento.location