1 class GameError(Exception):
5 def move_pos(direction, pos_yx):
8 elif direction == 'DOWN':
10 elif direction == 'RIGHT':
12 elif direction == 'LEFT':
18 def __init__(self, thing, name, args=(), kwargs={}):
26 if self.name == 'move':
27 if len(self.args) > 0:
28 direction = self.args[0]
30 direction = self.kwargs['direction']
31 test_pos = self.thing.position[:]
32 move_pos(direction, test_pos)
33 if test_pos[0] < 0 or test_pos[1] < 0 or \
34 test_pos[0] >= self.thing.world.map_size[0] or \
35 test_pos[1] >= self.thing.world.map_size[1]:
36 raise GameError('would move outside map bounds')
37 pos_i = test_pos[0] * self.thing.world.map_size[1] + test_pos[1]
38 map_tile = self.thing.world.map_[pos_i]
40 raise GameError('would move into illegal terrain')
45 def __init__(self, world, type_, position):
48 self.position = position
49 self.task = Task(self, 'wait')
54 def task_move(self, direction):
55 move_pos(direction, self.position)
57 def decide_task(self):
58 if self.position[1] > 1:
59 self.set_task('move', 'LEFT')
60 elif self.position[1] < 3:
61 self.set_task('move', 'RIGHT')
65 def set_task(self, task, *args, **kwargs):
66 self.task = Task(self, task, args, kwargs)
69 def proceed(self, is_AI=True):
70 """Further the thing in its tasks.
72 Decrements .task.todo; if it thus falls to <= 0, enacts method whose
73 name is 'task_' + self.task.name and sets .task = None. If is_AI, calls
74 .decide_task to decide a self.task.
77 if self.task.todo <= 0:
78 task = getattr(self, 'task_' + self.task.name)
79 task(*self.task.args, **self.task.kwargs)
81 if is_AI and self.task is None:
89 self.map_size = (5, 5)
90 self.map_ = 'xxxxx' +\
96 Thing(self, 'human', [3, 3]),
97 Thing(self, 'monster', [1, 1])
100 self.player = self.things[self.player_i]