3月将至,马上又到了樱花盛开的季节,今天的技能帖想教大家用Python绘制绘制一棵樱花树,将它送给身在武汉,或想去武汉的小伙伴,当樱花未开时,我们在线共赏樱,当樱花盛开时,我们相约珞珈山下!
希望如约而至的不止樱花,还有疫情过后平安的你!
以下使用的工具就是:
开发环境:Python3 + win10
开发工具:pycharm
出现的效果图:
源代码:
import turtle as Timport randomimport time# 画樱花的躯干(60,t)def Tree(branch, t):time.sleep(0.0005)if branch > 3:if 8 <= branch <= 12:if random.randint(0, 2) == 0:t.color('snow') # 白else:t.color('lightcoral') # 淡珊瑚色t.pensize(branch / 3)elif branch < 8:if random.randint(0, 1) == 0:t.color('snow')else:t.color('lightcoral') # 淡珊瑚色t.pensize(branch / 2)else:t.color('sienna') # 赭(zhě)色t.pensize(branch / 10) # 6t.forward(branch)a = 1.5 * random.random()t.right(20 * a)b = 1.5 * random.random()Tree(branch - 10 * b, t)t.left(40 * a)Tree(branch - 10 * b, t)t.right(20 * a)t.up()t.backward(branch)t.down()# 掉落的花瓣def Petal(m, t):for i in range(m):a = 200 - 400 * random.random()b = 10 - 20 * random.random()t.up()t.forward(b)t.left(90)t.forward(a)t.down()t.color('lightcoral') # 淡珊瑚色t.circle(1)t.up()t.backward(a)t.right(90)t.backward(b)# 绘图区域t = T.Turtle()# 画布大小w = T.Screen()t.hideturtle() # 隐藏画笔t.getscreen().tracer(5, 0)w.screensize(bg='wheat') # wheat小麦t.left(90)t.up()t.backward(150)t.down()t.color('sienna')# 画樱花的躯干Tree(60, t)# 掉落的花瓣Petal(200, t)w.exitonclick()
表白代码2
效果图:
源码:
from turtle import *from time import sleepdef go_to(x, y):up()goto(x, y)down()def big_Circle(size): #函数用于绘制心的大圆speed(1)for i in range(150):forward(size)right(0.3)def small_Circle(size): #函数用于绘制心的小圆speed(1)for i in range(210):forward(size)right(0.786)def line(size):speed(1)forward(51*size)def heart( x, y, size):go_to(x, y)left(150)begin_fill()line(size)big_Circle(size)small_Circle(size)left(120)small_Circle(size)big_Circle(size)line(size)end_fill()def arrow():pensize(10)setheading(0)go_to(-400, 0)left(15)forward(150)go_to(339, 178)forward(150)def arrowHead():pensize(1)speed(1)color('red', 'red')begin_fill()left(120)forward(20)right(150)forward(35)right(120)forward(35)right(150)forward(20)end_fill()def main():pensize(2)color('red', 'pink')#getscreen().tracer(30, 0) #取消注释后,快速显示图案heart(200, 0, 1) #画出第一颗心,前面两个参数控制心的位置,函数最后一个参数可控制心的大小setheading(0) #使画笔的方向朝向x轴正方向heart(-80, -100, 1.5) #画出第二颗心arrow() #画出穿过两颗心的直线arrowHead() #画出箭的箭头go_to(400, -300)write("author:520Python", move=True, align="left", font=("宋体", 30, "normal"))done()main()
表白代码3
效果图:
源代码:
import turtleimport timeturtle.speed(5) # 画笔移动的速度# 设置初始位置turtle.penup() # 提起画笔,移动画笔但并不会绘制图形turtle.left(90) # 逆时针转动画笔90度turtle.fd(200)turtle.pendown() # 放下画笔,移动画笔即开始绘制turtle.right(90)# 设置画笔的大小turtle.pensize(2)# 花蕊turtle.fillcolor("red") # 填充颜色turtle.begin_fill() # 开始填充turtle.circle(10, 180)turtle.circle(25, 110)turtle.left(50)turtle.circle(60, 45)turtle.circle(20, 170)turtle.right(24)turtle.fd(30)turtle.left(10)turtle.circle(30, 110)turtle.fd(20)turtle.left(40)turtle.circle(90, 70)turtle.circle(30, 150)turtle.right(30)turtle.fd(15)turtle.circle(80, 90)turtle.left(15)turtle.fd(45)turtle.right(165)turtle.fd(20)turtle.left(155)turtle.circle(150, 80)turtle.left(50)turtle.circle(150, 90)turtle.end_fill() # 结束填充# 花瓣1turtle.left(150)turtle.circle(-90, 70)turtle.left(20)turtle.circle(75, 105)turtle.setheading(60)turtle.circle(80, 98)turtle.circle(-90, 40)# 花瓣2turtle.left(180)turtle.circle(90, 40)turtle.circle(-80, 98)turtle.setheading(-83)# 叶子1turtle.fd(30)turtle.left(90)turtle.fd(25)turtle.left(45)turtle.fillcolor("green")turtle.begin_fill()turtle.circle(-80, 90)turtle.right(90)turtle.circle(-80, 90)turtle.end_fill()turtle.right(135)turtle.fd(60)turtle.left(180)turtle.fd(85)turtle.left(90)turtle.fd(80)# 叶子2turtle.right(90)turtle.right(45)turtle.fillcolor("green")turtle.begin_fill()turtle.circle(80, 90)turtle.left(90)turtle.circle(80, 90)turtle.end_fill()turtle.left(135)turtle.fd(60)turtle.left(180)turtle.fd(60)turtle.right(90)turtle.circle(200, 50) # 画一个圆 200 是半径,50 是弧度# 不让自动退出,放在程序的最后一行# 不然画画结束后会自动退出turtle.done()
学Python的安娴数据分析Python解答小可爱感谢各位大佬们的关注,有问题可私我免费解答,需要学习视频、文档、源码的可以看以下获取方式哔哩哔哩账号:学Python的安娴官方学习口口裙:11418-60209