我们从Python开源项目中,提取了以下18个代码示例,用于说明如何使用pygame.K_c()。
def handle_keyboard_event(self, event): """ Keyboard event handler. Wraps keyboard events into user event. Exits upon Ctrl-C. Distinguishes key up and key down. :param event: event to handle """ keys = pygame.key.get_pressed() if (keys[pygame.K_LCTRL] or keys[pygame.K_RCTRL]) and event.key == pygame.K_c: self.shutdown(event) elif event.type == pygame.KEYDOWN or event.type == pygame.KEYUP: if self.screensaver_dispatcher.saver_running: self.screensaver_dispatcher.cancel_screensaver() return self.handle_event(event) d = {} d[KEY_SUB_TYPE] = SUB_TYPE_KEYBOARD d[KEY_ACTION] = event.type d[KEY_KEYBOARD_KEY] = event.key event = pygame.event.Event(USER_EVENT_TYPE, **d) pygame.event.post(event)
def ask(self,question): word="" Text(self.game).draw_text2(self.default,question,green,wth//2,50) #example asking name pygame.display.flip() done = True while done: for event in pygame.event.get(): if event.type==pygame.QUIT: pygame.quit() quit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_a: word+=str(chr(event.key)) if event.key == pygame.K_b: word+=chr(event.key) if event.key == pygame.K_c: word+=chr(event.key) if event.key == pygame.K_d: word+=chr(event.key) if event.key == pygame.K_RETURN: done=False #events... return Text(self.game).draw_text2(self.default,word,green,wth//2,100)
def splash(): intro=True while intro: for event in pygame.event.get(): if event.type==pygame.QUIT: pygame.quit() quit() if event.type==pygame.KEYDOWN: if event.key == pygame.K_c: intro=False if event.key==pygame.K_q: pygame.quit() quit() display.fill(white) message("Wellcome!!!", black,300,300 ) message("Press 'c' to continue or 'q' to Quit",black,300,400) pygame.display.update() clock.tick(15)
def pause(): paused = True message_to_screen("PAUSED", red, -100, mfont) message_to_screen("Continue - [ESC]/C", white, 0, sfont) message_to_screen("Exit - Q", white, 50, sfont) pygame.display.update() while paused: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_c or event.key == pygame.K_ESCAPE: paused = False elif event.key == pygame.K_q: pygame.quit() quit() # gameDisplay.fill(background) clock.tick(5)
def _map_keys(self): key_map = self.io.key_map key_map[imgui.KEY_TAB] = pygame.K_TAB key_map[imgui.KEY_LEFT_ARROW] = pygame.K_LEFT key_map[imgui.KEY_RIGHT_ARROW] = pygame.K_RIGHT key_map[imgui.KEY_UP_ARROW] = pygame.K_UP key_map[imgui.KEY_DOWN_ARROW] = pygame.K_DOWN key_map[imgui.KEY_PAGE_UP] = pygame.K_PAGEUP key_map[imgui.KEY_PAGE_DOWN] = pygame.K_PAGEDOWN key_map[imgui.KEY_HOME] = pygame.K_HOME key_map[imgui.KEY_END] = pygame.K_END key_map[imgui.KEY_DELETE] = pygame.K_DELETE key_map[imgui.KEY_BACKSPACE] = pygame.K_BACKSPACE key_map[imgui.KEY_ENTER] = pygame.K_RETURN key_map[imgui.KEY_ESCAPE] = pygame.K_ESCAPE key_map[imgui.KEY_A] = pygame.K_a key_map[imgui.KEY_C] = pygame.K_c key_map[imgui.KEY_V] = pygame.K_v key_map[imgui.KEY_X] = pygame.K_x key_map[imgui.KEY_Y] = pygame.K_y key_map[imgui.KEY_Z] = pygame.K_z
def pause(): paused = True while paused: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_c: paused = False elif event.key == pygame.K_q: game_intro() gameDisplay.fill(white) message_display("C = continue or Q = Quit") pygame.display.update() # ===================================Controller Class=============================================
def pause(): paused = True message_to_screen("Paused", black, -100, size="large") message_to_screen("Press C to continue or Q to continue.", black, 25) pygame.display.update() while paused: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_c: paused = False elif event.key == pygame.K_q: pygame.quit() quit() # gameDisplay.fill(white) clock.tick(5)
def game_intro(): intro = True while intro: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_c: intro = False if event.key == pygame.K_q: pygame.quit() quit() gameDisplay.fill(white) message_to_screen("Welcome to Tanks!", green, -100, "large") message_to_screen("The objective of the game is to shoot and destroy.", black, -30) message_to_screen("the enemy tank before they destroy you.", black, 10) message_to_screen("The more enemies you destroy, the harder they get.", black, 50) button("play", 150, 500, 100, 50, green, light_green, action="play") button("controls", 350, 500, 100, 50, yellow, light_yellow, action="controls") button("quit", 550, 500, 100, 50, red, light_red, action="quit") pygame.display.update() clock.tick(15)
def game_intro(): intro = True while intro: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_c: intro = False if event.key == pygame.K_q: pygame.quit() quit() gameDisplay.fill(white) message_to_screen("Welcome to Slither", green, -100, "large") message_to_screen("The objective of the game is to eat red apples.", black, -30) message_to_screen("The more apples you eat, the longer you get.", black, 10) message_to_screen("If you run into yourself, or the edges, you die.", black, 50) message_to_screen("Press C to play, P to pause or Q to quit.", black, 180) pygame.display.update() clock.tick(15)
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 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 game(): exit=False over=False x=300; y=300 xChange=0; yChange=0 while not exit: # exit loop while over == True: # over loop display.fill(blue) message("Game over 'c' to continue 'q' to quit", yellow, 300,300) pygame.display.update() for event in pygame.event.get(): if event.type==pygame.QUIT: over=False exit=True if event.type == pygame.KEYDOWN: if event.key == pygame.K_q: exit = True over = False if event.key == pygame.K_c: game() for event in pygame.event.get(): # event handling if event.type==pygame.QUIT: exit=True k=pygame.key.get_pressed() if k[pygame.K_LEFT]: xChange-=10 elif k[pygame.K_RIGHT]: xChange+=10 elif k[pygame.K_UP]: yChange-=10 elif k[pygame.K_DOWN]: yChange+=10 else: xChange=0 yChange=0 if x==700: # borders xChange=-10 yChange=0 if x==10: xChange=10 yChange=0 if y==500: yChange=-10 xChange=0 if y==10: over=True x += xChange # continue from previous location y += yChange display.fill(black) # not rendered without update message("Move square in any direction...", yellow, 300,50) message("Too far up exits loop...", yellow, 300,70) pygame.draw.rect(display,white,[x,y,20,20]) # coords, w,h pygame.display.update() clock.tick(10) pygame.quit() quit()
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 handle_events(self): ''' Handle user input events ''' # Get the keys that are currently down keys = pygame.key.get_pressed() for event in self.input.events: if event.type == pygame.QUIT: print("QUIT") self.quit() break # No need to process any more events # Set the username if event.type == CustomEvent.SET_USER: print("Username: ", event.text) self.user = event.text self.welcome.run() # Handle the user input screen it it's running if self.name_enter.is_running(): if not self.name_enter.handle_event(event): break continue # Skip the rest of the events while entering username # Toggle sound if event.type == pygame.KEYDOWN: if event.key == self.config.controls.toggle_sound and\ not self.code_editor.is_running(): print("Toggling sound") self.sound.toggle_game_music() if event.key == self.config.controls.reset_game and\ not self.code_editor.is_running(): print("Resetting game") self.reset() break if event.key == pygame.K_c and\ not self.code_editor.is_running(): if pygame.key.get_mods() & pygame.KMOD_CTRL: print("Quit with cntrl-c") self.quit() break # Handle welcome screen events if it's running if self.welcome.is_running(): if not self.welcome.handle_event(event): break continue # Skip the rest of the events while the welcome screen is running # Handle code editor events if it's running if self.code_editor.is_running(): if not self.code_editor.handle_event(event): break # All the cool event handling happens in the level if not self.current_level.handle_event(event, keys): break