在数字化转型中,选择合适的跨平台开发框架不仅能提高效率,还有助于确保数据安全与合规性。
603
2022-11-22
初识python之画图神器篇
前言
前面我们写了一篇关于python实例的汇率转换的文章,有的小伙伴说它虽然可以解决我们生活中的实际问题,但是好像缺少点什么。
我仔细想了想,想起了一句话,“生活不止眼前的苟且,还有诗和远方!”,确实我们应该用python来解决和丰富我们的生活。那么这期文章我就为大家介绍关于python的艺术美——画图神器
在数学中有一种美,叫几何美,我们又称为艺术美,用具有规律的线条,迭代出美丽的图片,这就是许多艺术家在创作是的灵感渠道。下面我们就为大家准备了一个简单的五角星绘制。
****涉及第三库的导入,模块的导入,还有一些我想我现在也介绍不完,下面就让我们一起来领略美感吧!
import turtledef draw_recursive_pentargram(size): ''' 迭代绘制五角星 ''' count = 1 while count <= 5: turtle.backward(size) turtle.right(144) count += 1 # 五角星绘制完成,更新参数 size += 10 if size <= 100: draw_recursive_pentargram(size)def main(): ''' 主函数 ''' turtle.speed(0) # turtle.penup() turtle.forward(40) # turtle.pendown() turtle.pensize(2) turtle.pencolor("red") turtle.bgcolor('black') size = 50 draw_recursive_pentargram(size) turtle.exitonclick()if __name__ == "__main__": main()
# 小黄人绘制案例************************************************************************import turtlet = turtle.Turtle()wn = turtle.Screen()turtle.colormode(255)t.hideturtle()t.speed(10)t.penup()t.pensize(4)t.goto(100,0)t.pendown()t.left(90)t.color((0,0,0),(255,255,0))#身体绘制上色t.begin_fill()t.forward(200)t.circle(100,180)t.forward(200)t.circle(100,180)t.end_fill()#右眼睛绘制上色t.pensize(12)t.penup()t.goto(-100,200)t.pendown()t.right(100)t.circle(500,23)t.pensize(3)t.penup()t.goto(0,200)t.pendown()t.seth(270)t.color("black","white")t.begin_fill()t.circle(30)t.end_fill()t.penup()t.goto(15,200)t.pendown()t.color("black","black")t.begin_fill()t.circle(15)t.end_fill()t.penup()t.goto(35,205)t.color("black","white")t.begin_fill()t.circle(5)t.end_fill()#左眼睛绘制上色t.pensize(3)t.penup()t.goto(0,200)t.pendown()t.seth(90)t.color("black","white")t.begin_fill()t.circle(30)t.end_fill()t.penup()t.goto(-15,200)t.pendown()t.color("black","black")t.begin_fill()t.circle(15)t.end_fill()t.penup()t.goto(-35,205)t.color("black","white")t.begin_fill()t.circle(5)t.end_fill()#嘴绘制上色t.penup()t.goto(-20,100)t.pendown()t.seth(270)t.color("black","white")t.begin_fill()t.circle(20,180)t.left(90)t.forward(40)t.end_fill()#裤子绘制上色t.penup()t.goto(-100,0)t.pendown()t.seth(0)t.color("black","blue")t.begin_fill()t.forward(20)t.left(90)t.forward(40)t.right(90)t.forward(160)t.right(90)t.forward(40)t.left(90)t.forward(20)t.seth(270)t.penup()t.goto(-100,0)t.circle(100,180)t.end_fill()#左裤子腰带t.penup()t.goto(-70,20)t.pendown()t.color("black","blue")t.begin_fill()t.seth(45)t.forward(15)t.left(90)t.forward(60)t.seth(270)t.forward(15)t.left(40)t.forward(50)t.end_fill()t.left(180)t.goto(-70,30)t.dot()#右裤腰带t.penup()t.goto(70,20)t.pendown()t.color("black","blue")t.begin_fill()t.seth(135)t.forward(15)t.right(90)t.forward(60)t.seth(270)t.forward(15)t.right(40)t.forward(50)t.end_fill()t.left(180)t.goto(70,30)t.dot()#脚t.penup()t.goto(4,-100)t.pendown()t.seth(270)t.color("black","black")t.begin_fill()t.forward(30)t.left(90)t.forward(40)t.seth(20)t.circle(10,180)t.circle(400,2)t.seth(90)t.forward(20)t.goto(4,-100)t.end_fill()t.penup()t.goto(-4,-100)t.pendown()t.seth(270)t.color("black","black")t.begin_fill()t.forward(30)t.right(90)t.forward(40)t.seth(20)t.circle(10,-225)t.circle(400,-3)t.seth(90)t.forward(21)t.goto(-4,-100)t.end_fill()#左手t.penup()t.goto(-100,50)t.pendown()t.seth(225)t.color("black","yellow")t.begin_fill()t.forward(40)t.left(90)t.forward(35)t.seth(90)t.forward(50)t.end_fill()#右手t.penup()t.goto(100,50)t.pendown()t.seth(315)t.color("black","yellow")t.begin_fill()t.forward(40)t.right(90)t.forward(36)t.seth(90)t.forward(50)t.end_fill()#t.penup()t.goto(0,-100)t.pendown()t.forward(30)#t.penup()t.goto(0,-20)t.pendown()t.color("yellow")t.begin_fill()t.seth(45)t.forward(20)t.circle(10,180)t.right(90)t.circle(10,180)t.forward(20)t.end_fill()#t.penup()t.color("black")t.goto(-100,-20)t.pendown()t.circle(30,90)t.penup()t.goto(100,-20)t.pendown()t.circle(30,-90)#头顶t.penup()t.goto(2,300)t.pendown()t.begin_fill()t.seth(135)t.circle(100,40)t.end_fill()t.penup()t.goto(2,300)t.pendown()t.begin_fill()t.seth(45)t.circle(100,40)t.end_fill()turtle.exitonclick()
看似复杂而又繁琐的代码,它其实是由规律的,不信你可以自己看看它的写法。
樱花案例
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) # 6 t.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()
好的,本期的文章就介绍到这里,喜欢的小伙伴就让更多的人知晓吧。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~