【发布时间】:2021-08-04 13:11:29
【问题描述】:
我正在做一个项目,其中有四只海龟在一个圆圈的中间,它们每只都遵循随机路径,直到其中一只离开。最后一部分是,如果两只或多只海龟相互碰撞,它们会向中心退两步。我拥有除此之外的一切,我不知道如何继续。以下是我到目前为止的代码。任何人都可以帮忙吗?谢谢!
from random import randrange
import matplotlib.pyplot as plt
import numpy as np
def turtle1234():
x_points = []
y_points = []
x2_points = []
y2_points = []
x3_points = []
y3_points = []
x4_points = []
y4_points = []
x = 0
y = 0
x2 = 0
y2 = 0
x3 = 0
y3 = 0
x4 = 0
y4 = 0
while True:
x = x + randrange(-1,2)
y = y + randrange(-1,2)
x2 = x2 + randrange(-1,2)
y2 = y2 + randrange(-1,2)
x3 = x3 + randrange(-1,2)
y3 = y3 + randrange(-1,2)
x4 = x4 + randrange(-1,2)
y4 = y4 + randrange(-1,2)
x_points.append(x)
y_points.append(y)
x2_points.append(x2)
y2_points.append(y2)
x3_points.append(x3)
y3_points.append(y3)
x4_points.append(x4)
y4_points.append(y4)
if x**2+y**2 > 100**2:
print(x,y)
print('Turtle 1 is outside the circle first')
break
if x2**2+y2**2 > 100**2:
print(x2,y2)
print('Turtle 2 is outside the circle first')
break
if x3**2+y3**2 > 100**2:
print(x3,y3)
print('Turtle 3 is outside the circle first')
break
if x4**2+y4**2 > 100**2:
print(x4,y4)
print('Turtle 4 is outside the circle first')
break
plt.plot(x_points,y_points)
plt.plot(x2_points,y2_points)
plt.plot(x3_points,y3_points)
plt.plot(x4_points,y4_points)
x = np.linspace(-100,100,1000)
turtle1234()
plt.plot(x,-np.sqrt(100**2-x**2), color = 'b')
plt.plot(x, np.sqrt(100**2-x**2), color = 'b')
【问题讨论】:
-
你的问题是什么?到目前为止,您尝试过什么?