Python pygame.K_DOWN Examples

The following are 30 code examples of pygame.K_DOWN(). 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: sprites.py    From pygame_tutorials with MIT License 8 votes vote down vote up
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 #2
Source File: tilemap_demo2.py    From pygame_tutorials with MIT License 6 votes vote down vote up
def update(self):
        pg.display.set_caption(str((self.rect.x, self.rect.y)))
        self.vx, self.vy = 0, 0
        keys = pg.key.get_pressed()
        if keys[pg.K_UP]:
            self.vy = -5
        if keys[pg.K_DOWN]:
            self.vy = 5
        if keys[pg.K_LEFT]:
            self.vx = -5
        if keys[pg.K_RIGHT]:
            self.vx = 5
        # if self.vx != 0 and self.vy != 0:
        #     self.vx *= 0.7071
        #     self.vy *= 0.7071
        self.rect.x += self.vx
        if pg.sprite.spritecollide(self, walls, False, pg.sprite.collide_mask):
            self.rect.x -= self.vx
        self.rect.y += self.vy
        if pg.sprite.spritecollide(self, walls, False, pg.sprite.collide_mask):
            self.rect.y -= self.vy 
Example #3
Source File: sprites.py    From pygame_tutorials with MIT License 6 votes vote down vote up
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 #4
Source File: sprites.py    From pygame_tutorials with MIT License 6 votes vote down vote up
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 #5
Source File: sprites.py    From pygame_tutorials with MIT License 6 votes vote down vote up
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 #6
Source File: sprites.py    From pygame_tutorials with MIT License 6 votes vote down vote up
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 #7
Source File: sprites.py    From pygame_tutorials with MIT License 6 votes vote down vote up
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 #8
Source File: sprites.py    From pygame_tutorials with MIT License 6 votes vote down vote up
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 #9
Source File: movement.py    From pygame_tutorials with MIT License 6 votes vote down vote up
def update(self):
        self.vx, self.vy = 0, 0
        keystate = pg.key.get_pressed()
        if keystate[pg.K_UP]:
            self.vy = -5
        if keystate[pg.K_DOWN]:
            self.vy = 5
        if keystate[pg.K_LEFT]:
            self.vx = -2
        if keystate[pg.K_RIGHT]:
            self.vx = 1
        if self.vx != 0 and self.vy != 0:
            self.vx /= 1.414
            self.vy /= 1.414
        self.rect.x += self.vx
        self.rect.y += self.vy 
Example #10
Source File: motion_demo.py    From pygame_tutorials with MIT License 6 votes vote down vote up
def update(self):
        self.acc = vec(0, 0)
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT]:
            self.acc.x = -0.2
        if keys[pygame.K_RIGHT]:
            self.acc.x = 0.2
        if keys[pygame.K_UP]:
            self.acc.y = -0.2
        if keys[pygame.K_DOWN]:
            self.acc.y = 0.2

        self.vel += self.acc
        self.pos += self.vel + 0.5 * self.acc
        if self.pos.x > WIDTH:
            self.pos.x = 0
        if self.pos.x < 0:
            self.pos.x = WIDTH
        if self.pos.y > HEIGHT:
            self.pos.y = 0
        if self.pos.y < 0:
            self.pos.y = HEIGHT

        self.rect.center = self.pos 
Example #11
Source File: viewer.py    From generals-bot with MIT License 6 votes vote down vote up
def _handleKeypress(self, key):
		if self._clicked == None or self._moveEvent == None:
			return False
		column = self._clicked[0]
		row = self._clicked[1]

		target = None
		if key == pygame.K_LEFT:
			if column > 0:
				target = (column-1, row)
		elif key == pygame.K_RIGHT:
			if column < self._map.cols - 1:
				target = (column+1, row)
		elif key == pygame.K_UP:
			if row > 0:
				target = (column, row-1)
		elif key == pygame.K_DOWN:
			if row < self._map.rows - 1:
				target = (column, row+1)

		if target != None:
			self._moveEvent(self._clicked, target)
			self._clicked = target 
Example #12
Source File: engine.py    From Fruit-API with GNU General Public License v3.0 6 votes vote down vote up
def __human_control(self, key):
        if self.human_control:
            robot = self.robots[self.human_control_robot]
            if key == pygame.K_LEFT:
                robot.move(GlobalConstants.LEFT_ACTION, self.sprites)
            if key == pygame.K_RIGHT:
                robot.move(GlobalConstants.RIGHT_ACTION, self.sprites)
            if key == pygame.K_UP:
                robot.move(GlobalConstants.UP_ACTION, self.sprites)
            if key == pygame.K_DOWN:
                robot.move(GlobalConstants.DOWN_ACTION, self.sprites)
            if key == pygame.K_a:
                reward = robot.move(GlobalConstants.FIRE_ACTION, self.sprites)
                if reward > 0:
                    self.rewards[self.human_control_robot].append(reward)
                    self.total_score[self.human_control_robot] += reward 
Example #13
Source File: graphics.py    From highway-env with MIT License 6 votes vote down vote up
def handle_continuous_action_event(cls, action_type: ContinuousAction, event: pygame.event.EventType) -> None:
        action = action_type.last_action.copy()
        steering_index = action_type.space().shape[0] - 1
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT and action_type.lateral:
                action[steering_index] = 0.7
            if event.key == pygame.K_LEFT and action_type.lateral:
                action[steering_index] = -0.7
            if event.key == pygame.K_DOWN and action_type.longitudinal:
                action[0] = -0.7
            if event.key == pygame.K_UP and action_type.longitudinal:
                action[0] = 0.7
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_RIGHT and action_type.lateral:
                action[steering_index] = 0
            if event.key == pygame.K_LEFT and action_type.lateral:
                action[steering_index] = 0
            if event.key == pygame.K_DOWN and action_type.longitudinal:
                action[0] = 0
            if event.key == pygame.K_UP and action_type.longitudinal:
                action[0] = 0
        action_type.act(action) 
Example #14
Source File: manual-control-pygame.py    From DJITelloPy with MIT License 6 votes vote down vote up
def keydown(self, key):
        """ Update velocities based on key pressed
        Arguments:
            key: pygame key
        """
        if key == pygame.K_UP:  # set forward velocity
            self.for_back_velocity = S
        elif key == pygame.K_DOWN:  # set backward velocity
            self.for_back_velocity = -S
        elif key == pygame.K_LEFT:  # set left velocity
            self.left_right_velocity = -S
        elif key == pygame.K_RIGHT:  # set right velocity
            self.left_right_velocity = S
        elif key == pygame.K_w:  # set up velocity
            self.up_down_velocity = S
        elif key == pygame.K_s:  # set down velocity
            self.up_down_velocity = -S
        elif key == pygame.K_a:  # set yaw counter clockwise velocity
            self.yaw_velocity = -S
        elif key == pygame.K_d:  # set yaw clockwise velocity
            self.yaw_velocity = S 
Example #15
Source File: manual-control-pygame.py    From DJITelloPy with MIT License 6 votes vote down vote up
def keyup(self, key):
        """ Update velocities based on key released
        Arguments:
            key: pygame key
        """
        if key == pygame.K_UP or key == pygame.K_DOWN:  # set zero forward/backward velocity
            self.for_back_velocity = 0
        elif key == pygame.K_LEFT or key == pygame.K_RIGHT:  # set zero left/right velocity
            self.left_right_velocity = 0
        elif key == pygame.K_w or key == pygame.K_s:  # set zero up/down velocity
            self.up_down_velocity = 0
        elif key == pygame.K_a or key == pygame.K_d:  # set zero yaw velocity
            self.yaw_velocity = 0
        elif key == pygame.K_t:  # takeoff
            self.tello.takeoff()
            self.send_rc_control = True
        elif key == pygame.K_l:  # land
            not self.tello.land()
            self.send_rc_control = False 
Example #16
Source File: player.py    From Tutoriales_juegos_Python with GNU General Public License v3.0 6 votes vote down vote up
def handle_event(self, event):
        if event.type == pygame.QUIT:
            game_over = True

        if event.type == pygame.KEYDOWN:

            if event.key == pygame.K_LEFT:
                self.update('left')
            if event.key == pygame.K_RIGHT:
                self.update('right')
            if event.key == pygame.K_UP:
                self.update('up')
            if event.key == pygame.K_DOWN:
                self.update('down')

        if event.type == pygame.KEYUP:

            if event.key == pygame.K_LEFT:
                self.update('stand_left')
            if event.key == pygame.K_RIGHT:
                self.update('stand_right')
            if event.key == pygame.K_UP:
                self.update('stand_up')
            if event.key == pygame.K_DOWN:
                self.update('stand_down') 
Example #17
Source File: death.py    From The-Stolen-Crown-RPG with MIT License 6 votes vote down vote up
def update(self, keys):
        """
        Update arrow position.
        """
        if self.allow_input:
            if keys[pg.K_DOWN] and not keys[pg.K_UP] and self.index == 0:
                self.index = 1
                self.allow_input = False
                self.notify(c.CLICK)
            elif keys[pg.K_UP] and not keys[pg.K_DOWN] and self.index == 1:
                self.index = 0
                self.allow_input = False
                self.notify(c.CLICK)

            self.rect.y = self.pos_list[self.index]

        if not keys[pg.K_DOWN] and not keys[pg.K_UP]:
            self.allow_input = True 
Example #18
Source File: main_menu.py    From The-Stolen-Crown-RPG with MIT License 6 votes vote down vote up
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 #19
Source File: main.py    From python-examples with MIT License 6 votes vote down vote up
def handle_event(self, event):
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                self.turn_left = True
            elif event.key == pygame.K_RIGHT:
                self.turn_right = True
            elif event.key == pygame.K_UP:
                self.move_forward = True
            elif event.key == pygame.K_DOWN:
                self.move_backward = True
            
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                self.turn_left = False
            elif event.key == pygame.K_RIGHT:
                self.turn_right = False
            elif event.key == pygame.K_UP:
                self.move_forward = False
            elif event.key == pygame.K_DOWN:
                self.move_backward = False 
Example #20
Source File: example-2.py    From python-examples with MIT License 6 votes vote down vote up
def handle_event(self, event):

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                self.move_x -= self.speed
            elif event.key == pygame.K_RIGHT:
                self.move_x += self.speed
            elif event.key == pygame.K_UP:
                self.move_y -= self.speed
            elif event.key == pygame.K_DOWN:
                self.move_y += self.speed
            
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                self.move_x += self.speed
            elif event.key == pygame.K_RIGHT:
                self.move_x -= self.speed
            elif event.key == pygame.K_UP:
                self.move_y += self.speed
            elif event.key == pygame.K_DOWN:
                self.move_y -= self.speed 
Example #21
Source File: example-1.py    From python-examples with MIT License 6 votes vote down vote up
def handle_event(self, event):

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                self.move_x -= self.speed
            elif event.key == pygame.K_RIGHT:
                self.move_x += self.speed
            elif event.key == pygame.K_UP:
                self.move_y -= self.speed
            elif event.key == pygame.K_DOWN:
                self.move_y += self.speed
            
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                self.move_x += self.speed
            elif event.key == pygame.K_RIGHT:
                self.move_x -= self.speed
            elif event.key == pygame.K_UP:
                self.move_y += self.speed
            elif event.key == pygame.K_DOWN:
                self.move_y -= self.speed 
Example #22
Source File: Game23.py    From Games with MIT License 5 votes vote down vote up
def main(cfg):
	# 游戏初始化
	pygame.init()
	screen = pygame.display.set_mode(cfg.SCREENSIZE)
	pygame.display.set_caption('2048 —— 微信公众号:Charles的皮卡丘')
	# 播放背景音乐
	pygame.mixer.music.load(cfg.BGMPATH)
	pygame.mixer.music.play(-1)
	# 实例化2048游戏
	game_2048 = Game2048(matrix_size=cfg.GAME_MATRIX_SIZE, max_score_filepath=cfg.MAX_SCORE_FILEPATH)
	# 游戏主循环
	clock = pygame.time.Clock()
	is_running = True
	while is_running:
		screen.fill(pygame.Color(cfg.BG_COLOR))
		# --按键检测
		for event in pygame.event.get():
			if event.type == pygame.QUIT:
				pygame.quit()
				sys.exit()
			elif event.type == pygame.KEYDOWN:
				if event.key in [pygame.K_UP, pygame.K_DOWN, pygame.K_LEFT, pygame.K_RIGHT]:
					game_2048.setDirection({pygame.K_UP: 'up', pygame.K_DOWN: 'down', pygame.K_LEFT: 'left', pygame.K_RIGHT: 'right'}[event.key])
		# --更新游戏状态
		game_2048.update()
		if game_2048.isgameover:
			game_2048.saveMaxScore()
			is_running = False
		# --将必要的游戏元素画到屏幕上
		drawGameMatrix(screen, game_2048.game_matrix, cfg)
		start_x, start_y = drawScore(screen, game_2048.score, game_2048.max_score, cfg)
		drawGameIntro(screen, start_x, start_y, cfg)
		# --屏幕更新
		pygame.display.update()
		clock.tick(cfg.FPS)
	return endInterface(screen, cfg) 
Example #23
Source File: Game24.py    From Games with MIT License 5 votes vote down vote up
def main(cfg):
	# 游戏初始化
	pygame.init()
	screen = pygame.display.set_mode(cfg.SCREENSIZE)
	pygame.display.set_caption('Greedy Snake —— 微信公众号:Charles的皮卡丘')
	clock = pygame.time.Clock()
	# 播放背景音乐
	pygame.mixer.music.load(cfg.BGMPATH)
	pygame.mixer.music.play(-1)
	# 游戏主循环
	snake = Snake(cfg)
	apple = Apple(cfg, snake.coords)
	score = 0
	while True:
		screen.fill(cfg.BLACK)
		# --按键检测
		for event in pygame.event.get():
			if event.type == pygame.QUIT:
				pygame.quit()
				sys.exit()
			elif event.type == pygame.KEYDOWN:
				if event.key in [pygame.K_UP, pygame.K_DOWN, pygame.K_LEFT, pygame.K_RIGHT]:
					snake.setDirection({pygame.K_UP: 'up', pygame.K_DOWN: 'down', pygame.K_LEFT: 'left', pygame.K_RIGHT: 'right'}[event.key])
		# --更新贪吃蛇和食物
		if snake.update(apple):
			apple = Apple(cfg, snake.coords)
			score += 1
		# --判断游戏是否结束
		if snake.isgameover: break
		# --显示游戏里必要的元素
		drawGameGrid(cfg, screen)
		snake.draw(screen)
		apple.draw(screen)
		showScore(cfg, score, screen)
		# --屏幕更新
		pygame.display.update()
		clock.tick(cfg.FPS)
	return endInterface(screen, cfg) 
Example #24
Source File: gui_util.py    From Reinforcement-Learning-for-Self-Driving-Cars with Apache License 2.0 5 votes vote down vote up
def control_car(target_car, keydown):
    if keydown == pygame.K_UP:
        target_car.move('A')
    elif keydown == pygame.K_DOWN:
        target_car.move('D')
    else:
        target_car.move('M')

    if keydown == pygame.K_LEFT:
        target_car.switch_lane('L')
    elif keydown == pygame.K_RIGHT:
        target_car.switch_lane('R') 
Example #25
Source File: game_main.py    From Python24 with MIT License 5 votes vote down vote up
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 #26
Source File: 六个类合一.py    From Python24 with MIT License 5 votes vote down vote up
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 #27
Source File: pygame.py    From pyimgui with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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 
Example #28
Source File: quad example.py    From pygame_tutorials with MIT License 5 votes vote down vote up
def move_8way(self):
        keystate = pg.key.get_pressed()
        if keystate[pg.K_UP]:
            self.vy = -5
        if keystate[pg.K_DOWN]:
            self.vy = 5
        if keystate[pg.K_LEFT]:
            self.vx = -5
        if keystate[pg.K_RIGHT]:
            self.vx = 5
        if self.vx != 0 and self.vy != 0:
            self.vx *= 0.7071
            self.vy *= 0.7071 
Example #29
Source File: vector_intro.py    From pygame_tutorials with MIT License 5 votes vote down vote up
def get_keys(self):
        keys = pg.key.get_pressed()
        self.rot_speed = 0
        self.acc = vec(0, 0)
        if keys[pg.K_LEFT]:
            self.rot_speed = 90
        if keys[pg.K_RIGHT]:
            self.rot_speed = -90
        if keys[pg.K_UP]:
            self.acc = vec(250, 0)
        if keys[pg.K_DOWN]:
            self.acc = vec(-250, 0) 
Example #30
Source File: flocking2.py    From pygame_tutorials with MIT License 5 votes vote down vote up
def move_8way(self):
        keystate = pg.key.get_pressed()
        if keystate[pg.K_UP]:
            self.vel.y = -5
        if keystate[pg.K_DOWN]:
            self.vel.y = 5
        if keystate[pg.K_LEFT]:
            self.vel.x = -5
        if keystate[pg.K_RIGHT]:
            self.vel.x = 5