Tic Tac Toe Project in python

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
  2. from pygame.locals import *
  3. import time
  4. #Need to initialize global variable
  5. XO = 'x'
  6. winner = None
  7. draw = False
  8. width = 300
  9. height = 300
  10. white = (255, 255, 255)
  11. line_color = (10,10,10)
  12. #TicTacToe 3x3 board size matrix
  13. 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
  2. pgs.init()
  3. fps = 30
  4. CLOCK = pgs.time.Clock()
  5. screen = pgs.display.set_mode((width, height+100),0,32)
  6. pgs.display.set_caption("python Tic Tac Toe")

Step 04) Load and transform images

  1. #How to loading the image
  2. opening = pgs.image.load('pythonslearning.png')
  3. y_img = pgs.image.load('y.png')
  4. z_img = pgs.image.load('z.png')
  5. #How to resizing image
  6. y_img = pgs.transform.scale(y_img, (80,80))
  7. z_img = pgs.transform.scale(z_img, (80,80))
  8. opening = pgs.transform.scale(opening, (width, height+100))

Step 05) Define the functions


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


Step 06) Run the game forever

  1. game_opening()
  2. # Game is ready to play
  3. while(True):
  4. for event in pgs.event.get():
  5. if event.type == QUIT:
  6. pgs.quit()
  7. sys.exit()
  8. elif event.type is MOUSEBUTTONDOWN:
  9. userClick()
  10. if(winner or draw):
  11. reset_game()
  12. pgs.display.update()
  13. 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!!!
Previous
Next Post »

comparison python vs r vs sas vs spss

python  vs r vs sas vs spss : Welcome everyone, Today we will see comparison between  r vs python vs sas vs spss . So Let's start : ...

Get Free Crash Course Subscription Valid Only For 10000 Subscriber.

Enter your email address:

Delivered by FeedBurner