【发布时间】:2019-09-17 01:52:35
【问题描述】:
我需要从列表中随机选择一个人,然后在我按“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()然后更改颜色。
【问题讨论】:
标签: python python-3.x class turtle-graphics