最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用Python繪制圣誕樹教程詳解(附源代碼)

 更新時(shí)間:2023年12月05日 11:42:14   作者:艾派森  
又是一年一度的圣誕節(jié)快到了,提到圣誕節(jié),就不得不提圣誕樹,所以本文我們將使用Python繪制一棵圣誕樹,文中有詳細(xì)的代碼講解,具有一定的參考價(jià)值,需要的朋友可以參考下

又是一年一度的圣誕節(jié)快到了,作為程序猿那必須露一手,最終效果圖如下:

1.turtle庫(kù) 

turtle(海龜)是Python重要的標(biāo)準(zhǔn)庫(kù)之一,它能夠進(jìn)行基本的圖形繪制,其概念誕生于1969年。turtle是最有價(jià)值的程序設(shè)計(jì)入門實(shí)踐庫(kù),它是程序設(shè)計(jì)入門層面最常用的基本繪圖庫(kù)。

turtle的繪圖原理:

  • 有一只海龜處于畫布正中心,由程序控制在畫布上游走;
  • 海龜走過(guò)的軌跡形成了繪制的圖形
  • 海龜由程序控制,可改變其大小,顏色等 

2.實(shí)現(xiàn)步驟 

(1)導(dǎo)入庫(kù)

from turtle import *
from random import *
import math

(2)定義基本繪圖方法

def Rightdraw(Range,Fd,Right): 
    for i in range(Range): # Range循環(huán)次數(shù)
        fd(Fd)  # 向前Fd個(gè)距離
        right(Right) #在當(dāng)前行進(jìn)方向再向右偏轉(zhuǎn)Right度
 
def Leftdraw(Range,Fd,Left): 
    for i in range(Range): # Range循環(huán)次數(shù)
        fd(Fd)  # 向前Fd個(gè)距離
        left(Left) #在當(dāng)前行進(jìn)方向再向右偏轉(zhuǎn)Right度
 
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)

(3)畫樹身

# 樹頂層
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)

(4)畫裝飾小物件

五角星

# 畫五角星
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)    #繼續(xù)換角度
    end_fill()
    right(126)

雪花

# 繪制雪花
def drawSnow():
    hideturtle()
    speed(0)
    pencolor("white")
    pensize(2)
    for i in range(80): # 雪花數(shù)量
        changeMypos(randint(-248,248),randint(-100,248))
        petalNumber = 6 # 雪花花瓣數(shù)為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()

(5)祝福語(yǔ)

# 祝福語(yǔ)
color("dark red","red") #定義字體顏色
penup()
goto(0,-230)
write("Merry Christmas",align ="center",font=("Comic Sans MS",40,"bold"))#定義文字、位置、字體、大小

3.完整代碼

from turtle import *
from random import *
import math
 
# 繪圖方法
def Rightdraw(Range,Fd,Right): 
    for i in range(Range): # Range循環(huán)次數(shù)
        fd(Fd)  # 向前Fd個(gè)距離
        right(Right) #在當(dāng)前行進(jìn)方向再向右偏轉(zhuǎn)Right度
def Leftdraw(Range,Fd,Left): 
    for i in range(Range): # Range循環(huán)次數(shù)
        fd(Fd)  # 向前Fd個(gè)距離
        left(Left) #在當(dāng)前行進(jìn)方向再向右偏轉(zhuǎn)Right度
 
# 背景改為黑色
screensize(bg='black') 
 
# 重設(shè)海龜位置
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)    #繼續(xù)換角度
    end_fill()
    right(126)
 
# 繪制雪花
def drawSnow():
    hideturtle()
    speed(0)
    pencolor("white")
    pensize(2)
    for i in range(80): # 雪花數(shù)量
        changeMypos(randint(-248,248),randint(-100,248))
        petalNumber = 6 # 雪花花瓣數(shù)為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)
 
 
# 樹頂層
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)  
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)
 
# 打印祝福語(yǔ)
color("dark red","red") #定義字體顏色
penup()
goto(0,-230)
write("Merry Christmas",align ="center",font=("Comic Sans MS",40,"bold"))#定義文字、位置、字體、大小
 
# 調(diào)用下雪的函數(shù)
drawSnow()
 
done()

以上就是本次圣誕樹的畫法教程,其中里面的一些文字、顏色、參數(shù)等大家可自行調(diào)整繪制出自己喜歡的圣誕樹~

到此這篇關(guān)于使用Python繪制圣誕樹教程詳解(附源代碼)的文章就介紹到這了,更多相關(guān)Python繪制圣誕樹內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python uv包管理小結(jié)

    python uv包管理小結(jié)

    uv?是一個(gè)高性能的 Python 包管理工具,它不僅能夠高效地處理包管理和依賴解析,還提供了對(duì) Python 版本管理的支持,本文主要介紹了python uv包管理小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下
    2025-04-04
  • Python編程入門之Hello World的三種實(shí)現(xiàn)方式

    Python編程入門之Hello World的三種實(shí)現(xiàn)方式

    這篇文章主要介紹了Python編程入門之Hello World的三種實(shí)現(xiàn)方式,實(shí)例分析了print輸出函數(shù)的使用及控制臺(tái)輸出的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-11-11
  • python 常用的基礎(chǔ)函數(shù)

    python 常用的基礎(chǔ)函數(shù)

    這篇文章主要介紹了python 77種常用的基礎(chǔ)函數(shù),學(xué)習(xí)python的朋友可以收藏一下,簡(jiǎn)單了解一下,方便后期使用
    2018-07-07
  • python SQLAlchemy 數(shù)據(jù)庫(kù)連接池的實(shí)現(xiàn)

    python SQLAlchemy 數(shù)據(jù)庫(kù)連接池的實(shí)現(xiàn)

    SSQLAlchemy提供了強(qiáng)大的連接池和連接管理功能,可以有效地管理數(shù)據(jù)庫(kù)連接,本文主要介紹了python SQLAlchemy 數(shù)據(jù)庫(kù)連接池的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下
    2025-03-03
  • 利用Python?requests庫(kù)爬取高德地圖全國(guó)地鐵站點(diǎn)信息

    利用Python?requests庫(kù)爬取高德地圖全國(guó)地鐵站點(diǎn)信息

    requests?模塊是?python?基于?urllib,采用?Apache2?Licensed?開源協(xié)議的?HTTP?庫(kù),它比?urllib?更加方便,可以節(jié)約我們大量的工作,完全滿足?HTTP?測(cè)試需求,這篇文章主要介紹了利用Python?requests庫(kù)爬取高德地圖全國(guó)地鐵站點(diǎn)信息,需要的朋友可以參考下
    2024-03-03
  • paramiko使用tail實(shí)時(shí)獲取服務(wù)器的日志輸出詳解

    paramiko使用tail實(shí)時(shí)獲取服務(wù)器的日志輸出詳解

    這篇文章主要給大家介紹了關(guān)于paramiko使用tail實(shí)時(shí)獲取服務(wù)器的日志輸出的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • python使用 zip 同時(shí)迭代多個(gè)序列示例

    python使用 zip 同時(shí)迭代多個(gè)序列示例

    這篇文章主要介紹了python使用 zip 同時(shí)迭代多個(gè)序列,結(jié)合實(shí)例形式分析了Python使用zip遍歷迭代長(zhǎng)度相等與不等的序列相關(guān)操作技巧,需要的朋友可以參考下
    2019-07-07
  • Python執(zhí)行命令并保存輸出到文件的實(shí)現(xiàn)方法

    Python執(zhí)行命令并保存輸出到文件的實(shí)現(xiàn)方法

    文章介紹了在Python腳本中執(zhí)行系統(tǒng)命令并將輸出內(nèi)容保存到文件的多種方法,主要對(duì)比了os.system()、os.popen()和subprocess.run()、subprocess.Popen()等四種方法的實(shí)現(xiàn)原理、適用場(chǎng)景和具體代碼示例,還提供了最佳實(shí)踐,需要的朋友可以參考下
    2026-04-04
  • Python結(jié)合FastAPI搭建一個(gè)接口實(shí)現(xiàn)自動(dòng)生成文檔

    Python結(jié)合FastAPI搭建一個(gè)接口實(shí)現(xiàn)自動(dòng)生成文檔

    這篇文章主要為大家詳細(xì)介紹了Python如何結(jié)合FastAPI搭建一個(gè)接口,可以實(shí)現(xiàn)自動(dòng)生成文檔,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下
    2025-12-12
  • 講解Python中if語(yǔ)句的嵌套用法

    講解Python中if語(yǔ)句的嵌套用法

    這篇文章主要介紹了講解Python中if語(yǔ)句的嵌套用法,是Python入門當(dāng)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-05-05

最新評(píng)論

嘉荫县| 耒阳市| 慈利县| 和平县| 兰考县| 安岳县| 安仁县| 扬中市| 永春县| 任丘市| 高陵县| 塔城市| 星子县| 灵台县| 个旧市| 蛟河市| 阜阳市| 武宣县| 彰化县| 卢氏县| 长丰县| 慈溪市| 耿马| 定南县| 获嘉县| 桃江县| 浦城县| 松溪县| 乌鲁木齐市| 丰原市| 荃湾区| 花垣县| 神木县| 栖霞市| 凤冈县| 锦州市| 武功县| 永济市| 安阳市| 弥渡县| 新巴尔虎左旗|