Python 精灵模块_帧动画_纯画笔下雪效果.py

"""
   帧动画_纯画笔下雪效果.py
"""
from sprites import *

class Snow:
    def __init__(self,x,y,sw,sh):
        self.x = x
        self.y = y
        self.sw = sw
        self.sh = sh
        self.dx = 0
        self.dy = random.randint(-2,-1)

    def update(self):
        self.x += self.dx
        self.y += self.dy    

    def pos(self):
        return self.x,self.y

    def reborn(self):
        if self.y < -self.sh//2:
            self.y = random.randint(self.sw//2,2 * self.sw)        

def draw(hg,snow):
    """hg:海龟,snow:雪花点"""
    hg.goto(snow.pos())
    hg.dot(abs(snow.dy)*3)      # 速度越快,雪花点越大

width,height = 480,360
screen = Screen()
screen.tracer(0,0)
screen.bgcolor('black')
screen.setup(width,height)
screen.title("帧动画_纯画笔下雪效果 Python Sprites Module")

tom = Sprite(visible=False)          # 新建不可见精灵对象
tom.color('white')                   # 颜色为白色

snows = []
for _ in range(300):
    x = random.randint(-width//2,width//2)
    y = random.randint(height//2,2 * height)
    snows.append(Snow(x,y,width,height))

clock = Clock()                       # 新建时钟对象,用来固定fps
while True:
    tom.clear()                       # 清除所有雪花
    [snow.update() for snow in snows] # 移动每片雪花
    [snow.reborn() for snow in snows] # 雪花到了最下面则移到最上面    
    [draw(tom,snow) for snow in snows]# 重画每片雪花
    screen.update()                   # 更新显示
    clock.tick(60)
本作品采用《CC 协议》,转载必须注明作者和本文链接
python精灵模块
scratch
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!