尝试使用Python中的Turtle绘制棋盘 - 如何填写其他所有方块?

时间:2014-02-12 02:08:54

标签: python loops turtle-graphics

Python新手,我正在尝试绘制棋盘格。我绘制了电路板,但现在我必须定义一个函数(一个循环),用黑色填充每个其他正方形。我一直试图写一个循环来做这个有一段时间,有人可以帮忙吗?

这是我的代码:

import turtle


def drawGrid():
turtle.penup()
turtle.goto(-300, 250)
turtle.pendown()
turtle.forward(300)
turtle.right(90)
turtle.forward(300)
turtle.right(90)
turtle.forward(300)
turtle.right(90)
turtle.forward(300)

def drawColumns():
for i in range(4):
    turtle.right(90)
    turtle.forward(37.5)
    turtle.right(90)
    turtle.forward(300)
    turtle.left(90)
    turtle.forward(37.5)
    turtle.left(90)
    turtle.forward(300)

def drawRows():
turtle.left(180)
rows = 0 
while rows <= 3:
    rows += 1
    turtle.forward(37.5)
    turtle.right(90)
    turtle.forward(300)
    turtle.left(90)
    turtle.forward(37.5)
    turtle.left(90)
    turtle.forward(300)
    turtle.right(90)

def main():
drawGrid()
drawColumns()
drawRows()
if __name__ == "__main__":
main()

感谢您的帮助!

4 个答案:

答案 0 :(得分:3)

海龟填充方法适用于形状,即完全有界的区域。因此,不是绘制网格,而是需要考虑绘制一系列正方形。

所以让我们首先定义一个简单的函数来绘制一个填充的正方形。它需要一个乌龟物体和一个长度的侧面。     进口龟

def draw_filled_square(this_turtle, size):    
    """Draw a square by drawing a line and turning through 90 degrees 4 times"""
    this_turtle.pendown()
    this_turtle.fill(True)
    for _ in range(4):
        this_turtle.forward(size)
        this_turtle.right(90)
    this_turtle.fill(False)
    this_turtle.penup()

我们可以这样称呼它:

window = turtle.Screen()
myturtle = turtle.Turtle()
square_size = 90
myturtle.goto(-300, 200)

draw__filled_square(myturtle, square_size)

其中绘制一个方格。请注意,它会将其放回起始位置,因此我们需要在绘制下一个方格之前移动它。

现在,在实践中,只要我们绘制框的轮廓我们只需要绘制实心方块,未填充的方块可以表示负空间。但出于解释的目的,我也将绘制它们。

为未填充的方块定义函数很简单 - 只需复制现有函数,但在开头设置this_turtle.fill(False)。

任何时候需要在重复序列中计算某些东西(1,2,3,4,1,2,3,4 ......),它需要使用modulo(余数)。模数意味着余数,所以如果x modulo y为0,则意味着x可以被y整除。这转化为代码,就好像x%y == 0:

这是一个简单的鼓机来演示:

def drum_loop(x):
     # go bang on the fourth beat
     if x % 4 == 0:
         print("bang!")
     else:
         print("tish")

# prints tish, tish, tish, bang! tish, tish, tish, bang!
for i in range(1,9):
   drum_loop(i)

交替就像重复计数0,1,0,1一样。

所以我们可以这样绘制一行:

for i in range(8): 
    if i % 2 == 0:
        draw_filled_square(myturtle, square_size)
    else:
        draw_unfilled_square(myturtle, square_size)
    # move to start of next square
    myturtle.forward(square_size)

现在只是重复这个并不是可以做到的,但是应该很清楚你可以再次使用模2来使行正常交替。

通过定义一个行函数来执行此操作,该行函数将以黑色开始并以白色方块开始,然后从另一个循环中调用此函数。 (不要忘记每次开始行时都回到开头并向下移动。)

答案 1 :(得分:0)

import turtle

turtle.pensize(2)
turtle.penup()
turtle.goto(-160,-160)
turtle.pendown()
turtle.color("black")

for i in range(4):
    turtle.forward(320)
    turtle.left(90)

for y in range(-160,160,80):
    for x in range(-160,160,80):
        turtle.begin_fill()
        turtle.penup()
        turtle.goto(x,y)
        turtle.pendown()

        for k in range(4):
            turtle.forward(40)
            turtle.left(90)

        turtle.end_fill()       

for y in range(-120,160,80):
    for x in range(-120,160,80):
        turtle.begin_fill()
        turtle.penup()
        turtle.goto(x,y)
        turtle.pendown()
        for k in range(4):
            turtle.forward(40)
            turtle.left(90)
        turtle.end_fill()   

turtle.done()

答案 2 :(得分:0)

这是另一个例子,其中绘图为穷龟创造了比简单标记更多的工作。不要考虑绘制和填充框,而应将板本身视为一个填充的正方形,其上印有其他填充的正方形:

from turtle import Turtle, Screen

NUMBER_SQUARES = 8
SQUARE_SIZE = 40
BOARD_SIZE = SQUARE_SIZE * NUMBER_SQUARES
BORDER_FRACTION = 1.025  # add a slight edge to board

STAMP_SIZE = 20  # size of turtle square image

turtle = Turtle(shape='square', visible=False)
turtle.shapesize(BOARD_SIZE / STAMP_SIZE * BORDER_FRACTION)
turtle.color('black')
turtle.stamp()

turtle.shapesize(SQUARE_SIZE / STAMP_SIZE)
turtle.color('white')
turtle.penup()

for y in range(-NUMBER_SQUARES//2, NUMBER_SQUARES//2):
    parity = y % 2 == 0

    for x in range(-NUMBER_SQUARES//2, NUMBER_SQUARES//2):
        if parity:
            turtle.goto(x * SQUARE_SIZE + SQUARE_SIZE//2, y * SQUARE_SIZE + SQUARE_SIZE//2)
            turtle.stamp()

        parity = not parity

Screen().exitonclick()

此解决方案可以打印任何两种颜色(例如黑色和红色)的纸板,它不会呈现白色背景。只是通过冲压更好地生活的另一个例子。

答案 3 :(得分:0)

让我们看一下之字形方法(代码说明在内部注释)

import turtle

def drawGrid(rows, cols, size):
    turtle.sety(turtle.ycor() + size * rows) # Draw the initial line
    f = size * 2
    turtle.begin_fill()
    for j in range(cols): # Make the columns
        if j % 2:
            turtle.backward(f) # Make room for the zig-zag up and zig-zag down
        for i in range(rows * 2): # rows * 2 because there will be both zig-zag down and a zig-zag up for each row
            turtle.forward(size)
            turtle.right(90)
            if i % 2:
                turtle.left, turtle.right = turtle.right, turtle.left # Switch the zig-zag direction
        turtle.left(180)
    turtle.end_fill()
    turtle.sety(turtle.ycor() - size * rows) # Draw the final line
    
turtle.penup()
turtle.goto(-300, -50) # Set the bottom-left position of the grid
turtle.pendown()
turtle.speed("fastest") # Because I have no patience
drawGrid(8, 8, 40)

输出:

enter image description here

请注意,行和列的数量必须是偶数才能起作用。