我们从Python开源项目中,提取了以下4个代码示例,用于说明如何使用pygame.K_9。
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 get_event(self, event): if event.type == pg.QUIT: self.done = True elif event.type == self.CHANGESTATE: self.change_state() elif event.type == self.UPDATECALENDAR: self.update_calendar() elif event.type == self.UPDATEQUOTE: self.update_quote() elif event.type == self.UPDATEWEATHER: self.update_weather() elif event.type == self.NIGHTRADIO: if self.RESTING and self.RADIO_RUNNING: process_input('s41\n') pg.time.set_timer(self.NIGHTRADIO,0) print('Station Changed to Relaxation Radio, disabling timer') elif event.type == pg.KEYDOWN: if event.key == pg.K_ESCAPE: if self.RADIO_RUNNING: process_input('q') print('You pressed ESC... Quitting') self.done = True return if not self.RADIO_RUNNING: if event.key == pg.K_m and pg.key.get_mods() & pg.KMOD_CTRL: print('Attempting to start the music') self.RADIO_RUNNING = True start_pianobar(sys.platform) else: if event.key in self.keymap_dict: if event.key == pg.K_q: self.RADIO_RUNNING = False process_input('q') elif event.key == pg.K_r: print('choosing a random radio station') sl = readfile(self.cwd+'/scripts/stationlist') random_station = 's'+str(random.randint(0,(len(sl)-1)))+'\n' process_input(random_station) else: process_input(self.keymap_dict[event.key]) elif event.key == pg.K_9 and pg.key.get_mods() & pg.KMOD_SHIFT: process_input('(') elif event.key == pg.K_0 and pg.key.get_mods() & pg.KMOD_SHIFT: process_input(')')
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