猫史档案馆


【Python作品分享】疫情蔓延模拟器5人【作品秀】

用户:S_XS_X查看:2 回复:1 评论:2 创建时间:2020-03-27T19:16:19


【作品展示】

center_image

 

【作品介绍】

疫情蔓延模拟器5人重磅来袭,网上有人做过,过于复杂且无法运行,今天这个简单明了,多运行几次看看结果吧,祝大家身体都健健康康!

 

【作品源代码】

import math
import random
import time
import turtle

# 人数5人,医生人数1人
turtle.bgcolor("lightblue")
x1 = turtle.Pen()
x2 = turtle.Pen()
x3 = turtle.Pen()
x4 = turtle.Pen()
x5 = turtle.Pen()
doctor = turtle.Pen()

# 基本数据
dis1, dis2, x_1, x_2, x_3, x_4, x_5 = 100, 100, 1, 0, 0, 0, 0

# 判断健康状况函数
def f(x):
    if x == 1:
        return "red"
    else:
        return "green"

# 人员移动函数
def _setheading_(pen, x, y):
    pen.penup()
    pen.goto(x, y)
    pen.pendown()

# 计算距离函数
def distance(x1, y1, x2, y2):
    return math.sqrt((x1 - x2)**2 + (y1 - y2)**2)

# 随机移动函数
def random_place():
    return [random.randint(-300, 300), random.randint(-300, 300)]

# 主体部分
while (x_2 != 1 or x_3 != 1 or x_4 != 1 or x_5 != 1):
    # 若全部治愈则退出
    if x_1 == x_2 == x_3 == x_4 == x_5 == 0:
        break
    
    # 人员感染情况不一致则继续
    else:
        # 清空颜色圆点
        x1.clear()
        x2.clear()
        x3.clear()
        x4.clear()
        x5.clear()
        doctor.clear()

        # 人员及医生随机流动的坐标
        plsx1, plsy1 = random_place()[0], random_place()[1]
        plsx2, plsy2 = random_place()[0], random_place()[1]
        plsx3, plsy3 = random_place()[0], random_place()[1]
        plsx4, plsy4 = random_place()[0], random_place()[1]
        plsx5, plsy5 = random_place()[0], random_place()[1]
        plsx_doc, plsy_doc = random_place()[0], random_place()[1]

        # 人员及医生随机流动
        _setheading_(x1, plsx1, plsy1)
        _setheading_(x2, plsx2, plsy2)
        _setheading_(x3, plsx3, plsy3)
        _setheading_(x4, plsx4, plsy4)
        _setheading_(x5, plsx5, plsy5)
        _setheading_(doctor, plsx_doc, plsy_doc)

        # 人员流动计算距离
        if (distance(plsx1, plsy1, plsx2, plsy2) <= dis1) and (x_1 == 1 or x_2 == 1):
            x_1, x_2 = 1, 1
        if (distance(plsx1, plsy1, plsx3, plsy3) <= dis1) and (x_1 == 1 or x_3 == 1):
            x_1, x_3 = 1, 1
        if (distance(plsx1, plsy1, plsx4, plsy4) <= dis1) and (x_1 == 1 or x_4 == 1):
            x_1, x_4 = 1, 1
        if (distance(plsx1, plsy1, plsx5, plsy5) <= dis1) and (x_1 == 1 or x_5 == 1):
            x_1, x_5 = 1, 1
        if (distance(plsx2, plsy2, plsx3, plsy3) <= dis1) and (x_2 == 1 or x_3 == 1):
            x_2, x_3 = 1, 1
        if (distance(plsx2, plsy2, plsx4, plsy4) <= dis1) and (x_2 == 1 or x_4 == 1):
            x_2, x_4 = 1, 1
        if (distance(plsx2, plsy2, plsx5, plsy5) <= dis1) and (x_2 == 1 or x_5 == 1):
            x_2, x_5 = 1, 1
        if (distance(plsx3, plsy3, plsx4, plsy4) <= dis1) and (x_3 == 1 or x_4 == 1):
            x_3, x_4 = 1, 1
        if (distance(plsx3, plsy3, plsx5, plsy5) <= dis1) and (x_3 == 1 or x_5 == 1):
            x_3, x_5 = 1, 1
        if (distance(plsx4, plsy4, plsx5, plsy5) <= dis1) and (x_4 == 1 or x_5 == 1):
            x_4, x_5 = 1, 1
        
        # 医生治愈计算距离
        if (distance(plsx_doc, plsy_doc, plsx1, plsy1) <= dis2) and (x_1 == 1):
            x_1 = 0
        if (distance(plsx_doc, plsy_doc, plsx2, plsy2) <= dis2) and (x_2 == 1):
            x_2 = 0
        if (distance(plsx_doc, plsy_doc, plsx3, plsy3) <= dis2) and (x_3 == 1):
            x_3 = 0
        if (distance(plsx_doc, plsy_doc, plsx4, plsy4) <= dis2) and (x_4 == 1):
            x_4 = 0
        if (distance(plsx_doc, plsy_doc, plsx5, plsy5) <= dis2) and (x_5 == 1):
            x_5 = 0
        
        # 颜色圆点体现健康状况
        x1.dot(20, f(x_1))
        x2.dot(20, f(x_2))
        x3.dot(20, f(x_3))
        x4.dot(20, f(x_4))
        x5.dot(20, f(x_5))
        doctor.dot(20, "white")

        # 每次保留结果2.0秒
        time.sleep(2.0)

turtle.done()

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
S_XS_X

疫情蔓延模拟器5人重磅来袭,网上有人做过,过于复杂且无法运行,今天这个简单明了,多运行几次看看结果吧,祝大家身体都健健康康!

点赞2


评论