【问题标题】:Pygame bullet motion from point A to point B从 A 点到 B 点的 Pygame 子弹运动
【发布时间】:2017-10-12 13:56:34
【问题描述】:

我正在尝试使用名为“Pygame”(v1.9.2)的python 库制作游戏,并且我已经为玩家制作了一个角色。这个角色应该“射击”“子弹”或“咒语”到mouse_pos,我需要帮助的是“子弹”有一个恒定的速度分配给1self.speed = 1,如果我尝试增加当子弹到达mouse_pos时它会导致闪烁效果的速度,因为self.pos会高于或低于mouse_pos。

我怎样才能使这个子弹像子弹一样快速平稳地移动,并且仍然到达设置 mouse_pos 的确切点?

self.speed = 1 的示例

self.speed = 2 的示例
http://4.1m.yt/d_TmNNq.gif

相关代码在 update() 函数中
Sprites.py(Spell/Bullet 类)

class Spell(pygame.sprite.Sprite):
    def __init__(self,game,x,y,spelltype,mouse_pos):
        pygame.sprite.Sprite.__init__(self)
        self.game = game
        self.width = TILESIZE
        self.height = TILESIZE
        self.type = spelltype
        self.effects = [
                'effect_'+self.type+'_00',
                'effect_'+self.type+'_01',
                'effect_'+self.type+'_02'
        ]
        self.image = pygame.image.load(path.join(IMG_DIR,"attack/attack_"+self.type+".png")).convert_alpha()
        self.image = pygame.transform.scale(self.image,(self.width,self.height))
        self.rect = self.image.get_rect()
        self.rect.x = x+self.width
        self.rect.y = y+self.height
        self.speed = 1
        self.mouse_pos = mouse_pos
        self.idx = 0

    def update(self):
        if not self.rect.collidepoint(self.mouse_pos):
            if self.mouse_pos[0] < self.rect.x:
                self.rect.x -= self.speed
            elif self.mouse_pos[0] > self.rect.x:
                self.rect.x += self.speed

            if self.mouse_pos[1] < self.rect.y:
                self.rect.y -= self.speed
            elif self.mouse_pos[1] > self.rect.y:
                self.rect.y += self.speed
        else:
            self.image = pygame.image.load(path.join(IMG_DIR,"effects/"+self.effects[self.idx]+".png"))
            self.idx += 1
            if self.idx >= len(self.effects):
                self.kill()

【问题讨论】:

  • 要向特定方向射击子弹,您应该使用三角函数或向量。这是一个向您展示它如何与向量一起使用的答案:stackoverflow.com/a/42281315/6220679
  • 如果你的子弹飞得太快以至于它穿过了目标,你可能想测试子弹当前点和最后一点之间的线是否与你的目标相交。
  • 每次更新时我应该检查距离是奇数还是偶数?
  • @Lanting 好的,这有帮助。发布答案,以便我接受。

标签: python python-2.7 pygame equations


【解决方案1】:

勾股定理说明了 n 维空间中点之间的距离。对于你的情况

distance = math.sqrt(sum[ (self.mouse_pos[0] - self.rect.x) ** 2 
                        , (self.mouse_pos[1] - self.rect.y) ** 2
                        ]))
if distance < self.speed: 
    #remove the bullet from the scene

这不是一个很好的解决方案,因为子弹会不切实际地移动, 在不同的角度有不同的速度。事实上,它将以 sqrt(2) 的速度以 45' 角移动,并以 1 的速度在 90' 处移动。

但是,我假设您不想要三角学讲座。

编辑:这是三角学讲座。 您的程序将真实地移动子弹,因为它在 x 和 y 方向上全速移动它。你需要做的是让速度在两个方向上都变慢,这样 x 速度和 y 速度(向量)形成的三角形的斜边等于 1。由于速度总是 1,这个三角形,正好是单位圆。

       m
       |\
       | \
sin(A) |  \ This speed is 1
       |   \
       |   A\
       ------p
        cos(A)

如果你的角色在 p,鼠标在 m,那么你会发现它们之间的角度标记为 A。那么 x 速度将是 A 的余弦,y 速度将是 A 的正弦A.

你如何找到角度?你用反正切... 事实上,这是要使用的代码。

import math
if not self.rect.collidepoint(self.mouse_pos):
        angle = math.atan2( (self.mouse_pos[1] - self.rect.y)
                          , (self.mouse_pos[0] - self.rect.x)
        self.rect.x += self.speed * math.cos(angle)
        self.rect.y += self.speed * math.sin(angle)

或类似的东西。我的符号可能混淆了。

【讨论】:

    【解决方案2】:

    如果你的子弹飞得太快以至于它穿过目标,你可能想测试子弹当前点和最后一点之间的线是否与你的目标相交。

    更多细节可以在这里找到:https://gamedev.stackexchange.com/questions/18604/how-do-i-handle-collision-detection-so-fast-objects-are-not-allowed-to-pass-thro

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多