【问题标题】:Python 2.7 - Pygame - intersecting two moving spritesPython 2.7 - Pygame - 相交两个移动精灵
【发布时间】:2013-05-14 19:56:52
【问题描述】:

我有 2 个精灵(嗯... 3)。一个精灵以随机角度(以弧度为单位)以稳定的速度沿直线行进。另一个精灵正在向移动的精灵射击。目前,射弹精灵落在目标的稍微后面,因为射弹在发射时是根据目标的中心给出的。我可以让它不断更新,但如果弹丸像寻的导弹一样改变方向,那看起来会很愚蠢。

那么,地球我该怎么做呢?

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

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

编辑: 我突然意识到这将比我最初预期的要复杂一些,因为我会根据玩家的帧速率动态移动精灵。我只是试图计算弹丸和target.center之间的距离,将该数字除以到达目的地之前将通过的帧数,然后通过将其移动到目标的角度来更新self.destination,并将其移动和帧数相乘.然而,使用这种方法,如果玩家的屏幕由于某种原因(这很常见)而卡住,弹丸将完全错过。太好了……我又回到了原点,哈哈。

【问题讨论】:

    标签: python python-2.7 pygame angle


    【解决方案1】:

    如果你修正你的时间步长,你的物理学就会稳定。您可以保持可变的视频帧速率,但物理的时间步长不变。

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

    https://gamedev.stackexchange.com/questions/4995/predicting-enemy-position-in-order-to-have-an-object-lead-its-target 有很多信息和子链接

    【讨论】:

    • 在这种情况下我的时间步长无关紧要。我正在使用提供的模型寻找特定问题的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多