用户:S.X樹識查看:0 回复:0 评论:0 创建时间:2022-01-12T19:17:26
【作品展示】

【作品介绍】
疫情蔓延模拟点状图的移动
【作品源代码】
# 疫情蔓延模拟器
import math
import random
import time
import turtle
# 人数5人,医生人数1人
turtle.bgcolor("lightblue")
x1, x2, x3, x4, x5, doctor = turtle.Pen(), turtle.Pen(), turtle.Pen(), turtle.Pen(), turtle.Pen(), turtle.Pen()
# 自定义设置
speed = 3 # 人员移动速度
keep_time = 2 # 结果保留时间
_chance = 0.8 # 传染率
x1.speed(speed)
x2.speed(speed)
x3.speed(speed)
x4.speed(speed)
x5.speed(speed)
doctor.speed(speed)
# 基本数据
dis1, dis2, x_1, x_2, x_3, x_4, x_5 = 100, 100, 1, 1, 0, 0, 0
# 以一定概率执行函数的函数
def chance(function, chance):
from random import choice
if chance >= 0:
chance = int(round(chance, 2) * 100)
List = [1 for i in range(chance)] + [0 for i in range(100-chance)]
if choice(List) == 1:
return function
else:
return "green"
else:
return False
# 判断健康状况函数
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_1 != 1 or 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
# 颜色圆点体现健康状况
color1 = chance(f(x_1), _chance)
color2 = chance(f(x_2), _chance)
color3 = chance(f(x_3), _chance)
color4 = chance(f(x_4), _chance)
color5 = chance(f(x_5), _chance)
x1.dot(30, color1)
x2.dot(30, color2)
x3.dot(30, color3)
x4.dot(30, color4)
x5.dot(30, color5)
# 概率执行确定步骤
if color1 == "green":
x_1 = 0
if color2 == "green":
x_2 = 0
if color3 == "green":
x_3 = 0
if color4 == "green":
x_4 = 0
if color5 == "green":
x_5 = 0
# 医生圆点
doctor.dot(30, "white")
# 结果保留时间
time.sleep(keep_time)
turtle.done()
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn