Python Tic Tac Toe
Python Tic Tac Toe
Project Prerequisites
Good knowledge of pygame is required alongwith the knowledge of
functions in python. Sound knowledge of the numpy module is also
required.
1. Install Pygame:
Pygame is a set of python modules that are designed for writing video
games. You need to install pygame to start developing the project. Write
the command given below to install pygame.
Pip install pygame
2. Importing Modules for TIC TAC TOE Python
Project:
import numpy as np
import pygame
import math
import sys
Code Explanation:
a. Numpy: It is the fundamental package used for scientific computing in
python.
b. math:This module helps in performing mathematical operations on
numbers.
c. sys: It provides information about functions, methods and constants of
the python interpreter.
8. Rest Code:
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN and not gameover:
positiony = event.pos[1]
row = int(math.floor(positiony / sizeofsquare))
positionx = event.pos[0]
col = int(math.floor(positionx / sizeofsquare))
if player % 2 == 0:
if availablesquare(row, col):
marksquare(row, col, 1)
if win(1):
gameover = True
player += 1
else:
if availablesquare(row, col):
marksquare(row, col, 2)
if win(2):
gameover = True
player += 1
if fullboard():
gameover = True
drawfigures()
pygame.display.update()
Code Explanation:
a. exit(): It helps to exit the game.
b. update(): It displays surface objects on the monitor.
c. MOUSEBUTTONDOWN: It gets the current state of the mouse device.
d. floor: It returns the floor value of a number.
Python Tic Tac Toe Output