前言
今天就是平安夜,明天就是圣诞节了,先祝大家节日快乐。 网上有很多版本的圣诞树,内卷的很严重,当然我也不想和他们卷,我只想画属于我和她的。
废话不多说,上效果:
开发环境
- Python3.8
- turtle
- pygame
- tkinter
步骤一:加载bgm所念皆星河
这里我把MP3格式的音频转成了WAV格式,不知为何打包后MP3格式会报错
def music():
pygame.mixer.init()
track=pygame.mixer.music.load("star.wav")
pygame.mixer.music.play()
步骤二:窗口界面初始化
def window_init():
root=Tk()
root.title("点我有惊喜")
root.geometry("400x400")
root.resizable(False,False)
ziti=font.Font(family='微软雅黑',size=10,weight=font.BOLD)
mylabel=Label(root,text="请点击按钮有惊喜等着你",font=ziti)
mylabel.place(x=1,y=1,anchor=CENTER)
create=Button(root,text='惊喜按钮',command=reply,bg="green")
create.place(relx=0.5,rely=0.6,anchor=CENTER,width=100)
mylabel.pack()
root.mainloop()
步骤三:turtle初始化
def draw_init():
turtle.screensize(800,600, "white")
turtle.title("给xx的圣诞惊喜")
turtle.setup(0.5,0.75)
turtle.pensize(5)
turtle.speed(8)
turtle.bgcolor("white")
turtle.bgpic("snow.gif")
turtle.getscreen().colormode(255)
turtle.up()
turtle.goto(100,-100)
turtle.down()
turtle.color("red","green")
步骤四:画树的外轮廓
def draw_tree():
turtle.begin_fill()
for i in range(5):
turtle.forward(90 - i * 10)
turtle.left(145)
turtle.forward(130 - i * 10)
turtle.right(145)
turtle.right(145)
for j in range(5):
turtle.forward(90 + j * 10)
turtle.left(145)
turtle.forward(50 + j * 10)
turtle.right(145)
turtle.left(145)
turtle.forward(65)
turtle.right(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(60)
turtle.left(90)
turtle.forward(100)
turtle.right(90)
turtle.forward(65)
turtle.end_fill()
步骤五:画雪花
def draw_snow(num):
for a in range(num):
x = random.randint(-400,400)
y = random.randint(-300,300)
turtle.up()
turtle.goto(x,y)
turtle.down()
turtle.dot(10,"white")
步骤六:画花朵
def snow(snow_count):
turtle.hideturtle()
turtle.speed(300)
turtle.pensize(3)
for i in range(snow_count):
r = random.randint(0,255)
g = random.randint(0,255)
b = random.randint(0,255)
turtle.pencolor(r, g, b)
turtle.pu()
turtle.goto(random.randint(-350, 350), random.randint(-300, 300))
turtle.pd()
dens = random.randint(6, 12)
snowsize = random.randint(4, 12)
for _ in range(dens):
turtle.forward(snowsize)
turtle.backward(snowsize)
turtle.right(360 / dens)
步骤七:画五角星
def draw_star():
turtle.up()
turtle.goto(-25.53,240.47)
turtle.down()
turtle.color("yellow","yellow")
turtle.begin_fill()
for c in range(5):
turtle.forward(50)
turtle.right(144)
if abs(turtle.pos()) < 1:
break
turtle.end_fill()
开源
谢谢大家支持
https://github.com/windwithling/chirstmas-tree/tree/main
|