03.键盘状态跟踪与精灵删除
使用pyglet.window.key.KeyStateHandler,能
跟踪键盘上每个键的状态:
修改上一节中的Player类,现在可以从类中删除on_key_press()和方法on_key_release()方法,交给KeyStateHandler()处理,将其推送到事件堆栈中。
# 将其推送到事件堆栈中 game_window.push_handlers(play_ship.key_handler)
修改后的player类:
"""键盘输入""" import math from let import physicalobject, resources from pyglet.window import key class Player(physicalobject.Physicalobject): """玩家操控""" def __init__(self, *args, **kwargs): super().__init__(img=resources.player_image, *args, **kwargs) self.thrust = 300.0 # 速度 self.rotate_speed = 200.0 # 角度调整度数 self.key_handler = key.KeyStateHandler() def update(self, dt): super(Player, self).update(dt) if self.key_handler[key.LEFT]: # 向左转 self.rotation -= self.rotate_speed * dt # rotation为角度 if self.key_handler[key.RIGHT]: # 向右转 self.rotation += self.rotate_speed * dt if self.key_handler[key.UP]: # 向前行 angle_radians = -math.radians(self.rotation) # 度转换为弧度,radians参数是弧度 force_x = math.cos(angle_radians)*self.thrust*dt force_y = math.sin(angle_radians)*self.thrust*dt self.velocity_x += force_x self.velocity_y += force_y
增加发动机火焰:resources.py
# 火焰图像,将火焰图像的旋转中心向右移动,经过图像的末端 engine_image = pyglet.resource.image("engine_flame.png") engine_image.anchor_x = engine_image.width * 1.5 engine_image.anchor_y = engine_image.height / 2
创建和绘制火焰图像:
在Player类的构造函数在加载火焰,并设置默认不可见
# 创建和绘制火焰 self.engine_sprite = pyglet.sprite.Sprite(img=resources.engine_image, *args, **kwargs) self.engine_sprite.visible = False # 默认不可见
并修改update的[key.UP]:
# 火焰精灵显示在玩家的位置,跟随玩家旋转 self.engine_sprite.rotation = self.rotation self.engine_sprite.x = self.x self.engine_sprite.y = self.y self.engine_sprite.visible = True
else:
self.engine_sprite.visible = False
删除精灵操作:
当玩家死亡需要删除玩家与火焰
def delete(self): self.engine_sprite.delete() # 删除火焰精灵 super(Player, self).delete() # 调用Sprite自己的delete()方法删除玩家
修改后的完整Player类:
"""键盘输入""" import math import pyglet from let import physicalobject, resources from pyglet.window import key class Player(physicalobject.Physicalobject): """玩家操控""" def __init__(self, *args, **kwargs): super().__init__(img=resources.player_image, *args, **kwargs) self.thrust = 300.0 # 速度 self.rotate_speed = 200.0 # 角度调整度数 self.key_handler = key.KeyStateHandler() # 创建和绘制火焰 self.engine_sprite = pyglet.sprite.Sprite(img=resources.engine_image, *args, **kwargs) self.engine_sprite.visible = False # 默认不可见 def update(self, dt): super(Player, self).update(dt) if self.key_handler[key.LEFT]: # 向左转 self.rotation -= self.rotate_speed * dt # rotation为角度 if self.key_handler[key.RIGHT]: # 向右转 self.rotation += self.rotate_speed * dt if self.key_handler[key.UP]: # 向前行 angle_radians = -math.radians(self.rotation) # 度转换为弧度,radians参数是弧度 force_x = math.cos(angle_radians) * self.thrust * dt force_y = math.sin(angle_radians) * self.thrust * dt self.velocity_x += force_x self.velocity_y += force_y # 火焰精灵显示在玩家的位置,跟随玩家旋转 self.engine_sprite.rotation = self.rotation self.engine_sprite.x = self.x self.engine_sprite.y = self.y self.engine_sprite.visible = True else: self.engine_sprite.visible = False def delete(self): self.engine_sprite.delete() # 删除火焰精灵 super(Player, self).delete() # 调用Sprite自己的delete()方法删除玩家