"""
幀動畫_純畫筆下雪效果.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 協議》,轉載必須註明作者和本文連結