使用Python的 turtle库绘制中国结

需求:用python绘制一个与中国传统节日有关的图像,如春节→中国结

  1 import turtle as t
  2 def goto(x,y):
  3     t.penup()
  4     t.goto(x,y)
  5     t.pendown()
  6     
  7 def init():
  8     t.setup(800,800)
  9     t.pensize(10)
 10     t.pencolor("red")
 11     t.speed(14)
 12     
 13 def jiexin():
 14     m,n=0,200
 15     for i in range(11):
 16         goto(m,n)
 17         t.seth(-45)
 18         t.fd(200)
 19         m-=20/pow(2,0.5)
 20         n-=20/pow(2,0.5)
 21         
 22     m,n=0,200
 23     for j in range(11):
 24         goto(m,n)
 25         t.seth(-135)
 26         t.fd(200)
 27         m+=20/pow(2,0.5)
 28         n-=20/pow(2,0.5)
 29         
 30 def jiexiaoban():
 31     m=-20/pow(2,0.5)
 32     n=200-20/pow(2,0.5)
 33     for k in range(4):
 34         goto(m,n)
 35         t.seth(135)
 36         t.fd(20)
 37         t.circle(10,180)
 38         t.fd(20)
 39         m-=40/pow(2,0.5)
 40         n-=40/pow(2,0.5)
 41         
 42     m=20/pow(2,0.5)
 43     n=200-20/pow(2,0.5)
 44     for k in range(4):
 45         goto(m,n)
 46         t.seth(45)
 47         t.fd(20)
 48         t.circle(-10,180)
 49         t.fd(20)
 50         m+=40/pow(2,0.5)
 51         n-=40/pow(2,0.5)
 52  
 53     m=20/pow(2,0.5)
 54     n=200-200*pow(2,0.5)+20/pow(2,0.5)
 55     for k in range(4):
 56         goto(m,n)
 57         t.seth(-45)
 58         t.fd(20)
 59         t.circle(10,180)
 60         t.fd(20)
 61         m+=40/pow(2,0.5)
 62         n+=40/pow(2,0.5)
 63         
 64     m=-20/pow(2,0.5)
 65     n=200-200*pow(2,0.5)+20/pow(2,0.5)
 66     for k in range(4):
 67         goto(m,n)
 68         t.seth(-135)
 69         t.fd(20)
 70         t.circle(-10,180)
 71         t.fd(20)
 72         m-=40/pow(2,0.5)
 73         n+=40/pow(2,0.5)
 74         
 75 def waiyuan():
 76     goto(90*pow(2,0.5),200-110*pow(2,0.5))
 77     t.seth(-45)
 78     t.circle(20,270)
 79     
 80     goto(-90*pow(2,0.5),200-110*pow(2,0.5))
 81     t.seth(-135)
 82     t.circle(-20,270)
 83     
 84     goto(80*pow(2,0.5),200-120*pow(2,0.5))
 85     t.seth(-45)
 86     t.circle(40,270)
 87     
 88     goto(-80*pow(2,0.5),200-120*pow(2,0.5))
 89     t.seth(-135)
 90     t.circle(-40,270)
 91  
 92 def shengzi():
 93     goto(0,200)
 94     t.pensize(20)
 95     t.seth(90)
 96     t.fd(60)
 97     goto(0,320)
 98     t.pensize(12)
 99     t.seth(180)
100     t.circle(30,360)
101     
102     goto(0,200-200*pow(2,0.5))
103     t.pensize(40)
104     t.seth(-90)
105     t.fd(20)
106     t.pensize(2)
107     s=-20
108     for i in range(11):
109         goto(s,200-200*pow(2,0.5))
110         t.seth(-90)
111         t.fd(200)
112         s+=4
113         
114 def hanzi():
115     goto(-150,325)
116     t.write("幸福中国结",font=("Arial",40,"normal"))
117     
118 def main():
119     init()
120     jiexin()
121     jiexiaoban()
122     waiyuan()
123     shengzi()
124     hanzi()
125     t.hideturtle()
126     t.done()
127 main()
128         
原文地址:https://www.cnblogs.com/zzj420133722/p/12900738.html