Python Game Project Tic Tac Toe - Time to rekindle the childhood days

Python Game Project Tic Tac Toe - Time to rekindle the childhood days

·

0 min read

It’s no doubt, you must have played Tic Tac Toe in your school days and every one of us loves to play this game. You will be surprised to know that the game of Tic Tac Toe is known to exist since ancient Egypt times.

What is Tic Tac Toe?

Tic Tac Toe is one of the most played games and is the best time killer game that you can play anywhere with just a pen and paper. If you don’t know how to play this game don’t worry let us first understand that.

Top Class Python Projects with Source Code

The game is played by two individuals. First, I draw a board with a 3×3 square grid. The first player chooses ‘X’ and draws it on any of the square grid, then it’s the chance of the second player to draw ‘O’ on the available spaces. Like this, the players draw ‘X’ and ‘O’ alternatively on the empty spaces until a player succeeds in drawing 3 consecutive marks either in the horizontal, vertical or diagonal way. Then the player wins the game otherwise the game draws when all spots are filled.

tic-tac-toe-project-in-python.gif

Tic Tac Toe – About the Python Project

The interesting Python project was built using the pygame library. I will be explaining all the pygame object methods that were used in this project. Pygame is a great library that allowed me to create the window and draw images and shapes on the window. This way I captured mouse coordinates and identified the block where I needed to mark ‘X’ or ‘O’. Then I checked if the user wins the game or not.

Tic Tac Toe Python Project File

Prerequisites

To implement this game, I used the basic concepts of Python and Pygame which is a Python library for building cross-platform games. It contains the modules needed for computer graphics and sound libraries. To install the library, I used pip installer from the command line:

pip install pygame

Steps to Build a Python Project – Tic Tac Toe Game

First, let’s check the steps to build Tic Tac Toe program in Python:

  • Create the display window for our game.
  • Draw the grid on the canvas where we will play Tic Tac Toe.
  • Draw the status bar below the canvas to show which player’s turn is it and who wins the game.
  • When someone wins the game or the game is a draw then we reset the game.

I ran my game inside an infinite loop. It continuously looked for events and when a user presses the mouse button on the grid I will first get the X and Y coordinates of the mouse. Then I will check which square the user has clicked. Then I will draw the appropriate ‘X’ or ‘O’ image on the canvas. So that is basically what I did in this Python project idea.

Tic Tac Toe Python Project with Source Code

EndNote

With this project in Python, I have successfully made the Tic Tac Toe game. I used the popular pygame library for rendering graphics on a display window. I learned how to capture events from the keyboard or mouse and trigger a function when the mouse button is pressed. This way I calculated mouse position, I drew X or O on the display and checked if the player wins the game or not.

This Cheat Sheet is a treat for all the aspiring Python Programmers