圣誕節(jié)教你用Python繪制愛(ài)心圣誕樹(shù)
心血來(lái)潮的一個(gè)想法,分享一下代碼
代碼
# -*- coding: utf-8 -*- """ Created on Sat Dec 12 12:29:09 2020 @author: haoyu """ import turtle as t import random # 愛(ài)心函數(shù) # 將愛(ài)心分為兩個(gè)半圓與一個(gè)正方形 # r為半圓半徑,l = 2r為正方形邊長(zhǎng) # 調(diào)整半徑即可調(diào)整愛(ài)心大小 def loving_heart(r): l = 2 * r t.left(45) t.forward(l) t.circle(r, 180) t.right(90) t.circle(r, 180) t.forward(l) # 樹(shù)函數(shù)(遞歸) def tree(d, s): if d <= 0: return t.forward(s) tree(d - 1, s * .8) t.right(120) tree(d - 3, s * .5) t.right(120) tree(d - 3, s * .5) t.right(120) t.backward(s) #回退函數(shù) #畫(huà)愛(ài)心部分 t.penup() t.goto(0,200) #設(shè)置起點(diǎn)位置 t.pendown() t.pencolor('pink') #設(shè)置畫(huà)筆顏色 t.color('pink') t.begin_fill() #對(duì)圖形進(jìn)行填充 loving_heart(20) #執(zhí)行畫(huà)愛(ài)心函數(shù) t.end_fill() #畫(huà)樹(shù)部分 n = 100 t.speed('fastest') #t.Turtle().screen.delay(0) t.right(225) t.color("dark green") t.backward(n * 4.8) tree(15, n) t.backward(n / 5) #繪制落葉 for i in range(200): a = 200 - 400 * random.random() b = 10 - 20 * random.random() t.up() t.forward(b) t.left(90) t.forward(a) t.down() if random.randint(0, 1) == 0: t.color('tomato') else: t.color('wheat') t.circle(2) t.up() t.backward(a) t.right(90) t.backward(b) t.hideturtle()
結(jié)果
參考:https://www.cnblogs.com/felixwang2/p/10177515.html
介紹下其他方法如何用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(800,600) 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, 17): 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 += 2 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(17,20): y = 30*i for j in range(3): x = 30*j square.goto(x,-y+280) square.stamp() square.goto(-x,-y+280) square.stamp() turtle.exitonclick()
效果:
到此這篇關(guān)于圣誕節(jié)教你用Python繪制愛(ài)心圣誕樹(shù)的文章就介紹到這了,更多相關(guān)Python圣誕樹(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python讀取英文文件并記錄每個(gè)單詞出現(xiàn)次數(shù)后降序輸出示例
這篇文章主要介紹了Python讀取英文文件并記錄每個(gè)單詞出現(xiàn)次數(shù)后降序輸出,涉及Python文件讀取、字符串替換、分割以及字典遍歷、排序等相關(guān)操作技巧,需要的朋友可以參考下2018-06-06Python通過(guò)yagmail實(shí)現(xiàn)發(fā)送郵件代碼解析
這篇文章主要介紹了Python通過(guò)yagmail實(shí)現(xiàn)發(fā)送郵件代碼解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10Python單個(gè)項(xiàng)目列表轉(zhuǎn)換為整數(shù)的實(shí)現(xiàn)
本文主要介紹了Python單個(gè)項(xiàng)目列表轉(zhuǎn)換為整數(shù)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02Python Web框架Flask中使用七牛云存儲(chǔ)實(shí)例
這篇文章主要介紹了Python Web框架Flask中使用七牛云存儲(chǔ)實(shí)例,本文也對(duì)七牛云存儲(chǔ)Python SDK的一個(gè)使用示例,需要的朋友可以參考下2015-02-02python實(shí)現(xiàn)在圖片上畫(huà)特定大小角度矩形框
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)在圖片上畫(huà)特定大小角度矩形框,以及C++的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10解決出現(xiàn)Incorrect integer value: '''' for column ''id'' at row 1
這篇文章主要介紹了解決出現(xiàn)Incorrect integer value: '' for column 'id' at row 1的問(wèn)題的相關(guān)資料,希望通過(guò)本文能幫助到大家,讓大家遇到這樣的問(wèn)題及時(shí)的解決,需要的朋友可以參考下2017-10-10