def _rebuild_self(self): """ Same as `Layout()._rebuild_self`, but all child positions are also offset by `view_pos`. Obviously, only `view_size[1]` lines `view_size[0]` long are set as `chars` and `colors`. :return: """ chars = [[' ' for x in range(self.view_size[0])] \ for y in range(self.view_size[1])] colors = copy_shape(chars, None) for line in range(self.view_size[1]): for char in range(self.view_size[0]): for child in self._child_pointers[self.view_pos[1] + line] \ [self.view_pos[0] + char][::-1]: # Addressing the correct child position c = child.chars[ self.view_pos[1] + line - self.child_locations[child][ 1]] \ [self.view_pos[0] + char - self.child_locations[child][ 0]] if c != ' ': # Spacebars are used as empty space and are transparent chars[line][char] = c break colors[line][char] = \ child.colors[ self.view_pos[1] + line - self.child_locations[child][ 1]] \ [self.view_pos[0] + char - self.child_locations[child][ 0]] self.chars = chars self.colors = colors
def _rebuild_self(self): """ Same as `Layout()._rebuild_self`, but all child positions are also offset by `view_pos`. Obviously, only `view_size[1]` lines `view_size[0]` long are set as `chars` and `colors`. :return: """ chars = [[' ' for x in range(self.view_size[0])] \ for y in range(self.view_size[1])] colors = copy_shape(chars, 'white') for line in range(self.view_size[1]): for char in range(self.view_size[0]): for child in self._child_pointers[self.view_pos[1] + line] \ [self.view_pos[0] + char][::-1]: c_x = self.view_pos[0] + char - self.child_locations[ child][0] c_y = self.view_pos[1] + line - self.child_locations[ child][1] if child.chars[c_y][c_x] not in (' ', None, 32): # Skip all possible values for transparent empty char chars[line][char] = child.chars[c_y][c_x] colors[line][char] = child.colors[c_y][c_x] break self.chars = chars self.colors = colors
def _load_file(self): for line in open(self.filename): f = list(line.rstrip('\n')) if self.chars and len(f) != len(self.chars[0]): raise BearException('All lines should be equal length') if not self.chars: self.chars = [f] else: self.chars.append(f) self.colors = copy_shape(self.chars, self.default_color)
def __init__(self, size=3, freq=15, **kwargs): self.asterisks=[(random.randint(0, size-1), random.randint(0, size-1))\ for x in range(size)] chars = [[' ' for x in range(size)] for x in range(size)] colors = copy_shape(chars, 'white') for asterisk in self.asterisks: chars[asterisk[0]][asterisk[1]] = '*' colors[asterisk[0]][asterisk[1]] = random.choice( ('red', 'blue', 'white')) super().__init__(chars, colors, **kwargs) self.ticks_to_wait = freq self.ticks_skipped = 0 self.firework_size = size
def _parse_layer(self, layer_string, reverse_endian=True): """ Parse a file portion for a single layer :param layer_string: :param reverse_endian: :return: """ offset = 0 width = layer_string[offset:offset + self.layer_width_bytes] offset += self.layer_width_bytes height = layer_string[offset:offset + self.layer_height_bytes] offset += self.layer_height_bytes if reverse_endian: width = width[::-1] height = height[::-1] self.width = int(base64.b16encode(width), 16) self.height = int(base64.b16encode(height), 16) cells = [] for x in range(self.width): row = [] for y in range(self.height): cell_data_raw = layer_string[offset:offset + self.layer_cell_bytes] cell_data = self._parse_individual_cell(cell_data_raw, reverse_endian) row.append(cell_data) offset += self.layer_cell_bytes cells.append(row) cells = rotate_list(cells) chars = copy_shape(cells, None) colors = copy_shape(cells, self.default_color) for r in range(len(cells)): for c in range(len(cells[0])): chars[r][c] = cells[r][c][0] colors[r][c] = cells[r][c][1] return chars, colors
def _get_topmost_layer(self): if self.layer_count == 1: self.chars = deepcopy(self.layers[0][0]) self.colors = deepcopy(self.layers[0][1]) else: self.chars = [[' ' for x in range(self.width)] for y in range(self.height)] self.colors = copy_shape(self.chars, None) for row in range(self.height): for column in range(self.width): for layer in self.layers[::-1]: if layer[0][row][column] != ' ': self.chars[row][column] = layer[0][row][column] self.colors[row][column] = layer[1][row][column] break
def __init__(self, chars, colors, view_pos=(0, 0), view_size=(10, 10)): self.entities = {} self.widgets = {} self.widget_to_entity = {} # A dict from id(widget) to entity self.z_values = copy_shape(chars, None) # copy_shape does not work with lists correctly, so. for line in range(len(self.z_values)): for char in range(len(self.z_values[0])): self.z_values[line][char] = [] super().__init__(chars, colors) self.view_pos = view_pos[:] self.view_size = view_size[:] if not 0 <= view_pos[0] <= self.width - view_size[0] \ or not 0 <= view_pos[1] <= self.height - view_size[1]: raise BearLayoutException('Initial viewpoint outside ' + 'ScrollableLayout') if not 0 < view_size[0] <= len(chars[0]) \ or not 0 < view_size[1] <= len(chars): raise BearLayoutException('Invalid view field size') self._rebuild_self()
dispatcher.add_event( BearEvent(event_type='ecs_add', event_value=('cop', x, y))) t = BearTerminal(font_path='bear_hug/demo_assets/cp437_12x12.png', size='85x60', title='Brutality', filter=['keyboard', 'mouse']) dispatcher = BearEventDispatcher() loop = BearLoop(t, dispatcher) dispatcher.register_listener(ClosingListener(), ['misc_input', 'tick']) dispatcher.register_listener(EntityTracker(), ['ecs_create', 'ecs_destroy']) atlas = Atlas(XpLoader('bear_hug/demo_assets/test_atlas.xp'), 'bear_hug/demo_assets/test_atlas.json') chars = [['.' for x in range(44)] for y in range(49)] colors = copy_shape(chars, 'gray') layout = ECSLayout(chars, colors) dispatcher.register_listener(layout, 'all') create_cop(atlas, dispatcher, 5, 5) create_barrel(atlas, dispatcher, 20, 20) # Dev monitor, works outside ECS monitor = DevMonitor(*atlas.get_element('dev_bg'), dispatcher=dispatcher) dispatcher.register_listener(monitor, ['tick', 'service']) # A sound player jukebox = SoundListener({'step': 'dshoof.wav', 'shot': 'dsshotgn.wav'}) dispatcher.register_listener(jukebox, 'play_sound') # A label label = Label(""" This is a basic ECS test.
def __init__(self, size): chars = [[' ' for x in range(size[0])] for y in range(size[1])] colors = copy_shape(chars, 'dark gray') super().__init__(chars, colors)
def update_bg(self, color): new_bg = Widget(copy_shape(self.chars, '.'), copy_shape(self.colors, color)) self.background = new_bg