Python 乌龟在矩形中绘制相等的单元格

Python turtle drawing equal cells in a rectangle

我正在尝试使用 turtle 来绘制一个矩形,然后在其中绘制 32 个相等的单元格。但是我不知怎么的就是做不对,我不知道为什么。

这是我写的代码:

import turtle, tkinter, datetime, time

turtle.setx(-400)
turtle.sety(200)
turtle.pendown()
turtle.pencolor('#00807c')
for i in range (0,4):
    x = 800
    if i%2 == 0:
        turtle.fd(x)
    else:
        turtle.fd(x/2)
    turtle.right(90)

def cells(position):
    for i in range (0,4):
        x = 100
        turtle.fd(x)
        turtle.right(90)
        if turtle.pos() == position:
            turtle.fd(x)
            position = turtle.pos()

for j in range(0, 8):
    cells(turtle.pos())
turtle.done()

结果很奇怪,只画了三四个单元格就结束了。 如果有人可以帮助我解决这个问题,我将不胜感激。谢谢

我已经重写了你的代码,但我不明白你为什么要使用函数。我使用了 3 个循环:

loop 3(4):
   loop 2(8):
       loop 1(4):
  1. 第一个循环重复4次并绘制1个正方形的边
  2. 第二个循环运行第一个循环 8 次,所以它绘制了 8 个相邻的正方形
  3. 第三个循环运行第二个循环4次,所以它绘制了4行8个正方形。 这样就形成了一个包含 32 个单元格的区域。

我的代码:

import turtle, tkinter, datetime, time
turtle.penup()
turtle.hideturtle()
turtle.setx(-400)
turtle.sety(200)
turtle.pendown()
turtle.pencolor('#00807c')
turtle.speed(0)
turtle.pendown()
for i in range (4):
    x = 800
    if i%2 == 0:
        turtle.fd(x)
    else:
        turtle.fd(x/2)
    turtle.right(90)

for w in range (4):
        for i in range (8):
            for i in range (4):
                x = 100
                turtle.fd(x)
                turtle.right(90)
            turtle.forward(x)
        turtle.penup()
        turtle.goto(-400,turtle.ycor()-100)
        turtle.pendown()

turtle.done()

PS:我还更改了一些内容,例如:

  • 我把乌龟藏起来了
  • 我改变了速度(最大)
  • 我在一开始移动海龟之前添加了一个turtle.penup()命令,所以你看不到黑线。

亲切的问候

螺内酯

你可以创建两个函数,简化逻辑:一个画正方形,一个把海龟定位到画正方形的地方。然后,在两个嵌套循环(一个用于行,一个用于列)中使用一点索引算法,使用索引值和正方形的边长在正确的位置绘制:

可能是这样的:

import turtle, tkinter


def draw_square(side):
    """draws a square of side=side starting at the current turtle location
    """
    turtle.pendown()
    turtle.setheading(0)
    for _ in range(4):
        turtle.forward(side)
        turtle.left(90)
    turtle.penup()
    
def draw_grid(rows, cols):
    """positions the turtle at the correct location, 
    in order to draw a grid of squares
    """
    for jdx in range(rows):
        for idx in range(cols):
            turtle.penup()
            turtle.goto(startx + side*idx, starty + side*jdx)
            draw_square(side)
    

turtle.pencolor('#00807c')
side = 20
startx, starty = 0, 0          # this can be changed, 
                               # other locations used are relative to this starting point
turtle.penup()
turtle.goto(startx, starty)

rows, cols = 4, 8              # this can be changed
draw_grid(rows, cols)

turtle.goto(startx, starty)    # return to the starting point

   
turtle.done()

在这种情况下,我会从 绘图 切换到 冲压 以简化代码并加快速度:

from turtle import Screen, Turtle

WIDTH, HEIGHT = 800, 400

SQUARE = 100

CURSOR_SIZE = 20

def cells(t):
    x, y = t.position()

    for dy in range(HEIGHT // SQUARE):
        turtle.goto(x, y + dy * SQUARE)

        for dx in range(WIDTH // SQUARE):
            turtle.stamp()
            turtle.forward(SQUARE)

screen = Screen()

turtle = Turtle()
turtle.hideturtle()
turtle.shape('square')
turtle.shapesize(HEIGHT / CURSOR_SIZE, WIDTH / CURSOR_SIZE)
turtle.color('#00807c', 'white')
turtle.speed('fastest')
turtle.penup()

turtle.stamp()  # draw outer perimeter

turtle.shapesize(SQUARE / CURSOR_SIZE)
turtle.goto(SQUARE/2 - WIDTH/2, SQUARE/2 - HEIGHT/2)

cells(turtle)  # draw inner squares

screen.exitonclick()

我还会从代码主体中删除 magic 数字,并在开头声明它们,以便于调整它们。