圣誕節(jié)教你用Python繪制愛心圣誕樹
心血來潮的一個想法,分享一下代碼
代碼
# -*- coding: utf-8 -*- """ Created on Sat Dec 12 12:29:09 2020 @author: haoyu """ import turtle as t import random # 愛心函數(shù) # 將愛心分為兩個半圓與一個正方形 # r為半圓半徑,l = 2r為正方形邊長 # 調(diào)整半徑即可調(diào)整愛心大小 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ù)(遞歸) 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ù) #畫愛心部分 t.penup() t.goto(0,200) #設(shè)置起點(diǎn)位置 t.pendown() t.pencolor('pink') #設(shè)置畫筆顏色 t.color('pink') t.begin_fill() #對圖形進(jìn)行填充 loving_heart(20) #執(zhí)行畫愛心函數(shù) t.end_fill() #畫樹部分 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畫一個圣誕樹呢?
最簡單:
height = 5 stars = 1 for i in range(height): print((' ' * (height - i)) + ('*' * stars)) stars += 2 print((' ' * height) + '|')
效果:
哈哈哈哈,總有一種騙了大家的感覺。
其實(shí)本文是想介紹Turtle庫來畫圣誕樹。
方法:
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繪制愛心圣誕樹的文章就介紹到這了,更多相關(guān)Python圣誕樹內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python讀取英文文件并記錄每個單詞出現(xiàn)次數(shù)后降序輸出示例
這篇文章主要介紹了Python讀取英文文件并記錄每個單詞出現(xiàn)次數(shù)后降序輸出,涉及Python文件讀取、字符串替換、分割以及字典遍歷、排序等相關(guān)操作技巧,需要的朋友可以參考下2018-06-06Python通過yagmail實(shí)現(xiàn)發(fā)送郵件代碼解析
這篇文章主要介紹了Python通過yagmail實(shí)現(xiàn)發(fā)送郵件代碼解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-10-10Python單個項目列表轉(zhuǎn)換為整數(shù)的實(shí)現(xiàn)
本文主要介紹了Python單個項目列表轉(zhuǎn)換為整數(shù)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02Python Web框架Flask中使用七牛云存儲實(shí)例
這篇文章主要介紹了Python Web框架Flask中使用七牛云存儲實(shí)例,本文也對七牛云存儲Python SDK的一個使用示例,需要的朋友可以參考下2015-02-02python實(shí)現(xiàn)在圖片上畫特定大小角度矩形框
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)在圖片上畫特定大小角度矩形框,以及C++的實(shí)現(xiàn)方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下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的問題的相關(guān)資料,希望通過本文能幫助到大家,讓大家遇到這樣的問題及時的解決,需要的朋友可以參考下2017-10-10