我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用pygame.K_q()。
def Kaboom(score): init() gameDisplay.blit(GameOver,(382,175)) pygame.draw.rect(gameDisplay, white, (200, 400, 550, 50)) text = smallfont.render("Press [RETURN] to continue and [Q] to quit", True, darkBlue) gameDisplay.blit(text, [370,400]) text = smallfont.render("Score : " + str(score), True, red) gameDisplay.blit(text, [450,420]) pygame.display.update() gameExit = True while gameExit: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_RETURN: gameExit = False gameloop() if event.key == pygame.K_q: pygame.quit()
def manager(field): put_tile = False 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 (event.dict["key"] == pygame.K_RIGHT or event.dict["key"] == pygame.K_d): field.change_right_move(True) elif event.type == pygame.KEYUP and (event.dict["key"] == pygame.K_RIGHT or event.dict["key"] == pygame.K_d): field.change_right_move(False) elif event.type == pygame.KEYDOWN and (event.dict["key"] == pygame.K_LEFT or event.dict["key"] == pygame.K_q): field.change_left_move(True) elif event.type == pygame.KEYUP and (event.dict["key"] == pygame.K_LEFT or event.dict["key"] == pygame.K_q): field.change_left_move(False) elif event.type == pygame.MOUSEBUTTONDOWN and event.dict["button"] == 4: field.change_tile(1) elif event.type == pygame.MOUSEBUTTONDOWN and event.dict["button"] == 5: field.change_tile(-1) elif event.type == pygame.MOUSEBUTTONDOWN and event.dict["button"] == 1: field.put_tile()
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 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 ShowPhoto(photoFile): screen = pygameEngine.GetScreen() image = pygame.image.load(photoFile).convert() image = pygame.transform.scale(image, (config.WIDTH,config.HEIGHT)) screen.blit(image, (0,0)) pygame.display.update() sleep(1) pygameEngine.ShowNavButtons() i = 0 while True: # get one pygame event event = pygame.event.poll() # handle events # Button 1 = Quit if (event.type == pygame.MOUSEBUTTONUP and event.button == 1) or (event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE) or GPIO.input(config.GPIO_NUMBER_BUTTON_1): return -1 # Button 2 = Cycle old photos if (event.type == pygame.MOUSEBUTTONUP and event.button == 3) or (event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN) or GPIO.input(config.GPIO_NUMBER_BUTTON_2): return 1 # Button Esc or Q = Quit keys if event.type == pygame.KEYDOWN and (event.key == pygame.K_ESCAPE or event.key == pygame.K_q) : return -1
def game_over(self): print("game over") font = pygame.font.Font(None, 100) text = font.render("GAME OVER", True, WHITE) text_rect = text.get_rect() text_x = self.screen.get_width() // 2 - text_rect.width / 2 text_y = self.screen.get_height() // 2 - text_rect.height / 2 self.screen.blit(text, [text_x, text_y]) pygame.display.flip() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_q: sys.exit() elif event.key == pygame.K_p: self.restart_game()
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 check_keydown_events(event, ai_settings, screen, stats, sb, ship, aliens, bullets): """????""" if event.key == pygame.K_RIGHT: ship.moving_right = True if event.key == pygame.K_LEFT: ship.moving_left = True elif event.key == pygame.K_SPACE: fire_bullet(ai_settings, screen, ship, bullets) elif event.key == pygame.K_q: stats.save_high_score() sys.exit() elif event.key == pygame.K_p and not stats.game_active: start_game(ai_settings, screen, stats, sb, ship, aliens, bullets)
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 check_keydown_events(event, ai_settings, screen,ship,bullets): """Respond to keypresses.""" if event.key == pygame.K_RIGHT: ship.moving_right = True elif event.key == pygame.K_LEFT: ship.moving_left = True elif event.key == pygame.K_SPACE: fire_bullet(ai_settings, screen,ship,bullets) elif event.key == pygame.K_q: sys.exit()
def check_keydown_events(event, ai_settings, screen, ship, bullets): """Respond to keypresses.""" if event.key == pygame.K_RIGHT: ship.moving_right = True elif event.key == pygame.K_LEFT: ship.moving_left = True elif event.key == pygame.K_SPACE: fire_bullet(ai_settings, screen, ship, bullets) elif event.key == pygame.K_q: sys.exit()
def check_keydown_events(event, ai_settings, screen,ship,bullets): """Respond to keypresses.""" if event.key == pygame.K_RIGHT: ship.moving_right = True elif event.key == pygame.K_LEFT: ship.moving_left = True elif event.key == pygame.K_SPACE: fire_bullet(ai_settings,screen,ship,bullets) elif event.key == pygame.K_q: sys.exit()
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_RETURN: intro = False if event.key == pygame.K_ESCAPE or event.key == pygame.K_q: pygame.quit() quit() gameDisplay.fill(background) message_to_screen("___________", snake_color, -160, lfont) message_to_screen("| SLITHER |", snake_color, -95, lfont) message_to_screen("___________", snake_color, -80, lfont) message_to_screen("Eat as many apples as you can, but be careful with biting your tail!", red, 0, sfont) message_to_screen("Start game - [ENTER]", white,100, sfont) message_to_screen("Exit game - [ESC]/Q", white, 150, sfont) pygame.display.update() clock.tick(5)
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 display_frame(): for event in pg.event.get(): if event.type == pg.QUIT: sys.exit(0) if event.type == pg.KEYDOWN: if event.key == pg.K_q: sys.exit(0) pg.display.flip()
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 __init__(self): self._mapping = [ { # Player pygame.K_d:"right", pygame.K_s:"down", pygame.K_q:"left", pygame.K_z:"up" }, { # Player 1 pygame.K_RIGHT:"right", pygame.K_DOWN:"down", pygame.K_LEFT:"left", pygame.K_UP:"up" } ]
def CheckAction(): #Return -1 (idle) or 1 or 2 (Sequence 1 or 2) or 9 (exit) # get one pygame event event = pygame.event.poll() # handle physical buttons (connected to GPIO) if (event.type == pygame.MOUSEBUTTONUP and event.button == 1) or (event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE) or GPIO.input(config.GPIO_NUMBER_BUTTON_1): return 1 if (event.type == pygame.MOUSEBUTTONUP and event.button == 3) or (event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN) or GPIO.input(config.GPIO_NUMBER_BUTTON_2): return 2 # handle keyboard keys if event.type == pygame.KEYDOWN and (event.key == pygame.K_ESCAPE or event.key == pygame.K_q) : return 9 return -1
def main_menu(): global screen menu_song = pygame.mixer.music.load(path.join(sound_folder, "menu.ogg")) pygame.mixer.music.play(-1) title = pygame.image.load(path.join(img_dir, "main.png")).convert() title = pygame.transform.scale(title, (WIDTH, HEIGHT), screen) screen.blit(title, (0,0)) pygame.display.update() while True: ev = pygame.event.poll() if ev.type == pygame.KEYDOWN: if ev.key == pygame.K_RETURN: break elif ev.key == pygame.K_q: pygame.quit() quit() elif ev.type == pygame.QUIT: pygame.quit() quit() else: draw_text(screen, "Press [ENTER] To Begin", 30, WIDTH/2, HEIGHT/2) draw_text(screen, "or [Q] To Quit", 30, WIDTH/2, (HEIGHT/2)+40) pygame.display.update() #pygame.mixer.music.stop() ready = pygame.mixer.Sound(path.join(sound_folder,'getready.ogg')) ready.play() screen.fill(BLACK) draw_text(screen, "GET READY!", 40, WIDTH/2, HEIGHT/2) pygame.display.update()
def __init__(self): super(JackitConfigControls, self).__init__() self.up = pygame.K_w self.down = pygame.K_s self.left = pygame.K_a self.right = pygame.K_d self.jump = pygame.K_SPACE self.interact = pygame.K_e self.push = pygame.K_LSHIFT self.reset_code = pygame.K_q self.toggle_sound = pygame.K_m self.kill_self = pygame.K_k self.reset_game = pygame.K_n
def from_json(self, raw): ''' Load the object from JSON loaded from config file ''' # First pass make sure all are valid for key in raw: if isinstance(raw[key], str): if not hasattr(pygame, raw[key]): raise ConfigError( "Invalid control for {}. Must be a valid pygame key constant".format(key) ) else: raw[key] = getattr(pygame, raw[key]) elif isinstance(raw[key], int): pass else: raise ConfigError( "Controls must be a valid pygame key constant as a string or integer" ) # Check for duplicates values = list(raw.values()) values_set = set(values) if len(values) != len(values_set): raise ConfigError("Cannot have duplicate controls") self.up = raw.get('up', getattr(pygame, 'K_w')) self.down = raw.get('down', getattr(pygame, 'K_s')) self.left = raw.get('left', getattr(pygame, 'K_a')) self.right = raw.get('right', getattr(pygame, 'K_d')) self.jump = raw.get('jump', getattr(pygame, 'K_SPACE')) self.interact = raw.get('interact', getattr(pygame, 'K_e')) self.push = raw.get("push", getattr(pygame, "K_LSHIFT")) self.reset_code = raw.get("reset_code", getattr(pygame, "K_q")) self.toggle_sound = raw.get("toggle_sound", getattr(pygame, "K_m")) self.kill_self = raw.get("kill_self", getattr(pygame, "K_k")) self.reset_game = raw.get("reset_game", getattr(pygame, "K_n"))
def check_keydown_event(event, ai_settings, screen, ship, bullets): '''????''' if event.key == pygame.K_RIGHT: # ?????? ship.moving_right = True if event.key == pygame.K_LEFT: # ?????? ship.moving_left = True if event.key == pygame.K_SPACE: # ???? fire_bullet(ai_settings, screen, ship, bullets) if event.key == pygame.K_q: sys.exit()
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()