尝试用turtle画圣诞树
- 前言
- 开发环境
- 步骤一:加载bgm所念皆星河
- 步骤二:窗口界面初始化
- 步骤三:turtle初始化
- 步骤四:画树的外轮廓
- 步骤五:画雪花
- 步骤六:画花朵
- 步骤七:画五角星
- 开源
前言
今天就是平安夜,明天就是圣诞节了,先祝大家节日快乐。
网上有很多版本的圣诞树,内卷的很严重,当然我也不想和他们卷,我只想画属于我和她的。
废话不多说,上效果:
只想画属于我和她的圣诞树
开发环境
- 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()#调用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()#将Label添加到窗口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)#print(turtle.pos())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:#看画笔是否回到原点,回到原点为真breakturtle.end_fill()
开源
谢谢大家支持
https://github.com/windwithling/chirstmas-tree/tree/main