1 class GameError(Exception):
5 def move_pos(direction, pos_yx):
8 elif direction == 'DOWN':
10 elif direction == 'RIGHT':
12 elif direction == 'LEFT':
20 self.map_size = (5, 5)
21 self.map_ = 'xxxxx' +\
27 Thing(self, 'human', [3, 3]),
28 Thing(self, 'monster', [1, 1])
31 self.player = self.things[self.player_i]
33 def proceed_to_next_player_turn(self):
34 """Run game world turns until player can decide their next step.
36 Iterates through all non-player things, on each step
37 furthering them in their tasks (and letting them decide new
38 ones if they finish). The iteration order is: first all things
39 that come after the player in the world things list, then
40 (after incrementing the world turn) all that come before the
41 player; then the player's .proceed() is run, and if it does
42 not finish his task, the loop starts at the beginning. Once
43 the player's task is finished, the loop breaks.
46 for thing in self.things[self.player_i+1:]:
49 for thing in self.things[:self.player_i]:
51 self.player.proceed(is_AI=False)
52 if self.player.task is None:
57 def __init__(self, thing, name, args=(), kwargs={}):
65 if self.name == 'move':
66 if len(self.args) > 0:
67 direction = self.args[0]
69 direction = self.kwargs['direction']
70 test_pos = self.thing.position[:]
71 move_pos(direction, test_pos)
72 if test_pos[0] < 0 or test_pos[1] < 0 or \
73 test_pos[0] >= self.thing.world.map_size[0] or \
74 test_pos[1] >= self.thing.world.map_size[1]:
75 raise GameError('would move outside map bounds')
76 pos_i = test_pos[0] * self.thing.world.map_size[1] + test_pos[1]
77 map_tile = self.thing.world.map_[pos_i]
79 raise GameError('would move into illegal terrain')
84 def __init__(self, world, type_, position):
87 self.position = position
88 self.task = Task(self, 'wait')
93 def task_move(self, direction):
94 move_pos(direction, self.position)
96 def decide_task(self):
97 if self.position[1] > 1:
98 self.set_task('move', 'LEFT')
99 elif self.position[1] < 3:
100 self.set_task('move', 'RIGHT')
102 self.set_task('wait')
104 def set_task(self, task, *args, **kwargs):
105 self.task = Task(self, task, args, kwargs)
108 def proceed(self, is_AI=True):
109 """Further the thing in its tasks.
111 Decrements .task.todo; if it thus falls to <= 0, enacts method whose
112 name is 'task_' + self.task.name and sets .task = None. If is_AI, calls
113 .decide_task to decide a self.task.
116 if self.task.todo <= 0:
117 task = getattr(self, 'task_' + self.task.name)
118 task(*self.task.args, **self.task.kwargs)
120 if is_AI and self.task is None: