Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b7662b9

Browse files
Merge pull request avinashkranjan#851 from Lakhankumawat/TicTacToe-GUI
TicTacToe-GUI using Pygame 🐍
2 parents da0e6ed + ff899a4 commit b7662b9

File tree

2 files changed

+231
-0
lines changed

2 files changed

+231
-0
lines changed

‎TicTacToe-GUI/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
## Tic Tac Toe
2+
<img width="300" src="https://media.giphy.com/media/ChzovjKPuEiYe8ePih/giphy.gif" />
3+
4+
### How To Play
5+
6+
1. The game is played on a grid that's 3 squares by 3 squares.
7+
2. You are X, your friend (or the computer in this case) is O.
8+
3. Players take turns putting their marks in empty squares.
9+
4. The first player to get 3 of her marks in a row (up, down, across, or diagonally) is the winner.
10+
5. When all 9 squares are full, the game is over.
11+
12+
### How The Game Works
13+
14+
1. Our Tic Tac Toe is programmed in other to allow two users or players to play the game in the same time.
15+
2. It is GUI game & gives an instant alert when players wins or losses or draw the game basically tells us to restart.
16+
17+
### Details
18+
Libraries Required
19+
20+
| Library | Command To Install | Documentation |
21+
| :------------ | :------------ | :------------ |
22+
| Pygame | `pip install pygame` | https://pypi.org/project/pygame/ |
23+
| Sys | `pip install os-sys` | https://pypi.org/project/os-sys/ |
24+
25+
### Steps Required To Make This Awesome Game Live:
26+
- First of all, create a folder named any in your PC & drag it to your code editor.
27+
- Secondly, open your command prompt(CMD) & install the Pygame package by typing pip install pygame command and os-sys package by typing pip install os-sys .(I used windows10 OS)
28+
- Thirdly, make a file called TicTacToeGame.py . It will you to store all your code needed for the game.
29+
- Set your codes which are given below to your respective files.
30+
- Lastly just run this code and enjoy the game.
31+
32+
### How It Will Look
33+
<a href="https://imgbb.com/"><img width="350" src="https://i.ibb.co/0ZKQrW4/download.png" alt="download" border="0"></a>
34+
35+
### Author : [Lakhan Kumawat](https://github.com/Lakhankumawat)
36+
37+

‎TicTacToe-GUI/TicTacToe.py

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Tic Toe Using pygame , numpy and sys with Graphical User Interface
4+
"""
5+
import pygame, sys
6+
from pygame.locals import *
7+
import numpy as np
8+
#------
9+
#constants
10+
#-------
11+
width=800
12+
height=800
13+
#row and columns
14+
board_rows=3
15+
board_columns=3
16+
cross_width=25
17+
square_size=width//board_columns
18+
#colors in RGB format
19+
line_Width=15
20+
red=(255, 0, 0)
21+
bg_color=(28, 170, 156)
22+
line_color=(23, 145, 135)
23+
circle_color=(239,231,200)
24+
cross_color=(66,66,66)
25+
space=square_size//4
26+
#circle
27+
circle_radius=square_size//3
28+
circle_width=14
29+
pygame.init()
30+
screen = pygame.display.set_mode((height,width))
31+
pygame.display.set_caption('Tic Tac Toe!')
32+
screen.fill( bg_color )
33+
#color to display restart
34+
white = (255, 255, 255)
35+
green = (0, 255, 0)
36+
blue = (0, 0, 128)
37+
38+
font = pygame.font.Font('freesansbold.ttf', 25)
39+
40+
# create a text suface object,
41+
# on which text is drawn on it.
42+
text = font.render('Press R to restart', True, green, blue)
43+
44+
Won= font.render(" Won", True ,blue,green)
45+
leave=font.render("Press X to Exit" ,True, white, red)
46+
# create a rectangular object for the
47+
# text surface object
48+
leaveRect=text.get_rect()
49+
textRect = text.get_rect()
50+
winRect=Won.get_rect()
51+
winRect.center=(100,30)
52+
textRect.center = (width-400, 30)
53+
leaveRect.center=(width-120,30)
54+
board=np.zeros( (board_rows,board_columns))
55+
#print(board)
56+
#pygame.draw.line( screen ,red ,(10,10),(300,300),10)
57+
58+
def draw_figures():
59+
for row in range(board_rows):
60+
for col in range(board_columns):
61+
if board[row][col]==1:
62+
pygame.draw.circle(screen,circle_color,( int(col*square_size + square_size//2 ),int(row*square_size +square_size//2)),circle_radius,circle_width)
63+
elif board[row][col]==2:
64+
pygame.draw.line( screen ,cross_color ,(col*square_size + space,row*square_size +square_size -space),(col*square_size+square_size -space,row*square_size +space),cross_width)
65+
pygame.draw.line( screen ,cross_color ,(col*square_size +space,row*square_size +space),(col*square_size +square_size -space,row*square_size +square_size -space),cross_width)
66+
67+
def draw_lines():
68+
pygame.draw.line( screen ,line_color ,(0,square_size),(width,square_size),line_Width)
69+
#2nd horizontal line
70+
pygame.draw.line( screen ,line_color ,(0,2*square_size),(width,2*square_size),line_Width)
71+
#1st verticle
72+
pygame.draw.line( screen ,line_color ,(square_size,0),(square_size,height),line_Width)
73+
#2nd verticle
74+
pygame.draw.line( screen ,line_color ,(2*square_size,0),(2*square_size,height),line_Width)
75+
76+
#To mark which square player has chosen
77+
def mark_square(row,col,player):
78+
board[row][col]=player
79+
80+
# TO check the availablity of a square
81+
def available_square(row,col):
82+
return board[row][col]==0
83+
84+
#check board full or not
85+
def is_board_full():
86+
k=False
87+
for row in range(board_rows):
88+
for col in range(board_columns):
89+
if board[row][col]==0:
90+
k=False
91+
else:
92+
k=True
93+
return k
94+
95+
def check_win(player):
96+
#check verticle win
97+
for col in range(board_columns):
98+
if board[0][col]==player and board[1][col]==player and board[2][col]==player:
99+
draw_vertical_winning_line(col, player)
100+
return True
101+
#check Horizontal win
102+
for row in range(board_rows):
103+
if board[row][0]==player and board[row][1]==player and board[row][2]==player:
104+
draw_horizontal_winning_line(row, player)
105+
return True
106+
#check for asc win
107+
if board[2][0]==player and board[1][1]==player and board[0][2]==player:
108+
draw_asc_diagonal(player)
109+
return True
110+
if board[0][0]==player and board[1][1]==player and board[2][2]==player:
111+
draw_des_diagonal(player)
112+
return True
113+
114+
def draw_horizontal_winning_line(row,player):
115+
posY=row*square_size +square_size//2
116+
117+
if(player==1):
118+
color=circle_color
119+
else:
120+
color=cross_color
121+
122+
pygame.draw.line(screen, color, (15,posY), (width-15,posY),15)
123+
124+
def draw_vertical_winning_line(col,player):
125+
posX=col*square_size +square_size//2
126+
if(player==1):
127+
color=circle_color
128+
else:
129+
color=cross_color
130+
pygame.draw.line(screen, color, (posX,15), (posX,width-15),15)
131+
def draw_asc_diagonal(player):
132+
if(player==1):
133+
color=circle_color
134+
else:
135+
color=cross_color
136+
pygame.draw.line(screen,color,(15,height-15),(width-15,15),15)
137+
def draw_des_diagonal(player):
138+
if(player==1):
139+
color=circle_color
140+
else:
141+
color=cross_color
142+
pygame.draw.line(screen,color,(15,15),(width-15,height-15),15)
143+
144+
def restart():
145+
screen.fill(bg_color)
146+
draw_lines()
147+
player = 1
148+
for row in range(board_rows):
149+
for col in range(board_columns):
150+
board[row][col]=0
151+
draw_lines()
152+
#player
153+
player=1
154+
game_over=False
155+
while True: # main game loop
156+
for event in pygame.event.get(): #constantly looks for the event
157+
if event.type == pygame.QUIT: #if user clicks exit pygame.QUIT and sys exits
158+
pygame.quit()
159+
sys.exit()
160+
board_full=is_board_full()
161+
if board_full and not game_over:
162+
Won= font.render(" It's a Tie ", True ,blue,green)
163+
screen.blit(Won, winRect)
164+
screen.blit(text, textRect)
165+
screen.blit(leave,leaveRect)
166+
if event.type==pygame.MOUSEBUTTONDOWN and not game_over:
167+
mouseX= event.pos[0] #x
168+
mouseY= event.pos[1] #y
169+
clicked_row=int(mouseY // square_size)
170+
clicked_column=int(mouseX // square_size)
171+
if available_square(clicked_row, clicked_column):
172+
mark_square(clicked_row,clicked_column, player)
173+
if(check_win(player)):
174+
game_over=True
175+
Won= font.render("Player"+str(player)+" Won ", True ,blue,green)
176+
screen.blit(Won, winRect)
177+
screen.blit(text, textRect)
178+
screen.blit(leave,leaveRect)
179+
player=player%2 +1
180+
if not game_over and not board_full:
181+
Won= font.render("Player"+str(player)+" Turn ", True ,blue,green)
182+
screen.blit(Won, winRect)
183+
draw_figures()
184+
#to restart the game
185+
if event.type==pygame.KEYDOWN:
186+
if event.key==pygame.K_r:
187+
restart()
188+
game_over=False
189+
elif event.key==pygame.K_x:
190+
pygame.quit()
191+
sys.exit()
192+
#print(board)
193+
pygame.display.update()
194+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /