我有麻烦,我在pygame的子弹计算x和y的值

时间:2019-02-02 17:41:06

标签: python pygame

我正在尝试使用pygame在python中创建游戏。在我的游戏中,你是一个男人,周围有很多僵尸必须射击,但是我不明白如何使角色每次以设定的速度朝着鼠标的方向射击子弹。我的游戏是2D游戏,屏幕上显示了精灵的俯视图。

代码将如下所示:

class bullet:
       def __init__(self, x, y, speed):
               self.x = x
               self.y = y
               self.speed = speed
       def move(self):
               This is where you guys come in
       def draw(self):
               pygame.draw.circle(screen,(0,0,0),(self.x,self,y,5)

非常感谢您。

1 个答案:

答案 0 :(得分:0)

您需要了解angle,否则,您需要明确的x_speedy_speed ...

如果有角度可以计算出来

x_speed = math.cos(angle)*speed
y_speed = math.sin(angle)*speed

然后在每个步骤

new_x = old_x + x_speed
new_y = old_y + y_speed

您可以使用math.atan2获取角度

angle = math.atan2(character.x-mouse.x,character.y-mouse.y)
相关问题