def _get_code(self): code = terminal.read() while code not in self.used_codes: code = terminal.read() while terminal.has_input(): terminal.read() return code
def option_menu_input(): if terminal.has_input(): key = terminal.read() index = terminal.state(terminal.TK_CHAR) - ord('a') if key == terminal.TK_ESCAPE or index == 2: return OptionMenuSelection.BACK_TO_MAIN_MENU elif key == terminal.TK_CLOSE: save_game(World) terminal.close() sys.exit() elif index == 0: # change language if Texts.get_current_language() == 'fr': Texts.set_language('en') else: Texts.set_language('fr') show_main_options_menu() elif index == 1: # graphical mode if Interface.mode == GraphicalModes.ASCII: terminal.clear() Interface.change_graphical_mode(GraphicalModes.TILES) elif Interface.mode == GraphicalModes.TILES: terminal.clear() Interface.change_graphical_mode(GraphicalModes.ASCII) show_main_options_menu() return MainMenuSelection.NO_RESPONSE
def open_instance(self): self.item_option_index = 0 self.equipped_option_index = 0 self.initialize_window() self.prepare_window() self.refresh_window() self.activepopups.count += 1 terminal.refresh() self.proceed = True while self.proceed: while terminal.has_input() and self.proceed: self.get_next_char() # finished response if self.item_option_index is None or self.equipped_option_index is None: return None else: if self.selection_column == "items": if self.no_items: return None return self.item_options[self.item_option_index] else: if self.no_equipped_items: return None return self.equipped_items_options[self.equipped_option_index]
def handle_input(key=None): if key is None: if terminal.has_input(): key = terminal.read() if key == terminal.TK_H or key == terminal.TK_KP_4 or key == terminal.TK_LEFT: return {"move": (-1, 0)} elif key == terminal.TK_L or key == terminal.TK_KP_6 or key == terminal.TK_RIGHT: return {"move": (1, 0)} elif key == terminal.TK_K or key == terminal.TK_KP_8 or key == terminal.TK_UP: return {"move": (0, -1)} elif key == terminal.TK_J or key == terminal.TK_KP_2 or key == terminal.TK_DOWN: return {"move": (0, 1)} elif key == terminal.TK_Y or key == terminal.TK_KP_7: return {"move": (-1, -1)} elif key == terminal.TK_U or key == terminal.TK_KP_9: return {"move": (1, -1)} elif key == terminal.TK_B or key == terminal.TK_KP_1: return {"move": (-1, 1)} elif key == terminal.TK_N or key == terminal.TK_KP_3: return {"move": (1, 1)} elif key == terminal.TK_PERIOD or key == terminal.TK_KP_0 or key == terminal.TK_KP_PERIOD: return {"move": (0, 0)} elif key == terminal.TK_ESCAPE or key == terminal.TK_CLOSE: return {"exit": True} elif key == terminal.TK_R: return {"remake": True} return {}
def run_loop_iteration(self): while terminal.has_input(): char = terminal.read() self.terminal_read(char) should_continue = self.terminal_update() terminal.refresh() return should_continue
def game_main_loop(): game_quit = False while not game_quit: # clear blt.clear() # draw game draw_game() # refresh term blt.refresh() # avoid blocking the game with blt.read while not game_quit and blt.has_input(): player_action = game_handle_keys() map_calculate_fov() if player_action == "QUIT": game_quit = True # let the AIs take action if player_action != "no-action" and player_action != "mouse_click": for ent in GAME.current_entities: if ent.ai: ent.ai.take_turn() # save game save_game() # quit the game blt.close()
def update(): global key mouse.active_layer = 0 if terminal.has_input(): key = terminal.read() else: key = None return key
def play(self): playing = True while playing: if blt.has_input(): playing = self.input_handler.process() self.main_game_loop() blt.close()
def key_handling(): while blt.has_input(): key = blt.read() if key in QUIT_KEY: blt.close() return True print(key)
def target_tile(max_range=None): """Selects a tile either from a mouse click or selection""" x = player.x y = player.y mx = t.state(t.TK_MOUSE_X) my = t.state(t.TK_MOUSE_Y) render.draw_cursor(x, y) render.draw_max_range(player, max_range) message( "Select using the mouse or movements keys. Use M1, Enter, 5 to confirm and ESC/SPACE to cancel", colours.light_cyan) render_all() key = t.read() while key not in (t.TK_MOUSE_LEFT, t.TK_ENTER, t.TK_KP_ENTER, t.TK_KP_5, t.TK_ESCAPE, t.TK_SPACE): dx = x dy = y if key in DIR: (dir_x, dir_y) = DIR.get(key) dx += dir_x dy += dir_y zx = t.state(t.TK_MOUSE_X) zy = t.state(t.TK_MOUSE_Y) if zx != mx or zy != my: mx = zx my = zy dx = mx dy = my #Check for Out of Bounds/Range if 0 < dx and dx < MAP_WIDTH and 0 < dy and dy < MAP_HEIGHT: #Check for within range if player.distance(dx, dy) <= max_range and not gmap.is_blocked(dx, dy, None, tilesOnly=True) \ and gmap.is_explored(dx, dy): x = dx y = dy render.draw_cursor(x, y) if t.has_input(): key = t.read() else: key = None #Check if it was a mouse confirm or a key confirm/cancel render.draw_cursor(None, None) render.draw_max_range(None, None) if key == t.TK_MOUSE_LEFT: x = t.state(t.TK_MOUSE_X) y = t.state(t.TK_MOUSE_Y) #Cancel selection process elif key in (t.TK_ESCAPE, t.TK_SPACE): return (None, None) #Return coordinates selected return (x, y)
def _get_code(self): code = terminal.read() comand = self._convert_code_to_comand(code) while comand not in MAIN_MENU_USED_COMANDS: code = terminal.read() comand = self._convert_code_to_comand(code) while terminal.has_input(): terminal.read() return code
def handle_keys(): """All Key Handling for the game""" global fov_recompute global game_state keypress = False if t.has_input(): key = t.read() if key != t.TK_MOUSE_MOVE: keypress = True if not keypress: return 'no-turn' if key == t.TK_SLASH and t.state(t.TK_SHIFT): game_state = 'help' return 'help' if key == t.TK_ESCAPE: return 'exit' if game_state == 'playing': fov_recompute = True if key in (t.TK_J, t.TK_KP_8): player_move_or_attack(0, -1) elif key in (t.TK_K, t.TK_KP_2): player_move_or_attack(0, 1) elif key in (t.TK_H, t.TK_KP_4): player_move_or_attack(-1, 0) elif key in (t.TK_L, t.TK_KP_6): player_move_or_attack(1, 0) elif key in (t.TK_Y, t.TK_KP_7): player_move_or_attack(-1, -1) elif key in (t.TK_U, t.TK_KP_9): player_move_or_attack(1, -1) elif key in (t.TK_B, t.TK_KP_1): player_move_or_attack(-1, 1) elif key in (t.TK_N, t.TK_KP_3): player_move_or_attack(1, 1) elif key in (t.TK_SPACE, t.TK_KP_5): player_move_or_attack(0, 0) else: if key == t.TK_G: #pick up item for obj in objects: if obj.x == player.x and obj.y == player.y and obj.item: obj.item.pick_up() break elif key == t.TK_I: chosen_item = inventory_menu( 'Press the assigned key to use it, or any other to cancel.\n' ) if chosen_item is not None: chosen_item.use() return 'turn'
def get_new_direction(delay): new_direction = 'no direction' for i in range(delay): if terminal.has_input(): key = terminal.read() if key in CODE_TO_DIRECTION.keys(): new_direction = CODE_TO_DIRECTION[key] break return new_direction
def game_loop(game): while True: if blt.has_input(): code = blt.read() if code in codes_close: break ui.update(game, code) draw.update(game) blt.delay(1)
def input_escape_to_quit(): if terminal.has_input(): key = terminal.read() if key == terminal.TK_ESCAPE: return ItemMenuResult.SELECTED elif key == terminal.TK_CLOSE: save_game(World) terminal.close() return ItemMenuResult.NO_RESPONSE
def character_sheet_input(): if terminal.has_input(): key = terminal.read() if key == terminal.TK_ESCAPE: terminal.clear_area(0, 0, Interface.screen_width, Interface.screen_height) terminal.refresh() elif key == terminal.TK_CLOSE: save_game(World) terminal.close() sys.exit()
def main(self): # every frame while self.running: # read all of the events while blt.has_input(): event = blt.read() print(event) if event in (blt.TK_CLOSE, blt.TK_ESCAPE): self.running = False self.update() self.render()
def run_loop_iteration(self): while terminal.has_input(): char = terminal.read() if char == terminal.TK_CLOSE: return False if char == terminal.TK_C and blt_state.control: return False self.terminal_read(char) should_continue = self.terminal_update() terminal.refresh() return should_continue
def handleEvents(self): while term.has_input(): # try: event = term.read() if event in self.KEYMAP: self.handleKey(event) elif event is term.TK_MOUSE_MOVE: self.handleMouse(event) elif event in [term.TK_MOUSE_LEFT, term.TK_MOUSE_RIGHT]: self.handleClick(event) elif event in [term.TK_MOUSE_SCROLL]: self.handleScroll(event)
def main(): terminal.open() terminal.set('input.filter=[keyboard, close]') wl = Wall(FIELD_WIDTH, FIELD_HEIGHT, '+') wl.draw() snk = Snake(size=4) snk.draw() food = food_creator(snk) food.draw() terminal.refresh() while terminal.peek() != terminal.TK_CLOSE: if terminal.has_input(): snk.change_direction(terminal.read()) if snk.collision(wl) or snk.collision(snk): break if snk.collision(food): snk.eat(food) food = food_creator(snk) food.draw() else: snk.move() terminal.refresh() terminal.delay(100) if not terminal.has_input(): game_over() terminal.read() terminal.close()
def automatic_loop(state): ticks = 0 while state.automatic_mode: if ticks % FPS == 0: manager.current_state.logic() manager.current_state.render() ticks = 0 if terminal.has_input(): key = terminal.read() manager.current_state.handle_input(convert_to_action(key)) time.sleep(1 / FPS) ticks += 1
def blt_handle_global_input(game_state) -> str or int or None: command = {} if terminal.has_input(): key = terminal.read() if key == terminal.TK_CLOSE: exit(0) else: if not 88 < key < 99 and terminal.check(terminal.TK_CHAR): key = chr(terminal.state(terminal.TK_CHAR)) keymap = options.key_maps[0] if keymap.get(key) is not None: command = keymap.get(key) return command
def init(self): self.window_init() self.refresh_window() terminal.refresh() self.proceed = True self.return_reply = None while self.proceed: while terminal.has_input() and self.proceed: self.get_next_char() self.close() terminal.refresh() return self.return_reply
def inventory_selected_item_input(chosen_item): if terminal.has_input(): key = terminal.read() if key != terminal.TK_MOUSE_MOVE: if key == terminal.TK_ESCAPE: return ItemMenuResult.DESELECT, None elif key == terminal.TK_CLOSE: save_game(World) terminal.close() sys.exit() else: index = terminal.state(terminal.TK_CHAR) - ord('a') action_list = get_available_item_actions(chosen_item) if 0 <= index < len(action_list): return ItemMenuResult.ACTION, action_list[index] return ItemMenuResult.NO_RESPONSE, None
def spell_menu_input(known_spells): if terminal.has_input(): key = terminal.read() if key != terminal.TK_MOUSE_MOVE: if key == terminal.TK_ESCAPE: return ItemMenuResult.CANCEL, None, None elif key == terminal.TK_CLOSE: save_game(World) terminal.close() sys.exit() else: index = terminal.state(terminal.TK_CHAR) - ord('a') if 0 <= index < len(known_spells): print(f'spell select item {index} has been chosen.') return ItemMenuResult.SELECTED, States.TICKING, index return ItemMenuResult.NO_RESPONSE, None, None
def run(self): self.running = True while self.running: boss = self.boss level = self.level player = self.player if not player.dead: player.act() if self.should_restart is True: return else: if terminal.has_input(): key_press = terminal.read() if key_press == terminal.TK_CLOSE: sys.exit() elif key_press == terminal.TK_ESCAPE: self.should_restart = True return if player.moved: player.moved = False player.update() if not boss.dead: self.turn += 1 for actor in level.actors.copy(): if actor is player: continue if actor.level is not None: actor.act() actor.update() turn = self.turn if turn >= 10 and turn % 10 == 0 and not boss.dead and not player.dead: self.spawn_goblins(turn) if boss.dead and not level.get_actors_by_team(Team.Goblin): self.game_won = True if self.evolution_scene_active: self.evolution_scene.start() terminal.clear() self.scene.draw_top_gui() self.camera.draw() self.scene.update_messages() terminal.refresh()
def main_menu_input(): if terminal.has_input(): key = terminal.read() index = terminal.state(terminal.TK_CHAR) - ord('a') if key == terminal.TK_ESCAPE or index == 3: return MainMenuSelection.QUIT elif index == 0: return MainMenuSelection.NEWGAME elif index == 1: return MainMenuSelection.LOAD_GAME elif index == 2: return MainMenuSelection.OPTION elif key == terminal.TK_CLOSE: save_game(World) terminal.close() sys.exit() return MainMenuSelection.NO_RESPONSE
def run(): terminal.open() terminal.set("window: size=100x50, cellsize8x12, resizeable=true;") terminal.setf("font: Andux_cp866ish.png, size=8x12, codepage=437") terminal.composition(True) terminal.refresh() #p1 = Particle(50, 25, 0, 10, 90, 1) #p2 = Particle(50, 25, 0, 10, 70, 1) #emitter = Emitter(25, 25, 40, 90, .05, 2, 4) emitter = read_config() emitter.setColor([255, 138, 138, 255], [0, 0, 0, 0], [55, 138, 1, 255], [0, 0, 0, 0]) emitter.restart() #sudo_pool = [p1, p2] emitter.start(0) timer1 = time.perf_counter() flag = True while flag: timer2 = time.perf_counter() delta = timer2 - timer1 while terminal.has_input(): key = terminal.read() if key == 21: emitter = read_config() emitter.setColor([255, 138, 138, 255], [0, 0, 0, 0], [55, 138, 1, 255], [0, 0, 0, 0]) emitter.restart() emitter.start(0) if key == 27 or key == 224: flag = False emitter.update(delta, 1.0) terminal.clear() for p in emitter._particlePool: render(p, emitter._pos) terminal.refresh() system('clear') timer1 = time.perf_counter() exit
def known_cursed_inventory_input(item_list): # return ItemMenuResult, new_state, item selected if terminal.has_input(): key = terminal.read() if key != terminal.TK_MOUSE_MOVE: if key == terminal.TK_ESCAPE: return ItemMenuResult.CANCEL, None, None elif key == terminal.TK_CLOSE: save_game(World) terminal.close() sys.exit() else: index = terminal.state(terminal.TK_CHAR) - ord('a') if 0 <= index < len(item_list): print(f'curse removal input: item {index} has been chosen.') return ItemMenuResult.SELECTED, States.TICKING, item_list[index] return ItemMenuResult.NO_RESPONSE, None, None
def yes_no_input(): if terminal.has_input(): key = terminal.read() if key != terminal.TK_MOUSE_MOVE: if key == terminal.TK_ESCAPE: return YesNoResult.NO elif key == terminal.TK_CLOSE: save_game(World) terminal.close() sys.exit() else: index = terminal.state(terminal.TK_CHAR) - ord('a') if index == 0: return YesNoResult.NO elif index == 1: return YesNoResult.YES return YesNoResult.NO_RESPONSE
def update(): global key if terminal.has_input(): key = terminal.read() else: key = None
def clear(): while terminal.has_input(): terminal.read()