使用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列表(list)、字典(dict)、字符串(string)基本操作小結(jié)
這篇文章主要介紹了Python列表(list)、字典(dict)、字符串(string)基本操作小結(jié),本文總結(jié)了最基本最常用的一些操作,需要的朋友可以參考下2014-11-11探索Python中zoneinfo模塊處理時(shí)區(qū)操作實(shí)例
這篇文章主要為大家介紹了探索Python中zoneinfo模塊的用法實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01python使用Scrapy庫(kù)進(jìn)行數(shù)據(jù)提取和處理的方法詳解
在我們的初級(jí)教程中,我們介紹了如何使用Scrapy創(chuàng)建和運(yùn)行一個(gè)簡(jiǎn)單的爬蟲(chóng),在這篇文章中,我們將深入了解Scrapy的強(qiáng)大功能,學(xué)習(xí)如何使用Scrapy提取和處理數(shù)據(jù)2023-09-09Python實(shí)現(xiàn)創(chuàng)建模塊的方法詳解
導(dǎo)入一個(gè)模塊,我們一般都會(huì)使用?import?關(guān)鍵字,但有些場(chǎng)景下?import?難以滿(mǎn)足我們的需要。所以除了?import?之外還有很多其它導(dǎo)入模塊的方式,下面就來(lái)介紹一下2022-07-07Python列表創(chuàng)建與銷(xiāo)毀及緩存池機(jī)制
這篇文章主要介紹了Python列表創(chuàng)建與銷(xiāo)毀及緩存池機(jī)制,文章基于python展開(kāi)對(duì)列表創(chuàng)建與銷(xiāo)毀內(nèi)容的展開(kāi),具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-05-05關(guān)于keras.layers.Conv1D的kernel_size參數(shù)使用介紹
這篇文章主要介紹了關(guān)于keras.layers.Conv1D的kernel_size參數(shù)使用介紹,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05python實(shí)現(xiàn)的用于搜索文件并進(jìn)行內(nèi)容替換的類(lèi)實(shí)例
這篇文章主要介紹了python實(shí)現(xiàn)的用于搜索文件并進(jìn)行內(nèi)容替換的類(lèi),涉及Python針對(duì)文件及字符串的相關(guān)操作技巧,需要的朋友可以參考下2015-06-06