我们从Python开源项目中,提取了以下8个代码示例,用于说明如何使用pygame.K_2。
def test_input(self): i = inputs.Input() test_inputs = [(pygame.KEYDOWN, {'key':pygame.K_1}), (pygame.KEYDOWN, {'key':pygame.K_2}), (pygame.KEYDOWN, {'key':pygame.K_2}), (pygame.KEYDOWN, {'key':pygame.K_UP}), (pygame.KEYDOWN, {'key':pygame.K_LEFT}), (pygame.KEYUP, {'key':pygame.K_r}), # (pygame.KEYDOWN, {'key':pygame.K_q}) ] for e in [pygame.event.Event(*x) for x in test_inputs]: i.handle_events(e) self.assertEqual(i.get_swcha(), 0xAF) self.assertEqual(i.get_swchb(), 0x7F) self.assertEqual(i.get_paddle0(), 0x1) self.assertEqual(i.get_input7(), 0xFF)
def handle_event(self, event): if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: self.root.trigger_exit_to_main() elif event.key == pygame.K_p: self.toggle_pause() elif not self.paused: if event.key == pygame.K_SPACE: self.skip_turn = True elif event.key == pygame.K_RETURN: self.skip_game = True elif event.key in [pygame.K_LEFT, pygame.K_1]: self.parent.battle.award_victory(1) self.skip_game = True elif event.key in [pygame.K_RIGHT, pygame.K_2]: self.parent.battle.award_victory(2) self.skip_game = True
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 handle_events(self, event): if event.type== pygame.KEYDOWN: if event.key == pygame.K_UP: self.swcha ^= 0x10 elif event.key == pygame.K_DOWN: self.swcha ^= 0x20 elif event.key == pygame.K_LEFT: self.swcha ^= 0x40 self.paddle0 = 1 elif event.key == pygame.K_RIGHT: self.swcha ^= 0x80 self.paddle0 = -1 elif event.key == pygame.K_s: # Game select self.swchb ^= 0x1 elif event.key == pygame.K_r: # Game reset self.swchb ^= 0x2 elif event.key == pygame.K_1: self.swchb ^= 0x40 print("P0 dificulty %s"%(("hard", "easy")[self.swchb & 0x40 != 0])) elif event.key == pygame.K_2: self.swchb ^= 0x80 print("P1 dificulty %s"%(("hard", "easy")[self.swchb & 0x80 != 0])) elif event.key == pygame.K_c: # toggle black and white self.swchb ^= 0x8 elif event.key == pygame.K_z: # toggle black and white self.input7 &= 0x7F elif event.key == pygame.K_q: # Dodgy quit self.quit = 0x1 pygame.quit() sys.exit() # Custom key events elif event.key == pygame.K_LEFTBRACKET: self._save_state = 0x1 elif event.key == pygame.K_RIGHTBRACKET: self._restore_state = 0x1 elif event.type== pygame.KEYUP: if event.key == pygame.K_UP: self.swcha |= 0x10 elif event.key == pygame.K_DOWN: self.swcha |= 0x20 elif event.key == pygame.K_LEFT: self.swcha |= 0x40 self.paddle0 = 0 elif event.key == pygame.K_RIGHT: self.swcha |= 0x80 self.paddle0 = 0 elif event.key == pygame.K_s: # Game select self.swchb |= 0x1 elif event.key == pygame.K_r: # Game reset self.swchb |= 0x2 elif event.key == pygame.K_z: # toggle black and white self.input7 |= 0x80 # Custom key events elif event.key == pygame.K_LEFTBRACKET: self._save_state = 0x0 elif event.key == pygame.K_RIGHTBRACKET: self._restore_state = 0x0
def userInput(camera, display): deltaT = display.deltaT #keyboard down presses pressed = pygame.key.get_pressed() for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() if event.type == pygame.KEYDOWN: if event.key ==pygame.K_ESCAPE: pygame.quit() quit() if event.type == pygame.MOUSEMOTION: x, y = event.rel event.pos = (display.w/2,display.h/2) #look around if( abs(x)>1 ): camera.turnRight( -x*deltaT) if( abs(y)>1 ): camera.turnUp( -y*deltaT) #capture mouse pygame.mouse.set_pos(display.w/2., display.w/2) pygame.event.get(pygame.MOUSEMOTION) #steal the new mouse event and do nothing with it to reset it #print() #print(camera.posWorld.getString()) #print(camera.view()) #back forth if pressed[pygame.K_w]: camera.forward(deltaT) if pressed[pygame.K_s]: camera.forward(-deltaT) #up, down if pressed[pygame.K_SPACE]: camera.upward(-deltaT) if pressed[pygame.K_LSHIFT]: camera.upward(deltaT) #left right if pressed[pygame.K_a]: camera.sideward(deltaT) if pressed[pygame.K_d]: camera.sideward(-deltaT) #change openGL polygon mode if pressed[pygame.K_1]: glPolygonMode(GL_FRONT_AND_BACK, GL_POINT) if pressed[pygame.K_2]: glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) if pressed[pygame.K_3]: glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) #start the main
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