Python pygame.K_SPACE Examples
The following are 30
code examples of pygame.K_SPACE().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
pygame
, or try the search function
.
Example #1
Source File: gamestart.py From Games with MIT License | 8 votes |
def GameStartInterface(screen, sounds, cfg): dino = Dinosaur(cfg.IMAGE_PATHS['dino']) ground = pygame.image.load(cfg.IMAGE_PATHS['ground']).subsurface((0, 0), (83, 19)) rect = ground.get_rect() rect.left, rect.bottom = cfg.SCREENSIZE[0]/20, cfg.SCREENSIZE[1] clock = pygame.time.Clock() press_flag = False while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE or event.key == pygame.K_UP: press_flag = True dino.jump(sounds) dino.update() screen.fill(cfg.BACKGROUND_COLOR) screen.blit(ground, rect) dino.draw(screen) pygame.display.update() clock.tick(cfg.FPS) if (not dino.is_jumping) and press_flag: return True
Example #2
Source File: sprites.py From pygame_tutorials with MIT License | 8 votes |
def get_keys(self): self.rot_speed = 0 self.vel = vec(0, 0) keys = pg.key.get_pressed() if keys[pg.K_LEFT] or keys[pg.K_a]: self.rot_speed = PLAYER_ROT_SPEED if keys[pg.K_RIGHT] or keys[pg.K_d]: self.rot_speed = -PLAYER_ROT_SPEED if keys[pg.K_UP] or keys[pg.K_w]: self.vel = vec(PLAYER_SPEED, 0).rotate(-self.rot) if keys[pg.K_DOWN] or keys[pg.K_s]: self.vel = vec(-PLAYER_SPEED / 2, 0).rotate(-self.rot) if keys[pg.K_SPACE]: now = pg.time.get_ticks() if now - self.last_shot > BULLET_RATE: self.last_shot = now dir = vec(1, 0).rotate(-self.rot) pos = self.pos + BARREL_OFFSET.rotate(-self.rot) Bullet(self.game, pos, dir) self.vel = vec(-KICKBACK, 0).rotate(-self.rot)
Example #3
Source File: shmup-11.py From pygame_tutorials with MIT License | 6 votes |
def update(self): # unhide if hidden if self.hidden and pygame.time.get_ticks() - self.hide_timer > 1000: self.hidden = False self.rect.centerx = WIDTH / 2 self.rect.bottom = HEIGHT - 10 self.speedx = 0 keystate = pygame.key.get_pressed() if keystate[pygame.K_LEFT]: self.speedx = -8 if keystate[pygame.K_RIGHT]: self.speedx = 8 if keystate[pygame.K_SPACE]: self.shoot() self.rect.x += self.speedx if self.rect.right > WIDTH: self.rect.right = WIDTH if self.rect.left < 0: self.rect.left = 0
Example #4
Source File: endGame.py From Games with MIT License | 6 votes |
def endGame(screen, sounds, showScore, score, number_images, bird, pipe_sprites, backgroud_image, other_images, base_pos, cfg): sounds['die'].play() clock = pygame.time.Clock() while True: for event in pygame.event.get(): if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE): pygame.quit() sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE or event.key == pygame.K_UP: return boundary_values = [0, base_pos[-1]] bird.update(boundary_values, float(clock.tick(cfg.FPS))/1000.) screen.blit(backgroud_image, (0, 0)) pipe_sprites.draw(screen) screen.blit(other_images['base'], base_pos) showScore(screen, score, number_images) bird.draw(screen) pygame.display.update() clock.tick(cfg.FPS)
Example #5
Source File: shmup.py From pygame_tutorials with MIT License | 6 votes |
def update(self): # timeout for powerups if self.power >= 2 and pygame.time.get_ticks() - self.power_time > POWERUP_TIME: self.power -= 1 self.power_time = pygame.time.get_ticks() # unhide if hidden if self.hidden and pygame.time.get_ticks() - self.hide_timer > 1000: self.hidden = False self.rect.centerx = WIDTH / 2 self.rect.bottom = HEIGHT - 10 self.speedx = 0 keystate = pygame.key.get_pressed() if keystate[pygame.K_LEFT]: self.speedx = -8 if keystate[pygame.K_RIGHT]: self.speedx = 8 if keystate[pygame.K_SPACE]: self.shoot() self.rect.x += self.speedx if self.rect.right > WIDTH: self.rect.right = WIDTH if self.rect.left < 0: self.rect.left = 0
Example #6
Source File: shmup-14.py From pygame_tutorials with MIT License | 6 votes |
def update(self): # timeout for powerups if self.power >= 2 and pygame.time.get_ticks() - self.power_time > POWERUP_TIME: self.power -= 1 self.power_time = pygame.time.get_ticks() # unhide if hidden if self.hidden and pygame.time.get_ticks() - self.hide_timer > 1000: self.hidden = False self.rect.centerx = WIDTH / 2 self.rect.bottom = HEIGHT - 10 self.speedx = 0 keystate = pygame.key.get_pressed() if keystate[pygame.K_LEFT]: self.speedx = -8 if keystate[pygame.K_RIGHT]: self.speedx = 8 if keystate[pygame.K_SPACE]: self.shoot() self.rect.x += self.speedx if self.rect.right > WIDTH: self.rect.right = WIDTH if self.rect.left < 0: self.rect.left = 0
Example #7
Source File: sprites.py From pygame_tutorials with MIT License | 6 votes |
def get_keys(self): self.rot_speed = 0 self.vel = vec(0, 0) keys = pg.key.get_pressed() if keys[pg.K_LEFT] or keys[pg.K_a]: self.rot_speed = PLAYER_ROT_SPEED if keys[pg.K_RIGHT] or keys[pg.K_d]: self.rot_speed = -PLAYER_ROT_SPEED if keys[pg.K_UP] or keys[pg.K_w]: self.vel = vec(PLAYER_SPEED, 0).rotate(-self.rot) if keys[pg.K_DOWN] or keys[pg.K_s]: self.vel = vec(-PLAYER_SPEED / 2, 0).rotate(-self.rot) if keys[pg.K_SPACE]: now = pg.time.get_ticks() if now - self.last_shot > BULLET_RATE: self.last_shot = now dir = vec(1, 0).rotate(-self.rot) pos = self.pos + BARREL_OFFSET.rotate(-self.rot) Bullet(self.game, pos, dir) self.vel = vec(-KICKBACK, 0).rotate(-self.rot) choice(self.game.weapon_sounds['gun']).play() MuzzleFlash(self.game, pos)
Example #8
Source File: shmup-12.py From pygame_tutorials with MIT License | 6 votes |
def update(self): # unhide if hidden if self.hidden and pygame.time.get_ticks() - self.hide_timer > 1000: self.hidden = False self.rect.centerx = WIDTH / 2 self.rect.bottom = HEIGHT - 10 self.speedx = 0 keystate = pygame.key.get_pressed() if keystate[pygame.K_LEFT]: self.speedx = -8 if keystate[pygame.K_RIGHT]: self.speedx = 8 if keystate[pygame.K_SPACE]: self.shoot() self.rect.x += self.speedx if self.rect.right > WIDTH: self.rect.right = WIDTH if self.rect.left < 0: self.rect.left = 0
Example #9
Source File: sprites.py From pygame_tutorials with MIT License | 6 votes |
def get_keys(self): self.rot_speed = 0 self.vel = vec(0, 0) keys = pg.key.get_pressed() if keys[pg.K_LEFT] or keys[pg.K_a]: self.rot_speed = PLAYER_ROT_SPEED if keys[pg.K_RIGHT] or keys[pg.K_d]: self.rot_speed = -PLAYER_ROT_SPEED if keys[pg.K_UP] or keys[pg.K_w]: self.vel = vec(PLAYER_SPEED, 0).rotate(-self.rot) if keys[pg.K_DOWN] or keys[pg.K_s]: self.vel = vec(-PLAYER_SPEED / 2, 0).rotate(-self.rot) if keys[pg.K_SPACE]: now = pg.time.get_ticks() if now - self.last_shot > BULLET_RATE: self.last_shot = now dir = vec(1, 0).rotate(-self.rot) pos = self.pos + BARREL_OFFSET.rotate(-self.rot) Bullet(self.game, pos, dir) self.vel = vec(-KICKBACK, 0).rotate(-self.rot)
Example #10
Source File: sprites.py From pygame_tutorials with MIT License | 6 votes |
def get_keys(self): self.rot_speed = 0 self.vel = vec(0, 0) keys = pg.key.get_pressed() if keys[pg.K_LEFT] or keys[pg.K_a]: self.rot_speed = PLAYER_ROT_SPEED if keys[pg.K_RIGHT] or keys[pg.K_d]: self.rot_speed = -PLAYER_ROT_SPEED if keys[pg.K_UP] or keys[pg.K_w]: self.vel = vec(PLAYER_SPEED, 0).rotate(-self.rot) if keys[pg.K_DOWN] or keys[pg.K_s]: self.vel = vec(-PLAYER_SPEED / 2, 0).rotate(-self.rot) if keys[pg.K_SPACE]: now = pg.time.get_ticks() if now - self.last_shot > BULLET_RATE: self.last_shot = now dir = vec(1, 0).rotate(-self.rot) pos = self.pos + BARREL_OFFSET.rotate(-self.rot) Bullet(self.game, pos, dir) self.vel = vec(-KICKBACK, 0).rotate(-self.rot)
Example #11
Source File: sprites.py From pygame_tutorials with MIT License | 6 votes |
def get_keys(self): self.rot_speed = 0 self.vel = vec(0, 0) keys = pg.key.get_pressed() if keys[pg.K_LEFT] or keys[pg.K_a]: self.rot_speed = PLAYER_ROT_SPEED if keys[pg.K_RIGHT] or keys[pg.K_d]: self.rot_speed = -PLAYER_ROT_SPEED if keys[pg.K_UP] or keys[pg.K_w]: self.vel = vec(PLAYER_SPEED, 0).rotate(-self.rot) if keys[pg.K_DOWN] or keys[pg.K_s]: self.vel = vec(-PLAYER_SPEED / 2, 0).rotate(-self.rot) if keys[pg.K_SPACE]: now = pg.time.get_ticks() if now - self.last_shot > BULLET_RATE: self.last_shot = now dir = vec(1, 0).rotate(-self.rot) pos = self.pos + BARREL_OFFSET.rotate(-self.rot) Bullet(self.game, pos, dir) self.vel = vec(-KICKBACK, 0).rotate(-self.rot)
Example #12
Source File: main-2-class-move.py From python-examples with MIT License | 6 votes |
def handle_event(self, event): if event.type == pygame.KEYDOWN: if event.key == pygame.K_BACKSPACE: self.reset() if event.key == pygame.K_SPACE: self.speed_y = -20 if event.key == pygame.K_LEFT: self.speed_x -= 10 elif event.key == pygame.K_RIGHT: self.speed_x += 10 elif event.key == pygame.K_DOWN: self.gravity += 10 elif event.type == pygame.KEYUP: if event.key == pygame.K_LEFT: self.speed_x -= -10 elif event.key == pygame.K_RIGHT: self.speed_x += -10 elif event.key == pygame.K_DOWN: self.gravity += -10
Example #13
Source File: main_menu.py From The-Stolen-Crown-RPG with MIT License | 6 votes |
def normal_update(self, keys): if self.allow_input: if keys[pg.K_DOWN] and self.arrow.index == 0: self.arrow.index = 1 self.notify(c.CLICK) self.allow_input = False elif keys[pg.K_UP] and self.arrow.index == 1: self.arrow.index = 0 self.notify(c.CLICK) self.allow_input = False elif keys[pg.K_SPACE]: if self.arrow.index == 0: self.game_data = pickle.load(open("save.p", "rb")) self.next = c.TOWN self.state = c.TRANSITION_OUT else: self.next = c.OVERWORLD self.state = c.TRANSITION_OUT self.notify(c.CLICK2) self.arrow.rect.y = self.arrow.pos_list[self.arrow.index] if not keys[pg.K_DOWN] and not keys[pg.K_UP]: self.allow_input = True
Example #14
Source File: endGame.py From AIGames with MIT License | 6 votes |
def endGame(screen, sounds, showScore, score, number_images, bird, pipe_sprites, backgroud_image, other_images, base_pos, cfg, mode): if mode == 'train': return sounds['die'].play() clock = pygame.time.Clock() while True: for event in pygame.event.get(): if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE): pygame.quit() sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE or event.key == pygame.K_UP: return boundary_values = [0, base_pos[-1]] bird.update(boundary_values) screen.blit(backgroud_image, (0, 0)) pipe_sprites.draw(screen) screen.blit(other_images['base'], base_pos) showScore(screen, score, number_images) bird.draw(screen) pygame.display.update() clock.tick(cfg.FPS)
Example #15
Source File: endGame.py From AIGames with MIT License | 6 votes |
def endGame(screen, sounds, showScore, score, number_images, bird, pipe_sprites, backgroud_image, other_images, base_pos, cfg, mode): if mode == 'train': return sounds['die'].play() clock = pygame.time.Clock() while True: for event in pygame.event.get(): if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE): pygame.quit() sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE or event.key == pygame.K_UP: return boundary_values = [0, base_pos[-1]] bird.update(boundary_values) screen.blit(backgroud_image, (0, 0)) pipe_sprites.draw(screen) screen.blit(other_images['base'], base_pos) showScore(screen, score, number_images) bird.draw(screen) pygame.display.update() clock.tick(cfg.FPS)
Example #16
Source File: gamestart.py From AIGames with MIT License | 6 votes |
def GameStartInterface(screen, sounds, cfg): dino = Dinosaur(cfg.IMAGE_PATHS['dino']) ground = pygame.image.load(cfg.IMAGE_PATHS['ground']).subsurface((0, 0), (83, 19)) rect = ground.get_rect() rect.left, rect.bottom = cfg.SCREENSIZE[0]/20, cfg.SCREENSIZE[1] clock = pygame.time.Clock() press_flag = False while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE or event.key == pygame.K_UP: press_flag = True dino.jump(sounds) dino.update() screen.fill(cfg.BACKGROUND_COLOR) screen.blit(ground, rect) dino.draw(screen) pygame.display.update() clock.tick(cfg.FPS) if (not dino.is_jumping) and press_flag: return True
Example #17
Source File: game_functions.py From alien-invasion-game with MIT License | 6 votes |
def check_keydown_events(event: EventType, ai_settings: Settings , stats: GameStats, game_items: GameItems): """Respond when key is being pressed.""" if event.key == pygame.K_RIGHT: # Move ship to the right. game_items.ship.moving_right = True elif event.key == pygame.K_LEFT: # Move ship to the left. game_items.ship.moving_left = True elif event.key == pygame.K_SPACE: fire_bullet(ai_settings, game_items) elif event.key == pygame.K_q: quit_game(stats) elif event.key == pygame.K_RETURN: # ENTER key start_new_game(ai_settings, stats, game_items)
Example #18
Source File: sprites.py From pygame_tutorials with MIT License | 6 votes |
def get_keys(self): self.rot_speed = 0 self.vel = vec(0, 0) keys = pg.key.get_pressed() if keys[pg.K_LEFT] or keys[pg.K_a]: self.rot_speed = PLAYER_ROT_SPEED if keys[pg.K_RIGHT] or keys[pg.K_d]: self.rot_speed = -PLAYER_ROT_SPEED if keys[pg.K_UP] or keys[pg.K_w]: self.vel = vec(PLAYER_SPEED, 0).rotate(-self.rot) if keys[pg.K_DOWN] or keys[pg.K_s]: self.vel = vec(-PLAYER_SPEED / 2, 0).rotate(-self.rot) if keys[pg.K_SPACE]: now = pg.time.get_ticks() if now - self.last_shot > BULLET_RATE: self.last_shot = now dir = vec(1, 0).rotate(-self.rot) pos = self.pos + BARREL_OFFSET.rotate(-self.rot) Bullet(self.game, pos, dir) self.vel = vec(-KICKBACK, 0).rotate(-self.rot)
Example #19
Source File: sprites.py From pygame_tutorials with MIT License | 6 votes |
def get_keys(self): self.rot_speed = 0 self.vel = vec(0, 0) keys = pg.key.get_pressed() if keys[pg.K_LEFT] or keys[pg.K_a]: self.rot_speed = PLAYER_ROT_SPEED if keys[pg.K_RIGHT] or keys[pg.K_d]: self.rot_speed = -PLAYER_ROT_SPEED if keys[pg.K_UP] or keys[pg.K_w]: self.vel = vec(PLAYER_SPEED, 0).rotate(-self.rot) if keys[pg.K_DOWN] or keys[pg.K_s]: self.vel = vec(-PLAYER_SPEED / 2, 0).rotate(-self.rot) if keys[pg.K_SPACE]: now = pg.time.get_ticks() if now - self.last_shot > BULLET_RATE: self.last_shot = now dir = vec(1, 0).rotate(-self.rot) pos = self.pos + BARREL_OFFSET.rotate(-self.rot) Bullet(self.game, pos, dir) self.vel = vec(-KICKBACK, 0).rotate(-self.rot) MuzzleFlash(self.game, pos)
Example #20
Source File: game_main.py From Python24 with MIT License | 5 votes |
def __event(self): # 获取所有窗口中的事件监听 -> 列表 event_list = pygame.event.get() # 遍历所有的事件 for event in event_list: # 如果是鼠标点击关闭事件 if event.type == pygame.QUIT: self.game_over() # 监听按下事件 if event.type == pygame.KEYDOWN: # 是否按下的是Esc if event.key == pygame.K_ESCAPE: self.game_over() # 是否按下的是空格 if event.key == pygame.K_SPACE: self.hero_plane.shoot() # 监听键盘长按事件 -> 元组(0没按下,1长按了) 字母对应阿斯克码 pressed_keys = pygame.key.get_pressed() # 判断向上是否被长按(1) if pressed_keys[pygame.K_UP] or pressed_keys[pygame.K_w]: self.hero_plane.move_up() # 判断向下是否被长按(1) if pressed_keys[pygame.K_DOWN] or pressed_keys[pygame.K_s]: self.hero_plane.move_down() # 判断向左是否被长按(1) if pressed_keys[pygame.K_LEFT] or pressed_keys[pygame.K_a]: self.hero_plane.move_left() # 判断向右是否被长按(1) if pressed_keys[pygame.K_RIGHT] or pressed_keys[pygame.K_d]: self.hero_plane.move_right() # 4- 刷新窗口
Example #21
Source File: cli.py From stuntcat with GNU Lesser General Public License v2.1 | 5 votes |
def __pgbox(title, message): try: import pygame as pg pg.quit() #clean out anything running pg.display.init() pg.font.init() screen = pg.display.set_mode((460, 140)) pg.display.set_caption(title) font = pg.font.Font(None, 18) foreg, backg, liteg = (0, 0, 0), (180, 180, 180), (210, 210, 210) ok = font.render('Ok', 1, foreg, liteg) okbox = ok.get_rect().inflate(200, 10) okbox.centerx = screen.get_rect().centerx okbox.bottom = screen.get_rect().bottom - 10 screen.fill(backg) screen.fill(liteg, okbox) screen.blit(ok, okbox.inflate(-200, -10)) pos = [10, 10] for text in message.split('\n'): if text: msg = font.render(text, 1, foreg, backg) screen.blit(msg, pos) pos[1] += font.get_height() pg.display.flip() stopkeys = pg.K_ESCAPE, pg.K_SPACE, pg.K_RETURN, pg.K_KP_ENTER while 1: e = pg.event.wait() if e.type == pg.QUIT or \ (e.type == pg.KEYDOWN and e.key in stopkeys) or \ (e.type == pg.MOUSEBUTTONDOWN and okbox.collidepoint(e.pos)): break pg.quit() except pg.error: raise ImportError
Example #22
Source File: startGame.py From AIGames with MIT License | 5 votes |
def startGame(screen, sounds, bird_images, other_images, backgroud_image, cfg, mode): base_pos = [0, cfg.SCREENHEIGHT*0.79] base_diff_bg = other_images['base'].get_width() - backgroud_image.get_width() msg_pos = [(cfg.SCREENWIDTH-other_images['message'].get_width())/2, cfg.SCREENHEIGHT*0.12] bird_idx = 0 bird_idx_change_count = 0 bird_idx_cycle = itertools.cycle([0, 1, 2, 1]) bird_pos = [cfg.SCREENWIDTH*0.2, (cfg.SCREENHEIGHT-list(bird_images.values())[0].get_height())/2] bird_y_shift_count = 0 bird_y_shift_max = 9 shift = 1 clock = pygame.time.Clock() if mode == 'train': return {'bird_pos': bird_pos, 'base_pos': base_pos, 'bird_idx': bird_idx} while True: for event in pygame.event.get(): if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE): pygame.quit() sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE or event.key == pygame.K_UP: return {'bird_pos': bird_pos, 'base_pos': base_pos, 'bird_idx': bird_idx} sounds['wing'].play() bird_idx_change_count += 1 if bird_idx_change_count % 5 == 0: bird_idx = next(bird_idx_cycle) bird_idx_change_count = 0 base_pos[0] = -((-base_pos[0] + 4) % base_diff_bg) bird_y_shift_count += 1 if bird_y_shift_count == bird_y_shift_max: bird_y_shift_max = 16 shift = -1 * shift bird_y_shift_count = 0 bird_pos[-1] = bird_pos[-1] + shift screen.blit(backgroud_image, (0, 0)) screen.blit(list(bird_images.values())[bird_idx], bird_pos) screen.blit(other_images['message'], msg_pos) screen.blit(other_images['base'], base_pos) pygame.display.update() clock.tick(cfg.FPS)
Example #23
Source File: startGame.py From AIGames with MIT License | 5 votes |
def startGame(screen, sounds, bird_images, other_images, backgroud_image, cfg, mode): base_pos = [0, cfg.SCREENHEIGHT*0.79] base_diff_bg = other_images['base'].get_width() - backgroud_image.get_width() msg_pos = [(cfg.SCREENWIDTH-other_images['message'].get_width())/2, cfg.SCREENHEIGHT*0.12] bird_idx = 0 bird_idx_change_count = 0 bird_idx_cycle = itertools.cycle([0, 1, 2, 1]) bird_pos = [cfg.SCREENWIDTH*0.2, (cfg.SCREENHEIGHT-list(bird_images.values())[0].get_height())/2] bird_y_shift_count = 0 bird_y_shift_max = 9 shift = 1 clock = pygame.time.Clock() if mode == 'train': return {'bird_pos': bird_pos, 'base_pos': base_pos, 'bird_idx': bird_idx} while True: for event in pygame.event.get(): if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE): pygame.quit() sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE or event.key == pygame.K_UP: return {'bird_pos': bird_pos, 'base_pos': base_pos, 'bird_idx': bird_idx} sounds['wing'].play() bird_idx_change_count += 1 if bird_idx_change_count % 5 == 0: bird_idx = next(bird_idx_cycle) bird_idx_change_count = 0 base_pos[0] = -((-base_pos[0] + 4) % base_diff_bg) bird_y_shift_count += 1 if bird_y_shift_count == bird_y_shift_max: bird_y_shift_max = 16 shift = -1 * shift bird_y_shift_count = 0 bird_pos[-1] = bird_pos[-1] + shift screen.blit(backgroud_image, (0, 0)) screen.blit(list(bird_images.values())[bird_idx], bird_pos) screen.blit(other_images['message'], msg_pos) screen.blit(other_images['base'], base_pos) pygame.display.update() clock.tick(cfg.FPS)
Example #24
Source File: 六个类合一.py From Python24 with MIT License | 5 votes |
def __event(self): # 获取所有窗口中的事件监听 -> 列表 event_list = pygame.event.get() # 遍历所有的事件 for event in event_list: # 如果是鼠标点击关闭事件 if event.type == pygame.QUIT: self.game_over() # 监听按下事件 if event.type == pygame.KEYDOWN: # 是否按下的是Esc if event.key == pygame.K_ESCAPE: self.game_over() # 是否按下的是空格 if event.key == pygame.K_SPACE: self.hero_plane.shoot() # 监听键盘长按事件 -> 元组(0没按下,1长按了) 字母对应阿斯克码 pressed_keys = pygame.key.get_pressed() # 判断向上是否被长按(1) if pressed_keys[pygame.K_UP] or pressed_keys[pygame.K_w]: self.hero_plane.move_up() # 判断向下是否被长按(1) if pressed_keys[pygame.K_DOWN] or pressed_keys[pygame.K_s]: self.hero_plane.move_down() # 判断向左是否被长按(1) if pressed_keys[pygame.K_LEFT] or pressed_keys[pygame.K_a]: self.hero_plane.move_left() # 判断向右是否被长按(1) if pressed_keys[pygame.K_RIGHT] or pressed_keys[pygame.K_d]: self.hero_plane.move_right() # 4- 刷新窗口
Example #25
Source File: startGame.py From AIGames with MIT License | 5 votes |
def startGame(screen, sounds, bird_images, other_images, backgroud_image, cfg, mode): base_pos = [0, cfg.SCREENHEIGHT*0.79] base_diff_bg = other_images['base'].get_width() - backgroud_image.get_width() msg_pos = [(cfg.SCREENWIDTH-other_images['message'].get_width())/2, cfg.SCREENHEIGHT*0.12] bird_idx = 0 bird_idx_change_count = 0 bird_idx_cycle = itertools.cycle([0, 1, 2, 1]) bird_pos = [cfg.SCREENWIDTH*0.2, (cfg.SCREENHEIGHT-list(bird_images.values())[0].get_height())/2] bird_y_shift_count = 0 bird_y_shift_max = 9 shift = 1 clock = pygame.time.Clock() if mode == 'train': return {'bird_pos': bird_pos, 'base_pos': base_pos, 'bird_idx': bird_idx} while True: for event in pygame.event.get(): if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE): pygame.quit() sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE or event.key == pygame.K_UP: return {'bird_pos': bird_pos, 'base_pos': base_pos, 'bird_idx': bird_idx} sounds['wing'].play() bird_idx_change_count += 1 if bird_idx_change_count % 5 == 0: bird_idx = next(bird_idx_cycle) bird_idx_change_count = 0 base_pos[0] = -((-base_pos[0] + 4) % base_diff_bg) bird_y_shift_count += 1 if bird_y_shift_count == bird_y_shift_max: bird_y_shift_max = 16 shift = -1 * shift bird_y_shift_count = 0 bird_pos[-1] = bird_pos[-1] + shift screen.blit(backgroud_image, (0, 0)) screen.blit(list(bird_images.values())[bird_idx], bird_pos) screen.blit(other_images['message'], msg_pos) screen.blit(other_images['base'], base_pos) pygame.display.update() clock.tick(cfg.FPS)
Example #26
Source File: draw.py From rpisurv with GNU General Public License v2.0 | 5 votes |
def check_keypress(): try: for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_q or event.key == pygame.K_a or event.key == pygame.K_KP_DIVIDE or event.key == pygame.K_BACKSPACE: logger.debug("Keypress 'a' or 'q' or 'backspace' or 'keypad /' detected.") return "end_event" if event.key == pygame.K_n or event.key == pygame.K_SPACE or event.key == pygame.K_KP_PLUS: logger.debug("Keypress 'n' or 'space' or 'keypad +' detected.") return "next_event" if event.key == pygame.K_r or event.key == pygame.K_KP_PERIOD or event.key == pygame.K_COMMA: logger.debug("Keypress 'r' or ',' or 'keypad .' detected") return "resume_rotation" if event.key == pygame.K_p or event.key == pygame.K_KP_MULTIPLY: logger.debug("Keypress 'p' or 'keypad *' detected") return "pause_rotation" for numeric_key_counter, key in enumerate([pygame.K_F1,pygame.K_F2,pygame.K_F3,pygame.K_F4,pygame.K_F5,pygame.K_F6,pygame.K_F7,pygame.K_F8,pygame.K_F9,pygame.K_F10,pygame.K_F11,pygame.K_F12]): if event.key == key: logger.debug("Keypress 'F" + str(numeric_key_counter + 1) + "' detected") return numeric_key_counter for numeric_key_counter, key in enumerate([pygame.K_KP0,pygame.K_KP1,pygame.K_KP2,pygame.K_KP3,pygame.K_KP4,pygame.K_KP5,pygame.K_KP6,pygame.K_KP7,pygame.K_KP8,pygame.K_KP9]): if event.key == key: logger.debug("Keypress 'keypad " + str(numeric_key_counter + 1) + "' detected") return numeric_key_counter else: return None except pygame.error as e: logger.debug("Exception " + repr(e)) exit(0)
Example #27
Source File: kernal.py From RoboMaster-AI-Challenge-Simulator-2D with MIT License | 5 votes |
def play(self, file): self.memory = np.load(file) i = 0 stop = False flag = 0 while True: self.time = self.memory[i].time self.cars = self.memory[i].cars self.car_num = len(self.cars) self.compet_info = self.memory[i].compet_info self.detect = self.memory[i].detect self.vision = self.memory[i].vision self.bullets = self.memory[i].bullets for event in pygame.event.get(): if event.type == pygame.QUIT: return pressed = pygame.key.get_pressed() if pressed[pygame.K_TAB]: self.dev = True else: self.dev = False self.one_epoch() if pressed[pygame.K_SPACE] and not flag: flag = 50 stop = not stop if flag > 0: flag -= 1 if pressed[pygame.K_LEFT] and i > 10: i -= 10 if pressed[pygame.K_RIGHT] and i < len(self.memory)-10: i += 10 if i < len(self.memory)-1 and not stop: i += 1 self.clock.tick(200)
Example #28
Source File: pyint.py From Pyint_Pixel-Painter with GNU General Public License v3.0 | 5 votes |
def key_event_up(event): global penSize, undoed, holdingCTRL, colorScheme, selectedTool if event.key == pg.K_1: colorScheme = 1 elif event.key == pg.K_2: colorScheme = 2 if event.key == pg.K_e: selectedTool = 1 B_Buttons[1].clicked = True for subbutton in B_Buttons: if B_Buttons.index(subbutton) != selectedTool: subbutton.clicked = False elif event.key == pg.K_b: selectedTool = 0 B_Buttons[0].clicked = True for subbutton in B_Buttons: if B_Buttons.index(subbutton) != selectedTool: subbutton.clicked = False elif event.key == pg.K_g: selectedTool = 2 B_Buttons[2].clicked = True for subbutton in B_Buttons: if B_Buttons.index(subbutton) != selectedTool: subbutton.clicked = False elif event.key == pg.K_i: selectedTool = 3 B_Buttons[3].clicked = True for subbutton in B_Buttons: if B_Buttons.index(subbutton) != selectedTool: subbutton.clicked = False if event.key == pg.K_LCTRL: holdingCTRL = False if event.key == pg.K_SPACE: if holdingCTRL: g1.clean() undoed = True if event.key == pg.K_s: if holdingCTRL: shortcutPath = FileManager(1) SaveFile(g1, shortcutPath) if event.key == pg.K_z: if holdingCTRL: for i in range(g1.yCount): for j in range(g1.xCount): if round == 1: g1.change_color(j, i, g1.undoList[1][i][j]) if round == -1: g1.change_color(j, i, g1.undoList[0][i][j]) undoed = True
Example #29
Source File: PlotPyGame.py From Grid2Op with Mozilla Public License 2.0 | 5 votes |
def _event_looper(self, force=False): has_quit = False if self._deactivate_display: return force, has_quit for event in pygame.event.get(): if event.type == pygame.QUIT: has_quit = True return force, has_quit # pygame.quit() # exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: has_quit = True return force, has_quit if event.key == pygame.K_SPACE: self._get_plot_pause() # pause_surface = self.draw_plot_pause() # self.screen.blit(pause_surface, (320 + self.left_menu_shape[0], 320)) return not force, has_quit return force, has_quit
Example #30
Source File: gameend.py From AIGames with MIT License | 5 votes |
def GameEndInterface(screen, cfg): replay_image = pygame.image.load(cfg.IMAGE_PATHS['replay']) replay_image = pygame.transform.scale(replay_image, (35, 31)) replay_image_rect = replay_image.get_rect() replay_image_rect.centerx = cfg.SCREENSIZE[0] / 2 replay_image_rect.top = cfg.SCREENSIZE[1] * 0.52 gameover_image = pygame.image.load(cfg.IMAGE_PATHS['gameover']) gameover_image = pygame.transform.scale(gameover_image, (190, 11)) gameover_image_rect = gameover_image.get_rect() gameover_image_rect.centerx = cfg.SCREENSIZE[0] / 2 gameover_image_rect.centery = cfg.SCREENSIZE[1] * 0.35 clock = pygame.time.Clock() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE or event.key == pygame.K_UP: return True elif event.type == pygame.MOUSEBUTTONDOWN: mouse_pos = pygame.mouse.get_pos() if replay_image_rect.collidepoint(mouse_pos): return True screen.blit(replay_image, replay_image_rect) screen.blit(gameover_image, gameover_image_rect) pygame.display.update() clock.tick(cfg.FPS)