IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Python知识库 -> python代码~创意圣诞树 -> 正文阅读

[Python知识库]python代码~创意圣诞树

2022年圣诞节到来啦,很高兴这次我们又能一起度过~

圣诞节(Christmas)本身是一个宗教节,用来庆祝耶稣的诞辰,因而又名耶诞节

Hope all your Christmas dreams come true!   

愿你所有的圣诞梦想都成真!   

Hope you enjoy the happiness of Christmas and all the trimmings.   

祝你享尽圣诞佳节的快乐和圣诞的礼品。   

Warm hearted wishes for an old fashioned Christmas and a happy New Year filled with all your favorite things.   

传统佳节之际,献上圣诞的殷殷祝福,祝新年万事如意!   

I wish you a merry Christmas. All affection and best wishes to you and yours.   

以我所有的爱心与真诚祝你及全家圣诞快乐。   

In the season of joy I present my sincere wishes and kind thoughts. May the kind of Christmas outshine all the rest.   

在这欢乐的时节给你我最真的祝福和亲切的思念,愿你今年的圣诞比往年更璀璨。   

May the Christmas be a time of laughter and real enjoyment for you. Best wishes.   

愿圣诞不仅是你欢笑的时刻,更是你欣喜的日子。祝福你。   

May the glow of Christmas candle fill your heart with peace and pleasure and make your New Year bright. Have a love filled Christmas and New Year!   

愿圣诞的烛光带给你祥和与喜悦,祝你的圣诞和新年充满爱。   

May the bright and festive glow of Christmas candle warm the days all the year through. Hoping you will have a wonderful time enjoying Christmas Day and wishing you a New Year that is happy in every way.   

愿明亮喜庆的圣诞烛光温暖一年中的每个日日夜夜,祝你欢欢喜喜度圣诞,高高兴兴过新年!

一、前言

圣诞节快到了,是不是想用python画一个可爱的圣诞树,我在各大网站都查了一下,都不太美观,然后我就学习了一下别人的代码改写了一下,自己加了一些东西,弄的好看一些了,给大家开源出来,欢迎大家指正学习,也欢迎转载,请注明出处哦~

二、创意名

这个圣诞你很快乐,因为由我为你亲手绘制的圣诞树。

三、效果展示

?

四、实现步骤

一、Turtle是什么?

Turtle库是Python语言中一个很流行的绘制图像的函数库,想象一个小乌龟,在一个横轴为x、纵轴为y的坐标系原点,(0,0)位置开始,它根据一组函数指令的控制,在这个平面坐标系中移动,从而在它爬行的路径上绘制了图形。

1.引入库

from turtle import *
from random import *
import math

2.turtle绘图的基础知识

(1) 画笔运动命令

在这里插入图片描述

?(2) 画笔控制命令

在这里插入图片描述

(3) 全局控制命令

在这里插入图片描述?

在这里插入图片描述?

?

五、编码实现

完整代码如下所示:?

from turtle import *
from random import *
import math


# 绘图方法
def Rightdraw(Range, Fd, Right):
    for i in range(Range):  # Range循环次数
        fd(Fd)  # 向前Fd个距离
        right(Right)  # 在当前行进方向再向右偏转Right度


def Leftdraw(Range, Fd, Left):
    for i in range(Range):  # Range循环次数
        fd(Fd)  # 向前Fd个距离
        left(Left)  # 在当前行进方向再向右偏转Right度


# 背景改为黑色
screensize(bg='black')


# 重设海龟位置
def changeMypos(x, y, range=heading(), Fd=0):
    penup()
    goto(x, y)
    seth(range)
    fd(Fd)
    pendown()


def drawBranch(x, y, size=1):
    changeMypos(x, y)
    Leftdraw(6, 3, 9)
    seth(0)
    Rightdraw(6, 3, 9)
    seth(0)
    fd(6)


# 画五角星
def drawStar(x, y, Range, size):
    pensize(1)
    color("red", "yellow")
    begin_fill()
    changeMypos(x, y, Range)
    for i in range(5):  # 画五角星
        forward(10 * size)
        right(144)  # 五角星的角度
        forward(10 * size)
        left(72)  # 继续换角度
    end_fill()
    right(126)


# 绘制雪花
def drawSnow():
    hideturtle()
    speed(0)
    pencolor("white")
    pensize(2)
    for i in range(100):  # 雪花数量
        changeMypos(randint(-248, 248), randint(-100, 248))
        petalNumber = 6  # 雪花花瓣数为6
        snowSize = int(randint(2, 10))
        for j in range(petalNumber):
            fd(snowSize)
            backward(snowSize)
            right(360 / petalNumber)


# 圣诞袜子
def drawSock(x, y, range, size=1):
    # 绘制袜子的白边
    pensize(1)
    changeMypos(x, y, range)
    color("black", "white")
    begin_fill()
    fd(20 * size)
    circle(3 * size, 180)
    fd(20 * size)
    circle(3 * size, 180)
    end_fill()

    # 绘制袜子的下半部分
    color("white", "red")
    begin_fill()
    startx = x + 2 * size * math.cos(math.radians(range))
    starty = y + 2 * size * math.sin(math.radians(range))
    finalx = x + 18 * size * (math.cos(math.radians(range)))
    finaly = y + 18 * size * (math.sin(math.radians(range)))
    changeMypos(startx, starty, range - 90)
    fd(20 * size)  # 圆弧距离白边40
    seth(180 + range)
    fd(5 * size)  # 向袜子头延伸10
    circle(7 * size, 180)  # 袜子头处的半圆形
    fd(21 * size)  # 袜子宽42
    seth(90 + range)
    d = distance(finalx, finaly)  # 找到袜子底部与白边的距离
    fd(d)
    seth(range + 180)
    fd(16 * size)
    end_fill()


# 圣诞帽
def drawHat(x, y, range, size=1):
    # 绘制帽白边
    pensize(1)
    changeMypos(x, y, range)
    color("white", "white")
    begin_fill()
    fd(20 * size)
    circle(-3 * size, 180)
    fd(20 * size)
    circle(-3 * size, 180)
    end_fill()
    # 绘制帽子上半部分
    color("white", "red")
    begin_fill()
    startx = x + 2 * size * math.cos(math.radians(range))
    starty = y + 2 * size * math.sin(math.radians(range))
    finalx = x + 18 * size * (math.cos(math.radians(range)))
    finaly = y + 18 * size * (math.sin(math.radians(range)))
    changeMypos(startx, starty, range + 90)
    Rightdraw(18, 2 * size, 7)
    seth(190)
    Leftdraw(9, 2 * size, 8)
    goto(finalx, finaly)
    goto(startx, starty)
    end_fill()
    # 绘制圣诞帽上的小球
    changeMypos(startx, starty, range + 90)
    Rightdraw(18, 2 * size, 7)
    begin_fill()
    color("white", "white")
    circle(-2.5 * size)
    end_fill()


# 绘制彩带
def drawRibbon(x, y, range, size):
    begin_fill()
    color("red", "red")
    seth(range + 40)
    fd(15 * size * math.tan(math.radians(range + 40)))
    seth(range + 90)
    fd(20 / 3 * size)
    seth(range - 140)
    fd(15 * size * math.tan(math.radians(range + 40)))
    seth(range - 90)
    fd(20 / 3 * size)
    end_fill()


# 圣诞糖果
def drawCandy(x, y, range, size):
    # 绘制糖体
    pensize(1)
    changeMypos(x, y, range)
    color("white", "white")
    begin_fill()
    startx = x + 2 * size * math.cos(math.radians(range))
    starty = y + 2 * size * math.sin(math.radians(range))
    finalx = x + 8 * size * (math.cos(math.radians(range)))
    finaly = y + 8 * size * (math.sin(math.radians(range)))
    changeMypos(startx, starty, range + 90, 40 * size)
    circle(-40 / 3 * size, 180)
    circle(-8 / 3 * size, 180)
    circle(22 / 3 * size, 180)
    goto(finalx, finaly)
    goto(startx, starty)
    end_fill()
    # 绘制下面三条彩带
    color("white")
    changeMypos(startx, starty, range + 90)
    fd(10 / 3 * size)
    drawRibbon(xcor(), ycor(), range, size)
    changeMypos(xcor(), ycor(), range + 90, 13.3 * size)
    drawRibbon(xcor(), ycor(), range, size)
    changeMypos(xcor(), ycor(), range + 90, 13.3 * size)
    drawRibbon(xcor(), ycor(), range, size)
    # 绘制弧线段的彩带
    changeMypos(startx, starty, range + 90, 40 * size)
    circle(-13.3 * size, 55)
    x1 = xcor()
    y1 = ycor()
    begin_fill()
    circle(-13.3 * size, 80)
    right(75)
    fd(6.3 * size)
    right(115)
    circle(7 * size, 85)
    goto(x1, y1)
    end_fill()


setup(500, 500, startx=None, starty=None)
title("Merry Christmas")
speed(0)
pencolor("green")
pensize(10)
hideturtle()
changeMypos(0, 185, 0)

# shape(name= "classic")

# # 树顶层
seth(-120)
Rightdraw(10, 12, 2)
changeMypos(0, 185, -60)

Leftdraw(10, 12, 2)

changeMypos(xcor(), ycor(), -150, 10)

# # 第一层的波浪
for i in range(4):
    Rightdraw(5, 7, 15)
    seth(-150)
    penup()
    fd(2)
    pendown()

# # 树二层
changeMypos(-55, 70, -120)
Rightdraw(10, 8, 5)

changeMypos(50, 73, -60)
Leftdraw(10, 8, 5)

changeMypos(xcor(), ycor(), -120, 10)
seth(-145)
pendown()

# # 第二层的波浪
for i in range(5):
    Rightdraw(5, 9, 15)
    seth(-152.5)
    penup()
    fd(3)
    pendown()

# 树三层
changeMypos(-100, 0, -120)
Rightdraw(10, 6.5, 4.5)

changeMypos(80, 0, -50)
Leftdraw(10, 6, 3)

changeMypos(xcor(), ycor(), -120, 10)
seth(-145)

# # # 第三次的波浪
for i in range(6):
    Rightdraw(5, 9, 15)
    seth(-152)
    penup()
    fd(3)
    pendown()

# # 树四层
changeMypos(-120, -55, -130)
Rightdraw(7, 10, 4)

changeMypos(100, -55, -50)
Leftdraw(7, 10, 5)

changeMypos(xcor(), ycor(), -120, 10)
seth(-155)

# # 第四层的波浪
for i in range(7):
    Rightdraw(5, 9, 13)
    seth(-155)
    penup()
    fd(3)
    pendown()

# 树根
changeMypos(-70, -120, -85)
Leftdraw(3, 8, 3)

changeMypos(70, -120, -95)
Rightdraw(3, 8, 3)

changeMypos(xcor(), ycor(), -170, 10)
Rightdraw(10, 12, 2)

# 画树枝
drawBranch(45, -80)
drawBranch(-70, -25)
drawBranch(-20, 40)

# 添加挂件
drawHat(-25, 175, -10, 2.5)
drawCandy(-75, -50, -10, 1)  # -10别动了,这个绘制有点烦,我没做转角功能
drawCandy(10, 40, -10, 1.2)
drawStar(110, -90, 80, 1)
drawStar(-120, -100, 50, 1)
drawStar(-90, -50, 20, 1)
drawStar(90, -25, 30, 1)
drawSock(10, -35, -10, 2)
drawSock(-40, 100, 10, 1)
drawStar(-20, 40, 30, 1)
drawStar(10, 120, 90, 1)

# 打印祝福语
color("skyblue", "blue")  # 定义字体颜色
penup()
goto(0, -230)
write("Merry Christmas", align="center", font=("Comic Sans MS", 60, "bold"))  # 定义文字、位置、字体、大小

# 调用下雪的函数
drawSnow()

done()

  Python知识库 最新文章
Python中String模块
【Python】 14-CVS文件操作
python的panda库读写文件
使用Nordic的nrf52840实现蓝牙DFU过程
【Python学习记录】numpy数组用法整理
Python学习笔记
python字符串和列表
python如何从txt文件中解析出有效的数据
Python编程从入门到实践自学/3.1-3.2
python变量
上一篇文章      下一篇文章      查看所有文章
加:2022-12-25 11:04:55  更:2022-12-25 11:07:45 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/19 10:55:55-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码