Skip to content

Commit 225aaeb

Browse files
authored
Create tic-tac-toe-gui-in-python-using-pygame
This article will guide you and give you a basic idea of designing a game Tic Tac Toe using pygame library of Python. Pygame is a cross-platform set of Python modules designed for writing video games. It includes computer graphics and sound libraries designed to be used with the Python programming language. Let’s break the task in five parts: Importing the required libraries and setting up the required global variables. Designing the game display function, that will set a platform for other components to be displayed on the screen. Main algorithm of win and draw Getting the user input and displaying the “X” or “O” at the proper position where the user has clicked his mouse. Running an infinite loop, and including the defined methods in it.
1 parent 27dfb8c commit 225aaeb

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# importing the required libraries
2+
import pygame as pg
3+
import sys
4+
import time
5+
from pygame.locals import *
6+
7+
# declaring the global variables
8+
9+
# for storing the 'x' or 'o'
10+
# value as character
11+
XO = 'x'
12+
13+
# storing the winner's value at
14+
# any instant of code
15+
winner = None
16+
17+
# to check if the game is a draw
18+
draw = None
19+
20+
# to set width of the game window
21+
width = 400
22+
23+
# to set height of the game window
24+
height = 400
25+
26+
# to set background color of the
27+
# game window
28+
white = (255, 255, 255)
29+
30+
# color of the straightlines on that
31+
# white game board, dividing board
32+
# into 9 parts
33+
line_color = (0, 0, 0)
34+
35+
# setting up a 3 * 3 board in canvas
36+
board = [[None]*3, [None]*3, [None]*3]

0 commit comments

Comments
 (0)