AI智能
改变未来

用python画圣诞树、樱花树、卡通图案及打包成exe文件

文章目录

  • 用python画圣诞树、樱花树、卡通图案及打包成exe文件
  • 效果
  • 1、圣诞树–朴素
  • 2、圣诞树–可爱
  • 3、圣诞树–飘雪
  • 4、樱花树–飘落效果
  • 5、樱花树–暖色调
  • 6、哆啦a梦

用python画圣诞树、樱花树、卡通图案及打包成exe文件

如何将python代码生成exe文件,直接在桌面运行请参考文章:如何将python文件打包成exe格式

效果

1、圣诞树–朴素

import turtle# 定义圣诞树的绿叶函数def tree(d, s):if d <= 0:returnturtle.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)n = 100turtle.speed('fastest') # 设置速度turtle.left(90)turtle.forward(3 * n)turtle.color("orange", "yellow")turtle.left(126)# turtle.begin_fill()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)# 执行函数tree(15, n)turtle.backward(n / 5)

2、圣诞树–可爱

from turtle import *import timesetup(500, 500, startx=None, starty=None)speed(0)pencolor("pink")pensize(10)penup()hideturtle()goto(0, 150)showturtle()pendown()shape(name="classic")# 1seth(-120)for i in range(10):fd(12)right(2)penup()goto(0, 150)seth(-60)pendown()for i in range(10):fd(12)left(2)seth(-150)penup()fd(10)pendown()for i in range(5):fd(10)right(15)seth(-150)penup()fd(8)pendown()for i in range(5):fd(10)right(15)seth(-155)penup()fd(5)pendown()for i in range(5):fd(7)right(15)# 2penup()goto(-55, 34)pendown()seth(-120)for i in range(10):fd(8)right(5)penup()goto(50, 35)seth(-60)pendown()for i in range(10):fd(8)left(5)seth(-120)penup()fd(10)seth(-145)pendown()for i in range(5):fd(10)right(15)penup()fd(10)seth(-145)pendown()for i in range(5):fd(12)right(15)penup()fd(8)seth(-145)pendown()for i in range(5):fd(10)right(15)penup()seth(-155)fd(8)pendown()for i in range(5):fd(11)right(15)# 3penup()goto(-100, -40)seth(-120)pendown()for i in range(10):fd(6)right(3)penup()goto(80, -39)seth(-50)pendown()for i in range(10):fd(6)left(3)seth(-155)penup()fd(10)pendown()for i in range(5):fd(8)right(10)penup()fd(8)seth(-145)pendown()for i in range(7):fd(8)right(10)penup()fd(8)seth(-145)pendown()for i in range(7):fd(7)right(10)penup()fd(8)seth(-145)pendown()for i in range(7):fd(7)right(10)penup()fd(8)seth(-140)pendown()for i in range(7):fd(6)right(10)# 4penup()goto(-120, -95)seth(-130)pendown()for i in range(7):fd(10)right(5)penup()goto(100, -95)seth(-50)pendown()for i in range(7):fd(10)left(5)penup()seth(-120)fd(10)seth(-155)pendown()for i in range(6):fd(8)right(10)penup()seth(-160)fd(10)seth(-155)pendown()for i in range(6):fd(8)right(10)penup()seth(-160)fd(10)seth(-155)pendown()for i in range(6):fd(8)right(10)penup()seth(-160)fd(10)seth(-160)pendown()for i in range(6):fd(8)right(10)penup()seth(-160)fd(10)seth(-160)pendown()for i in range(6):fd(8)right(10)penup()seth(-160)fd(10)seth(-165)pendown()for i in range(5):fd(10)right(11)# 5penup()goto(-70, -165)seth(-85)pendown()for i in range(3):fd(5)left(3)penup()goto(70, -165)seth(-95)pendown()for i in range(3):fd(5)right(3)seth(-170)penup()fd(10)pendown()pendown()for i in range(10):fd(12)right(2)# 6penup()goto(70, -165)pendown()seth(-90)pensize(8)pencolor("#de8891")circle(-20, 90)penup()goto(30, -185)pendown()seth(-180)pensize(8)pencolor("#de8891")fd(40)penup()goto(-5, -170)pendown()seth(-180)pensize(8)pencolor("#de8891")fd(35)def guest(x, y, z):penup()goto(x, y)seth(-z)pendown()for angel in range(5):fd(10)right(10)def guet(x, y, z):penup()goto(x, y)seth(-z)pendown()for angel in range(5):fd(10)left(10)def qu(x, y, z):penup()goto(x, y)seth(-z)pendown()for angel in range(5):fd(6)right(10)seth(-150)fd(20)# 树枝guest(-70, -150, 160)guest(100, -150, 160)guet(110, -110, 50)guest(160, -140, 150)qu(80, -120, 180)guest(70, -85, 165)guest(-40, -85, 165)guet(90, -50, 50)guest(130, -80, 150)pencolor("pink")qu(-40, -60, 180)pencolor('#de8891')qu(80, -30, 180)pencolor("pink")qu(40, 10, 180)pencolor("#de8891")guest(-60, 30, 120)guest(-20, -20, 150)guet(45, 40, 60)guest(-30, 40, 170)guest(-30, 110, 115)guet(40, 90, 60)guest(80, 50, 160)pencolor("#de8891")def hdj(x, y):penup()goto(x, y)seth(80)pendown()pensize(2)circle(5)seth(10)fd(15)seth(120)fd(20)seth(240)fd(20)seth(180)fd(20)seth(-60)fd(20)seth(50)fd(20)seth(-40)fd(30)seth(-130)fd(5)seth(135)fd(30)seth(-60)fd(30)seth(-150)fd(6)seth(110)fd(30)def uit(x, y):penup()goto(x, y)pendown()pensize(2)circle(5)seth(-10)fd(15)seth(90)fd(15)seth(200)fd(15)seth(160)fd(15)seth(-90)fd(15)seth(10)fd(15)seth(-60)fd(20)seth(-180)fd(5)seth(110)fd(20)seth(-90)fd(20)seth(-180)fd(6)seth(70)fd(15)hideturtle()def yut(x, y, z):penup()goto(x, y)pendown()seth(z)for po in range(5):fd(4)left(36)def ytu(x, y, z):penup()goto(x, y)pendown()seth(z)for kk in range(5):fd(4)left(36)# 小蝴蝶结seth(0)uit(40, -160)hdj(-80, -120)yut(-67, -115, 120)yut(-86, -123, 150)hdj(40, -50)yut(52, -45, 130)yut(34, -55, 160)seth(0)uit(-20, -60)ytu(-4, -60, 100)ytu(-20, -60, 120)hdj(-30, 20)yut(-15, 25, 130)yut(-40, 20, 180)uit(30, 70)ytu(45, 70, 100)ytu(30, 70, 120)# 大蝴蝶结pencolor("#f799e6")pensize(5)penup()seth(0)goto(0, 150)pendown()circle(10)seth(-15)fd(40)seth(90)fd(40)seth(200)fd(40)seth(160)fd(40)seth(-90)fd(40)seth(15)fd(40)seth(-70)pencolor("#f799e6")pensize(4)fd(40)seth(-180)fd(10)seth(100)fd(40)seth(-100)fd(40)seth(-180)fd(10)seth(70)fd(40)penup()seth(0)goto(0, 130)pencolor("pink")pendown()def iou(x, y, z):penup()goto(x, y)pencolor("#f799e6")pendown()seth(z)for po in range(10):fd(4)left(18)seth(0)iou(35, 145, 100)iou(-7, 145, 110)pencolor("red")pensize(7)penup()goto(-35, 135)pendown()# 圣诞帽seth(-20)pensize(2)penup()goto(-30, -120)pencolor("black")pendown()fillcolor("red")fd(30)circle(4, 180)fd(30)circle(4, 180)penup()goto(-25, -115)seth(75)pendown()begin_fill()for i in range(5):fd(6)right(20)seth(-10)for i in range(5):fd(8)right(15)seth(145)for i in range(5):fd(5)left(2)seth(90)for i in range(5):fd(1)left(2)seth(-90)for i in range(4):fd(4)right(6)seth(161)fd(30)end_fill()pensize(1)pencolor("black")def koc(x, y, size):pensize(2)pencolor("black")penup()goto(x, y)pendown()begin_fill()fillcolor("yellow")for i in range(5):left(72)fd(size)right(144)fd(size)end_fill()# 星星seth(-15)koc(-120, -70, 10)seth(10)koc(100, -20, 10)seth(-10)koc(10, 40, 10)seth(30)koc(-80, 60, 10)koc(100, -150, 10)koc(-140, -150, 10)koc(20, 120, 10)# 袜子seth(-20)pensize(2)penup()goto(-20, 80)pencolor("black")pendown()fillcolor("red")fd(25)circle(4, 180)fd(25)circle(4, 180)penup()goto(-15, 80)pendown()begin_fill()fillcolor("red")seth(-120)fd(20)seth(150)fd(5)circle(7, 180)fd(15)circle(5, 90)fd(30)seth(160)fd(18)end_fill()penup()seth(0)goto(100, -230)pendown()write("Merry Christmas ", align="center", font=("Comic Sans MS", 24, "bold"))done()

3、圣诞树–飘雪

import turtle as t  # as就是取个别名,后续调用的t都是turtlefrom turtle import *import random as rimport timen = 100.0speed("fastest")  # 定义速度screensize(bg='black')  # 定义背景颜色,可以自己换颜色left(90)forward(3 * n)color("orange", "yellow")  # 定义最上端星星的颜色,外圈是orange,内部是yellowbegin_fill()left(126)for i in range(5):  # 画五角星forward(n / 5)right(144)  # 五角星的角度forward(n / 5)left(72)  # 继续换角度end_fill()right(126)def drawlight():  # 定义画彩灯的方法if r.randint(0, 30) == 0:  # 如果觉得彩灯太多,可以把取值范围加大一些,对应的灯就会少一些color('tomato')  # 定义第一种颜色circle(6)  # 定义彩灯大小elif r.randint(0, 30) == 1:color('orange')  # 定义第二种颜色circle(3)  # 定义彩灯大小else:color('dark green')  # 其余的随机数情况下画空的树枝color("dark green")  # 定义树枝的颜色backward(n * 4.8)def tree(d, s):  # 开始画树if d <= 0: returnforward(s)tree(d - 1, s * .8)right(120)tree(d - 3, s * .5)drawlight()  # 同时调用小彩灯的方法right(120)tree(d - 3, s * .5)right(120)backward(s)tree(15, n)backward(n / 2)for i in range(200):  # 循环画最底端的小装饰a = 200 - 400 * r.random()b = 10 - 20 * r.random()up()forward(b)left(90)forward(a)down()if r.randint(0, 1) == 0:color('tomato')else:color('wheat')circle(2)up()backward(a)right(90)backward(b)t.color("dark red", "red")  # 定义字体颜色t.write("Merry Christmas ", align="center", font=("Comic Sans MS", 40, "bold"))  # 定义文字、位置、字体、大小def drawsnow():  # 定义画雪花的方法t.ht()  # 隐藏笔头,ht=hideturtlet.pensize(2)  # 定义笔头大小for i in range(200):  # 画多少雪花t.pencolor("white")  # 定义画笔颜色为白色,其实就是雪花为白色t.pu()  # 提笔,pu=penupt.setx(r.randint(-350, 350))  # 定义x坐标,随机从-350到350之间选择t.sety(r.randint(-100, 350))  # 定义y坐标,注意雪花一般在地上不会落下,所以不会从太小的纵座轴开始t.pd()  # 落笔,pd=pendowndens = 6  # 雪花瓣数设为6snowsize = r.randint(1, 10)  # 定义雪花大小for j in range(dens):  # 就是6,那就是画5次,也就是一个雪花五角星# t.forward(int(snowsize))  #int()取整数t.fd(int(snowsize))t.backward(int(snowsize))# t.bd(int(snowsize))  #注意没有bd=backward,但有fd=forward,小bugt.right(int(360 / dens))  # 转动角度drawsnow()  # 调用画雪花的方法t.done()  # 完成,否则会直接关闭

4、樱花树–飘落效果

from turtle import *from random import *from math import *def tree(n,l):pd()#下笔#阴影效果t = cos(radians(heading()+45))/8+0.25pencolor(t,t,t)pensize(n/3)forward(l)#画树枝if n>0:b = random()*15+10 #右分支偏转角度c = random()*15+10 #左分支偏转角度d = l*(random()*0.25+0.7) #下一个分支的长度#右转一定角度,画右分支right(b)tree(n-1,d)#左转一定角度,画左分支left(b+c)tree(n-1,d)#转回来right(c)else:#画叶子right(90)n=cos(radians(heading()-45))/4+0.5pencolor(n,n*0.8,n*0.8)circle(3)left(90)#添加0.3倍的飘落叶子if(random()>0.7):pu()#飘落t = heading()an = -40 +random()*40setheading(an)dis = int(800*random()*0.5 + 400*random()*0.3 + 200*random()*0.2)forward(dis)setheading(t)#画叶子pd()right(90)n = cos(radians(heading()-45))/4+0.5pencolor(n*0.5+0.5,0.4+n*0.4,0.4+n*0.4)circle(2)left(90)pu()#返回t=heading()setheading(an)backward(dis)setheading(t)pu()backward(l)#退回bgcolor(0.5,0.5,0.5)#背景色ht()#隐藏turtlespeed(10)#速度 1-10渐进,0 最快tracer(0,0)pu()#抬笔backward(100)left(90)#左转90度pu()#抬笔backward(300)#后退300tree(12,100)#递归7层done()

5、樱花树–暖色调

import turtlefrom random import randomfrom random import randintdef draw_petal(turtle_obj, flower):# 绘制掉落的花瓣for i in range(int(flower)):# 有正有负就可以让画笔往二个方向走x = flower - 4 * flower * random()# 花瓣整体宽度(-10, 10)y = 10 - 20 * random()# 提笔,向前y,左转90,走x,落笔turtle_obj.penup()turtle_obj.forward(y)turtle_obj.left(90)turtle_obj.forward(x)turtle_obj.pendown()# 珊瑚色turtle_obj.pencolor("lightcoral")# 画圆turtle_obj.circle(1)# 回到起点# 提笔,后退x,右转90,后退y,落笔turtle_obj.penup()turtle_obj.backward(x)turtle_obj.right(90)turtle_obj.backward(y)turtle_obj.pendown()# 画树枝部分def draw_tree(turtle_obj, branch, tree_color):# 设置一个最小分支长度min_branch = 4if branch > min_branch:if branch < 8:# 以0.5的概率,向左、右分支if randint(0, 1) == 0:# 左为白色turtle_obj.pencolor("snow")else:# 右为珊瑚色turtle_obj.pencolor("lightcoral")# 枝干turtle_obj.pensize(branch / 2)elif 8 <= branch <= 16:# 以0.33的概率,分为左、中、右分支if randint(0, 2) == 0:# 左为白色turtle_obj.pencolor("snow")else:# 中、右为珊瑚色turtle_obj.pencolor("lightcoral")# 树枝turtle_obj.pensize(branch / 4)else:# 褐色turtle_obj.pencolor(tree_color)# 细枝turtle_obj.pensize(branch / 10)# 最开始的树干长度turtle_obj.forward(branch)# 随机度数因子a = 1.5 * random()# 顺时针旋转随机角度(0~30度)turtle_obj.right(20 * a)# 随机长度因子b = 1.5 * random()# 往右画,直到画不动为止draw_tree(turtle_obj, branch - 10 * b, tree_color)# 左转随机角度turtle_obj.left(40 * a)# 往左画,直到画不动位置draw_tree(turtle_obj, branch - 10 * b, tree_color)# 右转一定角度turtle_obj.right(20 * a)# 提笔turtle_obj.penup()# 递归结束回到起点turtle_obj.backward(branch)turtle_obj.pendown()def get_screen(width, height, color, speed):# 创建画幕screen_obj = turtle.Screen()# 画布大小:(width, height),颜色:colorscreen_obj.screensize(width, height, bg=color)screen_obj.setup(1.0, 1.0)# speed倍加速screen_obj.tracer(speed)return screen_objdef trees(tree_num):# 颜色color = ['brown', 'tan', 'black']for j in range(tree_num):# 树干颜色tree_color = color[randint(0, len(color) - 1)]# 画笔大小pensize = randint(2, 5)# 前进像素forward = ((-1) ** pensize) * pensize * randint(20, 50)# 后退像素if pensize <= 3:backward = ((-1) ** pensize) * (5 - pensize) * randint(10, 15)else:backward = pensize * randint(45, 50)# 创建画笔turtle_obj = turtle.Turtle()# 画笔粗细turtle_obj.pensize(pensize)# 提笔,向前forward,左转90,backward,落笔turtle_obj.penup()turtle_obj.forward(forward)turtle_obj.left(90)turtle_obj.backward(backward)turtle_obj.pendown()# 画笔颜色:褐色turtle_obj.pencolor(tree_color)# 枝干粗细branch = pensize * 15# 落花数flowers = branch# 第j棵树draw_tree(turtle_obj, branch, tree_color)# 花瓣draw_petal(turtle_obj, flowers)if __name__ == '__main__':# 创建画幕my_screen_width = 800my_screen_height = 600my_screen_color = 'wheat'#更改绘画速度,0最慢my_screen_speed = 10my_screen_obj = get_screen(my_screen_width, my_screen_height,my_screen_color, my_screen_speed)# 樱花树# 棵数my_tree_num = 3trees(my_tree_num)# 点击关闭画布my_screen_obj.exitonclick()

6、哆啦a梦

# * -- utf-8 -- *# Author: Tangimport turtle as tt.speed(10)t.pensize(8)t.hideturtle()t.screensize(500, 500, bg='white')# 猫脸t.fillcolor('#00A1E8')t.begin_fill()t.circle(120)t.end_fill()t.pensize(3)t.fillcolor('white')t.begin_fill()t.circle(100)t.end_fill()t.pu()t.home()t.goto(0, 134)t.pd()t.pensize(4)t.fillcolor("#EA0014")t.begin_fill()t.circle(18)t.end_fill()t.pu()t.goto(7, 155)t.pensize(2)t.color('white', 'white')t.pd()t.begin_fill()t.circle(4)t.end_fill()t.pu()t.goto(-30, 160)t.pensize(4)t.pd()t.color('black', 'white')t.begin_fill()a = 0.4for i in range(120):if 0 <= i < 30 or 60 <= i < 90:a = a + 0.08t.lt(3)  # 向左转3度t.fd(a)  # 向前走a的步长else:a = a - 0.08t.lt(3)t.fd(a)t.end_fill()t.pu()t.goto(30, 160)t.pensize(4)t.pd()t.color('black', 'white')t.begin_fill()for i in range(120):if 0 <= i < 30 or 60 <= i < 90:a = a + 0.08t.lt(3)  # 向左转3度t.fd(a)  # 向前走a的步长else:a = a - 0.08t.lt(3)t.fd(a)t.end_fill()t.pu()t.goto(-38, 190)t.pensize(8)t.pd()t.right(-30)t.forward(15)t.right(70)t.forward(15)t.pu()t.goto(15, 185)t.pensize(4)t.pd()t.color('black', 'black')t.begin_fill()t.circle(13)t.end_fill()t.pu()t.goto(13, 190)t.pensize(2)t.pd()t.color('white', 'white')t.begin_fill()t.circle(5)t.end_fill()t.pu()t.home()t.goto(0, 134)t.pensize(4)t.pencolor('black')t.pd()t.right(90)t.forward(40)t.pu()t.home()t.goto(0, 124)t.pensize(3)t.pencolor('black')t.pd()t.left(10)t.forward(80)t.pu()t.home()t.goto(0, 114)t.pensize(3)t.pencolor('black')t.pd()t.left(6)t.forward(80)t.pu()t.home()t.goto(0, 104)t.pensize(3)t.pencolor('black')t.pd()t.left(0)t.forward(80)# 左边的胡子t.pu()t.home()t.goto(0, 124)t.pensize(3)t.pencolor('black')t.pd()t.left(170)t.forward(80)t.pu()t.home()t.goto(0, 114)t.pensize(3)t.pencolor('black')t.pd()t.left(174)t.forward(80)t.pu()t.home()t.goto(0, 104)t.pensize(3)t.pencolor('black')t.pd()t.left(180)t.forward(80)t.pu()t.goto(-70, 70)t.pd()t.color('black', 'red')t.pensize(6)t.seth(-60)t.begin_fill()t.circle(80, 40)t.circle(80, 80)t.end_fill()t.pu()t.home()t.goto(-80, 70)t.pd()t.forward(160)t.pu()t.home()t.goto(-50, 50)t.pd()t.pensize(1)t.fillcolor("#eb6e1a")t.seth(40)t.begin_fill()t.circle(-40, 40)t.circle(-40, 40)t.seth(40)t.circle(-40, 40)t.circle(-40, 40)t.seth(220)t.circle(-80, 40)t.circle(-80, 40)t.end_fill()# 领带t.pu()t.goto(-70, 12)t.pensize(14)t.pencolor('red')t.pd()t.seth(-20)t.circle(200, 30)t.circle(200, 10)# 铃铛t.pu()t.goto(0, -46)t.pd()t.pensize(3)t.color("black", '#f8d102')t.begin_fill()t.circle(25)t.end_fill()t.pu()t.goto(-5, -40)t.pd()t.pensize(2)t.color("black", '#79675d')t.begin_fill()t.circle(5)t.end_fill()t.pensize(3)t.right(115)t.forward(7)t.mainloop()
赞(0) 打赏
未经允许不得转载:爱站程序员基地 » 用python画圣诞树、樱花树、卡通图案及打包成exe文件