在Python上绘制棋盘格

在Python上绘制棋盘格,python,Python,我想在Python上画一个棋盘,但我只得到一个黑色方块。 你能帮我修一下这个程序吗 import turtle def filled_square(size, color, x, y): turtle.setpos(x, y) turtle.color(color) turtle.begin_fill() for i in range(4): angle = 90 turtle.fd(size) turtle.lt(ang

我想在Python上画一个棋盘,但我只得到一个黑色方块。 你能帮我修一下这个程序吗

import turtle
def filled_square(size, color, x, y):
    turtle.setpos(x, y)
    turtle.color(color)
    turtle.begin_fill()
    for i in range(4):
       angle = 90
       turtle.fd(size)
       turtle.lt(angle)
    turtle.end_fill()
    turtle.up()
import sys
n = int(sys.argv[1])
s = int(sys.argv[2])
square_size = s//n
y=0
for i in range(n):
    x = 0
    for j in range(n):
        if (i+j)%2==0:
           filled_square(square_size, "red", x, y)
        else:
           filled_square(square_size, "black", x, y)
    x+=square_size
turtle.down()
turtle.done()
这里有几个问题

  • 在下一次迭代中将x重置为零时,增加x的值没有多大意义,因此初始赋值应该在for循环之上
  • 您永远不会更新y的值

现在你应该得到你想要的棋盘形状


替代解决方案:您可以通过根本不拥有x和y的值来避免簿记这些值的问题。你可以直接从i和j导出正方形的坐标

for i in range(n):
    for j in range(n):
        if (i+j)%2==0:
            filled_square(square_size, "red", i*square_size, j*square_size)
        else:
            filled_square(square_size, "black", i*square_size, j*square_size)
整合if和else块中的公共逻辑,并且只区分实际更改的值(即颜色)可能也很好


您需要同时增加x和y。还要注意,x在内部循环中应该递增。这是工作代码

import turtle
def filled_square(size, color, x, y):
    turtle.setpos(x, y)
    turtle.color(color)
    turtle.begin_fill()
    for i in range(4):
        angle = 90
        turtle.fd(size)
        turtle.lt(angle)
    turtle.end_fill()
    turtle.up()
import sys
n = int(sys.argv[1])
s = int(sys.argv[2])
square_size = s//n
y=0
for i in range(n):
    x = 0
    for j in range(n):
        if (i+j)%2==0:
            filled_square(square_size, "red", x, y)
        else:
            filled_square(square_size, "black", x, y)
        x+=square_size
    y+=square_size
turtle.down()
turtle.done()

我建议在棋盘/游戏工作中使用这样的函数式编程。这将使一切更易于维护,并使实现新功能更容易

import turtle

def filled_square(size, color, x, y):
    turtle.up()
    turtle.setpos(x, y)
    turtle.color(color)
    turtle.begin_fill()
    for i in range(4):
        angle = 90
        turtle.fd(size)
        turtle.lt(angle)
    turtle.end_fill()

def board(length, size, x_pos=0, y_pos=0):
    for y in range(length):
        for x in range(length):
            if (x+y)%2==0:
                filled_square(
                    size,  "red", (x * square_size) - x_pos, (y * square_size) - y_pos)
            else:
                filled_square(
                    size, "black", (x * square_size) - x_pos, (y * square_size) - y_pos)
    turtle.done()
关键字参数也很好。如果您决定添加更多颜色,只需添加两个参数即可

def board(length, size, x_pos=0, y_pos=0, color1="red", color2="black"):
    for y in range(length):
        for x in range(length):
            if (x+y)%2==0:
                filled_square(
                    size,  color1, (x * square_size) - x_pos, (y * square_size) - y_pos)
            else:
                filled_square(
                    size, color2, (x * square_size) - x_pos, (y * square_size) - y_pos)

您的代码格式出现问题。咨询和你的帖子。我修正了我的代码。很好的努力:-)但它仍然不太正确。我在
x=0
行上得到一个缩进错误。(你可能会想“这对你自己来说很容易,对吧?”这是真的,但我真的很想完全确定原始缩进是什么。因为这肯定关系到你的
x+=square\u size
行有多少缩进,我们无法从上下文中明确推断)用i和j填充正方形?我重新修改了代码。我很感谢你的帮助:)
import turtle
def filled_square(size, color, x, y):
    turtle.setpos(x, y)
    turtle.color(color)
    turtle.begin_fill()
    for i in range(4):
        angle = 90
        turtle.fd(size)
        turtle.lt(angle)
    turtle.end_fill()
    turtle.up()
import sys
n = int(sys.argv[1])
s = int(sys.argv[2])
square_size = s//n
y=0
for i in range(n):
    x = 0
    for j in range(n):
        if (i+j)%2==0:
            filled_square(square_size, "red", x, y)
        else:
            filled_square(square_size, "black", x, y)
        x+=square_size
    y+=square_size
turtle.down()
turtle.done()
import turtle

def filled_square(size, color, x, y):
    turtle.up()
    turtle.setpos(x, y)
    turtle.color(color)
    turtle.begin_fill()
    for i in range(4):
        angle = 90
        turtle.fd(size)
        turtle.lt(angle)
    turtle.end_fill()

def board(length, size, x_pos=0, y_pos=0):
    for y in range(length):
        for x in range(length):
            if (x+y)%2==0:
                filled_square(
                    size,  "red", (x * square_size) - x_pos, (y * square_size) - y_pos)
            else:
                filled_square(
                    size, "black", (x * square_size) - x_pos, (y * square_size) - y_pos)
    turtle.done()
def board(length, size, x_pos=0, y_pos=0, color1="red", color2="black"):
    for y in range(length):
        for x in range(length):
            if (x+y)%2==0:
                filled_square(
                    size,  color1, (x * square_size) - x_pos, (y * square_size) - y_pos)
            else:
                filled_square(
                    size, color2, (x * square_size) - x_pos, (y * square_size) - y_pos)