如何在不使用 define 的情况下使用 turtle-graphics 和嵌套循环创建棋盘?

How do I create a chessboard using turtle-graphics and nested loops without using define?

我正在尝试使用 Python 的嵌套循环来创建棋盘。我很难弄清楚如何用黑色填充特定的框以及如何创建 64 个框。到目前为止我的编码是:

import turtle

t = turtle.Turtle()
t.speed(0)
t.penup() 
t.goto(0, 0)
t.pendown() 

for j in range(-150, 100, 50):
    for i in range(-150, 150, 50):
        t.penup()
        t.goto(i, j)
        t.pendown()
        t.begin_fill()
        for k in range(4):
            t.forward(50) 
            t.left(90) 
            t.color("black")
            t.end_fill()

for j in range(-100, 150, 50):
    for i in range(-100, 150, 50):
        t.penup()
        t.goto(i, j)
        t.pendown()
        t.begin_fill()
        for k in range(4):
            t.forward(50) 
            t.left(90) 
            t.end_fill()
            t.hideturtle()

turtle.done()

尝试把画框封装成一个函数,让逻辑更清晰。您还可以切换填充标志并将其传递给函数。

import turtle

t = turtle.Turtle()
t.speed(0)
t.penup() 
t.goto(0, 0)
t.pendown() 

side=50  # height and width of box

def drawblock(x,y,fill):  # draw one box
        t.penup()
        t.goto((0-4+x)*side, (0-4+y)*side)  # 0,0 is center of screen
        t.pendown()
        if fill: t.begin_fill()
        for k in range(4):
            t.forward(50) 
            t.left(90) 
            t.color("black")
        if fill: t.end_fill()

fill = True
for x in range(8):
    fill = not fill  # toggle column
    for y in range(8):
       drawblock(x,y, fill)
       fill = not fill  # toggle row
    
turtle.done() 

---更新---

如果您想在不创建函数的情况下执行此操作,只需将函数逻辑复制到主循环中即可。

import turtle

t = turtle.Turtle()
t.speed(0)
t.penup() 
t.goto(0, 0)
t.pendown() 

side=50  # height and width of box

fill = True
for x in range(8):
    fill = not fill  # toggle column
    for y in range(8):
       #drawblock(x,y, fill)
        t.penup()
        t.goto((0-4+x)*side, (0-4+y)*side)  # 0,0 is center of screen
        t.pendown()
        if fill: t.begin_fill()
        for k in range(4):
            t.forward(50) 
            t.left(90) 
            t.color("black")
        if fill: t.end_fill()
        fill = not fill  # toggle row
    
turtle.done() 

输出

这样做就可以了:

import turtle

def drawSquare(turtule, isBlack = False):
  if isBlack:
    t.begin_fill()
  for k in range(4):
    t.forward(50) 
    t.left(90) 
    t.color("black")
  t.end_fill()

t = turtle.Turtle()
t.speed(0)
t.penup() 
t.goto(0, 0)
t.pendown() 

lastWhite = False
for j in range(-150, 250, 50):
  lastWhite = not lastWhite
  for i in range(-150, 250, 50):        
      t.penup()
      t.goto(i, j)
      t.pendown()
      if lastWhite:
        drawSquare(t, True)
        lastWhite = False
      else:
        drawSquare(t)
        lastWhite = True


t.hideturtle()
turtle.done()

编辑:此代码不使用 drawSquare 方法

import turtle

t = turtle.Turtle()
t.speed(0)
t.penup() 
t.goto(0, 0)
t.pendown() 

lastWhite = False
for j in range(-150, 250, 50):
  lastWhite = not lastWhite
  for i in range(-150, 250, 50):        
      t.penup()
      t.goto(i, j)
      t.pendown()
      if lastWhite:        
        t.begin_fill()
        for k in range(4):
          t.forward(50) 
          t.left(90) 
          t.color("black")
        t.end_fill()       
        lastWhite = False
      else:
        for k in range(4):
          t.forward(50) 
          t.left(90) 
          t.color("black")
        lastWhite = True


t.hideturtle()
turtle.done()


这里有一个稍微复杂的方法,它提供了更大的灵活性并利用模数运算符进行交替。

import turtle

# Define our board and tile dimensions.
board = {
    "width": 8,
    "height": 8
}

tile = {
    "width": 50,
    "height": 50
}

# Setup the turtle.
t = turtle.Turtle()
t.speed(0)
t.penup()
t.goto(0, 0)
t.color("black");

# Loop over the length of the board width and height.
for tile_x in range(board["width"]):
    for tile_y in range(board["height"]):
        
        # Before drawing tile, move to the current X and Y location in the loops multiplied by the tile width and height.
        t.goto(tile_x * tile["width"], tile_y * tile["height"])
        
        # Only turn on the fill for every odd number tile.
        if ((tile_x + tile_y) % 2):
            t.begin_fill()
        
        # Start drawing the tile.
        t.pendown()
        for side in range(4):
            # Using the side variable, we can specify the tile width or height (Side 0 and 2 use width and 1 and 3 use height).
            t.forward((tile["height"] if side % 2 else tile["width"]))
            t.left(90)
        
        # End fill regardless of whether we started.
        t.end_fill()
        # Stop drawing between tiles.
        t.penup()
    
turtle.done()

样本