wordpress 查看訪客站長工具seo綜合查詢源碼
文章目錄
- ??Python制作圣誕節(jié)賀卡
- 🐬展示效果
- 🌸代碼
- 🌴代碼剖析
- ??Python制作圣誕樹賀卡
- 🐬展示效果
- 🌸代碼
- 🌴代碼剖析
- 🌸總結(jié)
- 🎅圣誕節(jié)快樂!
??Python制作圣誕節(jié)賀卡
🐬展示效果
🌸代碼
import turtle
import random
import time
width = 600
height = 500
window = turtle.Screen()
window.setup(width, height)
window.bgcolor("sky blue")
window.title("Merry Christmas")
snowball_rate = 1, 3
snowball_size = 5, 15
wind_change = 1, 5
max_wind = 3
# Create all circle-shaped objectsdef make_circle(turtle_name, x, y, size, colour):turtle_name.color(colour)turtle_name.penup()turtle_name.setposition(x, y)turtle_name.dot(size)# Create new snowballs and store in list
list_of_snowballs = []def make_snowball():snowball = turtle.Turtle()snowball.color("white")snowball.penup()snowball.setposition(random.randint(-2*width, width/2), height/2)snowball.hideturtle()snowball.size = random.randint(*snowball_size)list_of_snowballs.append(snowball)def move_snowball(turtle_name, falling_speed=1, wind=0):turtle_name.clear()turtle_name.sety(turtle_name.ycor() - falling_speed)if wind:turtle_name.setx(turtle_name.xcor() + wind)turtle_name.dot(turtle_name.size)# Snowman: body
snowman = turtle.Turtle()
x_position = 0
y_positions = 75, 0, -100
size = 75
for y in y_positions:make_circle(snowman, x_position, y, size, "white")size = size * 1.5
# Snowman: buttons
button_seperation = 25
button_y_positions = [y_positions[1]-button_seperation,y_positions[1],y_positions[1]+button_seperation]
for y in button_y_positions:make_circle(snowman, x_position, y, 10, "black")
# Snowman: eyes
y_offset = 10
x_seperation = 15
for x in x_position-x_seperation, x_position+x_seperation:make_circle(snowman, x, y_positions[0] + y_offset, 20, "green")make_circle(snowman, x, y_positions[0] + y_offset, 10, "black")
# Snowman: nose
snowman.color("orange")
snowman.setposition(x_position - 10, y_positions[0]-y_offset)
snowman.shape("triangle")
snowman.setheading(200)
snowman.turtlesize(0.5, 2.5)
window.tracer(0)
# Ground
grass = turtle.Turtle()
grass.fillcolor("forest green")
grass.penup()
grass.setposition(-width/2, -height/2)
grass.begin_fill()
for _ in range(2):grass.forward(width)grass.left(90)grass.forward(70)grass.left(90)
grass.end_fill()
ground = turtle.Turtle()
for x in range(int(-width/2), int(width/2), int(width/200)):make_circle(ground, x, -180, random.randint(5, 20), "white")
text = turtle.Turtle()
text.color("red")
text.penup()
text.setposition(-100, 170)
text.write("Merry Christmas", font=("Apple Chancery", 30, "bold"), align="center")
text.setposition(130, 140)
text.color("dark green")
text.write("Dao", font=("Avenir", 30, "bold"), align="right")
text.color("black")
text.write("Bi", font=("Avenir", 30, "normal"), align="left")
text.setx(50)
text.write("from", font=("Apple Chancery", 20, "bold"), align="right")
text.hideturtle()
time_delay = 0
start_time = time.time()
wind = 0
wind_delay = 5
wind_timer = time.time()
wind_step = 0.1
while True:if time.time() - start_time > time_delay:make_snowball()start_time = time.time()time_delay = random.randint(*snowball_rate)/10for snowball in list_of_snowballs:move_snowball(snowball, wind=wind)if snowball.ycor() < -height/2:snowball.clear()list_of_snowballs.remove(snowball)if time.time() - wind_timer > wind_delay:wind += wind_stepif wind >= max_wind:wind_step = -wind_stepelif wind <= 0:wind_step = abs(wind_step)wind_timer = time.time()wind_delay = random.randint(*wind_change)/10window.update()
turtle.done()
🌴代碼剖析
這段Python代碼使用Turtle圖形庫創(chuàng)建了一個簡單的動畫,包括下雪、雪人和圣誕主題的風(fēng)景。以下是對代碼的逐步分析:
-
導(dǎo)入庫:
import turtle import random import time
turtle
:提供對象導(dǎo)向和過程導(dǎo)向的海龜圖形基元。random
:生成隨機(jī)數(shù)。time
:提供各種與時間相關(guān)的函數(shù)。
-
設(shè)置Turtle窗口:
width = 600 height = 500 window = turtle.Screen() window.setup(width, height) window.bgcolor("sky blue") window.title("Merry Christmas")
- 設(shè)置一個具有指定寬度、高度、背景顏色和標(biāo)題的窗口。
-
定義雪球參數(shù):
snowball_rate = 1, 3 snowball_size = 5, 15 wind_change = 1, 5 max_wind = 3
- 用于控制降雪速率、雪球大小、風(fēng)力變化和最大風(fēng)速的參數(shù)。
-
定義函數(shù):
make_circle
:在指定位置創(chuàng)建一個圓形對象。make_snowball
:創(chuàng)建一個新的雪球海龜并將其添加到列表中。move_snowball
:移動雪球海龜,根據(jù)下降速度和風(fēng)力更新其位置。
-
創(chuàng)建雪人:
snowman = turtle.Turtle() x_position = 0 y_positions = 75, 0, -100 size = 75
- 使用圓形表示雪人的身體、按鈕、眼睛,以及三角形表示鼻子。
-
創(chuàng)建地面:
grass = turtle.Turtle() grass.fillcolor("forest green") grass.penup() grass.setposition(-width/2, -height/2) grass.begin_fill() for _ in range(2):grass.forward(width)grass.left(90)grass.forward(70)grass.left(90) grass.end_fill()
- 創(chuàng)建一個綠色的地面以及一些表示雪花的白色圓圈。
-
創(chuàng)建文本:
text = turtle.Turtle() text.color("red") text.penup() text.setposition(-100, 170) text.write("Merry Christmas", font=("Apple Chancery", 30, "bold"), align="center")
- 創(chuàng)建顯示"圣誕快樂"的文本。
-
設(shè)置時間和風(fēng):
time_delay = 0 start_time = time.time() wind = 0 wind_delay = 5 wind_timer = time.time() wind_step = 0.1
- 設(shè)置時間和風(fēng)力的參數(shù)。
-
主循環(huán):
while True:# 生成雪球if time.time() - start_time > time_delay:make_snowball()start_time = time.time()time_delay = random.randint(*snowball_rate)/10# 移動雪球for snowball in list_of_snowballs:move_snowball(snowball, wind=wind)if snowball.ycor() < -height/2:snowball.clear()list_of_snowballs.remove(snowball)# 更新風(fēng)力if time.time() - wind_timer > wind_delay:wind += wind_stepif wind >= max_wind:wind_step = -wind_stepelif wind <= 0:wind_step = abs(wind_step)wind_timer = time.time()wind_delay = random.randint(*wind_change)/10window.update()
- 在主循環(huán)中,不斷生成雪球、移動雪球,以及更新風(fēng)力。
這段代碼通過使用Turtle圖形庫,創(chuàng)建了一個簡單而有趣的圣誕主題動畫。以下是我的一些心得:
-
Turtle圖形庫: Turtle是一個很好的用于學(xué)習(xí)編程和圖形設(shè)計的工具。它提供了簡單易懂的API,適合初學(xué)者入門,同時也可以創(chuàng)建出具有趣味性的圖形。
-
動畫設(shè)計: 通過循環(huán)和時間控制,代碼實(shí)現(xiàn)了雪球的不斷下落、雪人的繪制,以及風(fēng)力的隨機(jī)變化。這種動態(tài)的設(shè)計增加了視覺上的趣味性。
-
可讀性良好: 代碼的結(jié)構(gòu)清晰,函數(shù)的命名恰當(dāng),使得整個代碼可讀性很好。這對于理解代碼和后續(xù)維護(hù)非常重要。
-
創(chuàng)意表達(dá): 通過在屏幕上創(chuàng)建雪人、雪球和圣誕主題的背景,代碼成功地表達(dá)了圣誕節(jié)的氛圍,同時還添加了一些有趣的元素,比如風(fēng)力的變化。
-
實(shí)踐編程技能: 通過編寫和理解這段代碼,可以學(xué)到關(guān)于隨機(jī)數(shù)生成、時間控制、動畫設(shè)計等方面的編程技能。這對于培養(yǎng)實(shí)際的編程經(jīng)驗(yàn)很有幫助。
總體而言,這段代碼是一個有趣且教育性的例子,展示了如何使用Python和Turtle圖形庫創(chuàng)建簡單的動畫。
??Python制作圣誕樹賀卡
🐬展示效果
🌸代碼
# -*- coding: utf-8 -*-
import turtle as T
import random
import time# 繪圖區(qū)域
t = T.Turtle()
# 畫布大小
w = T.Screen()
t.hideturtle() # 隱藏畫筆
t.getscreen().tracer(5, 0)
w.screensize(bg='maroon')
t.left(90)
t.up()
t.forward(280)
t.down()
t.pensize(3)# 畫五角星
n=100
t.color("orange","yellow")
t.begin_fill()
t.left(126)for i in range(5):t.forward(n/5)t.right(144)t.forward(n/5)t.left(71)
t.end_fill()
t.left(60)
t.pensize(8)
t.forward(60)
t.right(20)
t.right(116)
t.pensize(6)# 畫樹冠
t.color('dark green')
n=130for i in range(6):time.sleep(0.5)a=1+i/2t.begin_fill()t.left(90)t.forward(n*a*0.707)t.left(135)t.forward(n*a)t.left(135)t.forward(n*a*0.707)t.end_fill()t.up()t.left(90)t.forward(n*a*0.707/3)t.left(135)t.forward(n*a/6)t.left(135)t.down()# 畫樹干
t.up()
t.right(135)
t.forward(30)
t.right(90)
t.forward(157)
t.down()
t.color('saddlebrown')
t.begin_fill()
t.forward(80)
t.right(90)
t.forward(45)
t.right(90)
t.forward(80)
t.right(90)
t.forward(45)
t.end_fill()t.up()
t.backward(45)
t.right(90)
t.backward(470)
t.down()# 畫燈
def light(l,t):t.pensize(3)colors = ["magenta","darkorange","red","blue"]for i in range(l):time.sleep(0.2)b = 70+16*ia = b/2*random.randint(-100,100)/100t.up()t.forward(b)t.left(90)t.forward(a)t.down()t.color("lightyellow",colors[i%4])t.begin_fill()t.circle(10)t.end_fill()t.up()t.backward(a)t.right(90)t.backward(b)t.down()t.pensize(1)# 畫雪花
def snow(m,t):for i in range(m):a = 400 - 800 * random.random()b = 600 - 800 * random.random()t.up()t.forward(b)t.left(90)t.forward(a)t.down()t.color('white')t.begin_fill()t.circle(1)t.end_fill()t.up()t.backward(a)t.right(90)t.backward(b)light(24,t)
snow(600, t)# 文字
t.goto(-200,200)
my_word = ("Merry Christmas")
t.write(my_word,font=("Edwardian Script ITC",40,"bold"))
time.sleep(0.3)
t.goto(-100,50)
my_word = ("and")
t.write(my_word,font=("Edwardian Script ITC",50,"bold"))
time.sleep(0.3)
t.goto(-150,-100)
my_word = ("Happy New Year")
t.write(my_word,font=("Edwardian Script ITC",40,"bold"))time.sleep(0.3)
t.clear()
w.screensize(bg='black')
t.goto(-200,0)
my_word = ("Prudued by: Justine")
t.write(my_word,font=("Edwardian Script ITC",45,"bold"))
t.goto(0,-100)
my_word = ("Dec. 24th, 2019")
t.write(my_word,font=("Edwardian Script ITC",20,"bold"))time.sleep(5)
🌴代碼剖析
這段代碼使用Python的Turtle圖形庫繪制了一個圣誕樹、五角星、燈光、雪花以及一些文字,并在最后展示了制作的日期和制作者信息。以下是對代碼的簡要分析:
-
設(shè)置繪圖環(huán)境:
t = T.Turtle() w = T.Screen() t.hideturtle() t.getscreen().tracer(5, 0) w.screensize(bg='maroon')
- 創(chuàng)建Turtle對象和Screen對象,設(shè)置畫筆屬性和背景顏色。
-
繪制五角星:
n = 100 t.color("orange", "yellow") t.begin_fill() t.left(126) for i in range(5):t.forward(n / 5)t.right(144)t.forward(n / 5)t.left(71) t.end_fill()
- 使用Turtle繪制一個填充的五角星。
-
繪制圣誕樹的樹冠:
t.color('dark green') n = 130 for i in range(6):# 逐層繪制樹冠
- 循環(huán)繪制樹冠的層次,根據(jù)層次逐漸增大,繪制成樹狀。
-
繪制圣誕樹的樹干:
t.up() t.right(135) t.forward(30) t.right(90) t.forward(157) t.down() t.color('saddlebrown') t.begin_fill() t.forward(80) t.right(90) t.forward(45) t.right(90) t.forward(80) t.right(90) t.forward(45) t.end_fill()
- 繪制樹干。
-
繪制燈光:
def light(l, t):# 繪制一串燈光
- 定義了一個函數(shù),用于繪制一串燈光,每個燈光的顏色隨機(jī)。
-
繪制雪花:
def snow(m, t):# 繪制雪花
- 定義了一個函數(shù),用于繪制雪花,雪花的位置和數(shù)量隨機(jī)。
-
展示文字信息:
t.goto(-200, 200) my_word = ("Merry Christmas") t.write(my_word, font=("Edwardian Script ITC", 40, "bold"))
- 在特定位置展示文字,包括"Merry Christmas"和"Happy New Year"等。
-
展示制作信息:
t.goto(-200, 0) my_word = ("Produced by: Justine") t.write(my_word, font=("Edwardian Script ITC", 45, "bold"))
- 在屏幕中央展示代碼的制作者信息和日期。
-
等待5秒后清空畫布:
time.sleep(5) t.clear() w.screensize(bg='black')
- 在展示信息后等待5秒鐘,然后清空畫布并更改背景顏色。
-
代碼注釋: 代碼中添加了注釋,使得代碼更易讀、易理解。
這段代碼通過Turtle圖形庫展示了一個簡單而有趣的圣誕主題圖形,并且通過文字展示了制作者信息和日期。
這段代碼使用Turtle圖形庫繪制了一個富有圣誕氛圍的圖形,包括五角星、圣誕樹、燈光和雪花等元素。以下是我的心得:
-
創(chuàng)意豐富: 代碼展示了作者對于圣誕節(jié)的創(chuàng)意和想象力。通過使用Turtle繪制五角星和樹形,以及添加燈光和雪花,使整個圖形充滿了節(jié)日的氛圍。
-
圖形效果: 通過合理選擇顏色、形狀和繪制順序,成功地繪制出了富有立體感的圣誕樹,五角星的設(shè)計也很有特色。同時,通過隨機(jī)顏色的燈光和雪花,增加了視覺上的多樣性。
-
動態(tài)效果: 使用Turtle的
tracer
函數(shù)以及time.sleep
函數(shù),使得圖形的繪制過程呈現(xiàn)出一定的動態(tài)效果。例如,逐層繪制圣誕樹的樹冠,以及依次點(diǎn)亮的燈光,都使圖形看起來更生動。 -
字體選擇: 通過選擇不同的字體(“Edwardian Script ITC”),成功地展示了"Merry Christmas"和"Happy New Year"等文本,使得圖形更具節(jié)日氛圍。
-
清晰結(jié)構(gòu): 代碼結(jié)構(gòu)相對清晰,模塊化程度較高,使用函數(shù)對不同元素進(jìn)行繪制,提高了代碼的可讀性和可維護(hù)性。
-
創(chuàng)作者信息: 最后展示了制作者信息和日期,這是一個很好的方式來為自己的作品留下標(biāo)記,并向其他人展示制作者的身份。
總體而言,這段代碼不僅僅是一個簡單的圖形繪制,更是通過細(xì)致的設(shè)計和創(chuàng)意,成功地表達(dá)了圣誕節(jié)的歡樂氛圍。同時,展示了Turtle圖形庫的靈活性和簡便性。
🌸總結(jié)
這段代碼通過使用Python的Turtle圖形庫,成功繪制了一個富有創(chuàng)意和節(jié)日氛圍的圣誕主題圖形。以下是對整體代碼的總結(jié):
-
圖形元素:
- 通過Turtle繪制了五角星、圣誕樹、燈光、雪花等圖形元素,展現(xiàn)了濃厚的圣誕節(jié)氛圍。
- 使用
tracer
函數(shù)和time.sleep
函數(shù)創(chuàng)造了動態(tài)效果,使得圖形呈現(xiàn)出逐步繪制的過程。
-
色彩和形狀:
- 巧妙選擇了顏色,如樹冠的深綠色、燈光的不同隨機(jī)顏色,以及雪花的白色,增添了視覺上的多樣性。
- 圣誕樹的形狀通過逐層繪制和旋轉(zhuǎn),成功表現(xiàn)出樹狀結(jié)構(gòu),五角星的設(shè)計獨(dú)特。
-
函數(shù)的使用:
- 合理使用函數(shù)對不同元素進(jìn)行繪制,提高了代碼的模塊化和可讀性。
- 分別封裝了繪制燈光和雪花的函數(shù),增加了代碼的結(jié)構(gòu)清晰度。
-
文本展示:
- 使用了不同的字體和字號,成功地展示了"Merry Christmas"和"Happy New Year"等文本信息,使圖形更具節(jié)日氛圍。
- 在最后展示了制作者信息和日期,為作品留下了標(biāo)記。
-
動態(tài)效果:
- 通過適度的延時和清空畫布的操作,使得整體圖形呈現(xiàn)出動態(tài)的效果,使觀賞者能夠更好地感受到節(jié)日的歡樂氛圍。
-
代碼清晰度:
- 代碼結(jié)構(gòu)相對清晰,注釋也有助于理解,提高了代碼的可讀性。
- 采用了適當(dāng)?shù)目s進(jìn)和空行,使得代碼布局整潔。
總的來說,這段代碼成功地通過簡單的圖形繪制展現(xiàn)了圣誕節(jié)的歡快氛圍,結(jié)合了創(chuàng)意、色彩和動態(tài)效果,展示了Turtle圖形庫的靈活性和便捷性。
🎅圣誕節(jié)快樂!
圣誕節(jié)快樂!愿你的圣誕充滿溫馨和喜悅,與親朋好友共度美好時光。愿你在新的一年里充滿幸福、健康和成功。祝福你和你的家人,平安喜樂!🎄🎅🎁