用Turtle绘制棋盘(偶数维度失败)

时间:2016-07-11 14:40:33

标签: python python-3.x loops turtle-graphics chess

我已经用乌龟图形编写了这个Python代码,用于按给定尺寸绘制棋盘。我面临的问题是,当我输入一个奇数时,一切正常:

  • 最后一个广场也被填满,我只是没有设法按时屏幕拍摄

但是当我输入偶数时,它就像:

enter image description here

以下是代码:

from turtle import *
import sys


def main():
    dimension = int(input('Enter dimension: '))
    side = 50

    x_coord = -250
    y_coord = 300

    turtle = Turtle()
    turtle.speed('fastest')
    turtle.pensize(5)

    for i in range(dimension ** 2):
        if not i % dimension:
            y_coord -= side
            turtle.penup()
            turtle.setx(x_coord)
            turtle.sety(y_coord)
            turtle.pendown()

        if not i % 2:
            turtle.begin_fill()

        for _ in range(4):
            turtle.forward(side)
            turtle.right(90)

        turtle.forward(side)
        turtle.end_fill()


if __name__ == '__main__':
    sys.exit(main())

4 个答案:

答案 0 :(得分:2)

A similar flag-based solution with alternate approaches. I don't understand what your main() layout gets you so I reworked it to be a potential library with the test code under __main__:

import turtle

def draw_board(dimension, x_coord, y_coord, side):

    parity = False

    for i in range(dimension ** 2):
        if i % dimension == 0:
            y_coord -= side
            turtle.penup()
            turtle.setpos(x_coord, y_coord)
            turtle.pendown()
            parity = parity != (dimension % 2 == 0)  # logical XOR

        if parity:
            turtle.begin_fill()

        for _ in range(4):
            turtle.forward(side)
            turtle.right(90)

        if turtle.filling():
            turtle.end_fill()

        turtle.forward(side)

        parity = not parity


if __name__ == '__main__':
    size = int(input('Enter dimension: '))

    turtle.speed('fastest')

    turtle.pensize(5)

    draw_board(size, -250, 300, 50)

    turtle.hideturtle()

    turtle.exitonclick()

答案 1 :(得分:1)

我没有查看你的代码,但似乎你遇到的问题是,与真正的棋盘不同,你在制作一个新的正方形线时会从白色变为黑色,反之亦然,这个例子:

黑,白,黑,白 黑,白,黑,白 等

或黑色,白色,黑色    白,黑,白    等

棋盘是:

黑,白,黑,白 白色,黑色,白色,黑色 BLACK ....等。

你看到了区别吗?

所以这似乎是问题,我试图修复你的代码,但我认为你可以管理

答案 2 :(得分:1)

我建议设置一个标志,指示何时填充,而不是仅当它是从该范围内的奇数时才执行,因为它不会从左到右变为黑白色,它到达然后结束然后从右到左。

无论如何,这是我的编辑,只是一个简单的布尔值,每次都会切换,除非转到新行。我还建议使用turtle.exitonclick代替sys.exit

from turtle import *

def main():
    dimension = int(input('Enter dimension: '))
    side = 50

    x_coord = -250
    y_coord = 300

    turtle = Turtle()
    turtle.speed('fastest')
    turtle.pensize(5)

    fill = False

    for i in range(dimension ** 2):
        if not i % dimension:
            y_coord -= side
            turtle.penup()
            turtle.setx(x_coord)
            turtle.sety(y_coord)
            turtle.pendown()
            if not dimension % 2:
                fill = not fill

        if fill:
            turtle.begin_fill()

        for _ in range(4):
            turtle.forward(side)
            turtle.right(90)

        turtle.forward(side)
        turtle.end_fill()
        fill = not fill

if __name__ == '__main__':
    main()
    exitonclick()

答案 3 :(得分:0)

对于Python 3,7,我会推荐这种方式

import turtle

turtle.speed(0)
turtle.up()
turtle.goto(-200,200)
turtle.down()

for row in range(8):

    for col in range(8):
        if col % 2 == row % 2:
            turtle.forward(50)
            continue
        turtle.begin_fill()

        for _ in range(4):
            turtle.forward(50)
            turtle.right(90)
        turtle.end_fill()
        turtle.forward(50)
    turtle.backward(400)
    turtle.right(90)
    turtle.forward(50)
    turtle.left(90)

turtle.width(6)

for _ in range(4):
    turtle.forward(400)
    turtle.left(90)

turtle.hideturtle()
turtle.done()