海龟图形:重复方块
Turtle Graphics: Repeating Squares
我正在尝试创建一个循环,它接受用户的输入并绘制许多正方形,但它会增加每个循环的正方形大小,但是 2 边保持连接。我将包括图形以更好地解释。
import turtle
squares = 1
while squares >= 1:
squares = int(input('How many squares would you like drawn?:'))
if squares == 0:
print("You must have at-least 1 square.")
squares = int(input('How many squares would you like drawn?:'))
else:
for count in range(squares):
turtle.forward(30)
turtle.left(90)
turtle.forward(30)
turtle.left(90)
turtle.forward(30)
turtle.left(90)
turtle.forward(30)
turtle.left(90)
turtle.done()
输入请求和绘图逻辑应该分开。
这是一种方法,在增加边长后,returns 乌龟在每一轮开始时。
import turtle
num_squares = 3
t = turtle.Turtle()
t.pendown()
side = side_unit = 30
while True:
try:
num_squares = int(input('input the number of squares'))
except ValueError:
print("please enter an integer")
if num_squares > 3:
break
for sq in range(1, num_squares + 1):
t.left(90)
t.forward(side)
t.left(90)
t.forward(side)
t.left(90)
t.forward(side)
t.left(90)
side = side_unit + 3 * sq # increase the size of the side
t.goto(0,0) # return to base
turtle.done()
在等待@ReblochonMasque 的解决方案完成绘图 100 个方块时,有足够的时间来实施替代的、更快的解决方案,基于冲压.
首先要注意的是,在提供的说明中,它说要绘制 100 个正方形来创建图中的设计,但该图仅由不到 50 个正方形组成。它也以某种非积分方式进行了缩放,这使得它看起来具有不同的线条粗细。
让我们关注问题的本质而不是例子。 OP 的最小面积为 1 平方,所以我保留了它。该解决方案自然也倾向于将正方形置于 window:
的中心
from turtle import Turtle, Screen
DELTA = 3
MINIMUM = DELTA * 2
CURSOR_SIZE = 20
num_squares = -1
while num_squares < 1:
try:
num_squares = int(input('Input the number of squares: '))
except ValueError:
print("please enter an integer.")
if num_squares < 1:
print("You must have at least 1 square.")
screen = Screen()
turtle = Turtle("square", visible=False)
turtle.fillcolor("white")
for size in range(((num_squares - 1) * DELTA) + MINIMUM, MINIMUM - 1, -DELTA):
turtle.goto(turtle.xcor() + DELTA/2, turtle.ycor() - DELTA/2)
turtle.shapesize(size / CURSOR_SIZE)
turtle.stamp()
screen.exitonclick()
这显然不是 OP 正在寻找的解决方案,但也许下次出现这样的问题时,OP 至少会考虑。
我正在尝试创建一个循环,它接受用户的输入并绘制许多正方形,但它会增加每个循环的正方形大小,但是 2 边保持连接。我将包括图形以更好地解释。
import turtle
squares = 1
while squares >= 1:
squares = int(input('How many squares would you like drawn?:'))
if squares == 0:
print("You must have at-least 1 square.")
squares = int(input('How many squares would you like drawn?:'))
else:
for count in range(squares):
turtle.forward(30)
turtle.left(90)
turtle.forward(30)
turtle.left(90)
turtle.forward(30)
turtle.left(90)
turtle.forward(30)
turtle.left(90)
turtle.done()
输入请求和绘图逻辑应该分开。
这是一种方法,在增加边长后,returns 乌龟在每一轮开始时。
import turtle
num_squares = 3
t = turtle.Turtle()
t.pendown()
side = side_unit = 30
while True:
try:
num_squares = int(input('input the number of squares'))
except ValueError:
print("please enter an integer")
if num_squares > 3:
break
for sq in range(1, num_squares + 1):
t.left(90)
t.forward(side)
t.left(90)
t.forward(side)
t.left(90)
t.forward(side)
t.left(90)
side = side_unit + 3 * sq # increase the size of the side
t.goto(0,0) # return to base
turtle.done()
在等待@ReblochonMasque 的解决方案完成绘图 100 个方块时,有足够的时间来实施替代的、更快的解决方案,基于冲压.
首先要注意的是,在提供的说明中,它说要绘制 100 个正方形来创建图中的设计,但该图仅由不到 50 个正方形组成。它也以某种非积分方式进行了缩放,这使得它看起来具有不同的线条粗细。
让我们关注问题的本质而不是例子。 OP 的最小面积为 1 平方,所以我保留了它。该解决方案自然也倾向于将正方形置于 window:
的中心from turtle import Turtle, Screen
DELTA = 3
MINIMUM = DELTA * 2
CURSOR_SIZE = 20
num_squares = -1
while num_squares < 1:
try:
num_squares = int(input('Input the number of squares: '))
except ValueError:
print("please enter an integer.")
if num_squares < 1:
print("You must have at least 1 square.")
screen = Screen()
turtle = Turtle("square", visible=False)
turtle.fillcolor("white")
for size in range(((num_squares - 1) * DELTA) + MINIMUM, MINIMUM - 1, -DELTA):
turtle.goto(turtle.xcor() + DELTA/2, turtle.ycor() - DELTA/2)
turtle.shapesize(size / CURSOR_SIZE)
turtle.stamp()
screen.exitonclick()
这显然不是 OP 正在寻找的解决方案,但也许下次出现这样的问题时,OP 至少会考虑。