def test_get_key(self): """Test get_key() and that escape sets running = 0""" # Init video system for this test pygame.init() key = K_RIGHT # 1) We keep running # Post a new event for pressing right key pygame.event.post(pygame.event.Event(KEYDOWN, key=K_RIGHT)) key, running = get_key(key) self.assertEqual(running, 1) # 2) Escape pressed, time to quit # Post an event for pressing escape pygame.event.post(pygame.event.Event(KEYDOWN, key=K_ESCAPE)) key, running = get_key(key) self.assertEqual(running, 0)
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. message("Use the mouse or the keyboard to select a tile...", tcod.blue) prevcolor = tcod.black mouse_lastx, mouse_lasty = (x, y) = render.prev_mouse_pos while True: # render the screen. this erases the inventory and shows the names of # objects under the mouse. render.render_all() tcod.console_flush() tcod.console_set_char_background( render.con, x, y, prevcolor, tcod.BKGND_SET ) # set last tile's bg color to normal tcod.sys_check_for_event(tcod.EVENT_MOUSE | tcod.EVENT_KEY_PRESS, key, mouse) if mouse.dx or mouse.dy: (mouse_lastx, mouse_lasty) = (x, y) = (mouse.cx, mouse.cy) x = mouse.cx y = mouse.cy x = x + render.camera_x y = y + render.camera_y key_pressed = game.get_key(key) if key_pressed in direction_keys: direction = direction_keys[key_pressed] x += direction[0] y += direction[1] if tcod.map_is_in_fov(terrain.map.fov_map, x, y): prevcolor = tcod.console_get_char_background(render.con, x, y) # for resetting the color later tcod.console_set_char_background(render.con, x, y, tcod.sky, tcod.BKGND_SET) # visualising the target tile else: x, y = game.player.x, game.player.y # if not in fov, reset it to the player coords if mouse.rbutton_pressed or key.vk == tcod.KEY_ESCAPE: tcod.console_set_char_background(render.con, x, y, prevcolor, tcod.BKGND_SET) 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 or key.vk == tcod.KEY_ENTER) and tcod.map_is_in_fov(terrain.map.fov_map, x, y) and (max_range is None or game.player.distance(x, y) <= max_range) ): tcod.console_set_char_background(render.con, x, y, prevcolor, tcod.BKGND_SET) return (x, y)
def pick_direction(): (w, h) = (20, 2) window = tcod.console_new(w, h) tcod.console_set_default_foreground(window, tcod.white) text = "Pick a direction." tcod.console_print_rect(window, 0, 0, w, h, text) x = SCREEN_WIDTH // 2 - w // 2 y = SCREEN_HEIGHT // 2 - h // 2 tcod.console_blit(window, 0, 0, w, h, 0, x, y, 1.0, 0.7) tcod.console_flush() global key tcod.sys_wait_for_event(tcod.EVENT_KEY_PRESS, key, mouse, True) # special case: changing to/from fullscreen if key.vk == tcod.KEY_F11: tcod.console_set_fullscreen(not tcod.console_is_fullscreen()) else: key_pressed = game.get_key(key) if key_pressed in direction_keys: return direction_keys[key_pressed] elif key_pressed == tcod.KEY_ESCAPE or tcod.console_is_window_closed(): return None