def assertConsolesEqual(a, b): for y in range(libtcodpy.console_get_height(a)): for x in range(libtcodpy.console_get_width(a)): assert libtcodpy.console_get_char(a, x, y) == \ libtcodpy.console_get_char(b, x, y) assert libtcodpy.console_get_char_foreground(a, x, y) == \ libtcodpy.console_get_char_foreground(b, x, y) assert libtcodpy.console_get_char_background(a, x, y) == \ libtcodpy.console_get_char_background(b, x, y)
def test_console_rexpaint_load_test_file(console): xp_console = libtcodpy.console_from_xp('libtcod/data/rexpaint/test.xp') assert xp_console assert libtcodpy.console_get_char(xp_console, 0, 0) == ord('T') assert libtcodpy.console_get_char(xp_console, 1, 0) == ord('e') assert (libtcodpy.console_get_char_background(xp_console, 0, 1) == libtcodpy.Color(255, 0, 0)) assert (libtcodpy.console_get_char_background(xp_console, 1, 1) == libtcodpy.Color(0, 255, 0)) assert (libtcodpy.console_get_char_background(xp_console, 2, 1) == libtcodpy.Color(0, 0, 255))
def test_array_read_write(): console = tcod.console.Console(width=12, height=10) FG = (255, 254, 253) BG = (1, 2, 3) CH = ord('&') tcod.console_put_char_ex(console, 0, 0, CH, FG, BG) assert console.ch[0, 0] == CH assert tuple(console.fg[0, 0]) == FG assert tuple(console.bg[0, 0]) == BG tcod.console_put_char_ex(console, 1, 2, CH, FG, BG) assert console.ch[2, 1] == CH assert tuple(console.fg[2, 1]) == FG assert tuple(console.bg[2, 1]) == BG console.clear() assert console.ch[1, 1] == ord(' ') assert tuple(console.fg[1, 1]) == (255, 255, 255) assert tuple(console.bg[1, 1]) == (0, 0, 0) ch_slice = console.ch[1, :] ch_slice[2] = CH console.fg[1, ::2] = FG console.bg[...] = BG assert tcod.console_get_char(console, 2, 1) == CH assert tuple(tcod.console_get_char_foreground(console, 2, 1)) == FG assert tuple(tcod.console_get_char_background(console, 2, 1)) == BG
def assert_char(console, x, y, ch=None, fg=None, bg=None): if ch is not None: try: ch = ord(ch) except TypeError: pass assert libtcodpy.console_get_char(console, x, y) == ch if fg is not None: assert libtcodpy.console_get_char_foreground(console, x, y) == fg if bg is not None: assert libtcodpy.console_get_char_background(console, x, y) == bg
def __getitem__(self, index): if isinstance(index, slice): raise TypeError('Console objects do not support slices. Yet.') x, y = index if x > self.width or x < 0 or y > self.height or y < 0: raise IndexError( 'Attempt to access cell ({0}, {1}), which is out of range. Console size is ({2}, {3}).' .format(x, y, self.width, self.height)) return ConsoleCell(chr(tcod.console_get_char(self._c, x, y)), tcod.console_get_char_foreground(self._c, x, y), tcod.console_get_char_background(self._c, x, y))
def draw_mouse(con, x, y, mouse_char, color): global old_mouse_pos, old_mouse_char, old_mouse_char_color # re draw the char that the mouse was at previously if old_mouse_pos[0] is not None: libtcod.console_set_default_foreground(con, old_mouse_char_color) libtcod.console_put_char(con, old_mouse_pos[0], old_mouse_pos[1], old_mouse_char, libtcod.BKGND_NONE) # get old variables for next time this function is called old_mouse_pos[0] = x old_mouse_pos[1] = y if libtcod.console_get_char(con, x, y) != mouse_char: old_mouse_char = libtcod.console_get_char(con, x, y) old_mouse_char_color = libtcod.console_get_char_foreground(con, x, y) # draw where the mouse is now libtcod.console_set_default_foreground(con, color) libtcod.console_put_char(con, x, y, mouse_char, libtcod.BKGND_NONE) libtcod.console_blit(con, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0)
def __getitem__(self, index): if isinstance(index, slice): raise TypeError('Console objects do not support slices. Yet.') x, y = index if x > self.width or x < 0 or y > self.height or y < 0: raise IndexError( 'Attempt to access cell ({0}, {1}), which is out of range. Console size is ({2}, {3}).'.format(x, y, self.width, self.height)) return ConsoleCell(chr(tcod.console_get_char(self._c, x, y)), tcod.console_get_char_foreground(self._c, x, y), tcod.console_get_char_background(self._c, x, y))
def test_console_fill(console): width = libtcodpy.console_get_width(console) height = libtcodpy.console_get_height(console) fill = [i % 256 for i in range(width * height)] libtcodpy.console_fill_background(console, fill, fill, fill) libtcodpy.console_fill_foreground(console, fill, fill, fill) libtcodpy.console_fill_char(console, fill) # verify fill bg, fg, ch = [], [], [] for y in range(height): for x in range(width): bg.append(libtcodpy.console_get_char_background(console, x, y)[0]) fg.append(libtcodpy.console_get_char_foreground(console, x, y)[0]) ch.append(libtcodpy.console_get_char(console, x, y)) assert fill == bg assert fill == fg assert fill == ch
def look_mode(): global MESSAGES from game import decode_key x, y = GAME.player.x, GAME.player.y _messages = MESSAGES MESSAGES = [] message('Look mode - use movement keys, ESC/q to exit.', T.green) new_ui_turn() _draw_messages() redraw = True while True: if redraw: T.console_blit(CON_MAP, 0, 0, MAP_W, MAP_H, None, 1, 1) c = T.console_get_char(CON_MAP, x, y) color = T.console_get_char_foreground(CON_MAP, x, y) T.console_put_char_ex(None, x+1, y+1, c, T.black, color) describe_tile(x, y) _draw_messages() T.console_flush() # now clear the message buffer of last messages while MESSAGES and MESSAGES[-1][0]: MESSAGES.pop() redraw = False cmd = decode_key(readkey()) if cmd == 'quit': break elif isinstance(cmd, tuple): name, args = cmd if name == 'walk': dx, dy = args if in_map(x+dx, y+dy): x, y = x+dx, y+dy redraw = True MESSAGES = _messages
def test_console_fill_numpy(console): width = libtcodpy.console_get_width(console) height = libtcodpy.console_get_height(console) fill = numpy.zeros((height, width), dtype=numpy.intc) for y in range(height): fill[y, :] = y % 256 libtcodpy.console_fill_background(console, fill, fill, fill) libtcodpy.console_fill_foreground(console, fill, fill, fill) libtcodpy.console_fill_char(console, fill) # verify fill bg = numpy.zeros((height, width), dtype=numpy.intc) fg = numpy.zeros((height, width), dtype=numpy.intc) ch = numpy.zeros((height, width), dtype=numpy.intc) for y in range(height): for x in range(width): bg[y, x] = libtcodpy.console_get_char_background(console, x, y)[0] fg[y, x] = libtcodpy.console_get_char_foreground(console, x, y)[0] ch[y, x] = libtcodpy.console_get_char(console, x, y) fill = fill.tolist() assert fill == bg.tolist() assert fill == fg.tolist() assert fill == ch.tolist()