Javatpoint Logo
Javatpoint Logo

Drawing a Tic Tac Toe Board using Turtle in Python

Making a Tic Tac Toe board in Python using Turtle Graphics is the task at hand. To start with, let's define what Turtle Graphics are.

The programming language Logo is frequently associated with turtle graphics. Seymour Papert decided to add turtle graphics support to Logo in the late 1960s to help his version of the turtle robot, which is really just a simple robot controlled from the user's workstation and built to perform the drawing work assigned to it to use a small collapsible pen set into or attached to the robot's body.

The Python language now has a Turtle graphics module in its standard library. Turtle in Python allows programmers to manipulate one or more turtles in a 2D space, just like its forebear in Logo did.

Turtle Graphics :

Turtle graphics are the vector graphics in computer graphics that use a relative cursor on a Cartesian plane. The turtle feature, which resembles a drawing board, allows us to control a turtle and draw with it.

Characteristics of turtle graphics :

  • forward(length) :
    Advances the pen by x units in the forward direction.
  • backward(length) :
    Advances the pen by x units in the reverse direction.
  • right(angle) :
    Turn the pen across an angle of x in the clockwise direction.
  • left(angle) :
    Turn the pen by an angle of x in the counter-clockwise direction.
  • penup() :
    It causes the turtle pen to cease drawing.
  • pendown() :
    Starts drawing with the turtle pen.

Approach for implementing the code :

Firstly, the turtle module will be imported in the following code so that we can create a Tic Tac Toe board in Turtle in Python.

Then to get a screen to work upon, we will use the Screen() method.

Next, we will define an instance of the Turtle library that has been imported in our code.

With the help of the color() method, we will be able to give a colour of our choice to the turtle pen. Violet colour was given here.

Then the width of the turtle will be set to "3" using the width() method.

Next, we will set the speed of drawing of the turtle according to our convenience, like we used "4", using the speed() method.

Later, a for loop was defined in order to construct the outer square of length 150. The for loop was executed 4 times, which contained the forward() method to advance the turtle and the left() method to turn the turtle to the left by a certain angle.

After the outer square was drawn, we moved further to draw the inner lines of the Tic Tac Toe Board. For that, we used the penup() method to stop drawing and move the pen without drawing. Then we set the location or coordinates of the place where we wanted our pen to start drawing, using the goto() method. The coordinates mentioned will be the new starting point for the drawing using the pen. Further, to start drawing again with the pen, we used the pendown() method to activate the pen again.

Ultimately, as the required Tic Tac Toe Board was drawn completely, we used the hideturtle() method to hide the turtle after completion of the drawing.

Complete Code :

Output:

The Tic Tac Toe board was created on the screen after the aforementioned code has been executed, as can be seen in the result shown below.

Drawing a Tic Tac Toe Board using Turtle in Python





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA