圣誕節(jié)教你用Python繪制愛心圣誕樹
心血來潮的一個(gè)想法,分享一下代碼
代碼
# -*- coding: utf-8 -*-
"""
Created on Sat Dec 12 12:29:09 2020
@author: haoyu
"""
import turtle as t
import random
# 愛心函數(shù)
# 將愛心分為兩個(gè)半圓與一個(gè)正方形
# 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畫一個(gè)圣誕樹呢?
最簡單:
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)文章
Python3爬蟲里關(guān)于Splash負(fù)載均衡配置詳解
在本篇文章里小編給大家分享了關(guān)于Python3爬蟲里關(guān)于Splash負(fù)載均衡配置的相關(guān)內(nèi)容,需要的朋友們可以學(xué)習(xí)參考下。2020-07-07
終于搞懂了Python中super(XXXX,?self).__init__()的作用了
本文主要介紹了終于搞懂了Python中super(XXXX,?self).__init__()的作用了,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
對django 2.x版本中models.ForeignKey()外鍵說明介紹
這篇文章主要介紹了對django 2.x版本中models.ForeignKey()外鍵說明介紹,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
在Python中采集Prometheus數(shù)據(jù)的詳細(xì)用法教程
Prometheus是一個(gè)開源的監(jiān)控和警報(bào)工具,專門用于記錄和查詢時(shí)間序列數(shù)據(jù),它提供了一個(gè)強(qiáng)大的查詢語言PromQL(Prometheus Query Language),允許用戶根據(jù)不同的標(biāo)簽和指標(biāo)選擇特定的時(shí)間序列數(shù)據(jù),本文將詳細(xì)介紹如何在Python中采集Prometheus數(shù)據(jù)2024-07-07

