Draw a Tic Tac Toe Board Using Python – Turtle


The wide range of libraries and tools makes python a popular and widely used programming language. One of the library which we will use in this article to complete the task of drawing a tic tac toe is Turtle. This library helps in creating graphics by using a virtual turtle. The commands are given to the virtual turtle on the window. In this article, we will use simple functions of the turtle library to draw the tic tac toe.

What is Tic Tac Toe Board?

Tic tac toe is a popular game, played on a 3*3 grid board. The 9 squares are present on the board, where the game is played using X and 0 as the marks. You can win the game by filling any three of your mark in a row. The first person filling the row either horizontally or vertically wins the match.

Introduction to Turtle Library

Python offers many built in libraries turtle is one of them. Turtle library helps in graphical part; the presence of variety of functions and methods makes it a unique library. You can create complex graphics and animations with less lines of code. This feature helps the developer to make things simpler for the user. Some of the functions or methods present in this library are as follows.

S.No

Function and Description

1

turtle()

This function is for creating a new turtle object.

2

right(angle)

This function turns the turtle right by the angle mentioned.

3

left(angle)

This function turns the turtle left by the angle mentioned.

4

turtle.forward(distance)

This function moves the turtle pointer/pen forward.

5

turtle.backward()

This function moves the turtle pointer/pen backward.

6

penup()

This function stops the turtle from drawing the lines, this function lifts the turtle’s pen up.

7

pendown()

This function puts the turtle’s pen down, for drawing lines.

8

speed (speed)

This function is used to adjust the movement of the turtle’s pen.

9

turtle.hideturtle()

This function is used to hide the turtle’s pen from the view.

10

turtle.showturtle()

This function is used to display the turtle’s pen on the screen.

11

turtle.colorturtle()

This function is used to set up the color of the turtle pen.

12

turtle.begin_fill()

This function is used to begin the process of filling the shape.

13

turtle.end_fill()

This function is used to end the filling process.

These are some of the functions which will also be included while drawing tic tac toe.

Steps to be Followed

Following are the steps to be followed to create a tic-tac-toe using Python –

Step 1: Importing turtle library is the first and most important step while working with it. Then we will create a window using the ‘turtle.Screen()’ function. Let’s set up the turtle’s pen size and the color.

We have used turtle.pensize(size) and turtle.pencolor(“color”) for giving color and size to it. The next step is to write codes for drawing the tic tac toe board. Tic tac toe board consists of four parallel horizontal and vertical lines that is two each. For drawing these lines, we have used four different functions.

Step 2: Start with the first two parallel horizontal lines. The functions used are ‘turtle.penup()’ function it lifts turtle’s pen up, ‘turtle.goto()’ for changing the position of turtle, ‘turtle.pendown()’ for starting the drawing, and turtle.forward() to draw the line up to the length mentioned. This is the first set of code for drawing the two parallel horizontal lines.

Step 3: The next set of codes will help us draw the two parallel vertical lines for the tic-tac-toe board. Same functions are used for drawing the vertical lines with an exception that is ‘turtle.setheading()’. This function helps in changing the direction of the turtle. Here, we have specified the angle to 270 so that the turtle moves in the downward direction that is towards the negative y direction.

These coordinates will ask the turtle to move in downward direction “turtle.forward()”, and for the upward direction, we have mentioned turtle.backward(). Hence, the coordinates play a major role in deciding the direction of the turtle head movement. You can close the window after the task is completed.

Example

Let’s write a code using the turtle library functions for drawing a tic-tac-toe board.

import turtle
# Create the window for the graphics to be seen
window = turtle.Screen()
# setting up the turtle 
turtle.pensize(5)
turtle.pencolor("dark blue")
# Draw the two parallel 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 the two parallel vertical lines
turtle.penup()
turtle.goto(-50, 100)
turtle.pendown()
turtle.setheading(270) 
turtle.forward(200)

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

# Close the window 
turtle.done()

Output

Common Errors and Their Solution

  • ‘TypeError: ‘int’ object is not callable’ this error is found if you are using turtle object as a function not as a method. For avoiding this error, you should use dot notation for solving the purpose.

  • Syntax error is the most common error it gives rise to one of the error which is AttributeError.

  • NameError: name ‘turtle’ is not defined; this error occurs if you use the turtle library functions without first importing the library. Make sure you import the library first by using “import turtle” at the beginning.

Conclusion

In this article, we have briefly discussed what is tic tac toe board and turtle library. Different functions are discussed in this article which is used for drawing the board. The easy syntax helps in creating a wide range of graphical animations where you can include various shapes. Complex objects can also be made with the use of conditional statements.

Updated on: 11-Oct-2023

385 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements