【问题标题】:How to change a random turtle's color?如何改变随机乌龟的颜色?
【发布时间】: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


    【解决方案1】:

    你有很多问题,但由于你没有包含你的整个代码,我只能给你一些关于如何继续的想法:

    记得我上次的回答,孤龟是共享的,所以把龟的颜色设置为病毒颜色是不够的。您需要将病毒信息与 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

    【讨论】:

    • 对于 World.cure_all(),我在每个人上调用 person.cure() 并设置 self.virus = None 但乌龟颜色没有变回来。
    • @Victoriavv,在Person.draw()方法中根据self.virus属性是否为None设置颜色,所以必须改回来。我只能说重新检查你的代码。
    猜你喜欢
    • 2015-12-29
    • 1970-01-01
    • 2018-05-24
    • 2020-08-11
    • 1970-01-01
    • 1970-01-01
    • 2013-12-17
    • 1970-01-01
    • 2022-12-09
    相关资源
    最近更新 更多