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

最美的圣诞树!用Python为你画雪夜的圣诞树

时间:2023-03-25 20:40:23 Python

今天是平安夜,明天就是圣诞节。这两天应该是苹果销量最好的日子了……说到圣诞节,就不得不提圣诞树了。在本文中,我们使用Python绘制一棵圣诞树,先睹为快。下面我们展开看看主要的代码实现。圣诞树的主要代码如下:#绘制第一层seth(-120)foriinrange(10):fd(12)right(2)penup()goto(0,150)seth(-60)pendown()foriinrange(10):fd(12)left(2)seth(-150)penup()fd(10)pendown()foriinrange(5):fd(10)right(15)seth(-150)penup()fd(8)pendown()foriinrange(5):fd(10)right(15)seth(-155)penup()fd(5)pendown()foriinrange(5):fd(7)right(15)#画第二层penup()goto(-55,34)pendown()seth(-120)foriinrange(10):fd(8)right(5)penup()goto(50,35)seth(-60)pendown()foriinrange(10):fd(8)left(5)seth(-120)penup()fd(10)seth(-145)pendown()foriinrange(5):fd(10)right(15)penup()fd(10)seth(-145)pendown()foriinrange(5):fd(12)right(15)penup()fd(8)seth(-145)pendown()foriinrange(5):fd(10)right(15)penup()seth(-155)fd(8)pendown()foriinrange(5)):fd(11)right(15)……星形五角星的主要代码如下:pensize(2)pencolor("yellow")penup()goto(x,y)pendown()begin_fill()fillcolor("yellow")foriinrange(5):左边(72)fd(size)right(144)fd(size)end_fill()帽子的主要代码如下:penup()goto(-30,-120)pencolor("white")pendown()fillcolor("white")begin_fill()fd(30)circle(4,180)fd(30)circle(4,180)end_fill()penup()goto(-25,-115)seth(75)pendown()fillcolor("red")begin_fill()foriinrange(5):fd(6)right(20)seth(-10)foriinrange(5):fd(8)right(15)seth(145)foriin范围(5):fd(5)left(2)seth(90)foriinrange(5):fd(1)left(2)seth(-90)foriinrange(4):fd(4)right(6)seth(161)fd(30)end_fill()pensize(1)pencolor("white")sockssocks主要代码如下:penup()goto(-20,80)pencolor("white")pendown()begin_fill()fillcolor("white")fd(25)circle(4,180)fd(25)circle(4,180)end_fill()penup()goto(-15,80)pendown()begin_fill()fillcolor("红色")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(70,-240)bowknot蝴蝶结的主要代码如下:penup()pencolor("#f799e6")goto(x,y)seth(80)pendown()笔大小(2)圆(5)塞思(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)sethflake和snowfall的主要代码实现效果如下:screen.delay(0)t=Turtle(visible=False,shape='circle')t.pencolor("white")t.fillcolor("white")t.penup()t.setheading(-90)t.goto(r.randint(-width/2,width/2),height/2)stars=[]foriinrange(200):星号=t.clone()s=r.random()/3star.shapesize(s,s)star.speed(int(s*10))star.setx(r.randint(-width/2,width/2))star.sety(height/2+r.randint(1,height))star.showturtle()stars.append(star)whileTrue:forstarinstars:star.sety(star.ycor()-8*star.speed())ifstar.ycor()<-height/2:star.hideturtle()star.setx(r.randint(-width/2,width/2))star.sety(height/2+r.randint(1,height))star.showturtle()以上是圣诞树的主要代码实现。当然,我们也可以给它加上背景音乐。我不会在这里详细介绍。有兴趣的可以参考:给圣诞树添加背景音乐源码,在公众号Python小二后台回复圣诞树即可获取。