怎么用Python绘制爱心圣诞树(python,开发技术)

时间:2024-04-28 05:31:19 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

代码

#-*-coding:utf-8-*-"""CreatedonSatDec1212:29:092020@author:haoyu"""importturtleastimportrandom#爱心函数#将爱心分为两个半圆与一个正方形#r为半圆半径,l=2r为正方形边长#调整半径即可调整爱心大小defloving_heart(r):l=2*rt.left(45)t.forward(l)t.circle(r,180)t.right(90)t.circle(r,180)t.forward(l)#树函数(递归)deftree(d,s):ifd<=0:returnt.forward(s)tree(d-1,s*.8)t.right(120)tree(d-3,s*.5)t.right(120)tree(d-3,s*.5)t.right(120)t.backward(s)#回退函数#画爱心部分t.penup()t.goto(0,200)#设置起点位置t.pendown()t.pencolor('pink')#设置画笔颜色t.color('pink')t.begin_fill()#对图形进行填充loving_heart(20)#执行画爱心函数t.end_fill()#画树部分n=100t.speed('fastest')#t.Turtle().screen.delay(0)t.right(225)t.color("darkgreen")t.backward(n*4.8)tree(15,n)t.backward(n/5)#绘制落叶foriinrange(200):a=200-400*random.random()b=10-20*random.random()t.up()t.forward(b)t.left(90)t.forward(a)t.down()ifrandom.randint(0,1)==0:t.color('tomato')else:t.color('wheat')t.circle(2)t.up()t.backward(a)t.right(90)t.backward(b)t.hideturtle()

结果

怎么用Python绘制爱心圣诞树

参考:https://www.cnblogs.com/felixwang2/p/10177515.html

介绍下其他方法如何用Python画一个圣诞树呢?

最简单:

height=5stars=1foriinrange(height):print((''*(height-i))+('*'*stars))stars+=2print((''*height)+'|')

效果:

怎么用Python绘制爱心圣诞树

哈哈哈哈,总有一种骗了大家的感觉。

其实本文是想介绍Turtle库来画圣诞树。

方法:

importturtlescreen=turtle.Screen()screen.setup(800,600)circle=turtle.Turtle()circle.shape('circle')circle.color('red')circle.speed('fastest')circle.up()square=turtle.Turtle()square.shape('square')square.color('green')square.speed('fastest')square.up()circle.goto(0,280)circle.stamp()k=0foriinrange(1,17):y=30*iforjinrange(i-k):x=30*jsquare.goto(x,-y+280)square.stamp()square.goto(-x,-y+280)square.stamp()ifi%4==0:x=30*(j+1)circle.color('red')circle.goto(-x,-y+280)circle.stamp()circle.goto(x,-y+280)circle.stamp()k+=2ifi%4==3:x=30*(j+1)circle.color('yellow')circle.goto(-x,-y+280)circle.stamp()circle.goto(x,-y+280)circle.stamp()square.color('brown')foriinrange(17,20):y=30*iforjinrange(3):x=30*jsquare.goto(x,-y+280)square.stamp()square.goto(-x,-y+280)square.stamp()turtle.exitonclick()

效果:

怎么用Python绘制爱心圣诞树

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:怎么用Python绘制爱心圣诞树的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:Python中如何实现字符串格式化下一篇:

10 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18