您好,登錄后才能下訂單哦!
今天小編給大家分享的是用python實(shí)現(xiàn)圣誕樹的代碼,相信很多人都不太了解,為了讓大家更加了解python實(shí)現(xiàn)圣誕樹的代碼,所以給大家總結(jié)了以下內(nèi)容,一起往下看吧。一定會(huì)有所收獲的哦。
python圣誕樹代碼
1、簡單的繪制圣誕樹
新建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繪制簡單圣誕樹
新建tree2py,輸入以下代碼
#導(dǎo)入turtle庫 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)入所依賴的庫 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)行:
關(guān)于用python實(shí)現(xiàn)圣誕樹的代碼就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的參考價(jià)值,可以學(xué)以致用。如果喜歡本篇文章,不妨把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。