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 place_stairs(rooms): num_upstairs = 0 num_downstairs = 0 downstairs = libtcod.random_get_int(0, MIN_DOWNSTAIRS, MAX_DOWNSTAIRS) upstairs = libtcod.random_get_int(0, MIN_UPSTAIRS, MAX_UPSTAIRS) while num_downstairs < downstairs or num_upstairs < upstairs: random_index = libtcod.random_get_int(0, 0, len(rooms) - 1) room = rooms[random_index] x = libtcod.random_get_int(0, room.x1 + 1, room.x2 - 1) y = libtcod.random_get_int(0, room.y1 + 1, room.y2 - 1) if (not is_blocked(x, y)) and (libtcod.console_get_char(con, x, y) not in ('<', '>')): dice = libtcod.random_get_int(0, 0, 100) if dice < 50 and num_upstairs < upstairs: stair = Object(x, y, '<', 'stairs', always_visible=True, color=libtcod.white) objects.append(stair) stair.send_to_back() num_upstairs += 1 elif num_downstairs < downstairs: stair = Object(x, y, '>', 'stairs', always_visible=True, color=libtcod.white) objects.append(stair) stair.send_to_back() num_downstairs += 1
def main(): # Setup player global player_x, player_y, player_cor, snake_len, fruit_x, fruit_y, high_score player_x = SCREEN_WIDTH // 2 player_y = SCREEN_HEIGHT // 2 player_cor = [] snake_len = 0 high_score = 0 fruit_x = randint(0, 80) fruit_y = randint(0, 50) game_over = "u lost" # Setup Font font_filename = 'arial10x10.png' tcod.console_set_custom_font( font_filename, tcod.FONT_TYPE_GREYSCALE | tcod.FONT_LAYOUT_TCOD) # Initialize screen title = 'Snake' root = tcod.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT, title, FULLSCREEN) # Set FPS tcod.sys_set_fps(LIMIT_FPS) exit_game = False while not tcod.console_is_window_closed() and not exit_game: tcod.console_set_default_foreground(0, tcod.white) tcod.console_put_char(0, player_x, player_y, '@', tcod.BKGND_NONE) tcod.console_flush() if player_x == fruit_x and player_y == fruit_y: snake_len += 1 high_score += 1 place_fruit() snake() handle_keys() if tcod.console_get_char(0, player_x, player_y) == 49: sleep(1) tcod.console_clear(root) tcod.console_flush() sleep(0.5) tcod.console_print_rect_ex(root, (SCREEN_WIDTH // 2) - len(game_over) // 2, SCREEN_HEIGHT // 2, 0, 0, 0, 0, "{}".format(game_over)) tcod.console_print_rect_ex( root, (SCREEN_WIDTH // 2) - len("Highscore: {}".format(high_score)) // 2, (SCREEN_HEIGHT // 2) + 2, 0, 0, 0, 0, "Highscore: {}".format(high_score)) tcod.console_flush() sleep(5) sys.exit()
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 place_fruit(): global fruit_x, fruit_y if tcod.console_get_char( 0, fruit_x, fruit_y, ) == 70: return else: fruit_x = randint(0, SCREEN_WIDTH) fruit_y = randint(0, SCREEN_HEIGHT) tcod.console_put_char(0, fruit_x, fruit_y, 'F', tcod.BKGND_NONE)
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 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()
def identify_symbol_at(x,y): asci = libtcod.console_get_char(0, getx(x),gety(y)) char = "{} ".format(chr(asci)) if (asci < 128 and not asci==32) else "" desc=IDENTIFIER.get(asci,"???") return "{}{}".format(char, desc)
def get_symbol(self, position): x, y = position return libtcod.console_get_char(x, y)
def get_char(self, x, y): x += self.x_offset y += self.y_offset return chr(dlib.console_get_char(self._intern,x,y)) #TODO return a Tile object instead
color_masks = [[0, 0, 255], [68,68,196], [66,66,193]] for angle in range(0, 360, 10): ship = libtcod.image_load('images/ship_{0}.png'.format(str(angle).zfill(3))) # libtcod.image_blit(ship, con, 8, 8, libtcod.BKGND_SET, 1.0, 1.0, 0) libtcod.image_blit_2x(ship, con, 0, 0) libtcod.console_blit(con, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0) frame = [] for y in range(0, SCREEN_HEIGHT): row = [] for x in range(0, SCREEN_WIDTH): b = libtcod.console_get_char_background(con,x,y) f = libtcod.console_get_char_foreground(con,x,y) c = libtcod.console_get_char(con,x,y) if c == 32: f = b c = 219 if [b[0], b[1], b[2]] in color_masks or [f[0], f[1], f[2]] in color_masks: row.append( None ) elif [b[0], b[1], b[2]] == [0,0,0] and [f[0], f[1], f[2]] == [0,0,0]: row.append( None ) else: row.append( [b, f, c] ) frame.append(row) # pp(frame) frames.append(frame) libtcod.console_flush()