Editor

Chessboard in Python Turtle

Beginner · Shapes · 52 lines · draws in about 11 seconds

A loop inside a loop visits all 64 squares row by row, and whether the row number plus the column number is even decides which squares are dark.

Chessboard in Python Turtle: the finished drawing
import turtle
screen = turtle.Screen()
screen.bgcolor("#2f2f2f")
screen.title("Chessboard")
t = turtle.Turtle()
t.speed(10)
SIZE = 45 # width of one square
START = -4 * SIZE # the board's bottom-left corner is at (START, START)
BORDER = 24 # width of the wooden frame
def draw_square(x, y, size, color):
# A filled square with its bottom-left corner at (x, y)
t.penup()
t.goto(x, y)
t.pendown()
t.color(color)
t.begin_fill()
for i in range(4):
t.forward(size)
t.left(90)
t.end_fill()
# The wooden border: one big square behind the board
draw_square(START - BORDER, START - BORDER, 8 * SIZE + 2 * BORDER, "saddlebrown")
# 64 squares, row by row. When row + col is even the square is dark,
# so the bottom-left square is dark, as on a real board.
for row in range(8):
for col in range(8):
if (row + col) % 2 == 0:
color = "#b58863"
else:
color = "#f0d9b5"
draw_square(START + col * SIZE, START + row * SIZE, SIZE, color)
# Letters along the bottom and numbers up the side, on the border
t.penup()
t.color("wheat")
for col in range(8):
t.goto(START + col * SIZE + SIZE / 2, START - 20)
t.write("abcdefgh"[col], align="center", font=("Arial", 12, "bold"))
for row in range(8):
t.goto(START - 12, START + row * SIZE + SIZE / 2 - 9)
t.write(str(row + 1), align="center", font=("Arial", 12, "bold"))
t.hideturtle()
turtle.done()

How the code works

Names for the sizes

SIZE is the width of one square. START puts the bottom-left corner of the board 4 squares left of and below the centre, so the board sits in the middle, and BORDER is the width of the frame.

SIZE = 45              # width of one square
START = -4 * SIZE      # the board's bottom-left corner is at (START, START)
BORDER = 24            # width of the wooden frame

A square from its corner

The function moves to (x, y) with the pen up, then draws four sides with a 90 degree turn after each and fills them. The same function draws the frame and every square.

def draw_square(x, y, size, color):
    # A filled square with its bottom-left corner at (x, y)
    t.penup()
    t.goto(x, y)
    t.pendown()
    t.color(color)
    t.begin_fill()
    for i in range(4):
        t.forward(size)
        t.left(90)
    t.end_fill()

Rows and columns

One big brown square goes down first, as the frame. The outer loop picks a row and the inner loop walks along it; (row + col) % 2 == 0 is true on every other square, so the colours alternate along a row and swap over on the next one.

# The wooden border: one big square behind the board
draw_square(START - BORDER, START - BORDER, 8 * SIZE + 2 * BORDER, "saddlebrown")

# 64 squares, row by row. When row + col is even the square is dark,
# so the bottom-left square is dark, as on a real board.
for row in range(8):
    for col in range(8):
        if (row + col) % 2 == 0:
            color = "#b58863"
        else:
            color = "#f0d9b5"
        draw_square(START + col * SIZE, START + row * SIZE, SIZE, color)

Labels on the frame

"abcdefgh"[col] picks one letter, and str(row + 1) turns the row number into text. write() with align="center" centres each label on the middle of its square.

# Letters along the bottom and numbers up the side, on the border
t.penup()
t.color("wheat")
for col in range(8):
    t.goto(START + col * SIZE + SIZE / 2, START - 20)
    t.write("abcdefgh"[col], align="center", font=("Arial", 12, "bold"))
for row in range(8):
    t.goto(START - 12, START + row * SIZE + SIZE / 2 - 9)
    t.write(str(row + 1), align="center", font=("Arial", 12, "bold"))

Make it yours

Change the code above and press Run. Some ideas:

  • Change "#b58863" to "seagreen" and "#f0d9b5" to "beige" for a green board.
  • Change SIZE = 45 to SIZE = 30 for a smaller board; everything else is worked out from SIZE.
  • Change "saddlebrown" to "black" for a black frame.

What it uses

Every turtle command the site supports is listed on Python turtle online.