3
我想畫一個瓷磚等邊三角形,看起來像這樣如何畫一個三角形的瓷磚與Python烏龜
使用Python的烏龜。我希望能夠有16,25,36,49或64個三角形。
我最初的嘗試很笨拙,因爲我還沒有想出如何將龜從一個三角形整齊地移動到下一個三角形。
這是我的(部分正確)的代碼
def draw_triangle(this_turtle, size,flip):
"""Draw a triangle by drawing a line and turning through 120 degrees 3 times"""
this_turtle.pendown()
this_turtle.fill(True)
for _ in range(3):
if flip:
this_turtle.left(120)
this_turtle.forward(size)
if not flip:
this_turtle.right(120)
this_turtle.penup()
myturtle.goto(250,0)
for i in range(4):
for j in range(4):
draw_triangle(myturtle, square_size,(j%2 ==0))
# move to start of next triangle
myturtle.left(120)
#myturtle.forward(square_size)
myturtle.goto(-250,(i+1)*square_size)
必須有這樣的一個優雅的方式?
好主意。我沒有想過這樣做。 – user2175783