python新冠状病毒疫情简单模拟程序_Python精灵模块

25 篇文章 6 订阅

第一种情况:
没有医护人员,没有防护措施!人们到处随便走动,
不用多久,全部感染病毒.

red_amounts = 30            # 1、已感染的数量
red_speed_k = 5             # 2、红点移动速度因子

lime_amounts = 300          # 3、未感染人群数量
lime_protects = 0           # 4、有防护措施的数量
lime_speed_k = 5            # 5、未感染人的移动速度

white_amounts = 0           # 6、白衣天使数量
white_speed_k = 0           # 7、白衣天使移动速度


第二种情况:

没有医护人员,50%的人有防护措施,人们不走动,
不用多久,始终是50%的人感染,50%的人未感染.

red_amounts = 30            # 1、已感染的数量
red_speed_k = 5             # 2、红点移动速度因子

lime_amounts = 300         # 3、未感染人群数量
lime_protects = 150        # 4、有防护措施的数量
lime_speed_k = 0           # 5、未感染人的移动速度

white_amounts = 0           # 6、白衣天使数量
white_speed_k = 0           # 7、白衣天使移动速度

说明有防护措施非常重要,可以避免不被感染!

第三种情况:

有50名医护人员,感染者亦不走动,
100%的未感染人有防护措施,人们不走动,
医防人员一诞生,便扑向感染人员,不用多久,感染人全部治好.

red_amounts = 30            # 1、已感染的数量
red_speed_k = 5             # 2、红点移动速度因子

lime_amounts = 300         # 3、未感染人群数量
lime_protects = 300        # 4、有防护措施的数量
lime_speed_k = 0           # 5、未感染人的移动速度

white_amounts = 50          # 6、白衣天使数量
white_speed_k = 5           # 7、白衣天使移动速度

这说明人们意识到事情的严重性及有医护人员的参与下,
病毒能很快被消灭!那么现在的情况就是这样的,所以大家
完全不要担心,疫情是完全可控的!

 

以下是Python源代码,本程序需要sprites模块支持,请先在命令提示符里输入

pip install sprites进行安装,如果安装得比较慢,到我的 lixingqiu.com 博客查找安装秘籍哟

"""
  新冠状病毒疫情简单模拟程序,以下程序用
  红点表示已经感染的人,用绿点表示未感染的人,用白点表示医生
  本程序方案可以自行设置,让其出现不同的结果。
"""

from sprites import *

radius = 150                # 活动半径

red_amounts = 30            # 1、已感染的数量
red_speed_k = 5             # 2、红点移动速度因子

lime_amounts = 300         # 3、未感染人群数量
lime_protects = 300        # 4、有防护措施的数量
lime_speed_k = 0           # 5、未感染人的移动速度

white_amounts = 50          # 6、白衣天使数量
white_speed_k = 5           # 7、白衣天使移动速度

width,height = 500,500
screen = Screen()
screen.tracer(0,0)
screen.bgcolor('black')
screen.setup(width,height)
screen.title('新冠状病毒疫情简单模拟程序')
reds = [Sprite(shape='dot') for x in range(red_amounts)]
for p in reds:
    p.scale(2)
    p.set_tag('red')
    p.randomheading()
    p.speed = red_speed_k * random.randint(1,5)  # 重定义speed    
    p.gotorandom()
    while p.distance((0,0))>radius:
         p.gotorandom()
    p.color('red')

limes = [Sprite(shape='dot') for x in range(lime_amounts)]
counter = 0
for p in limes:
    p.scale(2)
    p.set_tag('lime')
    p.randomheading()
    p.speed = lime_speed_k * random.randint(1,5)
    p.protect = False          # 自定义属性
    if counter < lime_protects:
        p.protect = True       # 如果带了口罩,碰到红色粒子不会变成红色粒子
        counter = counter + 1   
    p.gotorandom()
    while p.distance((0,0))>radius:
         p.gotorandom()
    p.color('lime')

whites = [Sprite(shape='dot') for x in range(white_amounts)]
for p in whites:
    p.scale(2)
    p.set_tag('white')
    rr = random.choice(reds)  # 随机挑一名红色感染者,
    p.heading(rr)
    p.speed = white_speed_k * random.randint(1,5)    
    p.gotorandom()
    while p.distance((0,0))>radius:
         p.gotorandom()
    p.color('white')
screen.update()
running = True
while running:
    for r in reds[:]:
        if reds == []:break
        r.fd(r.speed)
        if r.collide_others('white'):  # 碰到医生
            r.color('lime')
            r.set_tag('lime')
            r.protect = True
            r.speed=0
            reds.remove(r)
            limes.append(r)
        r.update()    
        if r.distance((0,0)) > radius:r.right(180)
    for g in limes[:]:
        if reds == []:break
        g.fd(g.speed)
        if g.protect == False and g.collide_others('red'):
            g.color('red')
            g.set_tag('red')
            g.speed = red_speed_k * random.randint(1,5)
            limes.remove(g)
            reds.append(g)
        g.update()
        g.bounce_on_edge()
    for w in whites:
        if reds == []:break
        if random.randint(1,100) == 1:
           rr = random.choice(reds)  # 随机挑一名红色感染者,
           w.heading(rr)
        w.fd(w.speed)
        w.update()
        if w.distance((0,0)) > radius:w.right(180)
    if len(reds) == 0 :running = False

        

 

  • 0
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李兴球

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值