# python纯画笔大雪纷飞景像模拟

```"""
python大雪纷飞景像模拟2.py
本程序中只有一个海龟对象,但却有330个雪花,
这是如何实现的呢?
"""

import time
from turtle import *
from random import randint

class Snow:
"""雪花类"""
def __init__(self,rect):
"""
rect:所在的矩形区域大小,值为(width,height)
"""

rect = width,height = 640,480
screen = Screen()
screen.tracer(0,0)
screen.bgcolor('black')
screen.setup(width,height)
screen.title("python大雪纷飞景像模拟2")

t = Turtle(visible=False)          # 新建海龟对象
t.penup()

snows = [Snow(rect) for i in range(330)]

```

VIP免费查看

“””
python大雪纷飞景像模拟2.py

“””

import time
from turtle import *
from random import randint

class Snow:
“””雪花类”””
def __init__(self,rect):
“””
rect:所在的矩形区域大小,值为(width,height)
“””
self.diameter = randint(2,10) # 直径
self.sw = rect[0] # 矩形宽度
self.sh = rect[1] # 矩形高度
self.reset()

def reset(self):
“””重置到最上面去”””
self.dx = randint(-1,1) # 水平速度
self.dy = -self.diameter//2 # 越大越快
self.x = randint(-self.sw//2,self.sw//2)
self.y = randint(self.sh,2 * self.sh)

def update(self):
self.x += self.dx
self.y += self.dy
if self.y <= -self.sh//2:self.reset() rect = width,height = 640,480 screen = Screen() screen.tracer(0,0) screen.bgcolor('black') screen.setup(width,height) screen.title("python大雪纷飞景像模拟2") t = Turtle(visible=False) # 负责画雪花的对象 t.penup() snows = [Snow(rect) for i in range(330)] while 1: t.clear() # 清除所画图形 for snow in snows: # 每片雪花都更新坐标 snow.update() for snow in snows: # 重画所有雪花 t.goto(snow.x,snow.y) t.dot(snow.diameter,'white') screen.update() # 刷新显示 [/rihide]