下午好,
我正在使用乌龟模拟病毒爆发。我想出了以下代码,我的问题将在代码之后:
import turtle
import random
import time
def make_population(amount):
"""
Creates a list representing a population with a certain amount of people.
"""
population = []
for person in range(amount):
population.append(turtle.Turtle())
for person in population:
person.shape("circle")
person.shapesize(0.2)
return population
def random_move(person):
"""
Makes a turtle move forward a random amount and then turn a random amount.
"""
person.forward(random.randint(0,20))
person.right(random.randint(-180,180))
def check_boundary(person):
"""
Checks if a turtle is still within the given boundaries.
"""
if -250 <= person.xcor() <= 250 and -250 <= person.ycor() <= 250:
return
person.setpos(random.randint(-200,200),random.randint(-200,200))
def infect_random(population):
"""
Gets a random item from the population list and turns one red
"""
infected = random.choice(population)
infected.color("red")
return infected
def infect_person(person):
"""
Makes the turtle infected
"""
infected_person = person.color("red")
return infected_person
def simulation(amount, moves = 0):
"""
Simulates a virus outbreak
"""
border = 500
window = turtle.Screen()
turtle.setup(500,500)
turtle.tracer(0)
population = make_population(amount)
for person in population:
person.penup()
person.setpos(random.randint(-250,250),random.randint(-250,250))
turtle.update()
infected = infect_random(population)
for move in range(moves):
turtle.tracer(0)
for person in population:
random_move(person)
if person.distance(infected) < 50:
infect_person(person)
check_boundary(person)
turtle.update()
time.sleep(0.5)
window.exitonclick()
因此,当模拟开始时,我会感染1个随机人,并且如果其他乌龟靠近,例如在50像素之内,它们也会被感染并变红。但是,这些新感染的乌龟不会感染其他乌龟,因为与最初的乌龟相比,它们没有被“感染”。我尝试将其更改为感染= infect_person(person),但这给我一个错误。我现在停留了一段时间,想知道是否有人可以提供帮助。我还考虑过要列出两个列表:“人口”和“受感染的人群”也许可以解决我的问题,但是我不知道如何在其余的代码中实现它。
预先感谢
答案 0 :(得分:2)
我认为解决方案是将低级乌龟操作从模拟中的人的高级别操作分为Person
的{{1}}子类:
Turtle
我们可以进一步处理乌龟计时器事件,以使人们更加自主,而不是from turtle import Screen, Turtle
from random import randint, choice
from time import sleep
class Person(Turtle):
population = []
def __init__(self):
super().__init__(shape='circle')
self.shapesize(0.2)
self.penup()
self.setpos(randint(-250, 250), randint(-250, 250))
Person.population.append(self)
@classmethod
def all_infected(cls):
return [person for person in cls.population if person.infected()]
def infect(self):
self.color('red')
def infected(self):
return self.pencolor() == 'red'
def random_move(self):
"""
Makes a turtle move forward a random amount and then turn a random amount.
"""
self.right(randint(-180, 180))
self.forward(randint(0, 20))
# checks if turtle is still within the given boundaries.
if not (-250 < self.xcor() < 250 and -250 < self.ycor() < 250):
self.undo() # undo forward()
def make_population(amount):
"""
Creates a list representing a population with a certain amount of people.
"""
for _ in range(amount):
Person()
def infect_random():
"""
Gets a random item from the population list and turns one red
"""
person = choice(Person.population)
person.infect()
def simulation(amount=20, moves=100):
"""
Simulates a virus outbreak
"""
make_population(amount)
infect_random()
screen.update()
for _ in range(moves):
for person in Person.population:
person.random_move()
if not person.infected():
for infected in Person.all_infected():
if person.distance(infected) < 50:
person.infect()
screen.update()
sleep(0.5)
screen = Screen()
screen.setup(500, 500)
screen.tracer(0)
simulation()
screen.exitonclick()
循环。
答案 1 :(得分:1)
我相信您已经举了一个小例子,但是我们错过了有关数据结构的信息,人是一类吗?
您不要将其重新分配为受感染的人。
当您感染第一批人
infected = infect_random(population)
您将其指定为受感染, 但是当您感染他人时,您不会 您将其变成红色返回此人:
def infect_person(person):
"""
Makes the turtle infected
"""
infected_person = person.color("red")
return infected_person
但是,母鸡在您的代码中没有分配它,
infect_person(person)
我建议您使用一种方式来了解谁被感染或未被感染。例如: 如果您使用过POO:
您可以添加一个字段is_infected
还使用一个列表来保留被感染者的索引吗?
这样做,您将必须更改附近是否有人被感染的测试方式。对于一个人附近的所有人,如果有人被感染了,那么我就被感染了……