python之「七巧板」

1       七巧板的關係

七巧板由2個大三角、1箇中三角、2個小三角、1個平行四邊形組成app

2       代碼

from turtle import *
len=200
座標=[]
def writestr(a):
    write(a, False, "left", ("Arial", 10, "normal"))
def 平行四邊形(x,y):
    座標.clear()
    penup()
    goto(x,y)
    writestr(1)
    座標.append([xcor(),ycor()])
    pendown()
    begin_fill()
    forward(len/2)
    writestr(2)
    座標.append([xcor(),ycor()])
    right(45)
    forward(len/2*1.414/2)
    writestr(3)
    座標.append([xcor(),ycor()])
    right(135)
    forward(len/2)
    writestr(4)
    座標.append([xcor(),ycor()])
    right(45)
    forward(len/2*1.414/2)
    right(135)
    end_fill()

def 正方形(x,y):
    begin_fill()
    座標.clear()
    penup()
    goto(x, y)
    writestr(1)
    座標.append([xcor(), ycor()])
    pendown()
    forward(len/2/1.414)
    writestr(2)
    座標.append([xcor(), ycor()])
    left(90)
    forward(len/2/1.414)
    writestr(3)
    座標.append([xcor(), ycor()])
    left(90)
    forward(len/2/1.414)
    writestr(4)
    座標.append([xcor(), ycor()])
    left(90)
    forward(len/2/1.414)
    end_fill()

def 大三角形(x,y):
    begin_fill()
    座標.clear()
    penup()
    goto(x, y)
    writestr(1)
    座標.append([xcor(), ycor()])
    pendown()
    forward(len/1.414)
    writestr(2)
    座標.append([xcor(), ycor()])
    left(135)
    forward(len)
    writestr(3)
    座標.append([xcor(), ycor()])
    left(135)
    forward(len/1.414)
    left(90)
    end_fill()

def 中三角形(x,y):
    begin_fill()
    座標.clear()
    penup()
    goto(x, y)
    writestr(1)
    座標.append([xcor(), ycor()])
    pendown()
    forward(len/2)
    writestr(2)
    座標.append([xcor(), ycor()])
    left(135)
    forward(len/2*1.414)
    writestr(3)
    座標.append([xcor(), ycor()])
    left(135)
    forward(len/2)
    left(90)
    end_fill()
def 小三角形(x,y):
    begin_fill()
    座標.clear()
    penup()
    goto(x, y)
    writestr(1)
    座標.append([xcor(), ycor()])
    pendown()
    forward(len/2/1.414)
    writestr(2)
    座標.append([xcor(), ycor()])
    left(135)
    forward(len/2)
    writestr(3)
    座標.append([xcor(), ycor()])
    left(135)
    forward(len/2/1.414)
    left(90)
    end_fill()

color("blue")
平行四邊形(-100,-100)
color("red")
setheading(-45)
正方形(座標[1][0],座標[1][1])
color("orange")
setheading(-45)
大三角形(座標[3][0],座標[3][1])

紫色x=座標[1][0]-abs(座標[1][0]-座標[0][0])*1/4
紫色y=座標[1][1]+abs(座標[0][1]-座標[1][1])*1/4-len/2

color("yellow")
setheading(-90)
大三角形(座標[2][0],座標[2][1])
綠色x=座標[2][0]-abs(座標[2][0]-座標[0][0])*1/4
綠色y=座標[0][1]
棕色x=(座標[1][0]+座標[2][0])/2
棕色y=(座標[1][1]+座標[2][1])/2

color("purple")
setheading(0)
中三角形(紫色x,紫色y)
color("green")
setheading(0)
小三角形(綠色x,綠色y)
color("brown")
setheading(-45)
小三角形(棕色x,棕色y)
mainloop()

 

3      效果圖

相關文章
相關標籤/搜索