import turtle as t
import tkinter as tk
import pygame
import random as r
import threading
import time
# 初始化背景音乐
def initialize_music():
file = r"./music/周杰伦-蜗牛.mp3"
pygame.mixer.init()
pygame.mixer.music.load(file)
pygame.mixer.music.play()
# 画圣诞树
def draw_christmas_tree():
n = 100.0
t.pensize(10)
t.speed("fastest")
t.screensize(888, 700, "black")
t.left(90)
t.forward(3 * n)
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(72)
t.end_fill()
t.right(126)
t.color("dark green")
t.backward(n * 4.8)
def draw_light():
if r.randint(0, 30) == 0:
t.color('tomato')
t.circle(6)
elif r.randint(0, 30) == 1:
t.color('orange')
t.circle(3)
else:
t.width(5)
t.color('dark green')
def draw_tree(d, s):
if d <= 0:
return
t.forward(s)
draw_tree(d - 1, s * .8)
t.right(120)
draw_tree(d - 3, s * .5)
draw_light()
t.right(120)
draw_tree(d - 3, s * .5)
t.right(120)
t.backward(s)
draw_tree(15, n)
t.backward(n / 2)
# 圣诞树下的装饰
def draw_decorations():
def zs():
for i in range(200):
a = 200 - 400 * r.random()
b = 10 - 20 * r.random()
t.up()
t.forward(b)
t.left(90)
t.forward(a)
t.down()
if r.randint(0, 1) == 0:
t.color('tomato')
else:
t.color('wheat')
t.circle(2)
t.up()
t.backward(a)
t.right(90)
t.backward(b)
zs()
# 画雪花
def draw_snowflakes():
t.hideturtle()
t.pensize(2)
for i in range(200):
t.pencolor("white")
t.penup()
t.setx(r.randint(-350, 350))
t.sety(r.randint(-100, 350))
t.pendown()
dens = 6
snowsize = r.randint(1, 10)
for j in range(dens):
t.forward(int(snowsize))
t.backward(int(snowsize))
t.right(int(360 / dens))
# 创建弹窗
def create_popup():
window = tk.Tk()
width = window.winfo_screenwidth()
height = window.winfo_screenheight()
a = r.randrange(0, width)
b = r.randrange(0, height)
window.title('圣诞节快乐')
window.geometry("200x50" + "+" + str(a) + "+" + str(b))
tk.Label(window,
text='圣诞节快乐!',
bg='pink',
font=('Comic Sans MS', 17),
width=18, height=2).pack()
window.mainloop()
# 主函数
def main():
initialize_music()
draw_christmas_tree()
draw_decorations()
draw_snowflakes()
# 创建线程池,管理弹窗线程
pool = []
for i in range(100):
thread = threading.Thread(target=create_popup)
pool.append(thread)
thread.start()
time.sleep(0.01)
for thread in pool:
thread.join()
if __name__ == "__main__":
main()
还没有评论,来说两句吧...