我们从Python开源项目中,提取了以下21个代码示例,用于说明如何使用pygame.K_r()。
def manager(jeff, field, frame, level_select): for event in pygame.event.get(): if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.dict["key"] == pygame.K_ESCAPE): sys.exit() elif event.type == pygame.KEYDOWN and jeff.is_dead() and event.dict["key"] == pygame.K_r: jeff = Jeff.Jeff([300, Constant.GROUND_HEIGHT - 50]) field.reload_level() elif event.type == pygame.KEYDOWN and (jeff.is_dead() or field.is_level_ended()) and event.dict["key"] == pygame.K_m: jeff = Jeff.Jeff([300, Constant.GROUND_HEIGHT - 50]) frame = "MENU" elif event.type == pygame.KEYDOWN and field.is_level_ended() and event.dict["key"] == pygame.K_n: next_level = level_select.get_next_level(field.get_level_file()) if next_level != "": jeff = Jeff.Jeff([300, Constant.GROUND_HEIGHT - 50]) field.load_level(next_level) elif event.type == pygame.KEYDOWN and event.dict["key"] == pygame.K_SPACE: jeff.jump(True) elif event.type == pygame.KEYUP and event.dict["key"] == pygame.K_SPACE: jeff.jump(False) return jeff, frame
def __init__(self, gameInstance): BaseController.__init__(self, gameInstance) keys = ( \ pygame.K_LEFT, #left pygame.K_RIGHT,#right pygame.K_UP, #acc pygame.K_DOWN, #brake pygame.K_LCTRL,#reverse pygame.K_r, #restart #pygame.K_ESC, #endGame pygame.K_RCTRL, #pygame.K_SPACE, \ ) self.keyToAction = dict() for key, action in zip(keys, self.gameInterface.gameInstance.actionsList): self.keyToAction.update({key: action}) self.keyToAction[pygame.K_RCTRL] = self.keyToAction[pygame.K_LCTRL]
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 check_keydown_events(settings, event, screen, tile_map): """Respond to key down events""" player = tile_map.player if event.key == pygame.K_ESCAPE: sys.exit() if event.key == pygame.K_a: generate_new_random_blob(settings, screen, settings.image_res.enemy_blob_images, tile_map) if event.key == pygame.K_r: reset_game(tile_map) if event.key == pygame.K_LEFT: if not player.idle_top: if player.dx == 0.0: player.dx = -1 * settings.player_dx player.facing_left = True if event.key == pygame.K_RIGHT: if not player.idle_top: if player.dx == 0.0: player.dx = settings.player_dx player.facing_left = False if event.key == pygame.K_F9: if settings.fullscreen == True: settings.fullscreen = False pygame.display.set_mode((800, 600)) else: settings.fullscreen = True pygame.display.set_mode((800, 600), pygame.FULLSCREEN)
def keyboard_loop(self): while not rospy.is_shutdown(): acc = 0 yaw = 0 keys = pygame.key.get_pressed() for event in pygame.event.get(): if event.type==pygame.QUIT:sys.exit() if(keys[pygame.K_s]): self.send_highway_start(1) if(keys[pygame.K_t]): self.send_highway_start(2) if(keys[pygame.K_UP]): acc = self.acc elif(keys[pygame.K_DOWN]): acc = -self.acc if(keys[pygame.K_LEFT]): yaw = self.yaw elif(keys[pygame.K_RIGHT]): yaw = -self.yaw if(keys[pygame.K_r]): state = 1 self.send_record_state(state) elif(keys[pygame.K_q]): state = 2 self.send_record_state(state) elif(keys[pygame.K_p]): state = 0 self.send_record_state(state) self.send_control(acc, yaw) self.rate.sleep()
def updateKeys(self, event): if event.type == pygame.KEYDOWN: if event.key == K_r: keys["r"] = True elif event.key == pygame.K_e: keys["e"] = True elif event.key == pygame.K_p: keys["p"] = True elif event.key == pygame.K_n: keys["n"] = True if event.type == pygame.KEYUP: if event.key == pygame.K_r: keys["r"] = False elif event.key == pygame.K_e: keys["e"] = False elif event.key == pygame.K_p: keys["p"] = False elif event.key == pygame.K_n: keys["n"] = False if event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: # Left Mouse keys["mouseL"] = True if event.button == 3: # Right Mouse keys["mouseR"] = True if event.type == pygame.MOUSEBUTTONUP: if event.button == 1: # Left Mouse keys["mouseL"] = False if event.button == 3: # Right Mouse keys["mouseR"] = False keys["mousePos"] = pygame.mouse.get_pos()
def process_input(self): for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if event.type == pygame.KEYDOWN: if self.state.paused: if event.key == pygame.K_r: self.state.paused = False if self.state.game_over: if event.key == pygame.K_r: self.state = State(self.ROWS, self.COLS) if event.key == pygame.K_ESCAPE: sys.exit() if self.state.running: if event.key == pygame.K_DOWN: self.state.move_piece(board.DIRECTION_DOWN) if event.key == pygame.K_LEFT: self.state.move_piece(board.DIRECTION_LEFT) if event.key == pygame.K_RIGHT: self.state.move_piece(board.DIRECTION_RIGHT) if event.key == pygame.K_x: self.state.rotate_piece() if event.key == pygame.K_z: self.state.rotate_piece(True) if event.key == pygame.K_SPACE: self.state.drop_piece() if event.key == pygame.K_p: self.state.paused = True
def init(): add_key(pygame.K_w) add_key(pygame.K_s) add_key(pygame.K_a) add_key(pygame.K_d) add_key(pygame.K_c) add_key(pygame.K_r) add_key(pygame.K_q) add_key(pygame.K_e) add_key(pygame.K_g) add_key(pygame.K_t) # Adds a new key to be tracked for input
def event_loop(self): for event in pg.event.get(): if event.type == pg.QUIT: self.done = True keys = pg.key.get_pressed() if keys[pg.K_r]: # Reset: press r self.done = True if keys[pg.K_q]: # Quit: press q exit()
def run(mapname, levelname = None, worldname = None): mapname = "res/levels/" + mapname game.reloadScreen() game.loadLevel(levelname, worldname, json.load(open(mapname)), Level.JSON_LOAD) fps_timer = pygame.time.get_ticks() cur_frames = 0 last_tick = pygame.time.get_ticks() running = True #main loop of the game while running and not game.finished: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False break elif event.type == pygame.KEYDOWN: if event.key == pygame.K_q: game.level.state = game.level.state+1 if game.level.state > 2: game.level.state = 1 elif event.key == pygame.K_r: game.level.killPlayer() game.call(event) elapsed = pygame.time.get_ticks() - last_tick last_tick = pygame.time.get_ticks() game.tick(elapsed/1000) cur_frames+=1 if pygame.time.get_ticks() - fps_timer >= 1000: pygame.display.set_caption(str(cur_frames)) fps_timer = pygame.time.get_ticks() cur_frames = 0
def update(self, button): keys = pygame.key.get_pressed() if focus == main: elapsed = min(1, self.cds.elapsed("move_keys")) if keys[pygame.K_LEFT] or keys[pygame.K_a]: self.offset[0] = self.offset[0] - (self.key_velocity[0] * elapsed) self.change = True if keys[pygame.K_RIGHT] or keys[pygame.K_d]: self.offset[0] = self.offset[0] + (self.key_velocity[0] * elapsed) self.change = True if keys[pygame.K_UP] or keys[pygame.K_w]: self.offset[1] = self.offset[1] - (self.key_velocity[1] * elapsed) self.change = True if keys[pygame.K_DOWN] or keys[pygame.K_s]: self.offset[1] = self.offset[1] + (self.key_velocity[1] * elapsed) self.change = True self.cds.start() if self.cds.check("save_load"): if keys[pygame.K_q]: save(fnbox.text) self.cds.start() if keys[pygame.K_e]: load(fnbox.text) self.cds.start() if keys[pygame.K_r]: self.new() if self.mmb_down and self.cds.check("navigate"): mpos = pygame.mouse.get_pos() diffx = (mpos[0] - self.mmb_start[0]) / 10 diffy = (mpos[1] - self.mmb_start[1]) / 10 self.offset = [ self.offset[0] + diffx, self.offset[1] + diffy ] self.cds.start() self.change = True
def Refresh(self,screen): self.UpdateChangeScale() #check if user clicked to change scale self.CalcGraph() total_surface = pygame.surface.Surface((self.widthTotal,self.height)) total_surface.blit( self.GetGraphSurface() , (0,0)) total_surface.blit( self.GetListSurface () , (self.widthA,0)) #self.BlitMaxMin( total_surface ) screen.blit(total_surface,(self.x,self.y)) for x in range(len(self.variables)): self.reference[self.variables[x]].show_item.UpdatePosition() self.reference[self.variables[x]].show_item.Refresh(screen) for x in range(len(self.variables)): if not self.reference[ self.variables[x] ].GetEnabled(): continue if self.playing: last_value = self.reference[ self.variables[x] ].last_value else: last_value = self.reference[self.variables[x]].hold_value surface = pygame.surface.Surface((40,20)) surface.fill((255,255,255)) AddBorder(surface) textValue = fonts.adamCG[str(15)].render( GetValueName( last_value ),1,(0,0,0)) BlitInCenter(surface,textValue) #print self.GetRelPos(x) screen.blit(surface , (self.x + self.widthA - 20 , self.y + self.GetRelPos(x) - 5 )) self.holdSelector.Refresh(screen) if self.holdSelector.GetSelected() == "Release": self.Release() elif self.holdSelector.GetSelected() == "Hold": self.Hold() if pygame.key.get_pressed()[pygame.K_h]: self.Hold() if pygame.key.get_pressed()[pygame.K_r]: self.Release() if self.drawMark: screen.blit(self.drawMark,(self.x + self.drawMarkX,self.y + self.drawMarkY))
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 handle_mouse_input(tileset): tile = None if tileset.is_at_mouse_pos(): tile = tileset.tile_at_mouse_pos() # Add column to Tileset if Input.key_typed(pygame.K_c): tileset.add_columns(1) # Add row to Tileset if Input.key_typed(pygame.K_r): tileset.add_rows(1) # Make Tile at mouse position not passable if Input.left_mouse_down: if tileset.is_at_mouse_pos(): tile.passable = False tile.color = Renderer.COLOR_BLACK # Make Tile at mouse position passable if Input.right_mouse_down: if tileset.is_at_mouse_pos(): tile.passable = True tile.color = (128, 128, 128) if Input.key_typed(pygame.K_q): if tileset.is_at(Input.mouse_x(), Input.mouse_y()): tile.passable = True tile.color = Renderer.COLOR_GREEN GenAlg.start_node = tile TileInteractor.start_node = tile if Input.key_typed(pygame.K_e): if tileset.is_at_mouse_pos(): tile.passable = True tile.color = Renderer.COLOR_RED GenAlg.end_node = tile TileInteractor.end_node = tile if Input.key_typed(pygame.K_g): GenAlg.init() # This is used for framerate
def process_events(self): keys = pygame.key.get_pressed() self.B1 = Button(self.width * 0.88, self.height * 0.04, self.width * 0.1, self.height * 0.07, I5) self.B2 = Button(self.width * 0.88, self.height * 0.04, self.width * 0.1, self.height * 0.07, I5) self.B3 = Button(self.width * 0.88, self.height * 0.04, self.width * 0.1, self.height * 0.07, I5) self.H1 = Button(self.width * 0.88, self.height * 0.84, self.width * 0.1, self.height * 0.07, I6) self.H2 = Button(self.width * 0.77, self.height * 0.84, self.width * 0.1, self.height * 0.07, I7) self.M1 = Menu(self.width, self.height, I0) self.screen_rect = self.screen.get_rect() for event in pygame.event.get(): if event.type == VIDEORESIZE: self.screen = pygame.display.set_mode(event.dict['size'], HWSURFACE | DOUBLEBUF | RESIZABLE) self.width = event.dict['size'][0] self.height = event.dict['size'][1] if event.type == pygame.QUIT: return True if keys[pygame.K_LCTRL] and keys[pygame.K_w]: return True if keys[pygame.K_LALT] and keys[pygame.K_F4]: return True if keys[pygame.K_ESCAPE]: if self.S0[1] == 1 or self.S0[2] == 1 or self.S0[3] == 1: self.S0 = [1, 0, 0, 0] if keys[pygame.K_r]: self.D1 self.D1 = Dice(self.width * 0.5, self.height * 0.5, self.width * 0.1, self.width * 0.1) if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1 and self.D1.rc == 0: self.D1.check_click(event.pos) if event.type == pygame.MOUSEBUTTONUP and event.button == 1: self.D1.click = False if self.D1.rc == 1: self.D1.rc = 2 if self.D1.rc == 3 and self.D1.vcc < 30: self.D1.rc = 0 self.D1.vcc = 0 if self.B1.rect.collidepoint(pygame.mouse.get_pos()) and self.S0[1] == 1: self.S0 = [1, 0, 0, 0] if self.B2.rect.collidepoint(pygame.mouse.get_pos()) and self.S0[2] == 1: self.S0 = [1, 0, 0, 0] if self.H1.rect.collidepoint(pygame.mouse.get_pos()) and self.S0[2] == 1: self.S0 = [1, 0, 0, 0] if self.H2.rect.collidepoint(pygame.mouse.get_pos()) and self.S0[2] == 1: self.S0 = [1, 0, 0, 0] if self.B3.rect.collidepoint(pygame.mouse.get_pos()) and self.S0[3] == 1: self.S0 = [1, 0, 0, 0] if self.M1.B1.rect.collidepoint(pygame.mouse.get_pos()) and self.S0[0] == 1: self.S0 = [0, 1, 0, 0] if self.M1.B2.rect.collidepoint(pygame.mouse.get_pos()) and self.S0[0] == 1: self.S0 = [0, 0, 1, 0] if self.M1.B3.rect.collidepoint(pygame.mouse.get_pos()) and self.S0[0] == 1: self.S0 = [0, 0, 0, 1] if self.M1.B4.rect.collidepoint(pygame.mouse.get_pos()) and self.S0[0] == 1: return True return False
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 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 keyboard_interaction(): global current_text_tag,last_time_text,animation_speed,NEXT_CRITICAL_ACTION if IS_RASPBERRY: check_hardware_buttons() for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: pygame.quit() exit() elif event.key == pygame.K_SPACE: camera.save_frame(frameManager) current_text_tag="scattato" elif event.key == pygame.K_UP: NEXT_CRITICAL_ACTION="changetosession"+str(frameManager.current_session+1) current_text_tag="cambio sessione" elif event.key == pygame.K_DOWN: NEXT_CRITICAL_ACTION="changetosession"+str(frameManager.current_session-1) current_text_tag="cambio sessione" elif event.key == pygame.K_x: frameManager.remove_frame() current_text_tag="rimosso" elif event.key == pygame.K_s: NEXT_CRITICAL_ACTION="save" current_text_tag="saving" elif event.key == pygame.K_r: camera.ruota() elif event.key == pygame.K_DELETE: frameManager.delete_all() elif event.key == pygame.K_BACKSPACE: frameManager.delete_current_scene() elif event.key == pygame.K_p: animation_speed/=2.0 if animation_speed<0.003125: animation_speed=0.003125 elif event.key == pygame.K_o: animation_speed*=2.0 if animation_speed>3.2: animation_speed=3.2 else: for n in range(10): if event.key == pygame.__dict__["K_"+str(n)]: nf=n-1 if nf<0: nf=9 NEXT_CRITICAL_ACTION="changetosession"+str(nf) current_text_tag="cambio sessione" last_time_text=time.time()