Draw a Tic Tac Toe Board Using Python – Turtle

Python's Turtle library provides an excellent way to create graphics using a virtual turtle that moves around the screen. In this article, we'll learn how to draw a tic-tac-toe board using simple turtle commands.

What is Tic Tac Toe Board?

Tic-tac-toe is a classic game played on a 3×3 grid board with 9 squares. Players take turns placing X and O marks, trying to get three of their marks in a row horizontally, vertically, or diagonally.

3×3 Grid Board

Essential Turtle Functions

Here are the key turtle functions we'll use to draw our board ?

Function Description
penup() Lifts the pen (no drawing)
pendown() Puts the pen down (starts drawing)
goto(x, y) Moves turtle to coordinates
forward(distance) Moves turtle forward
setheading(angle) Sets turtle direction

Drawing the Tic-Tac-Toe Board

Step 1: Setup and Horizontal Lines

First, we'll set up the turtle and draw two horizontal lines ?

import turtle

# Create window and setup turtle
window = turtle.Screen()
window.title("Tic Tac Toe Board")
turtle.pensize(5)
turtle.pencolor("dark blue")
turtle.speed(3)

# Draw first horizontal line
turtle.penup()
turtle.goto(-100, 50)
turtle.pendown()
turtle.forward(200)

# Draw second horizontal line  
turtle.penup()
turtle.goto(-100, -50)
turtle.pendown()
turtle.forward(200)

turtle.done()

Step 2: Adding Vertical Lines

Now we'll add the two vertical lines to complete the grid ?

import turtle

# Create window and setup turtle
window = turtle.Screen()
window.title("Tic Tac Toe Board")
turtle.pensize(5)
turtle.pencolor("dark blue")
turtle.speed(3)

# Draw horizontal lines
turtle.penup()
turtle.goto(-100, 50)
turtle.pendown()
turtle.forward(200)

turtle.penup()
turtle.goto(-100, -50)
turtle.pendown()
turtle.forward(200)

# Draw vertical lines
turtle.penup()
turtle.goto(-50, 100)
turtle.pendown()
turtle.setheading(270)  # Point downward
turtle.forward(200)

turtle.penup()
turtle.goto(50, 100)
turtle.pendown()
turtle.setheading(270)  # Point downward
turtle.forward(200)

# Hide turtle and keep window open
turtle.hideturtle()
turtle.done()

Enhanced Version with Color

Here's an enhanced version with a colored background and better styling ?

import turtle

# Setup window
window = turtle.Screen()
window.title("Colorful Tic Tac Toe Board")
window.bgcolor("lightgray")
window.setup(400, 400)

# Setup turtle
board = turtle.Turtle()
board.pensize(6)
board.pencolor("navy")
board.speed(5)

# Function to draw horizontal line
def draw_horizontal_line(y_pos):
    board.penup()
    board.goto(-120, y_pos)
    board.pendown()
    board.setheading(0)  # Point right
    board.forward(240)

# Function to draw vertical line  
def draw_vertical_line(x_pos):
    board.penup()
    board.goto(x_pos, 120)
    board.pendown()
    board.setheading(270)  # Point down
    board.forward(240)

# Draw the board
draw_horizontal_line(40)
draw_horizontal_line(-40)
draw_vertical_line(-40)
draw_vertical_line(40)

# Hide turtle
board.hideturtle()
window.exitonclick()  # Click to close

How It Works

The board consists of four lines that create a 3×3 grid:

  • Two horizontal lines: Positioned at y = 50 and y = -50
  • Two vertical lines: Positioned at x = -50 and x = 50
  • Coordinate system: Center of screen is (0, 0)
  • Line length: 200 pixels for complete grid coverage

Common Issues and Solutions

Error Cause Solution
NameError: 'turtle' not defined Missing import statement Add import turtle at the top
Lines don't connect properly Incorrect coordinates Use penup() before moving
Window closes immediately Missing done() Add turtle.done() at the end

Conclusion

Drawing a tic-tac-toe board with Python's turtle library demonstrates fundamental graphics programming concepts. By combining penup(), goto(), and forward() functions, you can create clean geometric shapes and grids for games or educational purposes.

Updated on: 2026-03-27T14:32:13+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements