圣诞

# -*- coding: utf-8 -*-

import turtle
import random
import time


# 定义圣诞树的绿叶函数
def tree(d, s):
    if d <= 0:
        return
    turtle.forward(s)
    tree(d - 1, s * .8)
    turtle.right(120)
    tree(d - 3, s * .5)
    drawlight()  # 同时调用小彩灯的方法
    turtle.right(120)
    tree(d - 3, s * .5)
    turtle.right(120)
    turtle.backward(s)


def drawlight():  # 定义画彩灯的方法
    if random.randint(0, 30) == 0:  # 如果觉得彩灯太多,可以把取值范围加大一些,对应的灯就会少一些
        turtle.color('tomato')  # 定义第一种颜色
        turtle.circle(6)  # 定义彩灯大小
    elif random.randint(0, 30) == 1:
        turtle.color('orange')  # 定义第二种颜色
        turtle.circle(3)  # 定义彩灯大小
    else:
        turtle.color('dark green')  # 其余的随机数情况下画空的树枝


def setTreeInit():
    """ 设置绘图速度
    'fastest' :  0
    'fast'    :  10
    'normal'  :  6
    'slow'    :  3
    'slowest' :  1
    """
    turtle.speed('fastest')  # 设置速度
    turtle.left(90)
    turtle.forward(3 * n)
    turtle.color("orange", "yellow")
    turtle.left(126)

    turtle.begin_fill()
    for i in range(5):
        turtle.forward(n / 5)
        turtle.right(144)
        turtle.forward(n / 5)
        turtle.left(72)
    turtle.end_fill()
    turtle.right(126)
    turtle.color("dark green")
    turtle.backward(n * 4.8)


def setMessage():
    msg = "Merry Christmas."
    turtle.color("red")
    setLocation(-30, -265)
    turtle.write(msg, move=False, align="center", font=("Arial", 22,))


#定义画雪
def drawsnow():
    turtle.ht()  #隐藏笔头,ht=hideturtle
    turtle.pensize(2)  #定义笔头大小
    for i in range(50): #50,其实画49朵雪花
        turtle.pencolor("white") #定义画笔颜色为白色,其实就是雪花为白色
        turtle.pu() #提笔,pu=penup
        turtle.setx(random.randint(-350,350)) #定义x坐标,随机从-350到350之间选择
        turtle.sety(random.randint(-200,330)) #定义y坐标
        turtle.pd() #落笔,pd=pendown
        dens = 6 #雪花瓣数设为6
        snowsize = random.randint(2,6) #定义雪花大小
        for j in range(dens): #就是6,那就是画5次,也就是一个雪花五角星
            turtle.fd(int(snowsize))
            turtle.backward(int(snowsize))
            turtle.right(int(360/dens))  #转动角度

# 移动绘画起点坐标位置(中心点作为坐标原点)
def setLocation(x, y):
    turtle.penup()
    turtle.home()
    turtle.goto(x, y)
    turtle.pendown()


def show_snowman(x, y):
    # 躯干部分
    # 第一个圆
    # turtle.screensize(bg='grey')
    turtle.penup()
    # turtle.goto(-120, 100) #asas
    turtle.color('white', 'white')
    turtle.pendown()
    turtle.begin_fill()
    turtle.circle(-60)
    turtle.end_fill()
    # 第二个圆
    turtle.penup()
    turtle.goto(x, y - 55)
    turtle.color('white', 'white')
    turtle.pendown()
    turtle.begin_fill()
    turtle.circle(-80)
    turtle.end_fill()

    # 围巾部分
    # 第一部分
    turtle.penup()
    turtle.goto(x, y - 80)
    turtle.pendown()
    turtle.color('grey', 'red')
    turtle.begin_fill()
    turtle.forward(52)
    turtle.circle(11, 180)
    turtle.forward(104)
    turtle.circle(11, 180)
    turtle.forward(52)
    turtle.end_fill()
    # 第二部分
    turtle.penup()
    turtle.goto(x - 48, y - 80)
    turtle.pendown()
    turtle.begin_fill()
    turtle.setheading(-90)
    turtle.forward(10)
    turtle.circle(-30, 30)
    turtle.circle(10, 60)
    turtle.circle(-50, 20)
    turtle.setheading(0)
    turtle.forward(20)
    turtle.setheading(90)
    turtle.circle(50, 20)
    turtle.circle(-10, 60)
    turtle.circle(30, 30)
    turtle.setheading(90)
    turtle.forward(10)
    turtle.end_fill()

    # 帽子部分
    # 帽子底部
    turtle.penup()
    turtle.goto(x - 25 * 1.73, y - 25)
    turtle.color('grey', 'white')
    turtle.setheading(30)
    turtle.pendown()
    turtle.begin_fill()
    turtle.forward(50)
    turtle.circle(5, 180)
    turtle.forward(50)
    turtle.circle(5, 180)
    turtle.end_fill()
    # 帽子中间
    turtle.penup()
    turtle.circle(5, -180)
    turtle.pendown()
    turtle.color('red', 'red')
    turtle.begin_fill()
    turtle.setheading(90)
    turtle.forward(50)
    turtle.right(120)
    turtle.forward(50)
    turtle.end_fill()
    # 帽子顶部
    turtle.penup()
    turtle.forward(-50)
    turtle.left(30)
    turtle.color('white', 'white')
    turtle.pendown()
    turtle.begin_fill()
    turtle.circle(5)
    turtle.end_fill()

    # 面部
    # 两个大眼睛
    turtle.penup()
    turtle.goto(x - 20, y - 35)
    turtle.pencolor('black')
    turtle.pendown()
    turtle.dot(10)
    turtle.penup()
    turtle.goto(x + 20, y - 35)
    turtle.pencolor('black')
    turtle.pendown()
    turtle.dot(10)
    # 鼻子
    turtle.penup()
    turtle.goto(x - 5, y - 45)
    turtle.color('grey', 'yellow')
    turtle.pendown()
    turtle.begin_fill()
    turtle.forward(10)
    turtle.circle(3, 180)
    turtle.forward(10)
    turtle.circle(3, 180)
    turtle.end_fill()
    # 嘴巴
    turtle.penup()
    turtle.goto(x - 10, y - 50)
    turtle.pencolor('red')
    turtle.pensize(3)
    turtle.setheading(-90)
    turtle.pendown()
    turtle.circle(10, 180)

    # 身体部分
    # 扣子
    turtle.penup()
    turtle.goto(x, y - 100)
    turtle.pencolor('black')
    turtle.dot(10)
    turtle.penup()
    turtle.goto(x, y - 120)
    turtle.pencolor('black')
    turtle.dot(10)
    turtle.penup()
    turtle.goto(x, y - 140)
    turtle.pencolor('black')
    turtle.dot(10)
    # 两只手臂,左手
    turtle.penup()
    turtle.goto(x - 60, y - 100)
    turtle.pencolor('brown')
    turtle.pensize(5)
    turtle.setheading(120)
    turtle.pendown()
    turtle.forward(50)
    turtle.forward(-10)
    turtle.right(60)
    turtle.forward(10)
    # 两只手臂,右手
    turtle.penup()
    turtle.goto(x + 60, y - 100)
    turtle.pencolor('brown')
    turtle.pensize(5)
    turtle.setheading(60)
    turtle.pendown()
    turtle.forward(50)
    turtle.forward(-10)
    turtle.right(-60)
    turtle.forward(10)


# 执行函数
if __name__ == '__main__':
    n = 80
    turtle.screensize(bg='black')

    setLocation(-160, -20)
    setTreeInit()
    tree(15, n)

    setLocation(140, 80)
    show_snowman(140, 80)

    setMessage()
    drawsnow()
    turtle.hideturtle()
    turtle.done()

    time.sleep(10000)


posted @ 2021-12-17 17:38  枫叶艾辰  阅读(70)  评论(0编辑  收藏  举报