如何改变随机乌龟的颜色?

时间:2019-04-28 02:02:21

标签: python python-3.x class turtle-graphics

我需要从列表中随机选择一个人,然后在按“ 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(),然后改变颜色。

1 个答案:

答案 0 :(得分:0)

您遇到了许多问题,但是由于您没有包括完整的代码,因此我只能为您提供一些有关如何进行的想法:

请记住我的最后一个答案:孤独的乌龟是共享的,因此将乌龟的颜色设置为病毒颜色是不够的。您需要将病毒信息与 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”时,您应该开始看到个人变成红色。

  

我也不知道如何计算那里有多少只红海龟。

别数红海龟! (您当前的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