Python 2.7 - Pygame - 交叉两个移动的精灵

时间:2013-05-14 19:56:53

标签: python python-2.7 pygame angle

我有2个精灵(好吧...... 3)。一个精灵以一个随机角度(以弧度表示)以稳定速度沿直线行进。另一个精灵正在向移动的精灵射击。目前,射弹精灵略微落在目标后面,因为射弹在射击时根据目标的中心给出目的地。我可以不断更新,但如果弹丸改变方向像导弹那样,那就太傻了。

那么,如何在 earth 上进行此操作?

class Projectile (pygame.sprite.Sprite):

    container = pygame.sprite.Group()

    def __init__ (self, pos, target):
        pygame.sprite.Sprite.__init__ (self, self.container)

        self.image = FIREBALL
        self.rect = self.image.get_rect()
        self.rect.center = pos
        self.true_loc = self.rect.center
        self.destination = target.rect.center
        self.speed = 200
        diffx = self.destination[0]-self.rect.center[0]
        diffy = self.destination[1]-self.rect.center[1]
        self.angle = math.atan2(diffx, diffy)

    def combineCoords (self, coord1, coord2):

        return map(sum, zip(*[coord1, coord2]))

    def update (self, time_passed):

        new_move = (math.sin(self.angle) * time_passed * self.speed,
                    math.cos(self.angle) * time_passed * self.speed)
        self.true_loc = self.combineCoords(self.true_loc, new_move)
        self.rect.center = self.true_loc

目标是一个类实例,它与所需的特定类属性基本相同。具体来说,我不知道如何根据弹丸到达那里的距离来计算目标将根据其角度和速度行进的距离。两者都有不同的速度和角度。我应该在数学课上注意......

编辑: 我突然意识到它会比我最初预期的稍微复杂一些,因为我根据玩家的帧速率动态移动精灵。我只是试图计算射弹和目标中心之间的距离,将该数字除以将到达目的地的帧数,然后通过将目标的角度乘以目标的角度来移动目标的角度来更新自我目标。 。然而,使用这种方法,如果玩家的屏幕由于某种原因(这是常见的)而出现故障,则抛射物将完全错过。太棒了......我回到原点,哈哈。

1 个答案:

答案 0 :(得分:1)

如果你修复了你的时间步,那么你的物理学就会有稳定性。你可以保持一个可变的视频帧速率,但物理的时间步长是恒定的。

着名的修补程序 - 您的时间步长链接http://gafferongames.com/game-physics/fix-your-timestep/  和一个pygame示例:http://www.pygame.org/wiki/ConstantGameSpeed?parent=CookBook

Theres是https://gamedev.stackexchange.com/questions/4995/predicting-enemy-position-in-order-to-have-an-object-lead-its-target

上的一堆信息和子链接
相关问题