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

1.1k views Asked by At

I am trying to create a chessboard using nested loops with Python. I am having a hard time figuring out how to fill specific boxes with black and how to create 64 boxes. My coding so far is:

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()
3

There are 3 answers

0
charbel On BEST ANSWER

This will do the trick:

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()

EDIT: this code doesn't use the drawSquare method

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()


4
Mike67 On

Try encapsulating the box draw in a function to make the logic clearer. You can also toggle the fill flag and pass it to the function.

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() 

--- UPDATE ---

If you want to do this without creating a function, just copy the function logic into the main loop.

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() 

Output

Chess

0
Zei On

Here's a slightly more complex approach that offer greater flexibility and makes use of the modulus operator for alternation.

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()

Sample A chess board using modulus function to alternate tile fill.