使用Python畫(huà)了一棵圣誕樹(shù)的實(shí)例代碼
分享給大家一篇文章,教你怎樣用Python畫(huà)了一棵圣誕樹(shù),快來(lái)學(xué)習(xí)。

如何用Python畫(huà)一個(gè)圣誕樹(shù)呢?
最簡(jiǎn)單:
height = 5
stars = 1
for i in range(height):
print((' ' * (height - i)) + ('*' * stars))
stars += 2
print((' ' * height) + '|')
效果:

哈哈哈哈,總有一種騙了大家的感覺(jué)。
其實(shí)本文是想介紹Turtle庫(kù)來(lái)畫(huà)圣誕樹(shù)。
import turtle
screen = turtle.Screen()
screen.setup(375, 700)
circle = turtle.Turtle()
circle.shape('circle')
circle.color('red')
circle.speed('fastest')
circle.up()
square = turtle.Turtle()
square.shape('square')
square.color('green')
square.speed('fastest')
square.up()
circle.goto(0, 280)
circle.stamp()
k = 0
for i in range(1, 13):
y = 30 * i
for j in range(i - k):
x = 30 * j
square.goto(x, -y + 280)
square.stamp()
square.goto(-x, -y + 280)
square.stamp()
if i % 4 == 0:
x = 30 * (j + 1)
circle.color('red')
circle.goto(-x, -y + 280)
circle.stamp()
circle.goto(x, -y + 280)
circle.stamp()
k += 3
if i % 4 == 3:
x = 30 * (j + 1)
circle.color('yellow')
circle.goto(-x, -y + 280)
circle.stamp()
circle.goto(x, -y + 280)
circle.stamp()
square.color('brown')
for i in range(13, 17):
y = 30 * i
for j in range(2):
x = 30 * j
square.goto(x, -y + 280)
square.stamp()
square.goto(-x, -y + 280)
square.stamp()
效果:

方法二:
import turtle
# 定義圣誕樹(shù)的綠葉函數(shù)
def tree(d, s):
if d <= 0:
return
turtle.forward(s)
tree(d - 1, s * .8)
turtle.right(120)
tree(d - 3, s * .5)
turtle.right(120)
tree(d - 3, s * .5)
turtle.right(120)
turtle.backward(s)
n = 100
""" 設(shè)置繪圖速度
'fastest' : 0
'fast' : 10
'normal' : 6
'slow' : 3
'slowest' : 1
"""
turtle.speed('fastest') # 設(shè)置速度
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)
# 執(zhí)行函數(shù)
tree(15, n)
turtle.backward(n / 5)
效果:

到此這篇關(guān)于使用Python畫(huà)了一棵圣誕樹(shù)的實(shí)例代碼的文章就介紹到這了,更多相關(guān)Python圣誕樹(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python調(diào)用訊飛語(yǔ)音合成API接口來(lái)實(shí)現(xiàn)文字轉(zhuǎn)語(yǔ)音
這篇文章主要為大家介紹了Python調(diào)用訊飛語(yǔ)音合成API接口來(lái)實(shí)現(xiàn)文字轉(zhuǎn)語(yǔ)音方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
pandas如何實(shí)現(xiàn)兩個(gè)dataframe相減
這篇文章主要介紹了pandas如何實(shí)現(xiàn)兩個(gè)dataframe相減方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
Pandas缺失值填充 df.fillna()的實(shí)現(xiàn)
本文主要介紹了Pandas缺失值填充 df.fillna()的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
Pandas DataFrame操作數(shù)據(jù)增刪查改
我們?cè)谟?nbsp;pandas 處理數(shù)據(jù)的時(shí)候,經(jīng)常會(huì)遇到用其中一列數(shù)據(jù)替換另一列數(shù)據(jù)的場(chǎng)景。這一類的需求估計(jì)很多人都遇到,當(dāng)然還有其它更復(fù)雜的。解決這類需求的辦法有很多,這里我們來(lái)推薦幾個(gè),這篇文章主要介紹了Pandas DataFrame操作數(shù)據(jù)的增刪查改2022-10-10
Python數(shù)據(jù)處理利器Slice函數(shù)用法詳解
這篇文章主要給大家介紹了關(guān)于Python數(shù)據(jù)處理利器Slice函數(shù)用法的相關(guān)資料,slice函數(shù)是Python中的一個(gè)內(nèi)置函數(shù),用于對(duì)序列進(jìn)行切片操作,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-03-03
Python實(shí)現(xiàn)希爾伯特變換(Hilbert transform)的示例代碼
希爾伯特變換(Hilbert transform)是一個(gè)對(duì)函數(shù)產(chǎn)生定義域相同的函數(shù)的線性算子,而且希爾伯特變換在信號(hào)處理中很重要,所以本文和大家分享了Python實(shí)現(xiàn)希爾伯特變換的代碼,需要的可以參考一下2023-04-04

