Tic Tac Toe Project in python

Spread the love

Tic Tac Toe Project in python:

What is Tic Tac Toe?

you can play this game  anywhere with just a pen and paper.Tic Tac Toe is one of the most favorite played game and is the best time killer game that it.
 If you don’t know how to play this game don’t worry let us first understand that.With this Python project by python tutorials point, we are going to build an interactive game of Tic Tac Toe where we will learn new thing along the way. So let’s start:
How to play this game:
The game is played by two players. First, player draw a board with a 3×3 square matrix grid. The first starting player choose ‘X’ and draws it on matrix grid then it is the chance of the second player to draw ‘O’ sign on the available spaces. similar to this, the players draw ‘X’ and ‘O’ alternatively on the empty spaces. Finally the player wins the game otherwise the game draws python

Steps Follows to a Python Tic Tac Toe Game Project –

Step #01) How to install pygame library:

Step #02) Initializing game components

Step #03) Initializing Pygame window

Step #04) Load and transform images

Step #05) Define the functions

Step #06) Run the game forever

We need to run our game inside an infinite loop. It will continuously look for events and when a user presses the mouse button on the grid we will first get the X and Y coordinates of the mouse. Then we will check which square the user has clicked. Then we will draw the appropriate ‘X’ or ‘O’ image on the canvas. So that is basically what we will do in this Python project idea.

Step 01) How to install pygame library:

To implement this game, we need to use the basic concepts of Python and Pygame library which is a Python library for building cross-platform game. It contain the modules needed for run  computer graphic and sound libraries. 
To install the library, you can need to import library, use pip installer  command line:
    1. pip install pygame

tic tac toe in python:

Step 02) Initializing game components

    1. import pygame as pgs,sys
    1. from pygame.locals import *
    1. import time
    1. #Need to initialize global variable
    1. XO = ‘x’
    1. winner = None
    1. draw = False
    1. width = 300
    1. height = 300
    1. white = (255, 255, 255)
    1. line_color = (10,10,10)
    1. #TicTacToe 3×3 board size matrix
    1. TTT = [[None]*3,[None]*3,[None]*3]

2 payer tic tac toe python beginner :

Step 03) Initializing Pygame window

    1. #How to initializing pygame windows
    1. pgs.init()
    1. fps = 30
    1. CLOCK = pgs.time.Clock()
    1. screen = pgs.display.set_mode((width, height+100),0,32)
    1. pgs.display.set_caption(“python Tic Tac Toe”)

Step 04) Load and transform images

    1. #How to loading the image
    1. opening = pgs.image.load(‘pythonslearning.png’)
    1. y_img = pgs.image.load(‘y.png’)
    1. z_img = pgs.image.load(‘z.png’)
    1. #How to resizing image
    1. y_img = pgs.transform.scale(y_img, (80,80))
    1. z_img = pgs.transform.scale(z_img, (80,80))
    1. opening = pgs.transform.scale(opening, (width, height+100))

Step 05) Define the functions

    1. def game_opening():
    1. screen.blit(opening,(0,0))
    1. pgs.display.update()
    1. time.sleep(1)
    1. screen.fill(white)
    1. # How to Drawingl lines
    1. pgs.draw.line(screen,line_color,(width/3,0),(width/3, height),7)
    1. pgs.draw.line(screen,line_color,(width/3*2,0),(width/3*2, height),7)
    1. #How to Drawing horizontal lines
    1. pgs.draw.line(screen,line_color,(0,height/3),(width, height/3),7)
    1. pgs.draw.line(screen,line_color,(0,height/3*2),(width, height/3*2),7)
    1. draw_status()
    1. def draw_status():
    1. global draw
    1. if winner is None:
    1. message = XO.upper() + “s Turns”
    1. else:
    1. message = winner.upper() + ” congrats u won!”
    1. if draw:
    1. message = ‘Ohh GameDraw!’
    1. font = pgs.font.Font(None, 30)
    1. text = font.render(message, 1, (255, 255, 255))
    1. #How to copy the rendered message onto the board
    1. screen.fill ((0, 0, 0), (0, 400, 500, 100))
    1. text_rect = text.get_rect(center=(width/2, 50050))
    1. screen.blit(text, text_rect)
    1. pgs.display.update()
    1. def check_win():
    1. global TTT, winner,draw
    1. # How to check for winning rows
    1. for row in range (0,3):
    1. if ((TTT [row][0] == TTT[row][1] == TTT[row][2]) and(TTT [row][0] is not None)):
    1. # then this row won
    1. winner = TTT[row][0]
    1. pg.draw.line(screen, (250,0,0), (0, (row + 1)*height/3 -height/6),
    1. (width, (row + 1)*height/3 – height/6 ), 4)
    1. break
    1. # similar check for winning column
    1. for col in range (0, 3):
    1. if (TTT[0][col] == TTT[1][col] == TTT[2][col]) and (TTT[0][col] is not None):
    1. # then this column won
    1. winner = TTT[0][col]
    1. #then draw winning line
    1. pgs.draw.line (screen, (250,0,0),((col + 1)* width/3 – width/6, 0),
    1. ((col + 1)* width/3 – width/6, height), 4)
    1. break
    1. # How to chek for diagonal winners
    1. if (TTT[0][0] == TTT[1][1] == TTT[2][2]) and (TTT[0][0] is not None):
    1. # How game won diagonally left to right in side
    1. winner = TTT[0][0]
    1. pg.draw.line (screen, (250,70,70), (50, 50), (350, 350), 4)
    1. if (TTT[0][2] == TTT[1][1] == TTT[2][0]) and (TTT[0][2] is not None):
    1. # Also game won diagonally right to left
    1. winner = TTT[0][2]
    1. pg.draw.line (screen, (250,70,70), (350, 50), (50, 350), 4)
    1. if(all([all(row) for row in TTT]) and winner is None ):
    1. draw = True
    1. draw_status()
    1. def drawXO(row,col):
    1. global TTT,XO
    1. if row==1:
    1. posx = 30
    1. if row==2:
    1. posx = width/3 + 30
    1. if row==3:
    1. posx = width/3*2 + 30
    1. if col==1:
    1. posy = 30
    1. if col==2:
    1. posy = height/3 + 30
    1. if col==3:
    1. posy = height/3*2 + 30
    1. TTT[row-1][col-1] = XO
    1. if(XO == ‘x’):
    1. screen.blit(x_img,(posy,posx))
    1. XO= ‘o’
    1. else:
    1. screen.blit(o_img,(posy,posx))
    1. XO= ‘x’
    1. pg.display.update()
    1. #then print(posx,posy)
    1. #Also print(TTT)
    1. def userClick():
    1. x,y = pg.mouse.get_pos()
    1. if(x<width/3):
    1. col = 1
    1. elif (x<width/3*2):
    1. col = 2
    1. elif(x<width):
    1. col = 3
    1. else:
    1. col = None
    1. if(y<height/3):
    1. row = 1
    1. elif (y<height/3*2):
    1. row = 2
    1. elif(y<height):
    1. row = 3
    1. else:
    1. row = None
    1. #then after print(row,col) let’s
    1. if(row and col and TTT[row-1][col-1] is None):
    1. global XO
    1. #Also we draw to x or o on screen
    1. drawXO(row,col)
    1. check_win()
    1. def reset_game():
    1. global TTT, winner,XO, draw
    1. time.sleep(3)
    1. XO = ‘x’
    1. draw = False
    1. game_opening()
    1. winner=None
    1. TTT = [[None]*3,[None]*3,[None]*3]

 

Step 06) Run the game forever

    1. game_opening()
    1. # Game is ready to play
    1. while(True):
    1. for event in pgs.event.get():
    1. if event.type == QUIT:
    1. pgs.quit()
    1. sys.exit()
    1. elif event.type is MOUSEBUTTONDOWN:
    1. userClick()
    1. if(winner or draw):
    1. reset_game()
    1. pgs.display.update()
    1. CLOCK.tick(fps)
Congrats! Finally game is complete and ready to play. 
Output: 
Tic Tac Project in python
Tic Tac Project in python
if you want to learn python for free then click on python tutorials point.
If you have any question about this section then comment please and if you like this post then please comment and share.
BEST OF LUCK!!!

I am Mr. Sachin pagar Embedded system engineer, the founder of Pythonslearning, a Passionate Educational Blogger and Author, who love to share the informative content on educational resources.

sachin Pagar

I am Mr. Sachin pagar Embedded system engineer, the founder of Pythonslearning, a Passionate Educational Blogger and Author, who love to share the informative content on educational resources.

Leave a Reply