使用Python畫了一棵圣誕樹的實例代碼
分享給大家一篇文章,教你怎樣用Python畫了一棵圣誕樹,快來學(xué)習(xí)。
如何用Python畫一個圣誕樹呢?
最簡單:
height = 5 stars = 1 for i in range(height): print((' ' * (height - i)) + ('*' * stars)) stars += 2 print((' ' * height) + '|')
效果:
哈哈哈哈,總有一種騙了大家的感覺。
其實本文是想介紹Turtle庫來畫圣誕樹。
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ù) 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畫了一棵圣誕樹的實例代碼的文章就介紹到這了,更多相關(guān)Python圣誕樹內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python列表(list)、字典(dict)、字符串(string)基本操作小結(jié)
這篇文章主要介紹了Python列表(list)、字典(dict)、字符串(string)基本操作小結(jié),本文總結(jié)了最基本最常用的一些操作,需要的朋友可以參考下2014-11-11探索Python中zoneinfo模塊處理時區(qū)操作實例
這篇文章主要為大家介紹了探索Python中zoneinfo模塊的用法實例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01python使用Scrapy庫進行數(shù)據(jù)提取和處理的方法詳解
在我們的初級教程中,我們介紹了如何使用Scrapy創(chuàng)建和運行一個簡單的爬蟲,在這篇文章中,我們將深入了解Scrapy的強大功能,學(xué)習(xí)如何使用Scrapy提取和處理數(shù)據(jù)2023-09-09Python實現(xiàn)創(chuàng)建模塊的方法詳解
導(dǎo)入一個模塊,我們一般都會使用?import?關(guān)鍵字,但有些場景下?import?難以滿足我們的需要。所以除了?import?之外還有很多其它導(dǎo)入模塊的方式,下面就來介紹一下2022-07-07關(guān)于keras.layers.Conv1D的kernel_size參數(shù)使用介紹
這篇文章主要介紹了關(guān)于keras.layers.Conv1D的kernel_size參數(shù)使用介紹,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05python實現(xiàn)的用于搜索文件并進行內(nèi)容替換的類實例
這篇文章主要介紹了python實現(xiàn)的用于搜索文件并進行內(nèi)容替換的類,涉及Python針對文件及字符串的相關(guān)操作技巧,需要的朋友可以參考下2015-06-06