python圣誕樹編寫實(shí)例詳解
python圣誕樹代碼
1、簡(jiǎn)單的繪制圣誕樹
新建tree1.py或者直接輸入下面代碼運(yùn)行
#聲明樹的高度
height = 5
#樹的雪花數(shù),初始為1
stars = 1
#以數(shù)的高度作為循環(huán)次數(shù)
for i in range(height):
print((' ' * (height - i)) + ('*' * stars))
stars += 2
#輸出樹干
print((' ' * height) + '|')

2、使用turtle繪制簡(jiǎn)單圣誕樹
新建tree2py,輸入以下代碼
#導(dǎo)入turtle庫(kù)
import turtle
#設(shè)置屏幕大小
screen = turtle.Screen()
screen.setup(800,600)
#獲取畫筆并設(shè)置一些屬性:圓形、紅色、快
circle = turtle.Turtle()
circle.shape('circle')
circle.color('red')
circle.speed('fastest')
#抬起畫筆
circle.up()
#重新獲取畫筆
square = turtle.Turtle()
#重新設(shè)置畫筆屬性:四方形、綠色、快
square.shape('square')
square.color('green')
square.speed('fastest')
#重新抬起畫筆
square.up()
#跳到指定坐標(biāo)位置
circle.goto(0,280)
#復(fù)制當(dāng)前圖形
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()
運(yùn)行:

3、使用Turtle繪制復(fù)雜圣誕樹
新建tree3.py,輸入以下代碼
#導(dǎo)入所依賴的庫(kù)
from turtle import *
import random
import time
n = 80.0
#設(shè)置速度快
speed("fastest")
#背景顏色 海貝殼色,偏粉色
screensize(bg='seashell')
left(90)
forward(3*n)
color("orange", "yellow")
begin_fill()
left(126)
for i in range(5):
forward(n/5)
right(144)
forward(n/5)
left(72)
end_fill()
right(126)
color("dark green")
backward(n*4.8)
def tree(d, s):
if d <= 0: return
forward(s)
tree(d-1, s*.8)
right(120)
tree(d-3, s*.5)
right(120)
tree(d-3, s*.5)
right(120)
backward(s)
tree(15, n)
backward(n/2)
for i in range(200):
a = 200 - 400 * random.random()
b = 10 - 20 * random.random()
up()
forward(b)
left(90)
forward(a)
down()
if random.randint(0, 1) == 0:
color('tomato')
else:
color('wheat')
circle(2)
up()
backward(a)
right(90)
backward(b)
time.sleep(60)
運(yùn)行:

以上就是python圣誕樹代碼的詳細(xì)內(nèi)容,感謝大家的學(xué)習(xí)和對(duì)腳本之家的支持。
相關(guān)文章
Python開發(fā)的HTTP庫(kù)requests詳解
Requests是用Python語(yǔ)言編寫,基于urllib,采用Apache2 Licensed開源協(xié)議的HTTP庫(kù)。它比urllib更加方便,可以節(jié)約我們大量的工作,完全滿足HTTP測(cè)試需求。Requests的哲學(xué)是以PEP 20 的習(xí)語(yǔ)為中心開發(fā)的,所以它比urllib更加Pythoner。更重要的一點(diǎn)是它支持Python3哦!2017-08-08
python語(yǔ)音識(shí)別實(shí)踐之百度語(yǔ)音API
這篇文章主要為大家詳細(xì)介紹了python語(yǔ)音識(shí)別實(shí)踐之百度語(yǔ)音API,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
Python基于scapy實(shí)現(xiàn)修改IP發(fā)送請(qǐng)求的方法示例
這篇文章主要介紹了Python基于scapy實(shí)現(xiàn)修改IP發(fā)送請(qǐng)求的方法,涉及Python網(wǎng)絡(luò)編程中使用scapy操作IP的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-07-07
關(guān)于flask路由app.route及路由參數(shù)的各種用法解析
我們?cè)陂_發(fā)過程中,編寫項(xiàng)目時(shí)所使用的路由往往是指代了框架/項(xiàng)目中用于完成路由功能的類,這個(gè)類一般就是路由類,簡(jiǎn)稱路由,這篇文章主要介紹了有關(guān)flask路由app.route及路由參數(shù)的各種用法解析,需要的朋友可以參考下2024-03-03
kNN算法python實(shí)現(xiàn)和簡(jiǎn)單數(shù)字識(shí)別的方法
這篇文章主要介紹了kNN算法python實(shí)現(xiàn)和簡(jiǎn)單數(shù)字識(shí)別的方法,詳細(xì)講述了kNN算法的優(yōu)缺點(diǎn)及原理,并給出了應(yīng)用實(shí)例,需要的朋友可以參考下2014-11-11
詳解Python如何實(shí)現(xiàn)查看WiFi密碼
這篇文章主要為大家詳細(xì)介紹了如何使用python來試試看看能不能讀取到已連接過WIFI的密碼,文中的示例代碼講解詳細(xì),?感興趣的小伙伴可以了解下2023-11-11
安裝出現(xiàn):Requirement?already?satisfied解決辦法
最近pip install的時(shí)候報(bào)錯(cuò),一大串Requirement already satisfied,所以下面這篇文章主要給大家介紹了關(guān)于安裝出現(xiàn):Requirement?already?satisfied的解決辦法,需要的朋友可以參考下2022-08-08
OpenCV(python)版實(shí)現(xiàn)文本分割之水平投影法
本文主要介紹了OpenCV(python)版實(shí)現(xiàn)文本分割之水平投影法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08

