-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions.py
115 lines (94 loc) · 3.54 KB
/
actions.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
from colors import Colors
# new comment
class ActionException(Exception):
pass
class Action:
def __init__(self, player, tile):
self.player = player
self.tile = tile
def perform(self):
raise NotImplemented
class Move(Action):
def perform(self):
try:
self.player.move(self.tile)
item = getattr(self.tile, "item", None)
if item:
return ("You walk. There is a %s on the floor." % item,
Colors.LIGHT_GRAY)
else:
return ("You walk", Colors.LIGHT_GRAY)
except ActionException as ex:
return ("You can't go that way: %s" % str(ex), Colors.DARK_RED)
class Wait(Action):
def __init__(self, message="You wait", color=None):
self.message = message
self.color = color or Colors.LIGHT_GRAY
def perform(self):
return (self.message, self.color)
class Drop(Action):
def __init__(self, player, tile, item):
super().__init__(player, tile)
self.item = item
def perform(self):
try:
item = self.player.drop(self.tile, self.item)
return ("You drop the %s" % item, Colors.LIGHT_GRAY)
except ActionException as ex:
return ("You can't drop that: %s" % ex, Colors.DARK_RED)
class PickUp(Action):
def perform(self):
try:
item = self.player.pickup(self.tile)
return ("You pick up the %s" % item, Colors.LIGHT_GRAY)
except ActionException as ex:
return ("You can't pick that up: %s" % ex, Colors.DARK_RED)
class Open(Action):
def perform(self):
try:
self.player.open(self.tile)
return ("You open the %s" % self.tile,
Colors.LIGHT_GRAY)
except ActionException as ex:
return ("You can't open the %s: %s" % (self.tile, ex),
Colors.DARK_RED)
except AttributeError as ex:
return ("There's nothing to open",
Colors.DARK_RED)
class OpenTeleport(Open):
def perform(self):
try:
self.player.open(self.tile)
self.player.x = 100
self.player.y = 100
return ("You open the %s" % self.tile,
Colors.LIGHT_GRAY)
except ActionException as ex:
return ("You can't open the %s: %s" % (self.tile, ex),
Colors.DARK_RED)
except Exception as ex:
return ("There's nothing to open",
Colors.DARK_RED)
class Close(Action):
def perform(self):
try:
self.player.close(self.tile)
return ("You close the %s" % (self.tile),
Colors.LIGHT_GRAY)
except ActionException as ex:
return ("You can't close the %s: %s" % (self.tile, ex),
Colors.DARK_RED)
except Exception as ex:
return ("There's nothing to close", Colors.DARK_RED)
class Use(Action):
def __init__(self, player, tile, item):
super().__init__(player, tile)
self.item = item
def perform(self):
try:
result = self.player.use(self.tile, self.item)
return ("You use the %s on the %s: %s" % (self.item, self.tile, result or "nothing happens"), Colors.LIGHT_GRAY)
except ActionException as ex:
return ("You can't use the %s on a %s: %s" % (self.item, self.tile, ex), Colors.DARK_RED)
except Exception as ex:
return ("You don't know what to do with that", Colors.DARK_RED)