Python 如何在pygame中使随机移动的对象以特定角度移动

Python 如何在pygame中使随机移动的对象以特定角度移动,python,pygame,simulation,trigonometry,angle,Python,Pygame,Simulation,Trigonometry,Angle,我目前正在用pygame用python构建一个蚂蚁模拟器。蚂蚁从中间开始,从那里随机移动。这场运动就是这样运作的。每隔几毫秒self.new\u line设置为True。然后计算新的self.deltax和self.deltay。现在,只要self.new\u line为False,我的蚂蚁每次迭代都会移动self.x+=deltax和self.y+=deltay。当self.new\u line再次为真时,ant将使用self.deltax和self.deltay的新值开始新行。deltax和

我目前正在用pygame用python构建一个蚂蚁模拟器。蚂蚁从中间开始,从那里随机移动。这场运动就是这样运作的。每隔几毫秒
self.new\u line
设置为True。然后计算新的
self.deltax
self.deltay
。现在,只要
self.new\u line
为False,我的蚂蚁每次迭代都会移动
self.x+=deltax
self.y+=deltay
。当
self.new\u line
再次为真时,ant将使用
self.deltax
self.deltay
的新值开始新行。deltax和deltay的值介于-2和+2之间,这意味着蚂蚁可以从各个角度移动

问题:

  • 然而,我希望我的蚂蚁只在一个特定的角度移动

    我该怎么做

  • 如果在ant上运行程序,您将看到ant的速度随着新行的变化而变化。怎样才能使我的蚂蚁有一个一致的速度

  • 我怎样才能使我的蚂蚁在特定角度的墙壁上反弹


  • 谢谢你的帮助。

    这是多个问题。目的是只问一个问题。我只回答其中两个问题。
    使用和生成指定范围内具有恒定长度和角度的方向向量:

    如果self.new\u行:
    角度=random.randint(-45,45)
    方向向量=pygame.math.Vector2(0,-self.VEL)。旋转(角度)
    self.deltax=direction\u vector.x
    self.deltay=方向\向量.y
    
    谢谢,你帮了我很多忙
    self.VEL = 3
    self.RAND = numpy.linspace(-self.VEL, +self.VEL, 100)  # calculating value between -2 and 2, a total of 100 values
    -----------------------------------------
    if self.new_line:
        self.deltax = random.choice(self.RAND)
        self.deltay = random.choice(self.RAND)
        self.new_line = False
    self.move(self.deltax, self.deltay)
    -----------------------------------------
    def move(self, deltax, deltay):
        self.x += deltax
        self.y += deltay