我们从Python开源项目中,提取了以下4个代码示例,用于说明如何使用pygame.K_8。
def get_num(): got_num = "" while(True): for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit(0) if event.type == pygame.KEYDOWN: if event.key == pygame.K_0: got_num += "0" if event.key == pygame.K_1: got_num += "1" if event.key == pygame.K_2: got_num += "2" if event.key == pygame.K_3: got_num += "3" if event.key == pygame.K_4: got_num += "4" if event.key == pygame.K_5: got_num += "5" if event.key == pygame.K_6: got_num += "6" if event.key == pygame.K_7: got_num += "7" if event.key == pygame.K_8: got_num += "8" if event.key == pygame.K_9: got_num += "9" if event.key == pygame.K_RETURN: if got_num != "": return int(got_num) #Call get_nested_values recursively if value is a dict or list, otherwise just # append value to values.
def __init__(self, screen): self.done = False self.screen = screen self.clock = pg.time.Clock() self.fps = 60 self.RADIO_STATION = '39' # or 1591330390268116913 self.BACKGROUND_IMG = 'img/bg/day_tree.png' self.SCREEN_WIDTH = SCREEN_WIDTH self.SCREEN_HEIGHT = SCREEN_HEIGHT self.RADIO_RUNNING = False self.RESTING = False self.SHOWGOALS = False self.grass = 'img/bg/day_grass.png' self.day_clouds = pg.sprite.RenderUpdates() self.night_clouds = pg.sprite.RenderUpdates() self.all_sprites_list = pg.sprite.RenderUpdates() self.quote_list = return_list('quotes.txt') self.contributions = return_list('contributions.txt') self.goals = return_list('goals.txt') self.weather = Weather() self.weather_observation = {} self.phrase = random.choice(self.quote_list) #self.cwd = os.getcwd() #windows self.cwd = '/home/pi/.config/pianobar' # linux ## DAIYE COLORS ### self.HEADER_COLOR = pg.Color('black') self.CONTENT_COLOR = pg.Color('indianred4') self.BG_COLOR = pg.Color('skyblue') self.current_events = [] self.upcoming_events = [] # User events: self.UPDATECALENDAR = pg.USEREVENT + 1 self.UPDATEQUOTE = pg.USEREVENT + 2 self.NIGHTRADIO = pg.USEREVENT + 3 self.CHANGESTATE = pg.USEREVENT + 4 self.UPDATEWEATHER = pg.USEREVENT + 5 #self.SHOWGOALS = pg.USEREVENT + 6 pg.time.set_timer(self.UPDATECALENDAR, 60000) #update calendar every 60 seconds pg.time.set_timer(self.UPDATEQUOTE, 20000) #update quote every 20 seconds pg.time.set_timer(self.NIGHTRADIO,300000) # check for relaxation radio time pg.time.set_timer(self.UPDATEWEATHER, 600000) pg.time.set_timer(self.CHANGESTATE, 300000) #pg.time.set_timer(self.SHOWGOALS, 6000) self.DAYGFX = load_gfx(os.path.join("img", "clouds", "day")) self.NTGFX = load_gfx(os.path.join("img", "clouds", "night")) self.BGIMG = load_gfx(os.path.join('img', 'bg')) self.keymap_dict = {pg.K_n: 'n', pg.K_PLUS: '+', pg.K_KP_PLUS: '+', pg.K_EQUALS: '+', pg.K_MINUS: '-', pg.K_KP_MINUS: '-', pg.K_p: 'p', pg.K_SPACE: 'p', pg.K_q: 'q', pg.K_r: 'r', pg.K_s: 's', pg.K_1: 's6\n', pg.K_2: 's4\n', pg.K_3: 's15\n', pg.K_4: 's25\n', pg.K_5: 's48\n', pg.K_6: 's37\n', pg.K_7: 's52\n', pg.K_8: 's16\n'}
def character_key(self, key): ''' Handles the rest of the keys ''' if key == pygame.K_LSHIFT or key == pygame.K_RSHIFT: return # Skip the event for the shift key itself try: # Handle the number pad if key == pygame.K_KP0: key = pygame.K_0 elif key == pygame.K_KP1: key = pygame.K_1 elif key == pygame.K_KP2: key = pygame.K_2 elif key == pygame.K_KP3: key = pygame.K_3 elif key == pygame.K_KP4: key = pygame.K_4 elif key == pygame.K_KP5: key = pygame.K_5 elif key == pygame.K_KP6: key = pygame.K_6 elif key == pygame.K_KP7: key = pygame.K_7 elif key == pygame.K_KP8: key = pygame.K_8 elif key == pygame.K_KP9: key = pygame.K_9 elif pygame.key.get_mods() & pygame.KMOD_SHIFT: if key >= 97 and key <= 122: key = ord(chr(key).upper()) else: if KEY_TO_SHIFT_MAP.get(chr(key), None) is not None: key = ord(KEY_TO_SHIFT_MAP[chr(key)]) self.text = ''.join(( self.text[:self.cursor_position], chr(key), self.text[self.cursor_position:] )) self.cursor_position += 1 except ValueError: self.game_engine.hud.display_hint("Attempt to enter an invalid character!", 2) return
def handle_controls(drone, event, pid): # takeoff / land if event.key == pygame.K_RETURN: drone.takeoff() elif event.key == pygame.K_SPACE: drone.land() # emergency elif event.key == pygame.K_BACKSPACE: drone.reset() # forward / backward elif event.key == pygame.K_w: drone.move_forward() elif event.key == pygame.K_s: drone.move_backward() # left / right elif event.key == pygame.K_a: drone.move_left() elif event.key == pygame.K_d: drone.move_right() # up / down elif event.key == pygame.K_UP: drone.move_up() elif event.key == pygame.K_DOWN: drone.move_down() # turn left / turn right elif event.key == pygame.K_LEFT: drone.turn_left() elif event.key == pygame.K_RIGHT: drone.turn_right() # speed elif event.key == pygame.K_1: drone.speed = 0.1 elif event.key == pygame.K_2: drone.speed = 0.2 elif event.key == pygame.K_3: drone.speed = 0.3 elif event.key == pygame.K_4: drone.speed = 0.4 elif event.key == pygame.K_5: drone.speed = 0.5 elif event.key == pygame.K_6: drone.speed = 0.6 elif event.key == pygame.K_7: drone.speed = 0.7 elif event.key == pygame.K_8: drone.speed = 0.8 elif event.key == pygame.K_9: drone.speed = 0.9 elif event.key == pygame.K_0: drone.speed = 1.0 elif event.key == pygame.K_u: pid.enabled = True