def draw(self, state): # only show if visible to the player if libtcod.map_is_in_fov(state.fov_map, self.x, self.y) or self.always_visible and state.game_map.game_map[self.x][ self.y].explored: # set the color and then draw the char that represents this object at its position libtcod.console_set_default_foreground(state.con, self.color) libtcod.console_put_char(state.con, self.x, self.y, self.char, libtcod.BKGND_NONE)
def take_turn(self, state): # a basic monster takes its turn. If you can see it, it can see you monster = self.owner if libtcod.map_is_in_fov(state.fov_map, monster.x, monster.y): # move towards player if far away if monster.distance_to(state.player) >= 2: monster.move_towards(state.objects, state.game_map, state.player.x, state.player.y) # attack if close enough elif state.player.fighter.hp > 0: monster.fighter.attack(state.player, state)
def closest_monster(state, max_range): closest_enemy = None closest_dist = max_range + 1 for object in state.objects: if ( object.fighter and not object == state.player and libtcod.map_is_in_fov(state.fov_map, object.x, object.y) ): dist = state.player.distance_to(object) if dist < closest_dist: closest_enemy = object closest_dist = dist return closest_enemy
def render_all(state): if state.fov_recompute: # recompute FOV if needed state.fov_recompute = False if state.game_type == Constants.BATTLE: libtcod.map_compute_fov( state.fov_map, state.player.x, state.player.y, MapConstants.BATTLE_TORCH_RADIUS, MapConstants.FOV_LIGHT_WALLS, MapConstants.FOV_ALGO, ) else: libtcod.map_compute_fov( state.fov_map, state.player.x, state.player.y, MapConstants.TORCH_RADIUS, MapConstants.FOV_LIGHT_WALLS, MapConstants.FOV_ALGO, ) for y in range(MapConstants.MAP_HEIGHT): for x in range(MapConstants.MAP_WIDTH): visible = libtcod.map_is_in_fov(state.fov_map, x, y) wall = state.game_map.get_map()[x][y].block_sight targeted = state.game_map.get_map()[x][y].targeted if not visible: # if not visible right now, player can only see if explored if state.game_map.get_map()[x][y].explored: if wall: libtcod.console_set_char_background( state.con, x, y, MapConstants.COLOR_DARK_WALL, libtcod.BKGND_SET ) elif targeted: libtcod.console_set_char_background( state.con, x, y, MapConstants.COLOR_TARGETED, libtcod.BKGND_SET ) else: libtcod.console_set_char_background( state.con, x, y, MapConstants.COLOR_DARK_GROUND, libtcod.BKGND_SET ) else: # it is visible if wall: libtcod.console_set_char_background( state.con, x, y, MapConstants.COLOR_LIGHT_WALL, libtcod.BKGND_SET ) elif targeted: libtcod.console_set_char_background( state.con, x, y, MapConstants.COLOR_TARGETED, libtcod.BKGND_SET ) else: libtcod.console_set_char_background( state.con, x, y, MapConstants.COLOR_lIGHT_GROUND, libtcod.BKGND_SET ) state.game_map.get_map()[x][y].explored = True # draw all objects in the list for object in state.objects: if object != state.player: object.draw(state) state.player.draw(state) libtcod.console_blit(state.con, 0, 0, MapConstants.SCREEN_WIDTH, MapConstants.SCREEN_HEIGHT, 0, 0, 0) # prepare to render the GUI panel libtcod.console_set_default_background(state.status_panel.get_panel(), libtcod.black) libtcod.console_clear(state.status_panel.get_panel()) # print the game messages, one line at a time y = 1 for (line, color) in state.status_panel.game_messages: libtcod.console_set_default_foreground(state.status_panel.get_panel(), color) libtcod.console_print_ex( state.status_panel.get_panel(), MapConstants.MSG_X, y, libtcod.BKGND_NONE, libtcod.LEFT, line ) y += 1 # show the player's stats state.status_panel.render_bar( 1, 1, MapConstants.BAR_WIDTH, "HP", state.player.fighter.hp, state.player.fighter.max_hp(state), libtcod.light_red, libtcod.darker_red, ) state.status_panel.render_bar( 1, 2, MapConstants.BAR_WIDTH, "MP", state.player.caster.mp, state.player.caster.max_mp(state), libtcod.light_blue, libtcod.darker_blue, ) libtcod.console_print_ex( state.status_panel.get_panel(), 1, 5, libtcod.BKGND_NONE, libtcod.LEFT, "Player level: " + str(state.player.level), ) libtcod.console_print_ex( state.status_panel.get_panel(), 1, 6, libtcod.BKGND_NONE, libtcod.LEFT, "Dungeon level: " + str(state.dungeon_level), ) libtcod.console_print_ex( state.status_panel.get_panel(), 1, 7, libtcod.BKGND_NONE, libtcod.LEFT, "Game State: " + str(state.get_game_state()), ) libtcod.console_print_ex( state.status_panel.get_panel(), 1, 8, libtcod.BKGND_NONE, libtcod.LEFT, "Player Action: " + str(state.get_player_action()), ) libtcod.console_print_ex( state.status_panel.get_panel(), 1, 9, libtcod.BKGND_NONE, libtcod.LEFT, "Score: " + str(state.score) ) # blit the contents of "panel" to the root console libtcod.console_blit( state.status_panel.get_panel(), 0, 0, MapConstants.SCREEN_WIDTH, MapConstants.PANEL_HEIGHT, 0, 0, MapConstants.PANEL_Y, )