def flush(self, area): """ Flush screen area. Arguments: area : the area to be flushed """ libtcod.console_blit(area['con'], 0, 0, area['w'], area['h'], 0, area['x'], area['y']) libtcod.console_flush()
def display_statusline (message=""): global status # display_player_stats() for x in range (libtcod.console_get_width(status_console)): libtcod.console_put_char (status_console, x, 0, ' ', libtcod.BKGND_NONE) libtcod.console_put_char (status_console, x, 1, ' ', libtcod.BKGND_NONE) libtcod.console_print_rect_ex(status_console, 1, 0, M.SCREEN_WIDTH, 2, libtcod.BKGND_NONE, libtcod.LEFT, message[:M.SCREEN_WIDTH*2].strip()) libtcod.console_blit(status_console,0,0,M.SCREEN_WIDTH, (M.SCREEN_HEIGHT-M.MAP_HEIGHT-1),0,0,M.MAP_HEIGHT+1,1) libtcod.console_flush()
def draw (self): """ Draw the map """ for x, y in self: self.draw_tile(x, y) # Draw objects for object in self.objects: object.draw() # Draw the map onto the display libtcod.console_blit(self.console, 0, 0, self.width, self.height, 0, 0, 0) libtcod.console_flush() # Clear objects for object in self.objects: object.clear()
def show_player_stats(): player_stats=libtcod.console_new(20,20) libtcod.console_set_default_background(player_stats,libtcod.darkest_grey) libtcod.console_set_default_foreground(player_stats, libtcod.white) # libtcod.console_clear(player_stats) libtcod.console_print_frame(player_stats, 0, 0, libtcod.console_get_width(player_stats), libtcod.console_get_height(player_stats), clear=True) height=0 libtcod.console_print_rect(player_stats, 1, 1, libtcod.console_get_width(player_stats)-2, libtcod.console_get_height(player_stats)-2, "Name: %s \nHealth: %s/%s\nView distance: %s\nStrength: %s\nTo hit: %s\nExp: %s"# %(P.player.name,P.player.health,P.player.max_health, P.player.view_distance, P.player.strength,P.player.to_hit,P.player.exp)) libtcod.console_print_ex(player_stats, libtcod.console_get_width(player_stats)//2, 0, libtcod.BKGND_DEFAULT, libtcod.CENTER, "Player Stats") libtcod.console_print_ex(player_stats, libtcod.console_get_width(player_stats)//2, libtcod.console_get_height(player_stats)-1, libtcod.BKGND_DEFAULT, libtcod.CENTER, "[spacebar]") libtcod.console_blit(player_stats,0,0, libtcod.console_get_width(player_stats), libtcod.console_get_height(player_stats), 0,5,5, 1.0,0.1) key = libtcod.console_check_for_keypress(libtcod.KEY_PRESSED) while not (libtcod.KEY_SPACE==key.vk): key = libtcod.console_check_for_keypress(libtcod.KEY_PRESSED) libtcod.console_blit(player_stats,0,0, libtcod.console_get_width(player_stats), libtcod.console_get_height(player_stats), 0,5,5, 1.0,0.1) libtcod.console_flush() R.render()
def display_status (): global status if status: libtcod.console_rect(status_console, 0, 0, M.SCREEN_WIDTH, (M.SCREEN_HEIGHT - M.MAP_HEIGHT), True) libtcod.console_set_default_foreground (status_console, libtcod.white) while len(status) > M.SCREEN_WIDTH*2: display_statusline(status[:M.SCREEN_WIDTH*2]) key = libtcod.console_wait_for_keypress(True) while not key.vk == libtcod.KEY_SPACE: key = libtcod.console_wait_for_keypress(True) status = status[M.SCREEN_WIDTH*2:] display_statusline(status) libtcod.console_blit(status_console,0,0,M.SCREEN_WIDTH, (M.SCREEN_HEIGHT-M.MAP_HEIGHT-1),0,0,M.MAP_HEIGHT+1,1) libtcod.console_flush() else: display_statusline() libtcod.console_flush()
def render(): libtcod.console_rect(cons.game_console, 0, 0, M.MAP_WIDTH, M.MAP_HEIGHT, True) for y in range (M.MAP_HEIGHT): for x in range (M.MAP_WIDTH): # Draw black for all areas the player has not seen yet if not M.gameworld[x][y].explored: libtcod.console_set_char_background(cons.game_console, x, y, libtcod.black, libtcod.BKGND_SET) # Draw all the floors. if M.gameworld[x][y].is_floor () and M.gameworld[x][y].explored: libtcod.console_set_char_background (cons.game_console, x, y, M.DARK_FLOOR_COLOR, libtcod.BKGND_SET) # Draw all the walls. elif M.gameworld[x][y].explored: libtcod.console_set_char_background(cons.game_console, x, y, M.DARK_WALL_COLOR, libtcod.BKGND_SET) # Draw all the light floors. if (libtcod.map_is_in_fov (P.player.fov, x, y) and libtcod.map_is_walkable (P.player.fov, x, y)): libtcod.console_set_char_background (cons.game_console, x, y, M.FLOOR_COLOR, libtcod.BKGND_SET) for item in M.gameworld[x][y].items: libtcod.console_set_default_foreground (cons.game_console, item.color) libtcod.console_put_char (cons.game_console, x, y, item.char, libtcod.BKGND_NONE) for character in M.gameworld[x][y].characters: libtcod.console_set_default_foreground (cons.game_console, character.color) libtcod.console_put_char (cons.game_console, x, y, character.char, libtcod.BKGND_NONE) M.gameworld[x][y].explored=True # Draw all the light walls. elif libtcod.map_is_in_fov (P.player.fov, x, y): libtcod.console_set_char_background (cons.game_console, x, y, M.WALL_COLOR, libtcod.BKGND_SET) M.gameworld[x][y].explored=True # Blits the game console to the root console. libtcod.console_blit(cons.game_console,0,0,M.MAP_WIDTH,M.MAP_HEIGHT,0,0,0,1)
def test_area(self, area, fcolor, bcolor): """ Draw routines to test game areas. Draws some test things in a specific area. Arguments: area : area to be tested fcolor : foreground color to use for test bcolor : background color to use for test """ libtcod.console_set_default_background(area['con'], bcolor) libtcod.console_set_default_foreground(area['con'], fcolor) libtcod.console_clear(area['con']) libtcod.console_print_frame(area['con'], 0, 0, area['w'], area['h'], False, None, area['name'] + ": " + str(area)) for i in range(area['h']): libtcod.console_print_ex(area['con'], 0, i, libtcod.BKGND_NONE, libtcod.LEFT, str(i + 1)) libtcod.console_blit(area['con'], 0, 0, area['w'], area['h'], 0, area['x'], area['y'])
def draw_items(): height = 0 libtcod.console_set_default_background(cons.items_console, libtcod.red) libtcod.console_set_default_foreground(cons.items_console, libtcod.white) libtcod.console_clear(cons.items_console) for item in tile_items: height+=libtcod.console_print_rect_ex(cons.items_console, 0, height, libtcod.console_get_width(cons.items_console), libtcod.console_get_height(cons.items_console), libtcod.BKGND_NONE, libtcod.LEFT, item.name) libtcod.console_blit(cons.items_console,0,0, libtcod.console_get_width(cons.items_console), height, 0,1,1, 1.0,0.5) libtcod.console_flush()
def display(self, con): if self.game.fov_recompute: #recompute FOV if needed (the player moved or something) self.game.fov_recompute = False libtcod.map_compute_fov(self.map.fov_map, self.game.player.x, self.game.player.y, TORCH_RADIUS, FOV_LIGHT_WALLS, FOV_ALGO) for y in range(self.height): for x in range(self.width): map_x, map_y = x + self.x_offset, y + self.y_offset if self.map.in_bounds(map_x, map_y): visible = libtcod.map_is_in_fov(self.map.fov_map, map_x, map_y) tt = self.map.tiles[map_x][map_y].type if visible: libtcod.console_set_char_background(con, x, y, tt.bg_color_lit, libtcod.BKGND_SET) libtcod.console_set_default_foreground(con, tt.fg_color_lit) libtcod.console_put_char(con, x, y, tt.char, libtcod.BKGND_NONE) # TODO: Doing this here bugs it if the player's light radius is not on screen self.map.tiles[map_x][map_y].explored = True elif self.map.tiles[map_x][map_y].explored: libtcod.console_set_char_background(con, x, y, tt.bg_color, libtcod.BKGND_SET) libtcod.console_set_default_foreground(con, tt.fg_color) libtcod.console_put_char(con, x, y, tt.char, libtcod.BKGND_NONE) else: libtcod.console_set_char_background(con, x, y, void_color, libtcod.BKGND_SET) libtcod.console_put_char(con, x, y, ' ', libtcod.BKGND_NONE) else: libtcod.console_set_char_background(con, x, y, void_color, libtcod.BKGND_SET) libtcod.console_put_char(con, x, y, ' ', libtcod.BKGND_NONE) for object in self.map.objects: object.draw(con, self.x_offset, self.y_offset) libtcod.console_blit(con, 0, 0, self.width, self.height, 0, 0, 0) libtcod.console_flush()
def fire(self, x, y, dx, dy): # The distance this bullet has traveled. steps = 0 shot_accuracy=self.accuracy libtcod.map_compute_fov (P.player.fov, x, y, int(P.player.view_distance*1.5), True,libtcod.FOV_SHADOW) R.render() libtcod.line_init(x, y, dx, dy) lx,ly=libtcod.line_step() while (not lx is None): steps = steps + 1 if not M.gameworld[lx][ly].characters: libtcod.console_set_char_background( cons.game_console, lx, ly, libtcod.white, libtcod.BKGND_OVERLAY) libtcod.console_blit(cons.game_console,0,0,M.MAP_WIDTH,M.MAP_HEIGHT,0,0,0,1) libtcod.console_flush() lx,ly=libtcod.line_step() else: if random.random() <= shot_accuracy: S.add_status("You hit!") libtcod.console_set_char_background( cons.game_console, lx, ly, libtcod.red, libtcod.BKGND_OVERLAY) libtcod.console_blit(cons.game_console,0,0,M.MAP_WIDTH,M.MAP_HEIGHT,0,0,0,1) libtcod.console_flush() M.gameworld[lx][ly].characters[-1].take_damage( int(self.damage*random.uniform(self.accuracy, 1.0))) else: S.add_status("You fire and miss!") break self.ammo = self.ammo-1 self.owner.name = self.owner.name.rsplit('(')[0] + '(' + str(self.ammo) + ')' P.player.compute_fov()
def draw_menu(con, header, options, w, screen_w, screen_h): if len(options) > 26: raise ValueError('Menu "' + header + '" has ' + str(len(options)) + ' options but max is 26') # calculate height (header is word-wrapped) header_h = ltc.console_get_height_rect(con, 0, 0, w, screen_h, header) h = len(options) + header_h # add space for border w += 2 h += 2 # create new console window window = ltc.console_new(w, h) # draw border and header draw_panel_border(window, w, h) dotext(window, 1, 1, header) # draw options y = header_h + 1 letter_index = ord('a') for o in options: text = '(' + chr(letter_index) + ') ' + o dotext(window, 1, y, text) y += 1 letter_index += 1 # blit menu to main console x = screen_w / 2 - w / 2 y = screen_h / 2 - h / 2 ltc.console_blit(window, 0, 0, w, h, 0, x, y, 1.0, 0.7) # show menu and wait for player choice ltc.console_flush() key = ltc.console_wait_for_keypress(True) index = key.c - ord('a') return index if 0 <= index < len(options) else None
def shoot(self): gun = -1 for i in range(len(self.equipped)): if self.equipped[i].gun and self.equipped[i].gun.ammo > 0: gun = i if not gun==-1: class Target: def __init__(self, x, y): self.x = x self.y = y target = Target(self.x, self.y) libtcod.console_blit(cons.game_console,0,0,M.MAP_WIDTH,M.MAP_HEIGHT,0,0,0,1) libtcod.console_flush() key = libtcod.console_wait_for_keypress(True) while not key.vk == libtcod.KEY_SPACE: R.render() if key.pressed: if ord('k') == key.c: target.y=target.y-1 elif ord('j') == key.c: target.y=target.y+1 elif ord('h') == key.c: target.x=target.x-1 elif ord('l') == key.c: target.x=target.x+1 elif ord('y') == key.c: target.x=target.x-1 target.y=target.y-1 elif ord('u') == key.c: target.x=target.x+1 target.y=target.y-1 elif ord('i') == key.c: target.x=target.x-1 target.y=target.y+1 elif ord('o') == key.c: target.x=target.x+1 target.y=target.y+1 libtcod.line_init(self.x, self.y, target.x, target.y) x,y=libtcod.line_step() # Clear the console that shows our target line. libtcod.console_clear(cons.gun_console) # Draw the target line on the gun console. while (not x is None): if (M.gameworld[x][y].is_floor() and libtcod.map_is_in_fov (player.fov, x, y) ): libtcod.console_set_char_background(cons.gun_console, x, y, libtcod.white, libtcod.BKGND_OVERLAY) target.x=x target.y=y x,y=libtcod.line_step() else: break # Draw the gun console to the root console. libtcod.console_blit(cons.gun_console,0,0,M.MAP_WIDTH,M.MAP_HEIGHT,0,0,0,0,0.5) libtcod.console_flush() key = libtcod.console_wait_for_keypress(True) self.equipped[gun].gun.fire(self.x, self.y, target.x, target.y) else: S.add_status("No gun in hand!")
def item_selector(items, default=None, equipped=[], title="INVENTORY"): libtcod.console_clear(cons.menu_console) libtcod.console_set_default_background(cons.menu_console, libtcod.black) libtcod.console_set_default_foreground(cons.menu_console, libtcod.white) libtcod.console_rect(cons.menu_console, 0, 0, M.MAP_WIDTH, M.MAP_HEIGHT, True) libtcod.console_print_ex(cons.menu_console, 40, 0, libtcod.BKGND_NONE, libtcod.CENTER, title) libtcod.console_print_ex(cons.menu_console, 1, M.SCREEN_HEIGHT-1, libtcod.LEFT, libtcod.BKGND_NONE, "[j / k]: Highlight item [SPACEBAR]: Select [q]: quit") count = 0 for item in items: libtcod.console_print_ex(cons.menu_console, 1, count+3, libtcod.BKGND_NONE, libtcod.LEFT, item.name) if item in equipped: libtcod.console_print_ex(cons.menu_console, libtcod.console_get_width(cons.menu_console)-1, count+3, libtcod.BKGND_NONE, libtcod.RIGHT, "(EQUIPPED)") count = count + 1 if default: count = items.index(default) else: count = count -1 key = libtcod.console_check_for_keypress(True) while not key.vk == libtcod.KEY_SPACE and not ord('q') == key.c: for i in range(len(items[count].name)): libtcod.console_set_char_background(cons.menu_console, i+1, count+3, libtcod.white) libtcod.console_set_char_foreground(cons.menu_console, i+1, count+3, libtcod.black) if key.pressed and key.c == ord('k') and count > 0: for i in range(len(items[count].name)): libtcod.console_set_char_background(cons.menu_console, i+1, count+3, libtcod.black) libtcod.console_set_char_foreground(cons.menu_console, i+1, count+3, libtcod.white) count = count -1 elif key.pressed and key.c == ord('j') and count < len(items)-1: for i in range(len(items[count].name)): libtcod.console_set_char_background(cons.menu_console, i+1, count+3, libtcod.black) libtcod.console_set_char_foreground(cons.menu_console, i+1, count+3, libtcod.white) count = count +1 key = libtcod.console_check_for_keypress(True) libtcod.console_blit(cons.menu_console,0,0,M.SCREEN_WIDTH,M.SCREEN_HEIGHT,0,0,0,1) libtcod.console_flush() if ord('q') == key.c: count=-1 return count
def main(): # setup screen ltc.console_set_custom_font('lucida10x10_gs_tc.png', ltc.FONT_TYPE_GREYSCALE | ltc.FONT_LAYOUT_TCOD) ltc.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT, 'python/ltc tutorial', fullscreen=False) ltc.sys_set_fps(LIMIT_FPS) con = ltc.console_new(LEVEL_WIDTH, LEVEL_HEIGHT) gui_panel = ltc.console_new(PANEL_WIDTH, PANEL_HEIGHT) # setup level leveldata, start_pos = leveltools.make_level(LEVEL_WIDTH, LEVEL_HEIGHT) fov_recompute = True # setup player leveldata.player = rlplayer.create_player(start_pos[0], start_pos[1]) leveldata.objects.insert(0, leveldata.player) rlmobs.populate_enemies(leveldata) rlitems.populate_items(leveldata) src.rlmsglog.m('Welcome to the dungeon!') # game state game_state = GS_PLAYING # MAIN LOOP while not ltc.console_is_window_closed(): # get user input mouse = ltc.Mouse() key = ltc.Key() ltc.sys_check_for_event(ltc.EVENT_KEY_PRESS | ltc.EVENT_MOUSE, key, mouse) if fov_recompute: leveldata.recompute_fov(TORCH_RADIUS) # draw level draw_level(con, leveldata) # noinspection PyTypeChecker draw_gui(gui_panel, con, leveldata, mouse) # display level ltc.console_blit(con, 0, 0, LEVEL_WIDTH, LEVEL_HEIGHT, 0, 0, 0) ltc.console_blit(gui_panel, 0, 0, PANEL_WIDTH, PANEL_HEIGHT, 0, 0, PANEL_Y) ltc.console_flush() clear_console(con) clear_console(gui_panel) # handle user input # noinspection PyTypeChecker player_taken_turn = handle_input(key, mouse, game_state, leveldata, con) if should_exit: break # all objects take their turn - player is first if game_state == GS_PLAYING and player_taken_turn: fov_recompute = True for o in leveldata.objects: o.take_turn(leveldata) if not leveldata.player.fighter.is_alive(): game_state = GS_DEAD
def blit(self, size, screen_position): rows, cols = size y, x = screen_position libtcod.console_blit(self.win, 0, 0, cols, rows, 0, x, y, 1.0, 1.0)
def render(self): libtcod.console_blit(self.con, 0, 0, self.width, self.height, 0, 0, 0) libtcod.console_flush()