实现子弹类,完成子弹的主要操作
- # 飞机子弹类
- class Bullet():
- def __init__(self, image_path=os.path.join(source_dir,'bullet.png'), background_size=(480, 700), plan=None, speed=1000):
- '''
- :param image_path: 子弹的图片地址
- :param background_size: 游戏窗口大小
- :param plan: 飞机对象
- :param speed: 子弹飞行速度
- '''
- self.image = pygame.image.load(image_path).convert_alpha()
- self.background_size = background_size
- self.speed = background_size[1] / speed
- # 子弹是否击中敌机
- self.destroyed = False
- self.position = self._get_position(plan)
-
- def _get_position(self, plan):
- '''
- 根据plan得到子弹发出位置
- :param plan: 飞机对象
- '''
- bullet_size = self.image.get_size()
- plan_width = plan.image_size[0]
- x = (plan_width-bullet_size[0]) / 2
- return [plan.position[0] + x, plan.position[1]]
-
- def update(self, time_passed):
- '''
- 改变子弹位置
- :param time_passed: 距离上次绘制图像到现在的时间
- '''
- # 如果子弹超出屏幕或者击中敌机,就设置self.position[1]为-100,在plan.draw的时候就移除它
- if self.position[1] + self.image.get_size()[1] <= 0 or self.destroyed:
- self.position[1] = -100
- return
-
- # 改变的距离 = 时间 * 速率
- self.position[1] -= time_passed * self.speed

这样,我们就把所有的操作都实现完了,接下来只需要使用 Game().run(),就可以运行我们的游戏了。 (编辑:晋中站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|