def play_game(): global key, mouse player_action = None mouse = libtcod.Mouse() key = libtcod.Key() # main loop while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event( libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) # render the screen render_all() libtcod.console_flush() # level up if needed check_level_up() # erase all objects at their old locations, before they move for object in objects: object.clear() # handle keys and exit game if needed player_action = handle_keys() if player_action == 'exit': save_game() break # let monsters take their turn if game_state == 'playing' and player_action != 'didnt-take-turn': for object in objects: if object.ai: object.ai.take_turn()
def play_game(): player_action = None while not libtcod.console_is_window_closed(): #draw all the things render_all() #some other shit, flushing the console? libtcod.console_flush() #clear off the @ ghosts created by movement for stuff in objects: stuff.clear() #handle keys player_action = handle_keys() if player_action == 'exit': save_game() break #monsters turn if game_state == 'playing' and player_action != 'didnt-take-turn': for stuff in objects: if stuff.ai: stuff.ai.take_turn()
def main_loop(self): while not libtcod.console_is_window_closed(): self.__render_all() libtcod.console_flush() self.__clear_all() action = self.__handle_keys() if action == 'exit': break elif self.__state == 'playing' and action != 'pass': for entity in Entity.entities: if entity.ai: entity.ai.take_turn()
def menu(header, options, width): if len(options) > 26: raise ValueError( 'Cannot have a menu with more than 26 options.') # calculate total height for the header (after auto-wrap) and one # line per option header_height = libtcod.console_get_height_rect(con, 0, 0, width, SCREEN_HEIGHT, header) if header == '': header_height = 0 height = len(options) + header_height # create an off-screen console that represents the menu's window window = libtcod.console_new(width, height) # print the header, with auto-wrap libtcod.console_set_default_foreground(window, libtcod.white) libtcod.console_print_rect_ex(window, 0, 0, width, height, libtcod.BKGND_NONE, libtcod.LEFT, header) # print all the options y = header_height letter_index = ord('a') for option_text in options: text = '(' + chr(letter_index) + ') ' + option_text libtcod.console_print_ex(window, 0, y, libtcod.BKGND_NONE, libtcod.LEFT, text) y += 1 letter_index += 1 # blit the contents of "window" to the root console x = SCREEN_WIDTH / 2 - width / 2 y = SCREEN_HEIGHT / 2 - height / 2 libtcod.console_blit(window, 0, 0, width, height, 0, x, y, 1.0, 0.7) # present the root console to the player and wait for a key-press libtcod.console_flush() key = libtcod.console_wait_for_keypress(True) # (special case) Alt+Enter: toggle fullscreen if key.vk == libtcod.KEY_ENTER and key.lalt: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen) # convert the ASCII code to an index; if it corresponds to an # option, return it index = key.c - ord('a') if index >= 0 and index < len(options): return index return None
def target_tile(max_range=None): #return the position of a tile left-clicked in player's FOV (optionally in a range), or (None,None) if right-clicked. while True: #render the screen. this erases the inventory and shows the names of objects under the mouse. render_graphics() libtcod.console_flush() key = libtcod.console_check_for_keypress() mouse = libtcod.mouse_get_status() #get mouse position and click status (x, y) = (mouse.cx, mouse.cy) if mouse.rbutton_pressed or key.vk == libtcod.KEY_ESCAPE: return (None, None) #cancel if the player right-clicked or pressed Escape #accept the target if the player clicked in FOV, and in case a range is specified, if it's in that range if (mouse.lbutton_pressed and libtcod.map_is_in_fov(fov_map, x, y) and (max_range is None or player.distance(x, y) <= max_range)): return (x, y)
def target_tile(max_range=None): #return the position of a tile left-clicked in player's FOV (optionally in a range), or (None,None) if right-clicked. while True: #render the screen. this erases the inventory and shows the names of objects under the mouse. render_graphics() libtcod.console_flush() key = libtcod.console_check_for_keypress() mouse = libtcod.mouse_get_status( ) #get mouse position and click status (x, y) = (mouse.cx, mouse.cy) if mouse.rbutton_pressed or key.vk == libtcod.KEY_ESCAPE: return (None, None ) #cancel if the player right-clicked or pressed Escape #accept the target if the player clicked in FOV, and in case a range is specified, if it's in that range if (mouse.lbutton_pressed and libtcod.map_is_in_fov(fov_map, x, y) and (max_range is None or player.distance(x, y) <= max_range)): return (x, y)
def menu(header, options, width): if len(options) > 26: raise ValueError('Cannot have a menu with more than 26 options.') #calc height of header header_height = libtcod.console_height_left_rect(con, 0, 0, width, SCREEN_HEIGHT, header) if header == '': header_height = 0 height = len(options) + header_height #offscreen conolse window = libtcod.console_new(width, height) #print the header libtcod.console_set_foreground_color(window, libtcod.white) libtcod.console_print_left_rect(window, 0, 0, width, height, libtcod.BKGND_NONE, header) y = header_height letter_index = ord('a') for option_text in options: text = '(' + chr(letter_index) + ') ' + option_text libtcod.console_print_left(window, 0, y, libtcod.BKGND_NONE, text) y += 1 letter_index += 1 #blit the contents to the main console x = SCREEN_WIDTH / 2 - width / 2 y = SCREEN_HEIGHT / 2 - height / 2 libtcod.console_blit(window, 0, 0, width, height, 0, x, y, 1.0, 0.7) #wait for keypress libtcod.console_flush() key = libtcod.console_wait_for_keypress(True) #convert ascii to index index = key.c - ord('a') if index >= 0 and index < len(options): return index return None
def menu(header, options, width): if len(options) > 26: raise ValueError('Cannot have a menu with more than 26 options.') #calculate total height for the header (after auto-wrap) and one line per option header_height = libtcod.console_height_left_rect(con, 0, 0, width, SCREEN_HEIGHT, header) height = len(options) + header_height #create an off-screen console that represents the menu's window window = libtcod.console_new(width, height) #print the header, with auto-wrap libtcod.console_set_foreground_color(window, libtcod.white) libtcod.console_print_left_rect(window, 0, 0, width, height, libtcod.BKGND_NONE, header) #print all the options y = header_height letter_index = ord('a') for option_text in options: text = '(' + chr(letter_index) + ') ' + option_text libtcod.console_print_left(window, 0, y, libtcod.BKGND_NONE, text) y += 1 letter_index += 1 #blit the contents of "window" to the root console x = SCREEN_WIDTH / 2 - width / 2 y = SCREEN_HEIGHT / 2 - height / 2 libtcod.console_blit(window, 0, 0, width, height, 0, x, y, 1.0, 0.7) #present the root console to the player and wait for a key-press libtcod.console_flush() key = libtcod.console_wait_for_keypress(True) #convert the ASCII code to an index; if it corresponds to an option, return it index = key.c - ord('a') if index >= 0 and index < len(options): return index return None
def target_tile(max_range=None): global key, mouse # return the position of a tile left-clicked in player's FOV # (optionally in a range), or (None,None) if right-clicked. while True: # render the screen. this erases the inventory and shows the # names of objects under the mouse. libtcod.console_flush() libtcod.sys_check_for_event( libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) render_all() (x, y) = (mouse.cx, mouse.cy) if mouse.rbutton_pressed or key.vk == libtcod.KEY_ESCAPE: # cancel if the player right-clicked or pressed Escape return (None, None) # accept the target if the player clicked in FOV, and in case # a range is specified, if it's in that range if (mouse.lbutton_pressed and libtcod.map_is_in_fov(fov_map, x, y) and (max_range is None or player.distance(x, y) <= max_range)): return (x, y)
def menu(header, options, width): if len(options) > 26: raise ValueError('Cannot have a menu with more than 26 options.') #calc height of header header_height = libtcod.console_height_left_rect(con, 0, 0, width, SCREEN_HEIGHT, header) if header == '': header_height = 0 height = len(options) + header_height #offscreen conolse window = libtcod.console_new(width, height) #print the header libtcod.console_set_foreground_color(window, libtcod.white) libtcod.console_print_left_rect(window, 0, 0, width, height, libtcod.BKGND_NONE, header) y = header_height letter_index= ord('a') for option_text in options: text = '(' + chr(letter_index) + ') ' + option_text libtcod.console_print_left(window, 0, y, libtcod.BKGND_NONE, text) y += 1 letter_index += 1 #blit the contents to the main console x = SCREEN_WIDTH/2 - width/2 y = SCREEN_HEIGHT/2 - height/2 libtcod.console_blit(window, 0, 0, width, height, 0, x, y, 1.0, 0.7) #wait for keypress libtcod.console_flush() key = libtcod.console_wait_for_keypress(True) #convert ascii to index index = key.c - ord('a') if index >= 0 and index < len(options): return index return None
#a warm welcoming message! message('Welcome stranger! Prepare to perish in the Tombs of the Ancient Kings.', libtcod.red) sound_general_start.play() while not libtcod.console_is_window_closed(): #render the screen render_graphics() #recompute ambient sound voumes render_ambience() #random ambient sounds render_random_ambient_sounds() libtcod.console_flush() #erase all objects at their old locations, before they move for object in objects: object.clear() #handle keys and exit game if needed player_action = handle_input() if player_action == 'exit': break #let monsters take their turn if game_state == 'playing' and player_action != 'didnt-take-turn': for object in objects: if object.ai: object.ai.take_turn()
libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD) libtcod.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT, 'Amulet of Yelpdor', False) libtcod.sys_set_fps(LIMIT_FPS) console = libtcod.console_new(SCREEN_WIDTH, SCREEN_HEIGHT) district = District() dungeon_map = generate_city_map(MAP_WIDTH, MAP_HEIGHT, district) dungeon_map.init_fov_map() screen = Screen(width=SCREEN_WIDTH, height=SCREEN_HEIGHT) camera = Camera(CAMERA_WIDTH, CAMERA_HEIGHT, dungeon_map) renderer = Renderer(console, screen, camera) messenger = Messenger(width=MESSENGER_WIDTH, height=MESSENGER_HEIGHT, screen=screen) player = Player(dungeon_map.spawn[0], dungeon_map.spawn[1], '@', libtcod.white) dungeon_map.objects.append(player) stats = Stats(48, 3, player) amulet = Amulet(player, 48, 12, district) player.amulet = amulet player.set_level(dungeon_map, district) while not libtcod.console_is_window_closed(): renderer.render(player, dungeon_map, amulet, stats) messenger.render() libtcod.console_flush() if handle_keys(): break
def screenFlush(self): libtcod.console_flush()