map_size = (5, 5)
terrain_map = ' ' * 25
position = (0, 0)
+ things = []
+
+ class Thing:
+ def __init__(self, position, symbol):
+ self.position = position
+ self.symbol = symbol
def draw_map(self):
"""Draw map view from .map_size, .terrain_map, .position."""
whole_map = []
for c in self.terrain_map:
whole_map += [c]
- pos_i = self.position[0] * (self.map_size[1] + 1) + self.position[1]
- whole_map[pos_i] = '@'
+ for t in self.things:
+ pos_i = t.position[0] * (self.map_size[1] + 1) + t.position[1]
+ whole_map[pos_i] = t.symbol
self.set_text(''.join(whole_map))
def get_yx(self, yx_string):
self.terrain_map = terrain_map
self.draw_map()
- def update_position(self, position_string):
- """Update self.position, ensure it's within map bounds."""
-
- def get_axis_position_from_argument(axis, token):
- if len(token) < 3 or token[:2] != axis + ':' or \
- not token[2:].isdigit():
- raise ArgumentError('Bad arg for ' + axis + ' position.')
- return int(token[2:])
-
- new_position = self.get_yx(position_string)
- if new_position[0] >= self.map_size[0] or \
- new_position[1] >= self.map_size[1]:
+ def update_things(self, thing_description):
+ """Append thing of thing_description to self.things."""
+ thing_types = {'human': '@', 'monster': 'M'}
+ tokens = thing_description.split()
+ if len(tokens) != 2:
+ raise ArgumentError('Wrong number of tokens.')
+ yx = self.get_yx(tokens[1])
+ if yx[0] >= self.map_size[0] or yx[1] >= self.map_size[1]:
raise ArgumentError('Position outside of map size bounds.')
- self.position = new_position
+ type_token = tokens[0]
+ prefix = 'TYPE:'
+ type_ = '?'
+ if len(type_token) <= len(prefix) or \
+ type_token[:len(prefix)] != prefix:
+ raise ArgumentError('Invalid type token.')
+ type_ = type_token[len(prefix):]
+ if type_ not in thing_types:
+ raise ArgumentError('Unknown thing type.')
+ self.things += [self.Thing(yx, thing_types[type_])]
self.draw_map()
+ def clear_things(self, _):
+ self.things = []
+
class InputHandler:
"""Delivers data from other thread to widget via message_container.
found_command = False
try:
found_command = (
+ mapdraw_command('NEW_TURN ', 'clear_things') or
mapdraw_command('TERRAIN\n', 'update_terrain') or
- mapdraw_command('POSITION ', 'update_position') or
+ mapdraw_command('THING ', 'update_things') or
mapdraw_command('MAP_SIZE ', 'update_map_size'))
except ArgumentError as e:
self.log_widget.add('ARGUMENT ERROR: ' + msg + '\n' + str(e))
class Thing:
- def __init__(self, position):
+ def __init__(self, type_, position):
+ self.type = type_
self.position = position
self.task = Task('wait')
'x.X.x\n'+\
'x...x\n'+\
'xxxxx'
- self.things = [Thing(position=[3, 3]), Thing([1, 1])]
+ self.things = [Thing('human', [3, 3]), Thing('monster', [1, 1])]
self.player_i = 0
self.player = self.things[self.player_i]
self.send_all('NEW_TURN ' + str(self.world.turn))
self.send_all('MAP_SIZE ' + self.stringify_yx(self.world.map_size))
self.send_all('TERRAIN\n' + self.world.map_)
- self.send_all('POSITION ' + self.stringify_yx(self.world.player.position))
+ for thing in self.world.things:
+ self.send_all('THING TYPE:' + thing.type + ' '
+ + self.stringify_yx(thing.position))
def cmd_fib(self, tokens, connection_id):
"""Reply with n-th Fibonacci numbers, n taken from tokens[1:].
self.send_all('NEW_TURN ' + str(self.world.turn))
self.send_all('MAP_SIZE ' + self.stringify_yx(self.world.map_size))
self.send_all('TERRAIN\n' + self.world.map_)
- self.send_all('POSITION ' + self.stringify_yx(self.world.player.position))
+ for thing in self.world.things:
+ self.send_all('THING TYPE:' + thing.type + ' '
+ + self.stringify_yx(thing.position))
self.pool_result = self.pool.map_async(fib, (35, 35))
def cmd_get_turn(self, connection_id):