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 ff899a4

Browse files
Requested Changes Made
1 parent 3afaecf commit ff899a4

File tree

1 file changed

+12
-48
lines changed

1 file changed

+12
-48
lines changed

‎TicTacToe-GUI/TicTacToe.py

Lines changed: 12 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
2-
31
# -*- coding: utf-8 -*-
42
"""
53
Tic Toe Using pygame , numpy and sys with Graphical User Interface
6-
74
"""
8-
95
import pygame, sys
106
from pygame.locals import *
117
import numpy as np
12-
138
#------
149
#constants
1510
#-------
@@ -19,7 +14,6 @@
1914
board_rows=3
2015
board_columns=3
2116
cross_width=25
22-
2317
square_size=width//board_columns
2418
#colors in RGB format
2519
line_Width=15
@@ -28,17 +22,14 @@
2822
line_color=(23, 145, 135)
2923
circle_color=(239,231,200)
3024
cross_color=(66,66,66)
31-
3225
space=square_size//4
3326
#circle
3427
circle_radius=square_size//3
3528
circle_width=14
36-
3729
pygame.init()
3830
screen = pygame.display.set_mode((height,width))
3931
pygame.display.set_caption('Tic Tac Toe!')
4032
screen.fill( bg_color )
41-
4233
#color to display restart
4334
white = (255, 255, 255)
4435
green = (0, 255, 0)
@@ -60,7 +51,6 @@
6051
winRect.center=(100,30)
6152
textRect.center = (width-400, 30)
6253
leaveRect.center=(width-120,30)
63-
6454
board=np.zeros( (board_rows,board_columns))
6555
#print(board)
6656
#pygame.draw.line( screen ,red ,(10,10),(300,300),10)
@@ -83,12 +73,10 @@ def draw_lines():
8373
#2nd verticle
8474
pygame.draw.line( screen ,line_color ,(2*square_size,0),(2*square_size,height),line_Width)
8575

86-
8776
#To mark which square player has chosen
8877
def mark_square(row,col,player):
8978
board[row][col]=player
9079

91-
9280
# TO check the availablity of a square
9381
def available_square(row,col):
9482
return board[row][col]==0
@@ -103,8 +91,7 @@ def is_board_full():
10391
else:
10492
k=True
10593
return k
106-
107-
94+
10895
def check_win(player):
10996
#check verticle win
11097
for col in range(board_columns):
@@ -133,70 +120,54 @@ def draw_horizontal_winning_line(row,player):
133120
color=cross_color
134121

135122
pygame.draw.line(screen, color, (15,posY), (width-15,posY),15)
136-
137-
138-
123+
139124
def draw_vertical_winning_line(col,player):
140125
posX=col*square_size +square_size//2
141126
if(player==1):
142127
color=circle_color
143128
else:
144-
color=cross_color
145-
129+
color=cross_color
146130
pygame.draw.line(screen, color, (posX,15), (posX,width-15),15)
147131
def draw_asc_diagonal(player):
148132
if(player==1):
149133
color=circle_color
150134
else:
151-
color=cross_color
152-
135+
color=cross_color
153136
pygame.draw.line(screen,color,(15,height-15),(width-15,15),15)
154137
def draw_des_diagonal(player):
155138
if(player==1):
156139
color=circle_color
157140
else:
158-
color=cross_color
159-
141+
color=cross_color
160142
pygame.draw.line(screen,color,(15,15),(width-15,height-15),15)
161-
162-
163-
143+
164144
def restart():
165145
screen.fill(bg_color)
166146
draw_lines()
167-
player = 1
168-
147+
player = 1
169148
for row in range(board_rows):
170149
for col in range(board_columns):
171-
board[row][col]=0
172-
173-
150+
board[row][col]=0
174151
draw_lines()
175152
#player
176153
player=1
177154
game_over=False
178-
179155
while True: # main game loop
180156
for event in pygame.event.get(): #constantly looks for the event
181157
if event.type == pygame.QUIT: #if user clicks exit pygame.QUIT and sys exits
182158
pygame.quit()
183159
sys.exit()
184-
185160
board_full=is_board_full()
186161
if board_full and not game_over:
187162
Won= font.render(" It's a Tie ", True ,blue,green)
188163
screen.blit(Won, winRect)
189164
screen.blit(text, textRect)
190-
screen.blit(leave,leaveRect)
191-
165+
screen.blit(leave,leaveRect)
192166
if event.type==pygame.MOUSEBUTTONDOWN and not game_over:
193167
mouseX= event.pos[0] #x
194-
mouseY= event.pos[1] #y
195-
196-
clicked_row=int(mouseY // square_size)
197-
198-
clicked_column=int(mouseX // square_size)
199-
168+
mouseY= event.pos[1] #y
169+
clicked_row=int(mouseY // square_size)
170+
clicked_column=int(mouseX // square_size)
200171
if available_square(clicked_row, clicked_column):
201172
mark_square(clicked_row,clicked_column, player)
202173
if(check_win(player)):
@@ -210,13 +181,6 @@ def restart():
210181
Won= font.render("Player"+str(player)+" Turn ", True ,blue,green)
211182
screen.blit(Won, winRect)
212183
draw_figures()
213-
214-
215-
216-
217-
218-
219-
220184
#to restart the game
221185
if event.type==pygame.KEYDOWN:
222186
if event.key==pygame.K_r:

0 commit comments

Comments
(0)

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