会唱歌的圣诞树!python之忍者神“turtle”

1 篇文章 0 订阅
1 篇文章 0 订阅


前言

python圣诞树turtle绘制,话不多说上代码。参考多方例子,后面上传打包好的exe,有关代码内容和打包方式等欢迎讨论。

一、圣诞树代码

import os
import random
# 添加音乐
import sys
import turtle as t
from turtle import *

import pygame.mixer


# 生成资源文件目录访问路径
def resource_path(relative_path):
    if getattr(sys, 'frozen', False):  # 是否Bundle Resource
        base_path = sys._MEIPASS
    else:
        base_path = os.path.abspath(".")
    return os.path.join(base_path, relative_path)


# 访问res文件夹下a.txt的内容
filename = resource_path(os.path.join("music", "music.mp3"))
pygame.mixer.init()
pygame.mixer.music.load(filename)
# 加载音乐文件
pygame.mixer.music.play()
# 播放音乐流
pygame.mixer.music.fadeout(600000)
# 消息事件循环,判断退出

n = 100
t.title("圣诞树")
# 最大速0
speed(8000)
screensize(400, 600, "pink")
# 画笔大小
pensize(3)
# t.hideturtle()
left(90)
forward(300)
color("orange", "yellow")
begin_fill()
right(162)
# 5角星
for i in range(5):
    forward(n / 5)
    left(72)
    forward(n / 5)
    right(144)
right(198)
end_fill()
penup()
t.goto(0, 260)
pendown()
end_fill()


def draw_light():
    if random.randint(0, 70) == 1:
        color("red")
        circle(3)
    elif random.randint(0, 70) == 2:
        color("orange")
        circle(2)
    elif random.randint(0, 70) == 3:
        color("tomato")
        circle(4)
    elif random.randint(0, 70) == 4:
        color("white")
        circle(2)
    else:
        color('dark green')


color("dark green")
backward(n * 4.8)


# 递归函数
def tree(d, s):  # 开始画树
    if d <= 0:
        return
    forward(s)
    tree(d - 1, s * .8)
    right(120)
    tree(d - 3, s * .5)
    draw_light()  # 同时调用小彩灯的方法
    right(120)
    tree(d - 3, s * .5)
    right(120)
    backward(s)

# 调用函数
tree(15, 100)
backward(50)
for i in range(60):  # 循环画最底端的小装饰
    a = 200 - 400 * random.random()
    b = 10 - 20 * random.random()
    up()
    forward(b)
    left(90)
    forward(a)
    down()
    if random.randint(0, 1) == 0:
        color('tomato')
    else:
        color('wheat')
    circle(2)
    up()
    backward(a)
    right(90)
    backward(b)


def draw_snowman(n, m, a, b, r, y, q, w, e, o, u):  # 画雪人 (n,m)是头和身子交点的坐标,a是头的大小,m是身体的大小
    penup()
    t.goto(n, m)
    t.goto(n, m - 4)
    pendown()
    t.pencolor("white")
    t.pensize(2)
    t.fillcolor("white")
    t.seth(0)
    t.begin_fill()
    t.circle(a)
    t.end_fill()
    t.goto(n, m + 8)
    t.seth(180)
    t.begin_fill()
    t.circle(b)
    t.end_fill()
    t.pencolor("black")
    t.fillcolor("black")
    t.penup()  # 右眼睛
    t.goto(n - a / 4, m + a)
    t.seth(0)
    t.pendown()
    t.begin_fill()
    t.circle(r)
    t.end_fill()
    t.penup()  # 左眼睛
    t.goto(n + a / 4, m + a)
    t.seth(0)
    t.pendown()
    t.begin_fill()
    t.circle(r)
    t.end_fill()
    t.penup()  # 画嘴巴
    t.goto(n - a / 13, m + a / 2)
    t.seth(0)

    t.left(260)
    t.pendown()
    pencolor("red")
    t.circle(u, 200)

    t.seth(0)
    # t.forward(3)
    t.seth(0)

    t.penup()  # 画扣子
    t.pencolor("red")
    t.fillcolor("red")
    t.goto(n, m - b / 4)
    t.pendown()
    t.begin_fill()
    t.circle(2)
    t.end_fill()
    t.penup()
    t.pencolor("yellow")
    t.fillcolor("yellow")
    t.goto(n, m - b / 2)
    t.pendown()
    t.begin_fill()
    t.circle(2)
    t.end_fill()
    t.penup()
    t.pencolor("orange")
    t.fillcolor("orange")
    t.goto(n, m - (3 * b) / 4)
    t.pendown()
    t.begin_fill()
    t.circle(2)
    t.end_fill()
    # 画帽子
    # penup()
    # t.goto(n, m + y)
    # pendown()
    # pencolor("black")
    # color("black")
    # begin_fill()
    # forward(q)
    # backward(w)
    # right(e)
    # forward(o)
    # left(e)
    # forward(o)
    # left(e)
    # forward(o)
    # end_fill()
    # 画帽子
    penup()
    t.goto(n, m + y * 1.2)
    pendown()
    color("white")
    circle(3)
    pencolor("red")
    color("red")
    begin_fill()
    right(65)
    forward(q)
    t.goto(n - 0.2 * y, m + 0.8 * y)
    t.goto(n, m + y * 1.2)
    end_fill()


# draw_snowman(-200, -200, 20, 30, 1.5, 45, 20, 40, 45, 17, 2.5)
# draw_snowman(-250, -200, 30, 40, 2, 70, 30, 60, 45, 25, 3.5)
draw_snowman(-200, -200, 20, 30, 1.5, 45, 20, 40, 45, 17, 2.5)
draw_snowman(-250, -200, 30, 40, 2, 70, 30, 60, 45, 25, 3.5)


def draw_snow():  # 画雪花
    t.ht()  # 隐藏笔头,ht=hideturtle
    t.pensize(2)  # 定义笔头大小
    for i in range(60):  # 画多少雪花
        t.pencolor("white")  # 定义画笔颜色为白色,其实就是雪花为白色
        t.pu()  # 提笔,pu=penup
        t.setx(random.randint(-350, 350))  # 定义x坐标,随机从-350到350之间选择
        t.sety(random.randint(-100, 350))  # 定义y坐标,注意雪花一般在地上不会落下,所以不会从太小的纵座轴开始
        t.pd()  # 落笔,pd=pendown
        dens = 6  # 雪花瓣数设为6
        snowsize = random.randint(1, 10)  # 定义雪花大小
        for j in range(dens):  # 就是6,那就是画5次,也就是一个雪花五角星
            # t.forward(int(snow_size))  #int()取整数
            t.fd(int(snowsize))
            t.backward(int(snowsize))
            # t.bd(int(snow_size))  #注意没有bd=backward,但有fd=forward,小bug
            t.right(int(360 / dens))  # 转动角度


draw_snow()
t.penup()
t.goto(-90, 320)
t.pendown()
t.color("dark red", "red")  # 定义字体颜色
t.penup()
t.write("圣诞快乐!", font=("Comic Sans MS", 16, "bold"))  # 定义文字、位置、字体、大小
t.end_fill()

t.mainloop()

效果

添加了背景音乐,可在相关代码替换

filename = resource_path(os.path.join("music", "music.mp3"))

圣诞树

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值