我试着画一个平铺的等边三角形,看起来像这样
用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)
一定有一种优雅的方式来做这件事?在