forked from devmrfitz/tictactoe
-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.py
285 lines (264 loc) · 8.8 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# Copyright (C) 2022 Sparsh Goenka
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import pygame as pg
from anim import Animate
import g
from frame import Frame
import sys
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
from gettext import gettext as _
# The main controller
class Main:
def __init__(self, journal=True):
self.journal = journal
self.running = True
self.canvas = None
self.score = [0, 0]
self.show_help = False
self.help_img = pg.image.load("help.jpg")
def set_canvas(self, canvas):
self.canvas = canvas
pg.display.set_caption(_("Tic-Tac-Toe"))
def write_file(self, file_path):
pass
def read_file(self, file_path):
pass
def quit(self):
self.running = False
def check_events(self):
for event in pg.event.get():
if event.type == pg.QUIT:
self.running = False
if event.type == pg.VIDEORESIZE:
pg.display.set_mode(event.size, pg.RESIZABLE)
break
if event.type == pg.MOUSEBUTTONUP:
if self.help_pos.collidepoint(pg.mouse.get_pos()):
self.show_help = not self.show_help
if self.show_help == True:
break
if self.canvas is not None:
self.canvas.grab_focus()
if self.frame.detect_click(pg.mouse.get_pos()):
self.set_turn()
if self.reset_rect.collidepoint(pg.mouse.get_pos()):
self.frame.reset(False)
self.score = [0, 0]
def draw_help(self):
pg.draw.circle(
g.WIN,
g.GREY,
self.help_pos.center,
40,
)
if self.show_help:
width = self.close_text.get_width()
height = self.close_text.get_height()
close_rect = (
(3 * g.WIDTH + g.FRAME_GAP * 3 - 2 * width) // 4,
(g.HEIGHT * 0.5 - g.FRAME_GAP * 1.5 - height) // 2
)
g.WIN.blit(
self.close_text, close_rect
)
pg.draw.rect(
g.WIN,
g.GREY,
pg.Rect(
50,
(g.HEIGHT - g.FRAME_GAP * 3) // 2 - 10,
g.WIDTH - 100,
g.FRAME_GAP * 3 + 20,
),
)
g.WIN.blit(
self.help_img,
(
(g.WIDTH - self.help_img.get_width()) // 2,
(g.HEIGHT - g.FRAME_GAP * 3) // 2,
),
)
for i, text in enumerate(self.help_text):
g.WIN.blit(
text,
(
(g.WIDTH - text.get_width()) // 2,
g.HEIGHT // 2 + 40 + 40 * i,
),
)
else:
width = self.question_text.get_width()
height = self.question_text.get_height()
question_rect = (
(3 * g.WIDTH + g.FRAME_GAP * 3 - 2 * width) // 4,
(g.HEIGHT * 0.5 - g.FRAME_GAP * 1.5 - height) // 2
)
g.WIN.blit(
self.question_text, question_rect
)
def draw(self):
g.WIN.fill(g.BLACK)
heading_w = self.heading.get_width()
heading_h = self.heading.get_height()
heading_rect = (
(g.WIDTH - heading_w) // 2,
(g.HEIGHT * 0.5 - g.FRAME_GAP * 1.5 - heading_h) // 2,
)
g.WIN.blit(
self.heading, heading_rect
)
tt_width = self.turn_text.get_width()
tt_height = self.turn_text.get_height()
tt_rect = (
(g.WIDTH - g.FRAME_GAP * 3 - 2 * tt_width) / 4,
(g.HEIGHT * 0.5 - g.FRAME_GAP * 1.5 - tt_height) // 2
)
g.WIN.blit(
self.turn_text, tt_rect
)
self.frame.draw()
self.cross_ui.update()
self.circle_ui.update()
scorex = self.font.render(str(self.score[0]), True, g.WHITE)
scoreo = self.font.render(str(self.score[1]), True, g.WHITE)
g.WIN.blit(
scorex,
(
(g.WIDTH - g.FRAME_GAP * 3 - 2 * scorex.get_width()) / 4,
(g.HEIGHT / 2 + g.FRAME_GAP / 4),
),
)
sc_width = scorex.get_width()
g.WIN.blit(
scoreo,
(
g.WIDTH - (g.WIDTH - g.FRAME_GAP * 3 + 2 * sc_width) / 4,
(g.HEIGHT / 2 + g.FRAME_GAP / 4),
),
)
self.draw_help()
pg.draw.rect(g.WIN, g.GREY, self.reset_rect)
pg.draw.circle(
g.WIN,
g.GREY,
(int(self.reset_rect.x), int(self.reset_rect.centery)),
self.reset_rect.height // 2,
)
pg.draw.circle(
g.WIN,
g.GREY,
(int(self.reset_rect.right), int(self.reset_rect.centery)),
self.reset_rect.height // 2,
)
g.WIN.blit(
self.reset_text,
(
g.WIDTH / 2 - self.reset_text.get_width() / 2,
g.HEIGHT - self.reset_text.get_height() - 70,
),
)
pg.display.update()
def reset(self):
self.frame = Frame(self, (g.WIDTH / 2, g.HEIGHT / 2))
self.set_turn()
def set_turn(self):
self.turn_text = pg.font.Font(None, 64).render(
["O " + _("Turn"), "", "X " + _("Turn")][self.frame.turn + 1],
True,
g.WHITE
)
# The main loop
def run(self):
for event in pg.event.get():
if event.type == pg.VIDEORESIZE:
pg.display.set_mode(event.size, pg.RESIZABLE)
break
g.init()
pg.font.init()
self.heading = pg.font.Font(None, 96).render(
_("Tic - Tac - Toe"),
True,
g.WHITE
)
self.reset_text = pg.font.Font(None, 56).render(
_("Reset"),
True,
g.WHITE
)
self.question_text = pg.font.Font(None, 72).render("?", True, g.WHITE)
self.close_text = pg.font.Font(None, 64).render("X", True, g.WHITE)
self.help_text = [
pg.font.Font(None, 36).render(
i,
True,
g.WHITE,
)
for i in (
_("Each player takes it in turn to place their X or O in"),
_("one of the empty squares in the grid by clicking on it."),
_("To win the game get three of your symbols in a line"),
_("horizontally, vertically or diagonally"),
)
]
self.help_pos = pg.Rect(
(3 * g.WIDTH + g.FRAME_GAP * 3) // 4 - 40,
(g.HEIGHT * 0.5 - g.FRAME_GAP * 1.5) // 2 - 40,
80,
80,
)
self.reset_rect = pg.Rect(
g.WIDTH / 2 - self.reset_text.get_width() / 2,
g.HEIGHT - self.reset_text.get_height() - 80,
self.reset_text.get_width(),
self.reset_text.get_height() + 20,
)
self.font = pg.font.Font(None, 72)
self.cross_ui = Animate(self, color=g.ORANGE).cross(
(
(g.WIDTH - g.FRAME_GAP * 3) / 4,
g.HEIGHT / 2 - g.FRAME_GAP / 4
), 43, 11
)
self.circle_ui = Animate(self, color=g.RED).circle(
(
g.WIDTH - (g.WIDTH - g.FRAME_GAP * 3) / 4,
g.HEIGHT / 2 - g.FRAME_GAP / 4
),
40,
8,
)
if self.canvas is not None:
self.canvas.grab_focus()
self.reset()
self.clock = pg.time.Clock()
while self.running:
if self.journal:
# Pump GTK messages.
while Gtk.events_pending():
Gtk.main_iteration()
self.check_events()
self.draw()
self.clock.tick(g.FPS)
pg.display.quit()
pg.quit()
sys.exit(0)
# Test if the script is directly ran
if __name__ == "__main__":
pg.init()
pg.display.set_mode((1024, 768))
main = Main(journal=False)
main.run()