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 draw_tile (self, x, y): """ Draw a single tile """ # If tile is visible: if libtcod.map_is_in_fov(self.fov, x, y): colors = Colors.light # Set tile to explored self.tiles[x][y].explored = True # If tile is not visible, but explored elif self.tiles[x][y].explored: colors = Colors.dark # If tile is not visible or explored, do not draw it else: colors = Colors.unseen color = colors.wall if self.tiles[x][y].filled else colors.ground libtcod.console_set_back(self.console, x, y, color, libtcod.BKGND_SET)
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 is_in_fov(self, x, y): return libtcod.map_is_in_fov(self.fov, x, y)
def is_visible(self, x, y): """ Returns true if the specified location is within the players FOV """ return ltc.map_is_in_fov(self.fov_map, x, y)
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 visible (self): """ Is the item visible to the player? """ return libtcod.map_is_in_fov(self.map.fov, self.x, self.y) or self.has_tag('always_show')
def draw(self, con, x_off, y_off): if libtcod.map_is_in_fov(self.map.fov_map, self.x, self.y): libtcod.console_set_default_foreground(con, self.color) libtcod.console_put_char(con, self.x - x_off, self.y - y_off, self.char, libtcod.BKGND_NONE)
def render(self, level, x, y, minx, miny, maxx, maxy, fov_map, main_area, *args, **kwargs): """ Renders current level. Takes the level map and draws each tile on it, according to its properties. Since the map may be bigger (or smaller) than the screen, it makes some calculations to center (or try it at best) on the given (x,y) coordinates. TODO: - draw level objects. It just draws the map (and the player but just in a testing manner) - instead of drawing from level.mapa, ui.py should call a draw routing in the wrapper, which should receive the contents to be drawn in a specific area, without any game details being revealed here. This would also remove the previous message() method Arguments: level : the world.level with the map to render (x,y) : the coordinates to be the center of the drawing (minx,miny) : the minimum coordinates from the map to be drawn (maxx,maxy) : the maximum coordinates from the map to be drawn fov_map main_area : the area where the map is to be drawn """ from world.tile import TILETYPES libtcod.console_clear(main_area['con']) map_ = level.mapa # determine console coordinates to begin rendering according to map size (conx, cony) = ((main_area['w'] - map_.w)//2 if map_.w < main_area['w'] else 0, (main_area['h'] - map_.h)//2 if map_.h < main_area['h'] else 0) # draw map tiles for my, cy in zip(range(miny, maxy), range(cony, map_.h + cony)): for mx, cx in zip(range(minx, maxx), range(conx, map_.w + conx)): try: visible = libtcod.map_is_in_fov(fov_map, mx, my) tile = map_.mapa[mx][my] except Exception as e: continue # it's out of the player's FOV, player will see it only if explored if not visible: if tile.explored: # draw map tile with char/color <- modifications for explored/not visible libtcod.console_put_char_ex(main_area['con'], cx, cy, ' ' if TILETYPES[tile.tipo]['just_color'] else TILETYPES[tile.tipo]['char'].encode('utf8'), libtcod.white, getcolorbyname(TILETYPES[tile.tipo]['nv_color'])) else: libtcod.console_put_char_ex(main_area['con'], cx, cy, ' ', libtcod.black, libtcod.black) # it's visible else: # draw map tile with char/color <- as is since it is visible libtcod.console_put_char_ex(main_area['con'], cx, cy, ' ' if TILETYPES[tile.tipo]['just_color'] else TILETYPES[tile.tipo]['char'].encode('utf8'), libtcod.white, getcolorbyname(TILETYPES[tile.tipo]['color'])) # since now it's visible, mark it as explored tile.explored = True for p in level.players: libtcod.console_set_char(main_area['con'], conx+x-minx, cony+y-miny, '@') # draw objects in map... # blit contents self.flush(main_area)
def render(self, level, x, y, minx, miny, maxx, maxy, fov_map, main_area, *args, **kwargs): """ Renders current level. Takes the level map and draws each tile on it, according to its properties. Since the map may be bigger (or smaller) than the screen, it makes some calculations to center (or try it at best) on the given (x,y) coordinates. TODO: - draw level objects. It just draws the map (and the player but just in a testing manner) - instead of drawing from level.mapa, ui.py should call a draw routing in the wrapper, which should receive the contents to be drawn in a specific area, without any game details being revealed here. This would also remove the previous message() method Arguments: level : the world.level with the map to render (x,y) : the coordinates to be the center of the drawing (minx,miny) : the minimum coordinates from the map to be drawn (maxx,maxy) : the maximum coordinates from the map to be drawn fov_map : fov map main_area : the area where the map is to be drawn """ from world.tile import TILETYPES main_area['con'].clear() map_ = level.mapa # determine console coordinates to begin rendering according to map size (conx, cony) = ((main_area['w'] - map_.w)//2 if map_.w < main_area['w'] else 0, (main_area['h'] - map_.h)//2 if map_.h < main_area['h'] else 0) # draw map tiles if util.debug: c = 0 for my, cy in zip(range(miny, maxy), range(cony, map_.h + cony)): for mx, cx in zip(range(minx, maxx), range(conx, map_.w + conx)): try: visible = libtcod.map_is_in_fov(fov_map, mx, my) tile = map_.mapa[mx][my] # BUG: sometimes None appears on the map(?!) except Exception as e: if util.debug: c += 1 continue # it's out of the player's FOV, player will see it only if explored if not visible: if tile.explored: # draw map tile with char/color <- modifications for explored/not visible main_area['con'].addstr(cy, cx, TILETYPES[tile.tipo]['char'].encode('utf8'), curses.color_pair(curses_wrapper.COLORS[TILETYPES[tile.tipo]['nv_color']]['n'])) else: # main_area['con'].addstr(cy, cx, ' ', # curses.color_pair(curses_wrapper.COLORS['black']['n'])) pass # it's visible else: # draw map tile with char/color <- as is since it is visible try: main_area['con'].addstr(cy, cx, TILETYPES[tile.tipo]['char'].encode('utf8'), curses.color_pair(curses_wrapper.COLORS[TILETYPES[tile.tipo]['color']]['n'])) main_area['con'].addstr(cony + y - miny, conx + x - minx, '@', curses.color_pair(curses_wrapper.COLORS['blue']['n'])) except Exception as e: pass # since now it's visible, mark it as explored tile.explored = True if util.debug: log.debug("none appeared %d times" % c) self.flush(main_area)
def find_blind_open_tile(): x,y=find_open_tile() while libtcod.map_is_in_fov (P.player.fov, x, y): x,y=find_open_tile() return [x,y]