如何使用turtle python进行可控病毒模拟

如何使用turtle python进行可控病毒模拟,python,python-3.x,python-turtle,Python,Python 3.x,Python Turtle,呃,我的任务是利用python turtle为一个学校项目制作一个可控的新冠病毒-19模拟。所以我做的是一群随机移动的海龟,随机挑选“被感染”和“被感染”的海龟。我试着用箭头键控制“有病”的海龟,如果前一只海龟被感染了,我还会选择另一只“健康”的海龟。问题是,我不知道如何分离控件,因为它们都标记在类“Person”下。我也不知道当一只海龟被感染时,如何让它选择另一只健康的海龟。这是混乱的代码。对不起,英语不好 from turtle import Screen, Turtle """ This

呃,我的任务是利用python turtle为一个学校项目制作一个可控的新冠病毒-19模拟。所以我做的是一群随机移动的海龟,随机挑选“被感染”和“被感染”的海龟。我试着用箭头键控制“有病”的海龟,如果前一只海龟被感染了,我还会选择另一只“健康”的海龟。问题是,我不知道如何分离控件,因为它们都标记在类“Person”下。我也不知道当一只海龟被感染时,如何让它选择另一只健康的海龟。这是混乱的代码。对不起,英语不好

from turtle import Screen, Turtle
"""
This imports the screen and turtle from the turtle module
"""
from random import randint, choice
"""
This is to use the random integer function and the random choosing of the infected
"""
from time import sleep
posessed = 0
class Person(Turtle):
    population = []

    def __init__(self):
        super().__init__(shape='circle')

        self.shapesize(0.4)
        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 all_infected(cls):
        return [person for person in cls.population if person.posessed()]

    def posess(self):
        self.color('yellow')

    def posessed(self):
        return self.pencolor() == 'yellow'


    def random_move(self):
        self.right(randint(-180,180))
        self.forward(randint(0,10))
        if not (-250 < self.xcor() <250 and -250 < self.ycor() < 250):
            self.undo() # this will undo forward()

def make_population(amount):

    for _ in range(amount):
        Person()

def posess_random():
    person = choice(Person.population)
    person.posess()
    posessed+1

def infect_random():
    person = choice(Person.population)
    person.infect()

def simulation():
    """ This will simulate the virus outbreak scenarios (quarantine, or not quarantine) """
    amount=int(input("Enter amount of people within the area: " ))
    moves=int(input("Enter the amount of moves these people will do: "))
    print("Entered amount of people: ", amount)
    print("Entered amount of movements: ", moves)
    make_population(amount)

    infect_random()
    posess_random()

    screen.update()
    for _ in range(moves):
        for person.infected in Person.population:
            person.random_move()

            if not person.infected():
                for infected in Person.all_infected():
                    if person.distance(infected) < 30:
                        person.infect()
                        #this is used to set distance to get infected. In this case I did 30

        screen.update()
        sleep(0.1)


screen = Screen()
screen.setup(500,500)
screen.tracer(0)
screen.bgcolor(str(input("Enter desired background color: ")))
simulation()

screen.exitonclick()
               #this should do it
从海龟导入屏幕,海龟
"""
这将从turtle模块导入屏幕和turtle
"""
从随机导入randint,选择
"""
这是使用随机整数函数和随机选择的函数
"""
从时间上导入睡眠
posessed=0
班级人员(海龟):
人口=[]
定义初始化(自):
超级()
self.shapesize(0.4)
self.penup()
self.setpos(randint(-250250),randint(-250250))
Person.population.append(self)
@类方法
def all_已感染(cls):
返回[如果person.infected(),则为cls.population中的person for person]
def感染(自我):
self.color('red'))
def感染(自身):
返回self.pencolor()=“红色”
def all_已感染(cls):
返回[如果person.posessed(),则cls.population中的person对应person]
def posess(自我):
self.color('yellow'))
def posessed(自我):
返回self.pencolor()=“黄色”
def随机移动(自):
自身权利(randint(-180180))
自前向(randint(0,10))
如果不是(-250
首先,此代码没有按发布的方式运行--我必须修复至少三个错误才能使其运行。令人惊讶的是,它只对前一个代码进行了一些小的更改,当然,前一个代码运行良好。而且没有返回到该代码的信用/链接!继续:

下面是我对代码的修改,以修复您引入的bug,并使“着魔人”可以手动移动。(一路上修复了“着魔人”的拼写。)如果被附体的人通过自己的动作或被感染者的动作与被感染者接触,则被附体会被感染,而一个随机的健康人(如果有的话)会被附体并可手动移动:

from turtle import Screen, Turtle
from random import randint, choice
from functools import partial
from time import sleep

INFECTION_DISTANCE = 30
PERSON_RADIUS = 8
WIDTH, HEIGHT = 500, 500
CURSOR_SIZE = 20

possessed = 0

class Person(Turtle):
    population = []

    def __init__(self):
        super().__init__(shape='circle')

        self.shapesize(PERSON_RADIUS / CURSOR_SIZE)
        self.penup()
        self.setpos(randint(-WIDTH/2, WIDTH/2), randint(-HEIGHT/2, HEIGHT/2))

        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'

    @classmethod
    def all_healthy(cls):
        return [person for person in cls.population if not person.infected()]

    def possess(self):
        self.color('green')

    def possessed(self):
        return self.pencolor() == 'green'

    def random_move(self):
        self.right(randint(-90, 90))
        self.forward(randint(0, 10))

        x, y = self.position()

        if not (PERSON_RADIUS - WIDTH/2 < x < WIDTH/2 - PERSON_RADIUS and PERSON_RADIUS - HEIGHT/2 < y < HEIGHT/2 - PERSON_RADIUS):
            self.undo()  # this will undo forward()

def make_population(amount):
    for _ in range(amount):
        Person()

def possess_random():
    possessed = None

    healthy = Person.all_healthy()

    if healthy:
        possessed = choice(healthy)
        possessed.possess()

        screen.onkey(partial(move_up, possessed), 'Up')
        screen.onkey(partial(move_down, possessed), 'Down')
        screen.onkey(partial(move_right, possessed), 'Right')
        screen.onkey(partial(move_left, possessed), 'Left')

    return possessed

def infect_random():
    person = None

    healthy = Person.all_healthy()

    if healthy:
        person = choice(healthy)
        person.infect()

    return person

def check_infection(person):
    for infected in Person.all_infected():
        if person.distance(infected) < INFECTION_DISTANCE:
            is_possessed = person.possessed()

            person.infect()

            if is_possessed:
                possess_random()

def simulation(amount, moves):
    """ This will simulate the virus outbreak scenarios (quarantine, or not quarantine) """
    make_population(amount)

    infect_random()
    possess_random()
    screen.update()

    for _ in range(moves):
        for person in Person.population:
            if not person.possessed():
                person.random_move()

            if not person.infected():
                check_infection(person)

        screen.update()
        sleep(0.1)

def move_up(possessed):
    y = possessed.ycor() + 10

    if y < HEIGHT/2 - PERSON_RADIUS:
        possessed.sety(y)
        check_infection(possessed)

def move_down(possessed):
    y = possessed.ycor() - 10

    if y > PERSON_RADIUS - HEIGHT/2:
        possessed.sety(y)
        check_infection(possessed)

def move_right(possessed):
    x = possessed.xcor() + 10

    if x < WIDTH/2 - PERSON_RADIUS:
        possessed.setx(x)
        check_infection(possessed)

def move_left(possessed):
    x = possessed.xcor() - 10

    if x > PERSON_RADIUS - WIDTH/2:
        possessed.setx(x)
        check_infection(possessed)

amount = int(input("Enter amount of people within the area: "))
moves = int(input("Enter the amount of moves these people will do: "))

screen = Screen()
screen.setup(WIDTH, HEIGHT)

screen.listen()
screen.tracer(False)

simulation(amount, moves)

screen.tracer(True)
screen.exitonclick()
从海龟导入屏幕,海龟
从随机导入randint,选择
从functools导入部分
从时间上导入睡眠
感染距离=30
人员半径=8
宽度,高度=500500
光标大小=20
拥有=0
班级人员(海龟):
人口=[]
定义初始化(自):
超级()
self.shapesize(人/半径/光标大小)
self.penup()
self.setpos(randint(-WIDTH/2,WIDTH/2),randint(-HEIGHT/2,HEIGHT/2))
Person.population.append(self)
@类方法
def all_已感染(cls):
返回[如果person.infected(),则为cls.population中的person for person]
def感染(自我):
self.color('red'))
def感染(自身):
返回self.pencolor()=“红色”
@类方法
def所有_健康(cls):
return[如果不是person.infected(),则为cls.population中的person-for-person]
def(自我):
self.color('green'))
(自我)的定义:
返回self.pencolor()=“绿色”
def随机移动(自):
自右(随机数(-90,90))
自我前锋(randint(0,10))
x、 y=自我位置()
如果不是(人眼半径-宽度/2人员半径-高度/2:
附体。塞蒂(y)
检查感染情况(已拥有)
def向右移动(拥有):
x=0.xcor()+10
如果x<宽度/2-人的半径:
拥有。setx(x)
检查感染情况(已拥有)
def向左移动(占有):
x=0.xcor()-10
如果x>人眼半径-宽度/2:
拥有。setx(x)
检查感染情况(已拥有)
amount=int(输入(“输入区域内的人数:”)
moves=int(输入(“输入这些人将要做的移动量:”)
screen=screen()
screen.setu