gpt4 book ai didi

python - 使用 range() 修改 x 和 y 以使用 Pythonturtle 创建网格

转载 作者:太空宇宙 更新时间:2023-11-03 21:41:03 25 4
gpt4 key购买 nike

如何修改 range() 函数中的 x 和 y 以使用 turtle 创建网格?我正在尝试使用 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() 运行一次?

最佳答案

我从您的代码和描述中猜测您想要如下所示的内容,其中 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()

enter image description here

关于python - 使用 range() 修改 x 和 y 以使用 Pythonturtle 创建网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52863806/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com