Creating a chessboard in Python using Turtle

Building upon your previous experience of creating a chessboard in the Python console, let’s now explore how to visualise the chessboard in Python using turtle graphics module. This approach will provide a graphical representation, enhancing the visual appeal and interactivity of your project.

Introduction to Turtle Graphics

The turtle module in Python offers a simple way to draw shapes and patterns using a virtual “turtle” that moves on the screen. It’s an excellent tool for creating graphics programmatically and is especially useful for educational purposes.

Steps to Draw a Chessboard Using Turtle

  1. Import the Turtle Module: Begin by importing the turtle module, which comes standard with Python installations. import turtle
  2. Set Up the Screen: Configure the drawing window (screen) where the turtle will render the chessboard. screen = turtle.Screen() screen.title("Chessboard") screen.bgcolor("white")
  3. Initialize the Turtle: Create a turtle object that will be used to draw the squares. drawer = turtle.Turtle() drawer.speed(0) # Fastest drawing speed
  4. Define the Square Drawing Function: Create a function that draws a filled square of a given size and color at a specified position. def draw_square(turtle, x, y, size, color): turtle.penup() turtle.goto(x, y) turtle.pendown() turtle.fillcolor(color) turtle.begin_fill() for _ in range(4): turtle.forward(size) turtle.right(90) turtle.end_fill()
  5. Draw the Chessboard: Utilize nested loops to draw an 8×8 grid of alternating colored squares. square_size = 50 # Length of each square's side start_x = -200 # Starting x-coordinate start_y = 200 # Starting y-coordinate for row in range(8): for col in range(8): x = start_x + col * square_size y = start_y - row * square_size color = "black" if (row + col) % 2 == 0 else "white" draw_square(drawer, x, y, square_size, color)
  6. Finalise the Drawing: Hide the turtle cursor and display the window until closed by the user. drawer.hideturtle() turtle.done()

Complete Code Example

Combining all the steps, here’s the full script to draw the chessboard:

import turtle

def draw_square(turtle, x, y, size, color):
    """Draws a square at (x, y) with the specified size and color."""
    turtle.penup()
    turtle.goto(x, y)
    turtle.pendown()
    turtle.fillcolor(color)
    turtle.begin_fill()
    for _ in range(4):
        turtle.forward(size)
        turtle.right(90)
    turtle.end_fill()

# Set up the screen
screen = turtle.Screen()
screen.title("Chessboard")
screen.bgcolor("white")

# Initialize the turtle
drawer = turtle.Turtle()
drawer.speed(0)  # Fastest drawing speed

# Constants
square_size = 50  # Length of each square's side
start_x = -200    # Starting x-coordinate
start_y = 200     # Starting y-coordinate

# Draw the chessboard
for row in range(8):
    for col in range(8):
        x = start_x + col * square_size
        y = start_y - row * square_size
        color = "black" if (row + col) % 2 == 0 else "white"
        draw_square(drawer, x, y, square_size, color)

# Finalize the drawing
drawer.hideturtle()
turtle.done()

Explanation

  • Coordinate System: The turtle graphics window uses a coordinate system where the center is (0,0). Positive x-values extend to the right, and positive y-values extend upward. Adjusting start_x and start_y positions the chessboard appropriately on the screen.
  • Color Alternation: The expression (row + col) % 2 == 0 determines the color of each square. If the sum of the row and column indices is even, the square is colored black; otherwise, it’s white. This creates the classic checkerboard pattern.

By following these steps, you can create a visually appealing chessboard using Python’s turtle module. This graphical representation not only enhances the user experience but also provides a foundation for further developments, such as adding chess pieces or implementing game logic.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top