python之“七巧板”


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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM