-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scene.py
497 lines (431 loc) · 20 KB
/
Scene.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
from turtle import home
import pygame as pg
from pygame.locals import *
from utility import *
class Scene:
def __init__(self):
self.next_scene = self
def HandleEvents(self, events):
pass
def Draw(self, screen):
pass
def ChangeNext(self, next_scene):
self.next_scene = next_scene
def Terminate(self):
self.ChangeNext(None)
class TicTacToeScene(Scene):
def __init__(self, screen, board=[[None]*3 for _ in range(3)], current_player=1, board_size=600):
Scene.__init__(self)
self.back_but_rect = None
self.pause_but_rect = None
self.restart_but_rect = None
self.board_rect = None
self.screen = screen
self.board = board
self.current_player = current_player
self.board_size = board_size
x_image = pg.image.load('x.png')
o_image = pg.image.load('o.png')
self.x_img = pg.transform.scale(x_image, (100,100))
self.o_img = pg.transform.scale(o_image, (100,100))
self.redraw = True
self.new_step = False
self.new_x = 0
self.new_y = 0
self.result = "Playing"
self.steps = 0
# self.Draw(screen)
def HandleEvents(self, events):
for event in events:
if event.type == MOUSEBUTTONDOWN:
print("mouse clicked")
mouse_pos = pg.mouse.get_pos()
if self.back_but_rect.collidepoint(mouse_pos):
print("back to menu")
self.ChangeNext(MenuScene(self.screen))
elif self.pause_but_rect.collidepoint(mouse_pos):
print("game paused")
self.ChangeNext(PauseScene(self.screen, self.board, self.current_player, "TicTacToe"))
elif self.restart_but_rect.collidepoint(mouse_pos):
print("game restart")
self.reset()
elif self.board_rect.collidepoint(mouse_pos):
self.check_step(mouse_pos)
def check_step(self, mouse_pos):
x, y = mouse_pos
if x < self.board_size/3:
x_index = 0
elif x < self.board_size*2/3:
x_index = 1
elif x <= self.board_size:
x_index = 2
if y < self.board_size/3:
y_index = 0
elif y < self.board_size*2/3:
y_index = 1
elif y <= self.board_size:
y_index = 2
if self.board[x_index][y_index] is None:
# new step
self.new_step = True
self.new_x = x_index
self.new_y = y_index
self.steps += 1
if self.current_player == 1:
self.board[x_index][y_index] = True
else:
self.board[x_index][y_index] = False
self.result = self.check_win()
if self.result != "Playing":
self.ChangeNext(ResultScene(self.screen, self.result, "TicTacToe"))
def reset(self):
self.current_player = 1
self.board = [[None]*3 for _ in range(3)]
self.redraw = True
def check_win(self):
last_move_color = self.board[self.new_x][self.new_y]
connected_pieces = check_horizontal_connection(self.board, last_move_color, self.new_x, self.new_y, 3, 3)
if connected_pieces >= 3:
return "Player " + str(self.current_player) + " win!"
connected_pieces = check_vertical_connection(self.board, last_move_color, self.new_x, self.new_y, 3, 3)
if connected_pieces >= 3:
return "Player " + str(self.current_player) + " win!"
connected_pieces = check_main_diagnal_connection(self.board, last_move_color, self.new_x, self.new_y, 3, 3)
if connected_pieces >= 3:
return "Player " + str(self.current_player) + " win!"
connected_pieces = check_inverse_diagnal_connection(self.board, last_move_color, self.new_x, self.new_y, 3, 3)
if connected_pieces >= 3:
return "Player " + str(self.current_player) + " win!"
# check for draws
if self.steps == 3*3:
return "Draw"
print("yes")
return "Playing"
def draw_game(self, screen):
screen.fill((255,255,255)) # fill the whole window with white: remove the previous scene
screen.fill((222,184,135), (0, 0, 600, 600))
pg.draw.line(screen,(0,0,0),(0,0),(0, self.board_size),7)
pg.draw.line(screen,(0,0,0),(self.board_size/3,0),(self.board_size/3, self.board_size),7)
pg.draw.line(screen,(0,0,0),(self.board_size/3*2,0),(self.board_size/3*2, self.board_size),7)
pg.draw.line(screen,(0,0,0),(self.board_size,0),(self.board_size, self.board_size),7)
# Drawing horizontal lines
pg.draw.line(screen,(0,0,0),(0,0),(self.board_size, 0),7)
pg.draw.line(screen,(0,0,0),(0,self.board_size/3),(self.board_size, self.board_size/3),7)
pg.draw.line(screen,(0,0,0),(0,self.board_size/3*2),(self.board_size, self.board_size/3*2),7)
pg.draw.line(screen,(0,0,0),(0,self.board_size),(self.board_size, self.board_size),7)
self.board_rect = pg.Rect(0,0,600,600)
font = pg.font.Font(None, 50)
self.pause_but_rect = draw_button(font, "PAUSE", screen, (255,0,0), (self.board_size+150, 150), self.board_size+4, 100, 300, 100)
self.back_but_rect = draw_button(font, "BACK", screen, (220,220,220), (self.board_size+150, 250), self.board_size+4, 200, 300, 100)
self.restart_but_rect = draw_button(font, "RESTART", screen, (220,220,0), (self.board_size+150, 350), self.board_size+4, 300, 300, 100)
# restart = font.render("RESTART", 1, (0, 0, 0))
# restart_rect = restart.get_rect(center=(self.board_size+150, 350))
# self.restart_but_rect = pg.Rect(self.board_size+4, 300, 300,100)
# pg.draw.rect(screen, (220,220,0), self.restart_but_rect)
# screen.blit(restart, restart_rect)
# display instruction
screen.fill ((255, 255, 255), (self.board_size+4, 0, 300,100))
font2 = pg.font.Font(None, 30)
instruction = font2.render("Player " + str(self.current_player) + "'s turn", 1, (0, 0, 0))
inst_rect = instruction.get_rect(center=(self.board_size+150, 50))
screen.blit(instruction, inst_rect)
def update_instruction(self, screen):
if self.current_player == 1:
self.current_player = 2
elif self.current_player == 2:
self.current_player = 1
screen.fill ((255, 255, 255), (self.board_size+4, 0, 300,100))
font2 = pg.font.Font(None, 30)
instruction = font2.render("Player " + str(self.current_player) + "'s turn", 1, (0, 0, 0))
inst_rect = instruction.get_rect(center=(self.board_size+150, 50))
screen.blit(instruction, inst_rect)
def draw_step(self, screen):
if self.current_player == 1:
screen.blit(self.x_img,(self.new_x*200+50, self.new_y*200+50))
else:
screen.blit(self.o_img,(self.new_x*200+50, self.new_y*200+50))
def draw_previous_steps(self, screen):
for x_index in range(3):
for y_index in range(3):
if self.board[x_index][y_index] == True:
screen.blit(self.x_img,(x_index*200+50, y_index*200+50))
elif self.board[x_index][y_index] == False:
screen.blit(self.o_img,(x_index*200+50, y_index*200+50))
def Draw(self, screen):
if self.redraw == True:
print("redraw")
self.draw_game(screen)
self.draw_previous_steps(screen)
self.redraw = False
if self.new_step == True:
print("new step")
self.draw_step(screen)
self.update_instruction(screen)
self.new_step = False
class GomokuScene(Scene):
def __init__(self, screen, board=[[None]*15 for _ in range(15)], current_player=1, board_size=600):
Scene.__init__(self)
self.back_but_rect = None
self.pause_but_rect = None
self.restart_but_rect = None
self.board_rect = None
self.screen = screen
self.board = board
self.current_player = current_player
self.board_size = board_size
x_image = pg.image.load('black600.png')
o_image = pg.image.load('white600.png')
self.x_img = pg.transform.scale(x_image, (30,30))
self.o_img = pg.transform.scale(o_image, (30,30))
self.redraw = True
self.new_step = False
self.new_x = 0
self.new_y = 0
self.result = "Playing"
self.steps = 0
# self.Draw(screen)
def HandleEvents(self, events):
for event in events:
if event.type == MOUSEBUTTONDOWN:
print("mouse clicked")
mouse_pos = pg.mouse.get_pos()
if self.back_but_rect.collidepoint(mouse_pos):
print("back to menu")
self.ChangeNext(MenuScene(self.screen))
elif self.pause_but_rect.collidepoint(mouse_pos):
print("game paused")
self.ChangeNext(PauseScene(self.screen, self.board, self.current_player, "Gomoku"))
elif self.restart_but_rect.collidepoint(mouse_pos):
print("game restart")
self.reset()
elif self.board_rect.collidepoint(mouse_pos):
self.check_step(mouse_pos)
def check_step(self, mouse_pos):
x, y = mouse_pos
if x != 600:
x_index = x//40
else:
x_index = 14
if y!= 600:
y_index = y//40
else:
y_index = 14
if self.board[x_index][y_index] is None:
# new step
self.new_step = True
self.steps += 1
self.new_x = x_index
self.new_y = y_index
if self.current_player == 1:
self.board[x_index][y_index] = True
else:
self.board[x_index][y_index] = False
self.result = self.check_win()
if self.result != "Playing":
self.ChangeNext(ResultScene(self.screen, self.result, "Gomoku"))
def reset(self):
self.current_player = 1
self.board = [[None]*15 for _ in range(15)]
self.redraw = True
def check_win(self):
last_move_color = self.board[self.new_x][self.new_y]
connected_pieces = check_horizontal_connection(self.board, last_move_color, self.new_x, self.new_y, 5, 15)
if connected_pieces >= 5:
return "Player " + str(self.current_player) + " win!"
connected_pieces = check_vertical_connection(self.board, last_move_color, self.new_x, self.new_y, 5, 15)
if connected_pieces >= 5:
return "Player " + str(self.current_player) + " win!"
connected_pieces = check_main_diagnal_connection(self.board, last_move_color, self.new_x, self.new_y, 5, 15)
if connected_pieces >= 5:
return "Player " + str(self.current_player) + " win!"
connected_pieces = check_inverse_diagnal_connection(self.board, last_move_color, self.new_x, self.new_y, 5, 15)
if connected_pieces >= 5:
return "Player " + str(self.current_player) + " win!"
# check for draw
if self.steps == 15*15:
return "Draw"
return "Playing"
def draw_game(self, screen):
screen.fill((255,255,255)) # fill the whole window with white: remove the previous scene
screen.fill((222,184,135), (0, 0, 600, 600))
for i in range(15):
pg.draw.line(screen,(0,0,0),(20,20+40*i),(580, 20+40*i),5)
pg.draw.line(screen,(0,0,0),(20+40*i,20),(20+40*i, 580),5)
self.board_rect = pg.Rect(0,0,600,600)
font = pg.font.Font(None, 50)
self.pause_but_rect = draw_button(font, "PAUSE", screen, (255,0,0), (self.board_size+150, 150), self.board_size, 100, 300, 100)
self.back_but_rect = draw_button(font, "BACK", screen, (220,220,220), (self.board_size+150, 250), self.board_size, 200, 300, 100)
self.restart_but_rect = draw_button(font, "RESTART", screen, (220,220,0), (self.board_size+150, 350), self.board_size, 300, 300, 100)
# display instruction
screen.fill ((255, 255, 255), (self.board_size+4, 0, 300,100))
font2 = pg.font.Font(None, 30)
instruction = font2.render("Player " + str(self.current_player) + "'s turn", 1, (0, 0, 0))
inst_rect = instruction.get_rect(center=(self.board_size+150, 50))
screen.blit(instruction, inst_rect)
def update_instruction(self, screen):
if self.current_player == 1:
self.current_player = 2
elif self.current_player == 2:
self.current_player = 1
screen.fill ((255, 255, 255), (self.board_size+4, 0, 300,100))
font2 = pg.font.Font(None, 30)
instruction = font2.render("Player " + str(self.current_player) + "'s turn", 1, (0, 0, 0))
inst_rect = instruction.get_rect(center=(self.board_size+150, 50))
screen.blit(instruction, inst_rect)
def draw_step(self, screen):
if self.current_player == 1:
screen.blit(self.x_img,(self.new_x*40+5, self.new_y*40+5))
else:
screen.blit(self.o_img,(self.new_x*40+5, self.new_y*40+5))
def draw_previous_steps(self, screen):
for x_index in range(15):
for y_index in range(15):
if self.board[x_index][y_index] == True:
screen.blit(self.x_img,(x_index*40+5, y_index*40+5))
elif self.board[x_index][y_index] == False:
screen.blit(self.o_img,(x_index*40+5, y_index*40+5))
def Draw(self, screen):
if self.redraw == True:
print("redraw")
self.draw_game(screen)
self.draw_previous_steps(screen)
self.redraw = False
if self.new_step == True:
print("new step")
self.draw_step(screen)
self.update_instruction(screen)
self.new_step = False
# pg.display.update()
class SelectGameScene(Scene):
def __init__(self, screen):
Scene.__init__(self)
self.redraw = True
self.screen = screen
self.options = ["Tic Tac Toe", "Gomoku"]
self.option_but_list = []
self.hovered_option = -1
# self.Draw(screen)
def HandleEvents(self, events):
mouse_pos = pg.mouse.get_pos()
for i, option_but in enumerate(self.option_but_list):
if option_but.collidepoint(mouse_pos):
if i != self.hovered_option:
self.hovered_option = i
self.redraw = True
for event in events:
if event.type == MOUSEBUTTONDOWN:
option_clicked = False
for i, option_but in enumerate(self.option_but_list):
if option_but.collidepoint(mouse_pos) and i==0:
option_clicked = True
self.ChangeNext(TicTacToeScene(self.screen, [[None]*3 for _ in range(3)], 1))
elif option_but.collidepoint(mouse_pos) and i==1:
option_clicked = True
self.ChangeNext(GomokuScene(self.screen, [[None]*15 for _ in range(15)], 1))
if option_clicked == False:
self.ChangeNext(MenuScene(self.screen))
def draw_drop_down_menu(self, screen):
pg.draw.line(screen,(0,0,0),(300, 270+60),(300, 270+60*(len(self.options)+1)),7)
pg.draw.line(screen,(0,0,0),(600, 270+60),(600, 270+60*(len(self.options)+1)),7)
pg.draw.line(screen,(0,0,0),(300, 270+60*(len(self.options)+1)),(600, 270+60*(len(self.options)+1)),7)
self.option_but_list = []
for i, option in enumerate(self.options):
font = pg.font.Font(None, 50)
if i == self.hovered_option:
color = (172, 216, 230) # light blue color
else:
color = (255, 255, 255)
option_but_area = draw_button(font, option, screen, color, (900/2, 300+60*(i+1)), 300, 270 + 60*(i+1), 300, 60)
self.option_but_list.append(option_but_area)
def Draw(self, screen):
if self.redraw == True:
self.draw_drop_down_menu(screen)
self.redraw = False
class MenuScene(Scene):
def __init__(self, screen):
Scene.__init__(self)
self.redraw = True
self.start_but_rect = None
self.screen = screen
# self.Draw(screen)
def HandleEvents(self, events):
for event in events:
if event.type == MOUSEBUTTONDOWN:
mouse_pos = pg.mouse.get_pos()
if self.start_but_rect.collidepoint(mouse_pos):
print("select game clicked")
self.ChangeNext(SelectGameScene(self.screen))
def draw_menu(self, screen):
screen.fill((255,255,255)) # white
font = pg.font.Font(None, 80)
text = font.render("Board Game Master", 1, (0, 0, 0))
font.set_italic(True)
font.set_bold(True)
# put the title at the center of the screen
text_rect = text.get_rect(center=(900/2, 200))
screen.blit(text, text_rect)
font2 = pg.font.Font(None, 50)
self.start_but_rect = draw_button(font2, "Select Game", screen, (0,255,0), (900/2, 300), 300, 270, 300, 60)
def Draw(self, screen):
if self.redraw == True:
self.draw_menu(screen)
self.redraw = False
class ResultScene(Scene):
def __init__(self, screen, result, game):
Scene.__init__(self)
self.redraw = True
self.result_message_rect = None
self.screen = screen
self.result = result
self.game = game
# self.Draw(screen)
def HandleEvents(self, events):
for event in events:
if event.type == MOUSEBUTTONDOWN:
mouse_pos = pg.mouse.get_pos()
if self.result_message_rect.collidepoint(mouse_pos):
if self.game == "TicTacToe":
self.ChangeNext(TicTacToeScene(self.screen, [[None]*3 for _ in range(3)], 1))
elif self.game == "Gomoku":
self.ChangeNext(GomokuScene(self.screen, [[None]*15 for _ in range(15)], 1))
def draw_result(self, screen):
font = pg.font.Font(None, 50)
msg = font.render(self.result, 1, (0, 0, 0))
msg_rect = msg.get_rect(center=(900/2, 600/2))
self.result_message_rect = pg.Rect(900/2-200, 600/2-150/2, 400,150)
pg.draw.rect(screen, (0,0,220), self.result_message_rect)
screen.blit(msg, msg_rect)
def Draw(self, screen):
if self.redraw == True:
self.draw_result(screen)
self.redraw = False
class PauseScene(Scene):
def __init__(self, screen, board, current_player, game):
Scene.__init__(self)
self.redraw = True
self.pause_message_rect = None
self.screen = screen
self.board = board
self.current_player = current_player
self.game = game
# self.Draw(screen)
def HandleEvents(self, events):
for event in events:
if event.type == MOUSEBUTTONDOWN:
mouse_pos = pg.mouse.get_pos()
if self.pause_message_rect.collidepoint(mouse_pos):
if self.game == "TicTacToe":
self.ChangeNext(TicTacToeScene(self.screen, self.board, self.current_player))
elif self.game == "Gomoku":
self.ChangeNext(GomokuScene(self.screen, self.board,self.current_player))
def draw_pause(self, screen):
font = pg.font.Font(None, 50)
pause = font.render("Game Paused", 1, (0, 0, 0))
pause_rect = pause.get_rect(center=(900/2, 600/2))
self.pause_message_rect = pg.Rect(900/2-200, 600/2-150/2, 400,150)
pg.draw.rect(screen, (0,0,220), self.pause_message_rect)
screen.blit(pause, pause_rect)
def Draw(self, screen):
if self.redraw == True:
self.draw_pause(screen)
self.redraw = False