Table of contents
1.
Introduction
2.
About the Tic Tac Toe Game
3.
Using Python Pygam, create a tic tac toe game.
3.1.
Step 1
3.2.
Step 2
3.3.
Step 3
3.4.
Step 4
3.5.
Step 5
3.6.
Step 6
3.7.
Step 7
3.8.
Step 8
3.8.1.
 
3.8.2.
Output
4.
Frequently asked questions
4.1.
Is pygame suitable for GUI?
4.2.
Is KIVY superior to pygame?
4.3.
Is Python suitable for GUI applications?
4.4.
Is Kivy suitable for mobile applications?
4.5.
Which programming language is the most user-friendly and effective for creating a graphical user interface?
5.
Conclusion
Last Updated: Mar 27, 2024

Tic Tac Toe GUI In Python using PyGame

Author Amarjeet kumar
3 upvotes
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this Python article, we'll learn how to use Pygame to develop a game in Python. Using Pygame, it is simple to make a game in Python. We'll look at how to make a Tic Tac Toe game with Python Pygame.
It's advisable to start with the Redis CLI to understand how to use RedisJSON. The examples below presume you're connected to a Redis server that supports RedisJSON.

About the Tic Tac Toe Game

  • One of the most popular games is tic tac toe. Let's have a look at how this game works in Python.
     
  • Tic tac toe is a game for two players. Noughts and crosses are represented by the letters "O" and "X."
     
  • X and O alternate denoting the positions in a 33-square grid.
     
  • The person who successfully places three of his or her markers in horizontal, vertical, or diagonal rows wins the game.
     
  • This game will be built using Python libraries.

Using Python Pygam, create a tic tac toe game.

Step 1

Writing the module

# MODULES
import pygame, sys
import numpy as np

Step 2

Initializing pygame

# initialises pygame
pygame.init()

Step 3

Defining constants that will use in program and give colors to it

# CONSTANTS
WID = 600
HEIGHT = 600
LIN_WID = 15
WN_LIN_WID = 15
BRD_ROW = 3
BRD_COL = 3
SQR_SIZ = 200
CIRCLE_RADIUS = 60
CIRCL_WID = 15
CROS_WID = 25
SPACE = 55
# rgb: red green blue
RED = (255, 0, 0)
BG_COLOR = (28, 170, 156)
LINE_COLOR = (23, 145, 135)
CIRCLE_COLOR = (239, 231, 200)
CROSS_COLOR = (66, 66, 66)

Step 4

Screen and display setting

#SCREEN
screen = pygame.display.set_mode( (WID, HEIGHT) )
pygame.display.set_caption( 'TIC TAC TOE' )
screen.fill( BG_COLOR ) 

Step 5

Console Board

# CONSOLE BOARD
board = np.zeros( (BRD_ROW, BRD_COL) )

Step 6

Required Functions

# FUNCTIONS

def draw_lines():
# horizontal
pygame.draw.line( screen, LINE_COLOR, (0, SQR_SIZ), (WID, SQR_SIZ), LIN_WID )
# horizontal
pygame.draw.line( screen, LINE_COLOR, (0, 2 * SQR_SIZ), (WID, 2 * SQR_SIZ), LIN_WID )

# vertical
pygame.draw.line( screen, LINE_COLOR, (SQR_SIZ, 0), (SQR_SIZ, HEIGHT), LIN_WID )
# vertical
pygame.draw.line( screen, LINE_COLOR, (2 * SQR_SIZ, 0), (2 * SQR_SIZ, HEIGHT), LIN_WID )

def draw_figures():
for row in range(BRD_ROW):
for col in range(BRD_COL):
if board[row][col] == 1:
pygame.draw.circle( screen, CIRCLE_COLOR, (int( col * SQR_SIZ + SQR_SIZ//2 ), int( row * SQR_SIZ + SQR_SIZ//2 )), CIRCLE_RADIUS, CIRCL_WID )
elif board[row][col] == 2:
pygame.draw.line( screen, CROSS_COLOR, (col * SQR_SIZ + SPACE, row * SQR_SIZ + SQR_SIZ - SPACE), (col * SQR_SIZ + SQR_SIZ - SPACE, row * SQR_SIZ + SPACE), CROS_WID ) 
pygame.draw.line( screen, CROSS_COLOR, (col * SQR_SIZ + SPACE, row * SQR_SIZ + SPACE), (col * SQR_SIZ + SQR_SIZ - SPACE, row * SQR_SIZ + SQR_SIZ - SPACE), CROS_WID )

def mark_square(row, col, plr):
board[row][col] = plr

def available_square(row, col):
return board[row][col] == 0

def is_board_full():
for row in range(BRD_ROW):
for col in range(BRD_COL):
if board[row][col] == 0:
return False

return True

def check_win(plr):
# vertical win match check
for col in range(BRD_COL):
if board[0][col] == plr and board[1][col] == plr and board[2][col] == plr:
draw_vertical_winning_line(col, plr)
return True

# horizontal win match check
for row in range(BRD_ROW):
if board[row][0] == plr and board[row][1] == plr and board[row][2] == plr:
draw_horizontal_winning_line(row, plr)
return True

# assc diagonal win check
if board[2][0] == plr and board[1][1] == plr and board[0][2] == plr:
draw_asc_diagonal(plr)
return True

# desce diagonal win chek
if board[0][0] == plr and board[1][1] == plr and board[2][2] == plr:
draw_desc_diagonal(plr)
return True

return False

def draw_vertical_winning_line(col, plr):
posX = col * SQR_SIZ + SQR_SIZ//2

if plr == 1:
color = CIRCLE_COLOR
elif plr == 2:
color = CROSS_COLOR

pygame.draw.line( screen, color, (posX, 15), (posX, HEIGHT - 15), LIN_WID )

def draw_horizontal_winning_line(row, plr):
posY = row * SQR_SIZ + SQR_SIZ//2

if plr == 1:
color = CIRCLE_COLOR
elif plr == 2:
color = CROSS_COLOR

pygame.draw.line( screen, color, (15, posY), (WID - 15, posY), WN_LIN_WID )

def draw_asc_diagonal(plr):
if plr == 1:
color = CIRCLE_COLOR
elif plr == 2:
color = CROSS_COLOR

pygame.draw.line( screen, color, (15, HEIGHT - 15), (WID - 15, 15), WIN_LINE_WID )

def draw_desc_diagonal(plr):
if plr == 1:
color = CIRCLE_COLOR
elif plr == 2:
color = CROSS_COLOR

pygame.draw.line( screen, color, (15, 15), (WID - 15, HEIGHT - 15), WN_LIN_WID )

def restart():
screen.fill( BG_COLOR )
draw_lines()
for row in range(BRD_ROW):
for col in range(BRD_COL):
board[row][col] = 0

draw_lines()

Step 7

Defining Variable

# VARIABLES
plr = 1
game_over = False

Step 8

Writing the module

# MAINLOOP

while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()

if event.type == pygame.MOUSEBUTTONDOWN and not game_over:

mouseX = event.pos[0] # x
mouseY = event.pos[1] # y

clicked_row = int(mouseY // SQR_SIZ)
clicked_col = int(mouseX // SQR_SIZE)

if available_square( clicked_row, clicked_col ):

mark_square( clicked_row, clicked_col, plr )
if check_win( plr ):
game_over = True
plr = plr % 2 + 1

draw_figures()

if event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
restart()
plr = 1
game_over = False

pygame.display.update()

 

Output

Frequently asked questions

Is pygame suitable for GUI?

Pygame GUI is a package that allows you to create graphical user interfaces for pygame games. The module is focused on the future, and it is compatible with Pygame 2 and Python 3.

Is KIVY superior to pygame?

Although pygame may provide somewhat better performance, kivy is a far more straightforward framework, and if you don't intend to construct a Crysis, I'd recommend kivy.

Is Python suitable for GUI applications?

Python has shown to be useful in a wide range of situations. However, few people are aware of its capabilities in building graphical user interfaces (GUI.) Indeed, there are a number of excellent Python GUI frameworks available that assist developers in quickly creating an interface to interact with their projects.

Is Kivy suitable for mobile applications?

The Kivy framework is your best choice if you're a Python developer looking to get started with mobile programming. Kivy allows you to create platform-agnostic apps that run on iOS, Android, Windows, MacOS, and Linux.

Which programming language is the most user-friendly and effective for creating a graphical user interface?

The Python programming language is ideal for creating basic GUI apps that don't require any prior knowledge. And it's simple to pick up.

Conclusion

I hope you now know how to make a grid game in Python, similar to tic tac toe, and how to utilise the pygame and sys modules. We learnt how to make a tic tac toe game in Python using Pygame, and we have the whole source code with instructions. 
Learn about the pyglet. It will clear the concept.

Are you planning to ace the interviews of reputed product-based companies like Amazon, Google, Microsoft, and more?

Please go check out our practice course

Attempt our Online Mock Test Series on Coding Ninjas Studio now!

Ninja, have a great time learning.

Live masterclass