Ниже моя оценка того, что вы после. Я импортировал Vec2D
из черепахи, чтобы упростить вашу логику:
from random import randint, randrange
from turtle import Vec2D
class Bug:
def __init__(self, number, worldXSize=80, worldYSize=80):
# the environment
self.number = number
self.worldXSize = worldXSize
self.worldYSize = worldYSize
# the bug
self.position = Vec2D(randrange(worldYSize), randrange(worldYSize))
print("Bug number", self.number, "has been created at", self.position)
# the action
def randomWalk(self):
self.position += randomMove()
def distance(self, other):
return abs(other.position - self.position)
# report
def reportPosition(self):
print("Bug number", self.number, "is located at", self.position)
distance, bug = max((self.distance(bug), bug.number) for bug in bugList if bug != self)
print("the farthest bug from me is", bug, "at a distance of", distance)
# returns -1, 0, 1 with equal probability
def randomMove():
return Vec2D(randint(-1, 1), randint(-1, 1))
nBugs = int(input("How many bugs? "))
worldXSize = int(input("X Size of the world? "))
worldYSize = int(input("Y Size of the world? "))
length = int(input("Length of the simulation in cycles? "))
bugList = [Bug(i + 1, worldXSize, worldYSize) for i in range(nBugs)]
for _ in range(length):
for bug in bugList:
bug.randomWalk()
print()
for bug in bugList:
bug.reportPosition()
Хотя я использовал worldXSize
и worldYSize
для ограничения места создания ошибок, я не ограничивал, где они могут быть - измените, как считаете нужным. Эта логика в вашем коде не имеет смысла:
self.xPos = (self.xPos + self.worldXSize) % self.worldXSize
как кажется, просто следующее делает то, что вы хотите (то же самое для координаты Y):
self.xPos = self.xPos % self.worldXSize
И я вообще не вижу, как работает ваш код, так как вы не преобразовывали входные данные из строк в числа, прежде чем использовать их в математических ситуациях.
OUTPUT
% python3 test.py
How many bugs? 3
X Size of the world? 100
Y Size of the world? 100
Length of the simulation in cycles? 4
Bug number 1 has been created at (94.00,9.00)
Bug number 2 has been created at (91.00,40.00)
Bug number 3 has been created at (29.00,18.00)
Bug number 1 is located at (93.00,9.00)
the farthest bug from me is 3 at a distance of 64.77653896280658
Bug number 2 is located at (90.00,41.00)
the farthest bug from me is 3 at a distance of 64.8459713474939
Bug number 3 is located at (29.00,19.00)
the farthest bug from me is 2 at a distance of 64.8459713474939
Bug number 1 is located at (94.00,9.00)
the farthest bug from me is 3 at a distance of 66.91038783328041
Bug number 2 is located at (90.00,40.00)
the farthest bug from me is 3 at a distance of 65.14598989960932
Bug number 3 is located at (28.00,20.00)
the farthest bug from me is 1 at a distance of 66.91038783328041
Bug number 1 is located at (95.00,9.00)
the farthest bug from me is 3 at a distance of 66.75327707311455
Bug number 2 is located at (90.00,39.00)
the farthest bug from me is 3 at a distance of 64.19501538281614
Bug number 3 is located at (29.00,19.00)
the farthest bug from me is 1 at a distance of 66.75327707311455
Bug number 1 is located at (96.00,8.00)
the farthest bug from me is 3 at a distance of 68.06614430096654
Bug number 2 is located at (89.00,39.00)
the farthest bug from me is 3 at a distance of 62.93647591023825
Bug number 3 is located at (29.00,20.00)
the farthest bug from me is 1 at a distance of 68.06614430096654
%