如何改变随机乌龟的颜色?
How to change a random turtle's color?
我需要从列表中随机选择一个人,然后在按 'x' 时将病毒感染他们(将他们的乌龟颜色更改为红色)。那我得统计一下有多少人被感染了
目前,我的代码没有改变任何海龟的颜色,我也不知道如何计算那里有多少只红海龟。
#used to infect
class Virus:
def __init__(self, colour, duration):
self.colour = colour
self.duration = duration
class Person:
def __init__(self, world_size):
self.world_size = world_size
self.radius = 7
self.location = turtle.position()
self.destination = self._get_random_location()
turtle.penup()
turtle.setposition(self.location)
turtle.setheading(turtle.towards(self.destination))
self.heading = turtle.heading()
#random locations are used to assign a destination for the person
#the possible locations should not be closer than 1 radius to the edge of the world
def _get_random_location(self):
x = random.randint( - 349, 349 )
y = random.randint( - 249, 249 )
return (x, y)
#draw a person using a dot. Use colour if implementing Viruses
def draw(self):
x, y = self.location
turtle.penup()
turtle.setposition(x, y - self.radius)
turtle.pendown()
turtle.begin_fill()
self.x = turtle.circle(self.radius)
turtle.end_fill()
#infect a person with the given virus
def infect(self, virus):
v_colour = virus.colour
turtle.colormode("v_colour")
class World:
def __init__(self, width, height, n):
self.size = (width, height)
self.hours = 0
self.people = []
self.infected = 0
for i in range(n):
self.add_person()
#add a person to the list
def add_person(self):
person = Person(1)
self.people.append(person)
#choose a random person to infect and infect with a Virus when press 'x'
def infect_person(self):
random_ppl = random.choice(self.people)
v = Virus("red", 100)
random_ppl.infect(v)
#remove all infections from all people when press'c'
def cure_all(self):
turtle.colormode('black')
self.infected = 0
def draw(self):
turtle.clear()
turtle.hideturtle()
turtle.setheading(0)
turtle.penup()
turtle.setposition(-350, -250)
turtle.pendown()
for i in range(2):
turtle.forward(500)
turtle.right(90)
turtle.forward(700)
turtle.right(90)
for item in self.people:
item.draw()
turtle.penup()
turtle.setposition(-350, 250)
turtle.write(f'Hours: {self.hours}', move=False, align='left')
self.count_infected()
turtle.setposition(0, 250)
turtle.write(f'Infected: {self.infected}', move=False, align='left')
turtle.update()
#Count the number of infected people
def count_infected(self):
p = Person(1)
p.draw()
dot = p.x
color = dot.color()
Color = color[0]
if Color is 'red':
self.infected += 1
#code for the keys
class GraphicalWorld:
""" Handles the user interface for the simulation
space - starts and stops the simulation
'z' - resets the application to the initial state
'x' - infects a random person
'c' - cures all the people
"""
def __init__(self):
self.WIDTH = 800
self.HEIGHT = 600
self.TITLE = 'COMPSCI 130 Project One'
self.MARGIN = 50 #gap around each side
self.PEOPLE = 200 #number of people in the simulation
self.framework = AnimationFramework(self.WIDTH, self.HEIGHT, self.TITLE)
self.framework.add_key_action(self.setup, 'z')
self.framework.add_key_action(self.infect, 'x')
self.framework.add_key_action(self.cure, 'c')
self.framework.add_key_action(self.toggle_simulation, ' ')
self.framework.add_tick_action(self.next_turn)
self.world = None
def setup(self):
""" Reset the simulation to the initial state """
print('resetting the world')
self.framework.stop_simulation()
self.world = World(self.WIDTH - self.MARGIN * 2, self.HEIGHT - self.MARGIN * 2, self.PEOPLE)
self.world.draw()
def infect(self):
""" Infect a person, and update the drawing """
print('infecting a person')
self.world.infect_person()
self.world.draw()
def cure(self):
""" Remove infections from all the people """
print('cured all people')
self.world.cure_all()
self.world.draw()
作业先调用World.infected_people()
不知道怎么把随机的人传给Person.infect()
然后再换颜色
你有很多问题,但由于你没有包含完整的代码,我只能给你一些如何继续的想法:
记得我上次的回答,孤龟是共享的,所以将龟的颜色设置为病毒颜色是不够的。您需要将病毒信息与 Person 一起存储,并在需要绘制人物时使用该信息。即:
# to Person.__init__ add the property
self.virus = None
使您的 Person.infect()
功能简单:
def infect(self, virus):
self.virus = virus
请注意,您的代码:turtle.colormode("v_colour")
是完全错误的,因为 colormode()
是错误的方法并且 v_colour
不应该在引号中。但无论如何我们都不需要那个。现在,在您的 Person.draw()
方法中,在 turtle.begin_fill()
之前添加以下内容:
if self.virus is not None:
turtle.color(self.virus.colour)
else:
turtle.color("black")
当您键入 'x' 时,您应该会开始看到个人变红。
I also don't know how to count how many red turtle are there.
不要算红海龟! (您当前的 count_infected()
是完全错误的——您不应该创建新人或绘制任何东西。)
您可以遍历 self.people
寻找 self.virus
属性 而不是 None
的人。但是 World
有一个 self.infected
属性,你应该在调用 infect_person()
时增加它。但是,由于您随机选择一个人,因此这不会按原样工作,因为您可能会为同一个人增加两次计数器。您需要更改 infect_person()
以继续抓取一个随机的人 直到 它找到一个尚未被感染的人。这是否有意义取决于赋值规则。
您的 World.cure_all()
函数需要重写以循环 self.people
调用每个人的 cure()
。 Person.cure()
方法应将 self.virus
属性 设置回 None
。
我需要从列表中随机选择一个人,然后在按 'x' 时将病毒感染他们(将他们的乌龟颜色更改为红色)。那我得统计一下有多少人被感染了
目前,我的代码没有改变任何海龟的颜色,我也不知道如何计算那里有多少只红海龟。
#used to infect
class Virus:
def __init__(self, colour, duration):
self.colour = colour
self.duration = duration
class Person:
def __init__(self, world_size):
self.world_size = world_size
self.radius = 7
self.location = turtle.position()
self.destination = self._get_random_location()
turtle.penup()
turtle.setposition(self.location)
turtle.setheading(turtle.towards(self.destination))
self.heading = turtle.heading()
#random locations are used to assign a destination for the person
#the possible locations should not be closer than 1 radius to the edge of the world
def _get_random_location(self):
x = random.randint( - 349, 349 )
y = random.randint( - 249, 249 )
return (x, y)
#draw a person using a dot. Use colour if implementing Viruses
def draw(self):
x, y = self.location
turtle.penup()
turtle.setposition(x, y - self.radius)
turtle.pendown()
turtle.begin_fill()
self.x = turtle.circle(self.radius)
turtle.end_fill()
#infect a person with the given virus
def infect(self, virus):
v_colour = virus.colour
turtle.colormode("v_colour")
class World:
def __init__(self, width, height, n):
self.size = (width, height)
self.hours = 0
self.people = []
self.infected = 0
for i in range(n):
self.add_person()
#add a person to the list
def add_person(self):
person = Person(1)
self.people.append(person)
#choose a random person to infect and infect with a Virus when press 'x'
def infect_person(self):
random_ppl = random.choice(self.people)
v = Virus("red", 100)
random_ppl.infect(v)
#remove all infections from all people when press'c'
def cure_all(self):
turtle.colormode('black')
self.infected = 0
def draw(self):
turtle.clear()
turtle.hideturtle()
turtle.setheading(0)
turtle.penup()
turtle.setposition(-350, -250)
turtle.pendown()
for i in range(2):
turtle.forward(500)
turtle.right(90)
turtle.forward(700)
turtle.right(90)
for item in self.people:
item.draw()
turtle.penup()
turtle.setposition(-350, 250)
turtle.write(f'Hours: {self.hours}', move=False, align='left')
self.count_infected()
turtle.setposition(0, 250)
turtle.write(f'Infected: {self.infected}', move=False, align='left')
turtle.update()
#Count the number of infected people
def count_infected(self):
p = Person(1)
p.draw()
dot = p.x
color = dot.color()
Color = color[0]
if Color is 'red':
self.infected += 1
#code for the keys
class GraphicalWorld:
""" Handles the user interface for the simulation
space - starts and stops the simulation
'z' - resets the application to the initial state
'x' - infects a random person
'c' - cures all the people
"""
def __init__(self):
self.WIDTH = 800
self.HEIGHT = 600
self.TITLE = 'COMPSCI 130 Project One'
self.MARGIN = 50 #gap around each side
self.PEOPLE = 200 #number of people in the simulation
self.framework = AnimationFramework(self.WIDTH, self.HEIGHT, self.TITLE)
self.framework.add_key_action(self.setup, 'z')
self.framework.add_key_action(self.infect, 'x')
self.framework.add_key_action(self.cure, 'c')
self.framework.add_key_action(self.toggle_simulation, ' ')
self.framework.add_tick_action(self.next_turn)
self.world = None
def setup(self):
""" Reset the simulation to the initial state """
print('resetting the world')
self.framework.stop_simulation()
self.world = World(self.WIDTH - self.MARGIN * 2, self.HEIGHT - self.MARGIN * 2, self.PEOPLE)
self.world.draw()
def infect(self):
""" Infect a person, and update the drawing """
print('infecting a person')
self.world.infect_person()
self.world.draw()
def cure(self):
""" Remove infections from all the people """
print('cured all people')
self.world.cure_all()
self.world.draw()
作业先调用World.infected_people()
不知道怎么把随机的人传给Person.infect()
然后再换颜色
你有很多问题,但由于你没有包含完整的代码,我只能给你一些如何继续的想法:
记得我上次的回答,孤龟是共享的,所以将龟的颜色设置为病毒颜色是不够的。您需要将病毒信息与 Person 一起存储,并在需要绘制人物时使用该信息。即:
# to Person.__init__ add the property
self.virus = None
使您的 Person.infect()
功能简单:
def infect(self, virus):
self.virus = virus
请注意,您的代码:turtle.colormode("v_colour")
是完全错误的,因为 colormode()
是错误的方法并且 v_colour
不应该在引号中。但无论如何我们都不需要那个。现在,在您的 Person.draw()
方法中,在 turtle.begin_fill()
之前添加以下内容:
if self.virus is not None:
turtle.color(self.virus.colour)
else:
turtle.color("black")
当您键入 'x' 时,您应该会开始看到个人变红。
I also don't know how to count how many red turtle are there.
不要算红海龟! (您当前的 count_infected()
是完全错误的——您不应该创建新人或绘制任何东西。)
您可以遍历 self.people
寻找 self.virus
属性 而不是 None
的人。但是 World
有一个 self.infected
属性,你应该在调用 infect_person()
时增加它。但是,由于您随机选择一个人,因此这不会按原样工作,因为您可能会为同一个人增加两次计数器。您需要更改 infect_person()
以继续抓取一个随机的人 直到 它找到一个尚未被感染的人。这是否有意义取决于赋值规则。
您的 World.cure_all()
函数需要重写以循环 self.people
调用每个人的 cure()
。 Person.cure()
方法应将 self.virus
属性 设置回 None
。