Python 如何在pygame中使随机移动的对象以特定角度移动
我目前正在用pygame用python构建一个蚂蚁模拟器。蚂蚁从中间开始,从那里随机移动。这场运动就是这样运作的。每隔几毫秒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和
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之间,这意味着蚂蚁可以从各个角度移动
问题:
谢谢你的帮助。这是多个问题。目的是只问一个问题。我只回答其中两个问题。
使用和生成指定范围内具有恒定长度和角度的方向向量:
如果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