更改N列和M行的Python Turtle代码

2024-05-14 15:35:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我的代码已经准备好显示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()

您可以在verniket.io/python上运行它来查看它当前是谁


Tags: inforifdefrangecolumnfillpu
1条回答
网友
1楼 · 发布于 2024-05-14 15:35:44

I have my code ready to show 3x3 equilateral triangles

不,不是真的。您已经准备好了显示1x3等边三角形的代码,只需将代码复制三次以模拟第二个维度。你的问题是你不能复制你的代码M次来解决这个问题

在这种情况下,答案是更少,但更聪明的代码。我们需要NM的嵌套循环,以及每个M按长度l向前移动,每个N按高度t垂直向前移动的能力:

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()

然而,这是一个完美的例子,stampingfilling简单。我们可以把海龟本身做成一个等边三角形,然后移动它并戳它:

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()

不仅简单,而且更快

相关问题 更多 >

    热门问题