gpt4 book ai didi

Python 2.7 - Pygame - 相交两个移动 Sprite

转载 作者:太空宇宙 更新时间:2023-11-03 18:58:52 29 4
gpt4 key购买 nike

我有 2 个 Sprite (呃……3 个)。一个 Sprite 以随机角度(以弧度为单位)以稳定速度沿直线行进。另一个 Sprite 正在向移动的 Sprite 射击。目前,射弹 Sprite 稍微落在目标后面,因为射弹在发射时是根据目标中心确定其目的地的。我可以让它不断更新,但如果射弹像寻的导弹一样改变方向,那就显得很愚蠢。

那么,我到底该怎么做呢?

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

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

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

最佳答案

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

著名的“修复你的时间步长”链接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 有大量信息和子链接

关于Python 2.7 - Pygame - 相交两个移动 Sprite ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16551784/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com