
馬上圣誕節了,一個人的圣誕節可能會有些孤獨,教你用代碼寫一棵超級治愈的圣誕樹,話不多說,安排!!!
【結尾有彩蛋?】
基本思路如下:
- 用 Python 畫一棵圣誕樹作為背景圖
- 在圣誕樹背景圖中添加雪落效果及音樂
下面來看一下具體實作,
首先,我們來畫一棵圣誕樹,主要用到的 Python 庫為 turtle,主要代碼實作如下:
n = 80.0
turtle.setup(700, 700, 0, 0)
turtle.speed("fastest")
turtle.screensize(bg='black')
turtle.left(90)
turtle.forward(3 * n)
turtle.color("orange", "yellow")
turtle.begin_fill()
turtle.left(126)
for i in range(5):
turtle.forward(n / 5)
turtle.right(144)
turtle.forward(n / 5)
turtle.left(72)
turtle.end_fill()
turtle.right(126)
turtle.color("dark green")
turtle.backward(n * 4.8)
def tree(d, s):
if d <= 0: return
turtle.forward(s)
tree(d - 1, s * .8)
turtle.right(120)
tree(d - 3, s * .5)
turtle.right(120)
tree(d - 3, s * .5)
turtle.right(120)
turtle.backward(s)
tree(15, n)
turtle.backward(n / 2)
for i in range(200):
a = 200 - 400 * random.random()
b = 10 - 20 * random.random()
turtle.up()
turtle.forward(b)
turtle.left(90)
turtle.forward(a)
turtle.down()
if random.randint(0, 1) == 0:
turtle.color('tomato')
else:
turtle.color('wheat')
turtle.circle(2)
turtle.up()
turtle.backward(a)
turtle.right(90)
turtle.backward(b)
time.sleep(60)
看一下效果:

接著將圣誕樹作為背景圖添加雪落效果及音樂,主要用到的 Python 庫為 pygame,主要代碼實作如下:
# 初始化 pygame
pygame.init()
#設定螢屏寬高,根據背景圖調整
bg_img = "bg.png"
bg_size = (609, 601)
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption("雪夜圣誕樹")
bg = pygame.image.load(bg_img)
# 雪花串列
snow_list = []
for i in range(150):
x_site = random.randrange(0, bg_size[0]) # 雪花圓心位置
y_site = random.randrange(0, bg_size[1]) # 雪花圓心位置
X_shift = random.randint(-1, 1) # x 軸偏移量
radius = random.randint(4, 6) # 半徑和 y 周下降量
snow_list.append([x_site, y_site, X_shift, radius])
# 創建時鐘物件
clock = pygame.time.Clock()
# 添加音樂
track = pygame.mixer.music.load('my.mp3') # 加載音樂檔案
pygame.mixer.music.play() # 播放音樂流
pygame.mixer.music.fadeout(600000) # 設定音樂結束時間
done = False
while not done:
# 訊息事件回圈,判斷退出
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
screen.blit(bg, (0, 0))
# 雪花串列回圈
for i in range(len(snow_list)):
# 繪制雪花,顏色、位置、大小
pygame.draw.circle(screen, (255, 255, 255), snow_list[i][:2], snow_list[i][3] - 3)
# 移動雪花位置(下一次回圈起效)
snow_list[i][0] += snow_list[i][2]
snow_list[i][1] += snow_list[i][3]
# 如果雪花落出螢屏,重設位置
if snow_list[i][1] > bg_size[1]:
snow_list[i][1] = random.randrange(-50, -10)
snow_list[i][0] = random.randrange(0, bg_size[0])
# 重繪螢屏
pygame.display.flip()
clock.tick(30)
# 退出
pygame.quit()
看一下最終效果:

這里就不放視頻了,大家如果想聽一下音樂效果可以自己執行一下,
源代碼已經打包好了,寫個學習,就能拿走,
👉Python學習路線匯總👈
Python所有方向的技術點做的整理,形成各個領域的知識點匯總,它的用處就在于,你可以按照上面的知識點去找對應的學習資源,保證自己學得較為全面,

溫馨提示:篇幅有限,已打包檔案夾,獲取方式在:文末
👉Python必備開發工具👈

👉精品Python學習書籍👈
當我學到一定基礎,有自己的理解能力的時候,會去閱讀一些前輩整理的書籍或者手寫的筆記資料,這些筆記詳細記載了他們對一些技術點的理解,這些理解是比較獨到,可以學到不一樣的思路

👉Python學習視頻600合集👈
觀看零基礎學習視頻,看視頻學習是最快捷也是最有效果的方式,跟著視頻中老師的思路,從基礎到深入,還是很容易入門的,


👉實戰案例👈
光學理論是沒用的,要學會跟著一起敲,要動手實操,才能將自己的所學運用到實際當中去,這時候可以搞點實戰案例來學習,

👉100道Python練習題👈
檢查學習結果,

👉面試刷題👈

學習路線

這份完整版的Python全套學習資料已經上傳CSDN,朋友們如果需要可以微信掃描下方CSDN官方認證二維碼【免費獲取】

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/382884.html
標籤:python
