Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我在pygame中计算子弹的x和y值时遇到问题_Python_Pygame - Fatal编程技术网

Python 我在pygame中计算子弹的x和y值时遇到问题

Python 我在pygame中计算子弹的x和y值时遇到问题,python,pygame,Python,Pygame,我正在尝试使用pygame在python中创建一个游戏。在我的游戏中,你是一个男人,你周围有很多僵尸,你必须射杀他们,但是我不知道如何让我的角色每次都以设定的速度朝鼠标方向射出一颗子弹。我的游戏是2D的,屏幕上有精灵的自顶向下视图 代码将如下所示: class bullet: def __init__(self, x, y, speed): self.x = x self.y = y self

我正在尝试使用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)

非常感谢您。

您需要知道
角度
,如果没有,您需要明确的
x\u速度
y\u速度

如果你有这个角度,你可以计算出来

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)

尝试查找向量归一化。谢谢!这使我找到了正确的答案。您需要减去xspeed(顺便说一句:)