def test_image(console, tmpdir): img = libtcodpy.image_new(16, 16) libtcodpy.image_clear(img, libtcodpy.Color(0, 0, 0)) libtcodpy.image_invert(img) libtcodpy.image_hflip(img) libtcodpy.image_rotate90(img) libtcodpy.image_vflip(img) libtcodpy.image_scale(img, 24, 24) libtcodpy.image_set_key_color(img, libtcodpy.Color(255, 255, 255)) libtcodpy.image_get_alpha(img, 0, 0) libtcodpy.image_is_pixel_transparent(img, 0, 0) libtcodpy.image_get_size(img) libtcodpy.image_get_pixel(img, 0, 0) libtcodpy.image_get_mipmap_pixel(img, 0, 0, 1, 1) libtcodpy.image_put_pixel(img, 0, 0, libtcodpy.Color(255, 255, 255)) libtcodpy.image_blit(img, console, 0, 0, libtcodpy.BKGND_SET, 1, 1, 0) libtcodpy.image_blit_rect(img, console, 0, 0, 16, 16, libtcodpy.BKGND_SET) libtcodpy.image_blit_2x(img, console, 0, 0) libtcodpy.image_save(img, tmpdir.join('test.png').strpath) libtcodpy.image_delete(img) # Not portable. #img = libtcodpy.image_from_console(console) #libtcodpy.image_refresh_console(img, console) #libtcodpy.image_delete(img) libtcodpy.image_delete(libtcodpy.image_load('../data/img/circle.png'))
def draw_main_menu(con, has_file=False): tcod.console_set_default_foreground(con, COL_A) img = tcod.image_load('small.png') tcod.image_set_key_color(img, tcod.red) tcod.image_blit(img, 0, 45, 30, tcod.BKGND_LIGHTEN, .5, .25, 0) xx=-20 yy=15 can_cont = "" can_del = '' if has_file: can_cont = '-- (c)ontinue' can_del = '-- (D)elete' options=( """ GAME TITLE """+chr(tcod.CHAR_BLOCK2)+chr(tcod.CHAR_BLOCK1)+chr(tcod.CHAR_BLOCK1)+""" | | \t-- (n)ew game | \t--\t%s | | | \t%s | \t-- (esc)cape """ % (can_cont, can_del) ) tcod.console_print_ex(con, game.GAME_WIDTH/4+xx, game.GAME_HEIGHT/3+yy, tcod.BKGND_LIGHTEN, tcod.LEFT,options) tcod.console_print(con, 2, game.GAME_HEIGHT-2, 'oyyooyoyoyoyyooyoyoy') tcod.console_flush() tcod.console_clear(con)
def image(self): if self._image is None: cached_image = Item.image_dict.get(self.name) if cached_image is None: self._image = tcod.image_load(os.path.join("images", "items", self.name) + ".png") tcod.image_set_key_color(self._image, tcod.pink) Item.image_dict[self.name] = self._image else: self._image = cached_image return self._image
def reset_map(self, pos=None): """Reset light map. If pos is a list of Positions, only reset those areas""" if self.light_enabled: assert not self.pos is None and not self.map is None, "resetting LightSource that is not placed on map" libtcod.image_clear(self.__tcod_light_image, self.raw_light_colour * self.intensity) libtcod.image_set_key_color(self.__tcod_light_image, libtcod.black) else: libtcod.image_clear(self.__tcod_light_image, libtcod.black) libtcod.image_set_key_color(self.__tcod_light_image, libtcod.black)
def print_char_big(self, icon_position, destination, console=0): source_pixel_x = (icon_position % ROW_LENGTH) * settings.TILE_WIDTH source_pixel_y = (icon_position / ROW_LENGTH) * settings.TILE_WIDTH dx, dy = destination if not self.font_image: self.font_image = libtcod.image_load(FONT_FILE_PATH) libtcod.image_set_key_color(self.font_image, libtcod.Color(0, 0, 0)) libtcod.image_blit_2x(self.font_image, console, dx, dy, source_pixel_x, source_pixel_y, settings.TILE_WIDTH, settings.TILE_WIDTH)
def display_screen(self): # display current screen contents, including graphics layer if active for x in range(self.width): for y in range(self.height): libtcod.console_put_char_ex(self.con, x, y, self.screen[x][y], self.foreground, self.background) libtcod.console_blit(self.con, 0, 0, self.width, self.height, 0, 0, 0) if self.graphics_mode and self.img is not None: libtcod.image_set_key_color(self.img, self.background) libtcod.image_blit_2x(self.img, 0, 0, 0) libtcod.console_flush()
def beastiary(width=10, height=10, title=None, text=None): # calculate total height for the header (after auto-wrap) and one line per option if width is None: width = Constants.MAP_CONSOLE_WIDTH - 10 if height is None: height = libtcod.console_get_height_rect(0, 0, 0, width, Constants.SCREEN_HEIGHT, text) + 7 pop = libtcod.console_new(width, height) # print the header, with auto-wrap libtcod.console_set_default_foreground(pop, Constants.UI_PopFore) libtcod.console_set_default_background(pop, Constants.UI_PopBack) libtcod.console_print_frame(pop, 0, 0, width, height, clear=True, flag=libtcod.BKGND_SET, fmt=title) # blit the contents of "window" to the root console x = 0 y = 0 button_text = 'Click to Continue' button = Button(button_text, width / 2, height - 3, function=close_window) img = libtcod.image_load('Images//cipher_warden_80x80_test_01.png') libtcod.image_set_key_color(img, Color(0, 0, 0)) # show the background image, at twice the regular console resolution libtcod.image_blit_2x(img, pop, 9, 2) libtcod.console_set_default_foreground(pop, Constants.UI_PopFore) libtcod.console_set_default_background(pop, Constants.UI_PopBack) Render.print_rect(pop, 3, 3, width - 6, height, text) background = libtcod.console_new(Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGHT) libtcod.console_blit(0, 0, 0, Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGHT, background, 0, 0, 1.0, 1.0) dragging = False click_x = None while True: Input.update() mouse = Input.mouse Render.blit(background, 0) # libtcod.console_blit(background, 0, 0, Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGHT, 0, 0, 0, 1.0, 1.0) libtcod.console_blit(pop, 0, 0, width, height, 0, x, y, 1.0, .85) if mouse.lbutton and x <= mouse.cx <= x + width and (mouse.cy == y or dragging): if click_x is None: click_x = mouse.cx - x x = mouse.cx - click_x # (width / 2) y = mouse.cy dragging = True else: dragging = False click_x = None if button.draw(x, y) == 'close': return libtcod.console_flush()
def beastiary(width=10, height=10, title=None, text=None): # calculate total height for the header (after auto-wrap) and one line per option if width is None: width = Constants.MAP_CONSOLE_WIDTH - 10 if height is None: height = libtcod.console_get_height_rect( 0, 0, 0, width, Constants.SCREEN_HEIGHT, text) + 7 pop = libtcod.console_new(width, height) # print the header, with auto-wrap libtcod.console_set_default_foreground(pop, Constants.UI_PopFore) libtcod.console_set_default_background(pop, Constants.UI_PopBack) libtcod.console_print_frame(pop, 0, 0, width, height, clear=True, flag=libtcod.BKGND_SET, fmt=title) # blit the contents of "window" to the root console x = 0 y = 0 button_text = 'Click to Continue' button = Button(button_text, width / 2, height - 3, function=close_window) img = libtcod.image_load('Images//cipher_warden_80x80_test_01.png') libtcod.image_set_key_color(img, Color(0, 0, 0)) # show the background image, at twice the regular console resolution libtcod.image_blit_2x(img, pop, 9, 2) libtcod.console_set_default_foreground(pop, Constants.UI_PopFore) libtcod.console_set_default_background(pop, Constants.UI_PopBack) Render.print_rect(pop, 3, 3, width - 6, height, text) background = libtcod.console_new(Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGHT) libtcod.console_blit(0, 0, 0, Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGHT, background, 0, 0, 1.0, 1.0) dragging = False click_x = None while True: Input.update() mouse = Input.mouse Render.blit(background, 0) # libtcod.console_blit(background, 0, 0, Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGHT, 0, 0, 0, 1.0, 1.0) libtcod.console_blit(pop, 0, 0, width, height, 0, x, y, 1.0, .85) if mouse.lbutton and x <= mouse.cx <= x + width and (mouse.cy == y or dragging): if click_x is None: click_x = mouse.cx - x x = mouse.cx - click_x # (width / 2) y = mouse.cy dragging = True else: dragging = False click_x = None if button.draw(x, y) == 'close': return libtcod.console_flush()
def reset_map(self, pos=None): """Reset light map. If pos is a list of Positions, only reset those areas""" if not self.light_enabled: libtcod.image_clear(self.__tcod_light_image, libtcod.black) libtcod.image_set_key_color(self.__tcod_light_image, libtcod.black) return assert not self.pos is None and not self.map is None, "resetting LightSource that is not placed on map" # [re-]calculating FOV of light within its map if pos is None: libtcod.map_clear(self.__tcod_light_map, False, False) cov = {} for o in self.map.find_all_within_r(self, Transparent, self.radius): # if there's something here already and it blocks light, light is blocked at pos if cov.get(o.pos, True): cov[o.pos] = not o.blocks_light() for (p, is_transparent) in cov.items(): # we're using the walkable bit to show that there is a tile that could be lit libtcod.map_set_properties(self.__tcod_light_map, self.radius + p.x - self.pos.x, self.radius + p.y - self.pos.y, is_transparent, True) else: if not isinstance(pos, list): pos = [pos] skip_calc = True for p in pos: if self.pos.distance_to(p) > self.radius: # pos isn't covered by this light; do nothing pass else: skip_calc = False is_transparent = True for o in self.map.find_all_at_pos(p): if isinstance(o, Transparent) and o.blocks_light(): is_transparent = False break libtcod.map_set_properties(self.__tcod_light_map, self.radius + p.x - self.pos.x, self.radius + p.y - self.pos.y, is_transparent, True) if skip_calc: # all pos were outside of light radius! return self.prepare_fov( False ) # TODO: calculate both True and False; use True only if light in LOS of player # use FOV data to create an image of light intensity, masked by opaque tiles # can optimise based on pos P: only need to recalculate area X # --- ---XX --- --XXX # / \ / XX / \ / XXX do this by splitting into quarters # | P| | PX | | | XXX and working out which to recalculate # | L | | L | | LP | | LPXX based on P-L # | | | | | | | XXX # \ / \ / \ / \ XXX # --- --- --- --XXX libtcod.image_clear(self.__tcod_light_image, libtcod.black) libtcod.image_set_key_color(self.__tcod_light_image, libtcod.black) r = self.radius rd2 = r / 2 i1 = self.raw_light_colour * self.intensity for x in range(r * 2 + 1): for y in range(r * 2 + 1): #print("(%d,%d)"%(x,y)) if libtcod.map_is_in_fov(self.__tcod_light_map, x, y): d = hypot(r - x, r - y) if d > rd2: libtcod.image_put_pixel(self.__tcod_light_image, x, y, i1 * (1.0 - (d - rd2) / rd2)) #print(" %s %s"%(d,i1*(1.0-(d-rd2)/rd2))) else: libtcod.image_put_pixel(self.__tcod_light_image, x, y, i1)
def play_arena(ui_con): console_buffer = tcod.ConsoleBuffer(constant.SCREEN_WIDTH, constant.SCREEN_HEIGHT) map_size = (constant.SCREEN_WIDTH * 1, constant.SCREEN_HEIGHT * 1) the_map = game_map.make_map(*map_size) fov_map = tcod.map_new(*map_size) player = entity.Entity(map_size[0] / 2, map_size[1] / 2, "@", tcod.black) player.inventory_component = item.Inventory(player, constant.INENTORY_SIZE[0], constant.INENTORY_SIZE[1], constant.WEIGHT_LIMIT) blah = "Makarov PM" test_makarov = item.Item(blah, item_types.firearms[blah][-1]) test_makarov.gun_component = item.Gun(test_makarov, *item_types.firearms[blah][:-1]) makarov_entity = entity.Entity(map_size[0] / 2 + 1, map_size[1] / 2 + 1, "]", tcod.dark_grey, item_component=test_makarov, is_walkable=True) entity_list = [player, makarov_entity] camera_x, camera_y = (player.x, player.y) fov_map = fov.update_fov_map(the_map, fov_map) tcod.map_compute_fov(fov_map, player.x, player.y, constant.VISION_RANGE, True, tcod.FOV_BASIC) fov_recompute = True in_menu = False selected_inv_square = None test = tcod.image_load(os.path.join("images", "weapons", "Makarov PM.png")) tcod.image_set_key_color(test, tcod.pink) # Set initial values for key and mouse event; required to pass into sys_check_for_event key = tcod.console_check_for_keypress(tcod.KEY_PRESSED) mouse_status = tcod.mouse_get_status() while True: # Get input tcod.sys_check_for_event(tcod.EVENT_KEY_PRESS | tcod.EVENT_MOUSE, key, mouse_status) if not in_menu: # Update camera. This must be done before rendering (center_x, center_y) = geometry.get_point_ahead(player.x, player.y, mouse_status.cx + camera_x, mouse_status.cy + camera_y, constant.CAMERA_DISTANCE) (camera_x, camera_y) = geometry.update_camera(center_x, center_y, the_map.width, the_map.height) player_facing_point = (mouse_status.cx, mouse_status.cy) # Update FOV if fov_recompute: tcod.map_compute_fov(fov_map, player.x, player.y, constant.VISION_RANGE, True, tcod.FOV_BASIC) fov.update_entity_fov(entity_list, the_map, fov_map) # Render the map and entities the_map.render(console_buffer, fov_map, camera_x, camera_y, player.x, player.y, *player_facing_point) console_buffer.blit(0) # Only entities in the player's line of sight should be drawn for _entity in reversed(entity_list): if fov.in_player_fov(_entity.x, _entity.y, player.x, player.y, mouse_status.cx + camera_x, mouse_status.cy + camera_y, fov_map): _entity.render(0, camera_x, camera_y) # fps display tcod.console_print_ex(0, constant.SCREEN_WIDTH - 1, 0, tcod.BKGND_NONE, tcod.RIGHT, str(tcod.sys_get_fps())) # If in inventory, draw inventory grid if in_menu == "inventory": tcod.mouse_show_cursor(True) tcod.console_clear(ui_con) ui.draw_checkerboard(ui_con, constant.INENTORY_SIZE[0], constant.INENTORY_SIZE[1], constant.SQUARE_SIZE, tcod.grey, tcod.dark_grey) ui.draw_inventory_items(ui_con, player.inventory_component) if selected_inv_square is not None: tcod.console_print_frame(ui_con, selected_inv_square[0] * constant.SQUARE_SIZE, selected_inv_square[1] * constant.SQUARE_SIZE, constant.SQUARE_SIZE, constant.SQUARE_SIZE, False, tcod.BKGND_NONE, False) tcod.console_blit(ui_con, 0, 0, constant.INENTORY_SIZE[0] * constant.SQUARE_SIZE, constant.INENTORY_SIZE[1] * constant.SQUARE_SIZE, 0, constant.INVENTORY_X, constant.INVENTORY_Y) tcod.console_flush() fov_recompute = False # Handle input if not in_menu: if key.vk == tcod.KEY_LEFT: # Move left if not entity.check_collision(player.x - 1, player.y, the_map, entity_list): player.x -= 0 if player.x == 0 else 1 fov_recompute = True elif key.vk == tcod.KEY_RIGHT: # Move right if not entity.check_collision(player.x + 1, player.y, the_map, entity_list): player.x += 0 if player.x == the_map.width else 1 fov_recompute = True elif key.vk == tcod.KEY_UP: # Move up if not entity.check_collision(player.x, player.y - 1, the_map, entity_list): player.y -= 0 if player.y == 0 else 1 fov_recompute = True elif key.vk == tcod.KEY_DOWN: # Move down if not entity.check_collision(player.x, player.y + 1, the_map, entity_list): player.y += 0 if player.y == the_map.height else 1 fov_recompute = True elif key.c == ord("i"): in_menu = "inventory" elif key.c == ord(","): for _entity in entity_list: if (_entity.item_component is not None and _entity.x == player.x and _entity.y == player.y): player.inventory_component.add(_entity.item_component) entity_list.remove(_entity) elif key.vk == tcod.KEY_ESCAPE: # Quit back to main menu break elif in_menu == "inventory": if mouse_status.lbutton_pressed: prev_square = selected_inv_square selected_inv_square = ((mouse_status.cx - constant.INVENTORY_X) / constant.SQUARE_SIZE, (mouse_status.cy - constant.INVENTORY_Y) / constant.SQUARE_SIZE) if selected_inv_square == prev_square: selected_inv_square = None elif not ((0 <= selected_inv_square[0] < constant.INENTORY_SIZE[0]) and (0 <= selected_inv_square[1] < constant.INENTORY_SIZE[1])): selected_inv_square = prev_square elif key.c == ord("i"): tcod.mouse_show_cursor(False) in_menu = ""
def reset_map(self, pos=None): """Reset light map. If pos is a list of Positions, only reset those areas""" if not self.light_enabled: libtcod.image_clear(self.__tcod_light_image, libtcod.black) libtcod.image_set_key_color(self.__tcod_light_image, libtcod.black) return assert not self.pos is None and not self.map is None, "resetting LightSource that is not placed on map" # [re-]calculating FOV of light within its map if pos is None: libtcod.map_clear(self.__tcod_light_map, False, False) cov = {} for o in self.map.find_all_within_r(self, Transparent, self.radius): # if there's something here already and it blocks light, light is blocked at pos if cov.get(o.pos, True): cov[o.pos] = not o.blocks_light() for (p, is_transparent) in cov.items(): # we're using the walkable bit to show that there is a tile that could be lit libtcod.map_set_properties(self.__tcod_light_map, self.radius + p.x - self.pos.x, self.radius + p.y - self.pos.y, is_transparent, True) else: if not isinstance(pos, list): pos = [pos] skip_calc = True for p in pos: if self.pos.distance_to(p) > self.radius: # pos isn't covered by this light; do nothing pass else: skip_calc = False is_transparent = True for o in self.map.find_all_at_pos(p): if isinstance(o, Transparent) and o.blocks_light(): is_transparent = False break libtcod.map_set_properties(self.__tcod_light_map, self.radius + p.x - self.pos.x, self.radius + p.y - self.pos.y, is_transparent, True) if skip_calc: # all pos were outside of light radius! return self.prepare_fov(False) # TODO: calculate both True and False; use True only if light in LOS of player # use FOV data to create an image of light intensity, masked by opaque tiles # can optimise based on pos P: only need to recalculate area X # --- ---XX --- --XXX # / \ / XX / \ / XXX do this by splitting into quarters # | P| | PX | | | XXX and working out which to recalculate # | L | | L | | LP | | LPXX based on P-L # | | | | | | | XXX # \ / \ / \ / \ XXX # --- --- --- --XXX libtcod.image_clear(self.__tcod_light_image, libtcod.black) libtcod.image_set_key_color(self.__tcod_light_image, libtcod.black) r = self.radius rd2 = r / 2 i1 = self.raw_light_colour * self.intensity for x in range(r * 2 + 1): for y in range(r * 2 + 1): #print("(%d,%d)"%(x,y)) if libtcod.map_is_in_fov(self.__tcod_light_map, x, y): d = hypot(r - x, r - y) if d > rd2: libtcod.image_put_pixel(self.__tcod_light_image, x, y, i1 * (1.0 - (d - rd2) / rd2)) #print(" %s %s"%(d,i1*(1.0-(d-rd2)/rd2))) else: libtcod.image_put_pixel(self.__tcod_light_image, x, y, i1)