def run(self): UPDATE_INTERVAL_MS = self.update_interval_ms UPDATE_PER_FRAME_LIMIT = self.update_per_frame_limit clock = 0 previous_time = perf_counter() next_update = 0 proceed = True while proceed: current_time = perf_counter() if current_time - previous_time > (UPDATE_INTERVAL_MS * UPDATE_PER_FRAME_LIMIT): clock += UPDATE_INTERVAL_MS * UPDATE_PER_FRAME_LIMIT else: clock += current_time - previous_time while clock >= next_update: time_elapsed = UPDATE_INTERVAL_MS time_current = next_update self.update(time_elapsed, time_current) next_update += UPDATE_INTERVAL_MS previous_time = perf_counter() self.render() if terminal.has_input(): key = terminal.read() if key is terminal.TK_CLOSE: proceed = False else: proceed = self.process_input(key)
def main(): global WINDOW_WIDTH, WINDOW_HEIGHT, CELLSIZE blt.open_() blt.set_("window: size={}x{}, cellsize={}, title='Grid Test';" "font: default".format( str(WINDOW_WIDTH), str(WINDOW_HEIGHT), CELLSIZE)) blt.clear() blt.refresh() blt.color("white") g = Quadrant() blt.clear() for x in range(30): for y in range(30): if [x, y] in g.all_nodes: blt.print_(x * 2, y * 2, 'O') elif [x, y] in g.roads: # if x != 0 and x != 10 and y != 0 and y != 10: # blt.layer(1) # blt.print_(x * 2, y * 2, str(g.roads.index([x, y]))) # blt.layer(0) # else: blt.print_(x * 2, y * 2, '+') blt.refresh() proceed = True while proceed: key = 0 while blt.has_input(): key = blt.read() if key == blt.TK_CLOSE or key == blt.TK_Q: proceed = False blt.close()
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)!!! header_height = len(textwrap.wrap(header, width)) height = len(options) + header_height #set co-ords of menu menu_x = SCREEN_WIDTH/2 - width/2 menu_y = SCREEN_HEIGHT/2 - height/2 #paint menu background terminal.layer(5) terminal.color(MENU_BACKGROUND_COLOR) #menu for y_bg in range(height): for x_bg in range(width): terminal.put(menu_x + x_bg, menu_y + y_bg, 20) #print the header, with auto-wrap terminal.layer(6) terminal.color('#FFFFFF') y = 0 for line in textwrap.wrap(header, width): terminal.print_(menu_x, menu_y + y, line) y += 1 #position of options, below header (y) y = menu_y + header_height letter_index = ord('a') #print all the options for option_text in options: text = '(' + chr(letter_index) + ') ' + option_text #libtcod.console_print_ex(window, 0, y, libtcod.BKGND_NONE, libtcod.LEFT, text) terminal.print_(menu_x, y, text) y += 1 letter_index += 1 #present the root console to the player and wait for a key-press terminal.refresh() key = terminal.read() #temporary halt of operation, wait for keypress/click #clear the menu from screen terminal.layer(5) terminal.clear_area(menu_x, menu_y, width, height) if terminal.state(terminal.TK_CHAR): #!! maybe no if statement here? #convert the ASCII code to an index; if it corresponds to an option, return it index = terminal.state(terminal.TK_CHAR) - 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. global key, mouse while True: #render the screen. this erases the inventory and shows the names of objects under the mouse. render_all() terminal.refresh() key = terminal.read() #temporary halt of operation, wait for keypress/click #render_all() (x, y) = (terminal.state(terminal.TK_MOUSE_X), terminal.state(terminal.TK_MOUSE_Y)) #if mouse.rbutton_pressed or key.vk == libtcod.KEY_ESCAPE: if key == terminal.TK_ESCAPE or key == terminal.TK_MOUSE_RIGHT: 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 if (key == terminal.TK_MOUSE_LEFT 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 handle_keys(): #global fov_recompute # Read keyboard input global key key = terminal.read() if key == terminal.TK_ESCAPE: # Close terminal return 'exit' if game_state == 'playing': #??? it was 'exit()' a = 'player moved' if key == terminal.TK_KP_2: player_move_or_attack(0, 1) return a elif key == terminal.TK_KP_8: player_move_or_attack(0, -1) return a elif key == terminal.TK_KP_6: player_move_or_attack(1, 0) return a elif key == terminal.TK_KP_4: player_move_or_attack(-1, 0) return a elif key == terminal.TK_KP_7: player_move_or_attack(-1, -1) return a elif key == terminal.TK_KP_9: player_move_or_attack(1, -1) return a elif key == terminal.TK_KP_1: player_move_or_attack(-1, 1) return a elif key == terminal.TK_KP_3: player_move_or_attack(1, 1) return a elif key == terminal.TK_KP_5: return a else: #test for other keys if key == terminal.TK_G: #pick up an item for object in objects: #look for an item in the player's tile if object.x == player.x and object.y == player.y and object.item: object.item.pick_up() break if key == terminal.TK_I: #show the inventory; if an item is selected, use it chosen_item = inventory_menu('Press the key next to an item to use it, or any other to cancel.\n') if chosen_item is not None: chosen_item.use() if key == terminal.TK_D: #show the inventory; if an item is selected, drop it chosen_item = inventory_menu('Press the key next to an item to drop it, or any other to cancel.\n') if chosen_item is not None: chosen_item.drop() if key == terminal.TK_C: #show character info level_up_xp = LEVEL_UP_BASE + player.level * LEVEL_UP_FACTOR msgbox('Character Information\n\nLevel: ' + str(player.level) + '\nExperience: ' + str(player.fighter.xp) + ' / ' + str(level_up_xp) + '\n\nMaximum HP: ' + str(player.fighter.max_hp) + '\nAttack: ' + str(player.fighter.power) + '\nDefense: ' + str(player.fighter.defense), CHARACTER_SCREEN_WIDTH) if key == terminal.TK_SHIFT: key = terminal.read() if key == terminal.TK_PERIOD and stairs.x == player.x and stairs.y == player.y: #go down stairs, if the player is on them next_level() if key == terminal.TK_BACKSPACE: debug() return 'didnt-take-turn' return 'didnt-take-turn'
bl.open() bl.refresh() SCREEN_WIDTH = bl.state(bl.TK_WIDTH) SCREEN_HEIGHT = bl.state(bl.TK_HEIGHT) the_map = Map(SCREEN_WIDTH, SCREEN_HEIGHT, 3, 3, 9, 9) at = AtMan(5, 5, the_map) left_apostrophe = MapChild(0, 1, "'", the_map) right_apostrophe = MapChild(2, 1, "'", the_map) dud = MapChild(4, 4, "\u2193", the_map) the_map.draw_view() eventhandler.register(the_map, *ARROW_EVENTS + WASD_EVENTS) menu = ui.Menu(60, 0, SCREEN_WIDTH - 1, SCREEN_HEIGHT - 1) paint(gridtools.hollow_rectangle(2, 2, 10, 10)) paint(gridtools.rectangle(60, 0, SCREEN_WIDTH - 1, SCREEN_HEIGHT - 1)) while True: drawing_system.run() ui_drawing_system.run() event = bl.read() if event == bl.TK_CLOSE or event == bl.TK_ESCAPE: break eventhandler.fire(event) bl.put(1, 1, bl.state(bl.TK_WCHAR)) bl.refresh() bl.close()
import PyBearLibTerminal as terminal terminal.open() terminal.printf(2, 1, "β") terminal.put(2, 2, "β") terminal.refresh() while True: if terminal.has_input(): key = terminal.read() print(key) if key == terminal.TK_Q | terminal.TK_KEY_RELEASED: print("released") break elif key == terminal.TK_Q: break terminal.close()
color10 = bltColor('sky') line.draw_line((l1[0], l1[1]), (l2[0], l2[1]), color10) terminal.color(bltColor('255, 255,64,64')) terminal.print_(15, 8, "[wrap=25x5][align=left-bottom]Hello my name is rudy!!!!!!!! I want to be your friend") terminal.color('yellow') terminal.printf(l2[0] + 1, l2[1], '<') terminal.refresh() if terminal.has_input(): key = terminal.read() if key == terminal.TK_CLOSE: break elif key == terminal.TK_UP: l2 = (l2[0], l2[1] - 1) elif key == terminal.TK_DOWN: l2 = (l2[0], l2[1] + 1) elif key == terminal.TK_LEFT: l2 = (l2[0] - 1, l2[1]) elif key == terminal.TK_RIGHT: l2 = (l2[0] + 1, l2[1]) terminal.close()