如何用Python繪制簡(jiǎn)易動(dòng)態(tài)圣誕樹
代碼:
import random
import time
from math import pi, cos, sin
from tkinter import *
CANVAS_WIDTH = 640 # 畫布的寬
CANVAS_HEIGHT = 640 # 畫布的高
TREE_COLOR = "#2e8b57" # 樹的顏色
TRUNK_COLOR = "#8b4513" # 樹干的顏色
STAR_COLOR = "#ffff00" # 星星的顏色
LIGHT_COLOR = "#ff4500" # 燈光的顏色
BACKGROUND_COLOR = "#000000" # 背景顏色
TEXT_COLOR = "#ffffff" # 文字顏色
TEXT_FONT = ('Arial', 24, 'bold') # 文字的字體
# 圣誕樹的大小和位置
TREE_BOTTOM = CANVAS_HEIGHT - 50
TREE_TOP = TREE_BOTTOM - 200
TREE_WIDTH = 200
TRUNK_WIDTH = 40
TRUNK_HEIGHT = 50
def draw_tree(canvas):
# 樹的底部三角形部分
canvas.create_polygon(
CANVAS_WIDTH // 2, TREE_TOP, # 頂部
CANVAS_WIDTH // 2 - TREE_WIDTH, TREE_BOTTOM, # 左下角
CANVAS_WIDTH // 2 + TREE_WIDTH, TREE_BOTTOM, # 右下角
fill=TREE_COLOR, outline="green", width=2
)
# 樹的中部三角形部分
canvas.create_polygon(
CANVAS_WIDTH // 2, TREE_TOP + 40, # 頂部
CANVAS_WIDTH // 2 - TREE_WIDTH * 0.8, TREE_BOTTOM - 40, # 左下角
CANVAS_WIDTH // 2 + TREE_WIDTH * 0.8, TREE_BOTTOM - 40, # 右下角
fill=TREE_COLOR, outline="green", width=2
)
# 樹的頂部三角形部分
canvas.create_polygon(
CANVAS_WIDTH // 2, TREE_TOP + 80, # 頂部
CANVAS_WIDTH // 2 - TREE_WIDTH * 0.6, TREE_BOTTOM - 80, # 左下角
CANVAS_WIDTH // 2 + TREE_WIDTH * 0.6, TREE_BOTTOM - 80, # 右下角
fill=TREE_COLOR, outline="green", width=2
)
# 樹干
canvas.create_rectangle(
CANVAS_WIDTH // 2 - TRUNK_WIDTH // 2, TREE_BOTTOM, # 左上角
CANVAS_WIDTH // 2 + TRUNK_WIDTH // 2, TREE_BOTTOM + TRUNK_HEIGHT, # 右下角
fill=TRUNK_COLOR, outline=TRUNK_COLOR
)
def draw_star(canvas, x, y, size):
# 繪制一個(gè)星星
canvas.create_oval(
x - size, y - size, x + size, y + size, # 左上角和右下角
fill=STAR_COLOR, outline=STAR_COLOR
)
def draw_lights(canvas, x, y, size, color):
# 繪制一顆燈泡
canvas.create_oval(
x - size, y - size, x + size, y + size, # 左上角和右下角
fill=color, outline=color
)
def draw_dynamic_star(canvas, frame):
# 讓星星閃爍
if frame % 30 < 15:
draw_star(canvas, CANVAS_WIDTH // 2, TREE_TOP - 40, 15)
def draw_dynamic_lights(canvas, frame):
# 動(dòng)態(tài)燈泡
num_lights = 15
for i in range(num_lights):
angle = 2 * pi * i / num_lights # 每個(gè)燈泡的位置角度
x = CANVAS_WIDTH // 2 + (TREE_WIDTH * 0.6) * cos(angle)
y = TREE_TOP + 80 + (TREE_WIDTH * 0.6) * sin(angle)
# 每個(gè)燈泡隨機(jī)閃爍
color = LIGHT_COLOR if random.random() > 0.5 else "black"
draw_lights(canvas, x, y, 5, color)
def draw_text(canvas):
# 顯示圣誕祝福
canvas.create_text(CANVAS_WIDTH // 2, CANVAS_HEIGHT - 20, text="Merry Christmas!", fill=TEXT_COLOR, font=TEXT_FONT)
def draw_dynamic_tree(main, canvas, frame):
# 清空畫布
canvas.delete('all')
# 繪制圣誕樹
draw_tree(canvas)
# 繪制閃爍的星星
draw_dynamic_star(canvas, frame)
# 繪制動(dòng)態(tài)燈光
draw_dynamic_lights(canvas, frame)
# 繪制祝福語(yǔ)
draw_text(canvas)
# 設(shè)置每幀調(diào)用
main.after(100, draw_dynamic_tree, main, canvas, frame + 1)
if __name__ == '__main__':
root = Tk() # 創(chuàng)建一個(gè)Tk對(duì)象
canvas = Canvas(root, bg=BACKGROUND_COLOR, height=CANVAS_HEIGHT, width=CANVAS_WIDTH)
canvas.pack()
# 動(dòng)態(tài)繪制圣誕樹
draw_dynamic_tree(root, canvas, 0)
# 啟動(dòng)應(yīng)用
root.mainloop()
效果:

總結(jié)
到此這篇關(guān)于如何用Python繪制簡(jiǎn)易動(dòng)態(tài)圣誕樹的文章就介紹到這了,更多相關(guān)Python繪制動(dòng)態(tài)圣誕樹內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python爬蟲獲取整個(gè)站點(diǎn)中的所有外部鏈接代碼示例
這篇文章主要介紹了Python爬蟲獲取整個(gè)站點(diǎn)中的所有外部鏈接代碼示例,具有一定借鑒價(jià)值,需要的朋友可以參考下2017-12-12
python獲取當(dāng)前運(yùn)行函數(shù)名稱的方法實(shí)例代碼
這篇文章主要介紹了python獲取當(dāng)前運(yùn)行函數(shù)名稱的方法實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-04-04
Python爬蟲使用實(shí)例wallpaper問(wèn)題記錄
本文介紹解決中文亂碼的方法,以及Python爬蟲處理數(shù)據(jù)、圖片URL的技巧,包括使用正則表達(dá)式處理字符串、URL替換等,還涉及單線程與多線程的應(yīng)用場(chǎng)景,如電腦壁紙和手機(jī)壁紙爬取,適合進(jìn)行Web數(shù)據(jù)抓取和處理的開發(fā)者參考2024-09-09
python中copy()與deepcopy()的區(qū)別小結(jié)
接觸python有一段時(shí)間了,一直沒(méi)有系統(tǒng)的學(xué)習(xí)過(guò),也對(duì)copy,deepcoy傻傻的分不清,故抽出時(shí)間來(lái)理一下。 下面這篇文章主要給大家介紹了關(guān)于python中copy()與deepcopy()的區(qū)別的相關(guān)資料,需要的朋友可以參考下2018-08-08
Python實(shí)現(xiàn)將藍(lán)底照片轉(zhuǎn)化為白底照片功能完整實(shí)例
這篇文章主要介紹了Python實(shí)現(xiàn)將藍(lán)底照片轉(zhuǎn)化為白底照片功能,結(jié)合完整實(shí)例形式分析了Python基于cv2庫(kù)進(jìn)行圖形轉(zhuǎn)換操作的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-12-12

