二级python——turtle绘制雪景艺术图

# SnowView.py
import turtle
import random


# -------绘制雪花---------------
def drawSnow():
    turtle.hideturtle() # 隐藏画笔体现艺术效果
    turtle.pensize(2) # 画笔大小
    for i in range(100): # 100片雪花
        # 雪花的RGB颜色参数,随机生成一个【0.0, 1.0)之间的数
        r, g, b = random.random(), random.random(), random.random()
        turtle.pencolor((r, g, b)) # 画笔颜色
        turtle.penup() # 抬起画笔
        turtle.setx(random.randint(-350, 350)) # 将x轴移动到指定位置
        turtle.sety(random.randint(1, 270))
        
        turtle.pendown() # 落笔
        dens = random.randint(8, 12) # 花瓣数量
        snowsize = random.randint(10, 14) # 雪花大小
        for j in range(dens):
            turtle.forward(snowsize)
            turtle.backward(snowsize)
            turtle.right(360/dens)
            
# ---------绘制雪地--------------------
def drawGround():
    turtle.hideturtle()
    for i in range(400):
        turtle.pensize(random.randint(5, 10))
        x = random.randint(-400, 350)
        y = random.randint(-280, -1)
        r, g, b = -y/280, -y/280, -y/280
        turtle.pencolor((r, g, b))
        turtle.penup()
        turtle.goto(x, y)
        turtle.pendown()
        turtle.forward(random.randint(40, 100))
# -------------------------------------------------
turtle.setup(800, 600, 200, 200) # 窗口大小
turtle.tracer(False)
turtle.bgcolor("black") # 窗口背景颜色
drawSnow()
drawGround()
turtle.done()

二级python——turtle绘制雪景艺术图_第1张图片

你可能感兴趣的:(计算机二级python等级考试)