python:简单的正方形网格

时间:2019-03-19 23:37:51

标签: python grid turtle-graphics

最近,我一直在尝试找出20 x 20的正方形网格。到目前为止,我只是想出了如何绘制4,无论我把#放在什么位置。如果有人能找出我的代码缺陷,将不胜感激!

我要实现的目标

what I'm trying to achieve

我的代码

import turtle
turtle.hideturtle()
t = turtle.Turtle()
t.hideturtle()

def draw_square(t, size, num, angle):

for i in range(num):
    for x in range(4):
        turtle.forward(size)
        turtle.left(90)
    turtle.right(angle)

draw_square(t, 25, 4, 90)

5 个答案:

答案 0 :(得分:1)

您的代码具有的嵌套循环。但是,考虑到绘制网格的方式,您确实需要三个嵌套循环。为了找出从您所要到达的地方最少的代码量,我想到了以下内容:

from turtle import Screen, Turtle

def draw_square(turtle, size, num):
    for y in range(num):
        for x in range(num):
            for _ in range(4):
                turtle.forward(size)
                turtle.left(90)

            turtle.forward(size)

        parity = y % 2 == 0
        turn = turtle.left if parity else turtle.right

        turn(90)
        turtle.forward(size * 2 * parity)
        turn(90)

screen = Screen()

yertle = Turtle(visible=False)
yertle.speed('fastest')  # because I have no patience

draw_square(yertle, 25, 20)

screen.exitonclick()

此代码在绘制时效率低下,因为要重绘相同的行,因此需要避免。我个人最喜欢的解决方案是使用生成器的 Tholian Web 方法:

from turtle import Turtle, Screen

UNIT_SIZE, GRID_SQUARES = 25, 20

GRID_SIZE = GRID_SQUARES * UNIT_SIZE

def half_grid(turtle):
    speed = turtle.speed()

    for brick in range(GRID_SQUARES):
        direction = [turtle.right, turtle.left][brick % 2 == 1]

        for _ in range(0, GRID_SIZE, speed):
            turtle.forward(speed)
            yield(0)

        direction(90)

        for _ in range(0, UNIT_SIZE, speed):
            turtle.forward(speed)
            yield(0)

        direction(90)

    for _ in range(0, GRID_SIZE, speed):
        turtle.forward(speed)
        yield(0)

heckle = Turtle(shape='arrow')
heckle.speed(5)  # speed needs to be a factor of UNIT_SIZE
heckle.penup()
heckle.goto(-GRID_SIZE / 2, -GRID_SIZE / 2)
heckle.pendown()
heckle.left(90)

jeckle = Turtle(shape='arrow')
jeckle.speed(5)
jeckle.penup()
jeckle.goto(GRID_SIZE / 2, -GRID_SIZE / 2)
jeckle.pendown()
jeckle.left(180)

generator1, generator2 = half_grid(heckle), half_grid(jeckle)

while (next(generator1, 1) + next(generator2, 1) < 2):
    pass

heckle.hideturtle()
jeckle.hideturtle()

screen = Screen()
screen.exitonclick()

enter image description here

但这可能是出于您的目的而过分...

答案 1 :(得分:0)

您的主要问题是,在每个方块之后,您将乌龟向左旋转90度。这意味着您将一遍又一遍地制作相同的4个正方形。

相反,您必须前进size单位乌龟并绘制下一个方块。这样做20次; 然后,您可以转到下一行。

为此,我建议您编写一个循环以绘制一行。然后写几行代码回到起点,但以size个单位为单位。在该过程中循环20次。

答案 2 :(得分:0)

有几种方法可以使用Python绘制20 x 20的网格,以下是几种方法:

使用乌龟:

import turtle

def draw_box(t,x,y,size,fill_color):
    t.penup() # don't draw
    t.goto(x,y) # move to position
    t.pendown() # draw

    t.fillcolor(fill_color)
    t.begin_fill()  # set fill color

    for i in range(0,4):
        grid.forward(size) # move forward
        grid.right(90) # turn right 90 degrees

    t.end_fill()


def draw_grid():
    fill_color = 'white'
    start_x = -320 # starting x position of the grid
    start_y = -100 # starting y position of the grid
    box_size = 30 # pixel size of each square in the grid
    for i in range(0,20): # 20x20 grid
        for j in range(0,20):
            draw_box(grid,start_x+j*box_size,start_y+i*box_size,box_size,fill_color)


grid = turtle.Turtle()
draw_grid()
turtle.done()

使用枕头(PIL):

from PIL import Image, ImageDraw

def main():
    steps = 25
    height = 600
    width = 600
    img = Image.new(mode='L', size=(height, width), color=255) # create blank image
    # img = Image.open('filename.png') # or load an image

    draw = ImageDraw.Draw(img)
    y_start = 0
    y_end = img.height
    step_size = int(img.width / steps)

    for x in range(0, img.width, step_size):
        line = ((x, y_start), (x, y_end))
        draw.line(line, fill='black')

    x_start = 0
    x_end = img.width

    for y in range(0, img.height, step_size):
        line = ((x_start, y), (x_end, y))
        draw.line(line, fill='black')

    del draw
    img.show()


if __name__ == '__main__':
    main()

答案 3 :(得分:0)

这是我做的一种有趣的方法,先绘制行然后列:

import turtle as tur


def make_grid(rows, columns):
    tur.color('black', 'white')
    tur.begin_fill()
    dir = 1
    tur.forward(20 * columns)
    for _ in range(rows):
        if dir > 0:
            tur.right(90)
            tur.forward(20)
            tur.right(90)
            tur.forward(20 * columns)
        else:
            tur.left(90)
            tur.forward(20)
            tur.left(90)
            tur.forward(20 * columns)
        dir *= -1
    tur.penup()
    tur.home()
    tur.pendown()
    tur.right(90)
    dir = 1
    tur.forward(20 * rows)
    for _ in range(columns):
        if dir > 0:
            tur.left(90)
            tur.forward(20)
            tur.left(90)
            tur.forward(20 * rows)
        else:
            tur.right(90)
            tur.forward(20)
            tur.right(90)
            tur.forward(20 * rows)
        dir *= -1
    tur.end_fill()
    tur.done()


make_grid(10, 10)

我认为像这样观看比与连续制作一堆正方形更有趣。显然,可以通过调整正方形大小来改进它。

答案 4 :(得分:0)

这是用于在python中绘制一个简单网格的代码,该网格具有可变的宽度,高度和网格中的单位数量。

if ($test === true) {

    // my cursor gets here

}