为 N 列和 M 行更改我的 Python Turtle 代码
Change my Python Turtle code for N columns and M rows
我的代码已准备好显示 3x3 等边三角形,但我不知道如何更改它,所以 N 等边三角形的数量可以显示在一列中(一个三角形在另一个上面)和 M 等边三角形的数量可以显示在一行中(一个三角形紧挨着另一个三角形)。任何帮助深表感谢!
我的代码是:
import turtle
ara = turtle.Turtle() #name of the turtle
ara.speed(0)
ara.pensize(10)
def pile_left(t, l, n): #first column
t = l * 3 ** 0.5 / 2
for j in range(n):
if j == 0 : ara.fillcolor('red')
if j == 1 : ara.fillcolor('orange')
if j == 2 : ara.fillcolor('green')
ara.pu()
ara.shape('turtle')
ara.begin_fill()
for i in range(3):
ara.lt(120)
ara.fd(l)
ara.end_fill()
ara.sety(ara.ycor() + t)
pile_left(ara, 60, 3)
def pile_middle(t, l, n): #second column
t = l * 3 ** 0.5 / 2
ara.goto(60,0)
for j in range(n):
if j == 0 : ara.fillcolor('purple')
if j == 1 : ara.fillcolor('yellow')
if j == 2 : ara.fillcolor('peachpuff')
ara.pu()
ara.shape('turtle')
ara.begin_fill()
for i in range(3):
ara.lt(120)
ara.fd(l)
ara.end_fill()
ara.sety(ara.ycor() + t)
pile_middle(ara, 60, 3)
def pile_right(t, l, n): #third column
t = l * 3 ** 0.5 / 2
ara.goto(120,0)
for j in range(n):
if j == 0 : ara.fillcolor('grey')
if j == 1 : ara.fillcolor('brown')
if j == 2 : ara.fillcolor('blue')
ara.pu()
ara.shape('turtle')
ara.begin_fill()
for i in range(3):
ara.lt(120)
ara.fd(l)
ara.end_fill()
ara.sety(ara.ycor() + t)
pile_right(ara, 60, 3)
turtle.mainloop()
你可以在饰品上运行它。io/python看看它目前在做什么。
I have my code ready to show 3x3 equilateral triangles
不,不是真的。您已准备好显示 1x3 等边三角形的代码,只需将代码复制三次即可模拟第二个维度。你的问题是你无法将你的代码复制 M
次来解决这个问题。
这种情况下的答案是更少但更智能的代码。我们将需要 N
和 M
的嵌套循环,以及每个 M
向前移动长度 l
和垂直高度 t
的能力对于每个 N
:
from turtle import Screen, Turtle
COLORS = ['red', 'orange', 'green', 'purple', 'yellow', 'pink', 'grey', 'brown', 'blue']
def pile(turtle, length, columns, rows):
height = length * 3 ** 0.5 / 2
x_origin = turtle.xcor()
color = 0
for _ in range(rows):
for _ in range(columns):
turtle.color(COLORS[color % len(COLORS)])
turtle.begin_fill()
for _ in range(3):
turtle.forward(length)
turtle.left(120)
turtle.end_fill()
turtle.forward(length)
color += 1
turtle.setposition(x_origin, turtle.ycor() + height)
screen = Screen()
yertle = Turtle('turtle')
yertle.speed('fastest')
yertle.penup()
pile(yertle, 60, 4, 5)
yertle.hideturtle()
screen.exitonclick()
然而,这是一个完美的例子,其中 stamping
比 filling
更简单。我们可以把乌龟本身做成等边三角形,然后移动,戳戳:
from turtle import Screen, Turtle
COLORS = ['red', 'orange', 'green', 'purple', 'yellow', 'pink', 'grey', 'brown', 'blue']
CURSOR_SIZE = 20
def pile(turtle, length, columns, rows):
turtle.shapesize(length / CURSOR_SIZE)
height = length * 3 ** 0.5 / 2
y_origin = turtle.ycor()
color = 0
for _ in range(columns):
for _ in range(rows):
turtle.color(COLORS[color % len(COLORS)])
turtle.stamp()
turtle.forward(height)
color += 1
turtle.setposition(turtle.xcor() + length, y_origin)
screen = Screen()
yertle = Turtle('triangle')
yertle.speed('fastest')
yertle.setheading(90)
yertle.penup()
pile(yertle, 60, 4, 5)
yertle.hideturtle()
screen.exitonclick()
不仅更简单,而且更快!
我的代码已准备好显示 3x3 等边三角形,但我不知道如何更改它,所以 N 等边三角形的数量可以显示在一列中(一个三角形在另一个上面)和 M 等边三角形的数量可以显示在一行中(一个三角形紧挨着另一个三角形)。任何帮助深表感谢!
我的代码是:
import turtle
ara = turtle.Turtle() #name of the turtle
ara.speed(0)
ara.pensize(10)
def pile_left(t, l, n): #first column
t = l * 3 ** 0.5 / 2
for j in range(n):
if j == 0 : ara.fillcolor('red')
if j == 1 : ara.fillcolor('orange')
if j == 2 : ara.fillcolor('green')
ara.pu()
ara.shape('turtle')
ara.begin_fill()
for i in range(3):
ara.lt(120)
ara.fd(l)
ara.end_fill()
ara.sety(ara.ycor() + t)
pile_left(ara, 60, 3)
def pile_middle(t, l, n): #second column
t = l * 3 ** 0.5 / 2
ara.goto(60,0)
for j in range(n):
if j == 0 : ara.fillcolor('purple')
if j == 1 : ara.fillcolor('yellow')
if j == 2 : ara.fillcolor('peachpuff')
ara.pu()
ara.shape('turtle')
ara.begin_fill()
for i in range(3):
ara.lt(120)
ara.fd(l)
ara.end_fill()
ara.sety(ara.ycor() + t)
pile_middle(ara, 60, 3)
def pile_right(t, l, n): #third column
t = l * 3 ** 0.5 / 2
ara.goto(120,0)
for j in range(n):
if j == 0 : ara.fillcolor('grey')
if j == 1 : ara.fillcolor('brown')
if j == 2 : ara.fillcolor('blue')
ara.pu()
ara.shape('turtle')
ara.begin_fill()
for i in range(3):
ara.lt(120)
ara.fd(l)
ara.end_fill()
ara.sety(ara.ycor() + t)
pile_right(ara, 60, 3)
turtle.mainloop()
你可以在饰品上运行它。io/python看看它目前在做什么。
I have my code ready to show 3x3 equilateral triangles
不,不是真的。您已准备好显示 1x3 等边三角形的代码,只需将代码复制三次即可模拟第二个维度。你的问题是你无法将你的代码复制 M
次来解决这个问题。
这种情况下的答案是更少但更智能的代码。我们将需要 N
和 M
的嵌套循环,以及每个 M
向前移动长度 l
和垂直高度 t
的能力对于每个 N
:
from turtle import Screen, Turtle
COLORS = ['red', 'orange', 'green', 'purple', 'yellow', 'pink', 'grey', 'brown', 'blue']
def pile(turtle, length, columns, rows):
height = length * 3 ** 0.5 / 2
x_origin = turtle.xcor()
color = 0
for _ in range(rows):
for _ in range(columns):
turtle.color(COLORS[color % len(COLORS)])
turtle.begin_fill()
for _ in range(3):
turtle.forward(length)
turtle.left(120)
turtle.end_fill()
turtle.forward(length)
color += 1
turtle.setposition(x_origin, turtle.ycor() + height)
screen = Screen()
yertle = Turtle('turtle')
yertle.speed('fastest')
yertle.penup()
pile(yertle, 60, 4, 5)
yertle.hideturtle()
screen.exitonclick()
然而,这是一个完美的例子,其中 stamping
比 filling
更简单。我们可以把乌龟本身做成等边三角形,然后移动,戳戳:
from turtle import Screen, Turtle
COLORS = ['red', 'orange', 'green', 'purple', 'yellow', 'pink', 'grey', 'brown', 'blue']
CURSOR_SIZE = 20
def pile(turtle, length, columns, rows):
turtle.shapesize(length / CURSOR_SIZE)
height = length * 3 ** 0.5 / 2
y_origin = turtle.ycor()
color = 0
for _ in range(columns):
for _ in range(rows):
turtle.color(COLORS[color % len(COLORS)])
turtle.stamp()
turtle.forward(height)
color += 1
turtle.setposition(turtle.xcor() + length, y_origin)
screen = Screen()
yertle = Turtle('triangle')
yertle.speed('fastest')
yertle.setheading(90)
yertle.penup()
pile(yertle, 60, 4, 5)
yertle.hideturtle()
screen.exitonclick()
不仅更简单,而且更快!