如何在range()
函数中修改x和y以使用turtle创建网格?我正在尝试使用乌龟图形创建一个在每个之间都具有固定间隔的正方形网格。现在我正在使用这个:
blocks = turtle.Turtle()
blocks.color("gray")
blocks.speed(0)
blocks.shape("square")
blocks.turtlesize(1.9)
blocks.penup()
blocks.setpos(-212, 130)
blocks_x = 85
blocks_y = 85
for pos in range(4):
blocks.setpos(blocks.xcor() + blocks_x, blocks.ycor())
blocks.stamp()
blocks.setpos(blocks.xcor(), blocks.ycor() - blocks_y)
for pos in range(4):
blocks.setpos(blocks.xcor() + blocks_x, blocks.ycor())
blocks.stamp()
blocks.setpos(blocks.xcor(), blocks.ycor() - blocks_y)
for pos in range(4):
blocks.setpos(blocks.xcor() + blocks_x, blocks.ycor())
blocks.stamp()
blocks.setpos(blocks.xcor(), blocks.ycor() - blocks_y)
for pos in range(4):
blocks.setpos(blocks.xcor() + blocks_x, blocks.ycor())
blocks.stamp()
blocks.setpos(blocks.xcor(), blocks.ycor() - blocks_y)
它可以执行我想要的操作,但是,我想知道是否有一种方法可以在打印完所有x之后是否修改y坐标,以便可以一次使用range()
函数? / p>
答案 0 :(得分:0)
尝试
turtle.goto([x coordinate],[y coordinate])
这基本上会将乌龟传送到这些坐标
答案 1 :(得分:0)
我从您的代码和描述中猜测,您需要类似以下内容,其中x位置和 y位置由嵌套for
循环控制:
from turtle import Screen, Turtle
BLOCKS_X, BLOCKS_Y = 85, 85
BLOCK_SIZE = 38
CURSOR_SIZE = 20
screen = Screen()
blocks = Turtle('square', visible=False)
blocks.penup()
blocks.color('gray')
blocks.speed('fastest')
blocks.turtlesize(BLOCK_SIZE / CURSOR_SIZE)
x_start, y_start = -1.5 * BLOCKS_X, 1.5 * BLOCKS_Y
blocks.setpos(x_start, y_start)
for _ in range(4):
for _ in range(4):
blocks.stamp()
blocks.forward(BLOCKS_X)
blocks.setpos(x_start, blocks.ycor() - BLOCKS_Y)
screen.mainloop()