实现子弹类,完成子弹的首要操纵
- # 飞机子弹类
- 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(),就可以运行我们的游戏了。 (编辑:湖南网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|