当前位置: 首页 > 后端技术 > Python

圣诞节!教你用Python画圣诞树

时间:2023-03-26 17:09:13 Python

如何用Python画圣诞树?最简单的:1height=523stars=14foriinrange(height):5print((''*(height-i))+('*'*stars))6stars+=27print((''*height)+'|')效果:哈哈哈哈,总有种坑大家的感觉。其实这篇文章想介绍一下画圣诞树的Turtle库。方法一:1importturtle2screen=turtle.Screen()3screen.setup(800,600)4circle=turtle.Turtle()5circle.shape('circle')6circle.color('red')7circle.speed('fastest')8circle.up()9square=turtle.Turtle()10square.shape('square')11square.color('green')12square.speed('fastest')13square.up()14circle.goto(0,280)15circle.stamp()16k=017foriinrange(1,17):18y=30*i19forjinrange(i-k):20x=30*j21square.goto(x,-y+280)22square.stamp()23square.goto(-x,-y+280)24square.stamp()25ifi%4==0:26x=30*(j+1)27circle.color('red')28圆。goto(-x,-y+280)29circle.stamp()30circle.goto(x,-y+280)31circle.stamp()32k+=233ifi%4==3:34x=30*(j+1)35circle.color('yellow')36circle.goto(-x,-y+280)37circle.stamp()38circle.goto(x,-y+280)39圆。stamp()40square.color('brown')41foriinrange(17,20):42y=30*i43forjinrange(3):44x=30*j45square.goto(x,-y+280)46square.stamp()47square.goto(-x,-y+280)48square.stamp()49turtle.exitonclick()效果:方法二:1fromturtleimport*2importrandom3importtime45n=80.067speed("fastest")8screensize(bg='seashell')9left(90)10forward(3*n)11color("orange","yellow")12begin_fill()13left(126)1415foriinrange(5):16forward(n/5)17right(144)18forward(n/5)19left(72)20end_fill()21right(126)2223color("深绿色")24backward(n*4.8)25deftree(d,s):26如果d<=0:return27forward(s)28tree(d-1,s*.8)29right(120)30tree(d-3,s*.5)31right(120)32tree(d-3,s*.5)33right(120)34backward(s)35tree(15,n)36backward(n/2)3738foriinrange(200):39a=200-400*random.random()40b=10-20*random.random()41up()42forward(b)43left(90)44forward(a)45down()46ifrandom.randint(0,1)==0:47color('tomato')48else:49color('wheat')50circle(2)51up()52backward(a)53right(90)54backward(b)55time.sleep(60)效果:如果你是有兴趣就试试吧!