代码改变世界

python之“七巧板”

2020-08-12 22:24  夜雨瞳  阅读(2184)  评论(0编辑  收藏  举报

1       七巧板的关系

七巧板由2个大三角、1个中三角、2个小三角、1个平行四边形组成

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      效果图