1
-
2
-
3
1
# -*- coding: utf-8 -*-
4
2
"""
5
3
Tic Toe Using pygame , numpy and sys with Graphical User Interface
6
-
7
4
"""
8
-
9
5
import pygame , sys
10
6
from pygame .locals import *
11
7
import numpy as np
12
-
13
8
#------
14
9
#constants
15
10
#-------
19
14
board_rows = 3
20
15
board_columns = 3
21
16
cross_width = 25
22
-
23
17
square_size = width // board_columns
24
18
#colors in RGB format
25
19
line_Width = 15
28
22
line_color = (23 , 145 , 135 )
29
23
circle_color = (239 ,231 ,200 )
30
24
cross_color = (66 ,66 ,66 )
31
-
32
25
space = square_size // 4
33
26
#circle
34
27
circle_radius = square_size // 3
35
28
circle_width = 14
36
-
37
29
pygame .init ()
38
30
screen = pygame .display .set_mode ((height ,width ))
39
31
pygame .display .set_caption ('Tic Tac Toe!' )
40
32
screen .fill ( bg_color )
41
-
42
33
#color to display restart
43
34
white = (255 , 255 , 255 )
44
35
green = (0 , 255 , 0 )
60
51
winRect .center = (100 ,30 )
61
52
textRect .center = (width - 400 , 30 )
62
53
leaveRect .center = (width - 120 ,30 )
63
-
64
54
board = np .zeros ( (board_rows ,board_columns ))
65
55
#print(board)
66
56
#pygame.draw.line( screen ,red ,(10,10),(300,300),10)
@@ -83,12 +73,10 @@ def draw_lines():
83
73
#2nd verticle
84
74
pygame .draw .line ( screen ,line_color ,(2 * square_size ,0 ),(2 * square_size ,height ),line_Width )
85
75
86
-
87
76
#To mark which square player has chosen
88
77
def mark_square (row ,col ,player ):
89
78
board [row ][col ]= player
90
79
91
-
92
80
# TO check the availablity of a square
93
81
def available_square (row ,col ):
94
82
return board [row ][col ]== 0
@@ -103,8 +91,7 @@ def is_board_full():
103
91
else :
104
92
k = True
105
93
return k
106
-
107
-
94
+
108
95
def check_win (player ):
109
96
#check verticle win
110
97
for col in range (board_columns ):
@@ -133,70 +120,54 @@ def draw_horizontal_winning_line(row,player):
133
120
color = cross_color
134
121
135
122
pygame .draw .line (screen , color , (15 ,posY ), (width - 15 ,posY ),15 )
136
-
137
-
138
-
123
+
139
124
def draw_vertical_winning_line (col ,player ):
140
125
posX = col * square_size + square_size // 2
141
126
if (player == 1 ):
142
127
color = circle_color
143
128
else :
144
- color = cross_color
145
-
129
+ color = cross_color
146
130
pygame .draw .line (screen , color , (posX ,15 ), (posX ,width - 15 ),15 )
147
131
def draw_asc_diagonal (player ):
148
132
if (player == 1 ):
149
133
color = circle_color
150
134
else :
151
- color = cross_color
152
-
135
+ color = cross_color
153
136
pygame .draw .line (screen ,color ,(15 ,height - 15 ),(width - 15 ,15 ),15 )
154
137
def draw_des_diagonal (player ):
155
138
if (player == 1 ):
156
139
color = circle_color
157
140
else :
158
- color = cross_color
159
-
141
+ color = cross_color
160
142
pygame .draw .line (screen ,color ,(15 ,15 ),(width - 15 ,height - 15 ),15 )
161
-
162
-
163
-
143
+
164
144
def restart ():
165
145
screen .fill (bg_color )
166
146
draw_lines ()
167
- player = 1
168
-
147
+ player = 1
169
148
for row in range (board_rows ):
170
149
for col in range (board_columns ):
171
- board [row ][col ]= 0
172
-
173
-
150
+ board [row ][col ]= 0
174
151
draw_lines ()
175
152
#player
176
153
player = 1
177
154
game_over = False
178
-
179
155
while True : # main game loop
180
156
for event in pygame .event .get (): #constantly looks for the event
181
157
if event .type == pygame .QUIT : #if user clicks exit pygame.QUIT and sys exits
182
158
pygame .quit ()
183
159
sys .exit ()
184
-
185
160
board_full = is_board_full ()
186
161
if board_full and not game_over :
187
162
Won = font .render (" It's a Tie " , True ,blue ,green )
188
163
screen .blit (Won , winRect )
189
164
screen .blit (text , textRect )
190
- screen .blit (leave ,leaveRect )
191
-
165
+ screen .blit (leave ,leaveRect )
192
166
if event .type == pygame .MOUSEBUTTONDOWN and not game_over :
193
167
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 )
200
171
if available_square (clicked_row , clicked_column ):
201
172
mark_square (clicked_row ,clicked_column , player )
202
173
if (check_win (player )):
@@ -210,13 +181,6 @@ def restart():
210
181
Won = font .render ("Player" + str (player )+ " Turn " , True ,blue ,green )
211
182
screen .blit (Won , winRect )
212
183
draw_figures ()
213
-
214
-
215
-
216
-
217
-
218
-
219
-
220
184
#to restart the game
221
185
if event .type == pygame .KEYDOWN :
222
186
if event .key == pygame .K_r :
0 commit comments