turtle.setpos() xy轴包含在while循环中

问题描述 投票:0回答:1

我一直在玩乌龟并尝试沿 x 和 y 轴绘制正方形网格。

我使用 while 循环沿 x 轴绘制正方形,但是我无法沿 y 轴绘制正方形。如果有人能够提供一些指导,那就太好了。

谢谢

import turtle

def square(x,y):
    turtle.penup()
    turtle.setpos(x,y)
    turtle.pendown()
    for i in range(4):
        turtle.forward(60)
        turtle.right(90)

y = 300
x = -100
while x < 300:
x += 100

while y < 300:
    y -= 100
    if y == 0:
        break
square(x, y)
python turtle-graphics
1个回答
0
投票

这应该更像你希望的那样。您可能需要调整 x 和 y 的起始值以及增量才能根据需要绘制正方形:

import turtle

def square(x,y):
    turtle.penup()
    turtle.setpos(x,y)
    turtle.pendown()
    for i in range(4):
        turtle.forward(60)
        turtle.right(90)

y = 300
x = -100

while x < 300:
    x += 100
    square(x, y)

while y > 0:
    y -= 100
    square(x, y)
© www.soinside.com 2019 - 2024. All rights reserved.