-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
583 lines (499 loc) · 26.2 KB
/
main.py
File metadata and controls
583 lines (499 loc) · 26.2 KB
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
# PACKAGES NEEDED:
# pip install pygame
# IMPORTS
import pygame
import socket
import colors
import figure
from random import randint
from math import floor
from board import initialize_board, DefaultFigureCodec
from threading import Thread
# GLOBAL VARS
pygame.init()
pygame.font.init()
DISPLAY_INFO = pygame.display.Info()
SCREEN_W = 1536 # DISPLAY_INFO.current_w*0.8
SCREEN_H = 864 # DISPLAY_INFO.current_h*0.8
SCREEN = pygame.display.set_mode((SCREEN_W, SCREEN_H))
pygame.display.set_caption("CHESS")
CLOCK = pygame.time.Clock()
XL_FONT = pygame.font.SysFont("Arial", 64)
L_FONT = pygame.font.SysFont("Arial", 48)
M_FONT = pygame.font.SysFont("Arial", 30)
S_FONT = pygame.font.SysFont("Arial", 18)
FPS = 60
GAME_DEBUG = False
TRANSMISSION_DEBUG = True
LAST_REC_DATA = None
LAST_REC_ADDR = None
CLICKED_TILE = None
POSSIBLE_MOVES = []
MOVE_PLAYED = None
MOVE_REQUESTED = False
# OBJECT CLASSES
# SIMPLE BORDER BUTTON
class Button:
def __init__(self, pos_x, pos_y, width, height, b_text = "", font_color=colors.WHITE, border_color=colors.WHITE,
border=0, border_radius=-1):
self.x = pos_x
self.y = pos_y
self.w = width
self.h = height
self.rect = pygame.Rect(pos_x, pos_y, width, height)
self.b_text = b_text
self.b_text_len = len(b_text)
self.font_color = font_color
self.border_color = border_color
self.border = border
self.border_radius = border_radius
self.clicked = False
def draw(self):
action = False
pos = pygame.mouse.get_pos()
pygame.draw.rect(SCREEN, self.border_color, self.rect, self.border, self.border_radius)
b_text_obj = None
w_char_s = 0
center_height_offset = 0
if self.h < 40:
b_text_obj = S_FONT.render(self.b_text, True, self.font_color)
center_height_offset = 11
w_char_s = 12
elif self.h < 64:
b_text_obj = M_FONT.render(self.b_text, True, self.font_color)
center_height_offset = 18
w_char_s = 21
elif self.h < 100:
b_text_obj = L_FONT.render(self.b_text, True, self.font_color)
center_height_offset = 29
w_char_s = 32
else:
b_text_obj = XL_FONT.render(self.b_text, True, self.font_color)
center_height_offset = 40
w_char_s = 44
SCREEN.blit(b_text_obj, (self.x + self.w/2 - self.b_text_len*w_char_s/2.66, self.y + self.h/2 -
center_height_offset))
if self.rect.collidepoint(pos):
if pygame.mouse.get_pressed()[0] == 1 and not self.clicked:
self.clicked = True
action = True
if pygame.mouse.get_pressed()[0] == 0:
self.clicked = False
return action
# BOARD TILE
class BoardTile:
def __init__(self, tile_pos, tile_size, tile_color):
self.pos_x = tile_pos[0]
self.pox_y = tile_pos[1]
self.size = tile_size
self.color = tile_color
self.rect = pygame.Rect(self.pos_x, self.pox_y, self.size, self.size)
self.clicked = False
def draw(self):
pygame.draw.rect(SCREEN, self.color, self.rect)
if self.rect.collidepoint(pygame.mouse.get_pos()):
if pygame.mouse.get_pressed()[0] == 1:
if not self.clicked:
self.clicked = True
return False
else:
if self.clicked:
self.clicked = False
return True
else:
return False
else:
self.clicked = False
return False
def sketch(self):
pygame.draw.rect(SCREEN, self.color, self.rect)
# FUNCTIONS
# RENDER STRING TO SCREEN
def render_text(screen_text, screen_pos_x, screen_pos_y, size, font_color=colors.WHITE):
text_obj = None
if size.lower() == "s":
text_obj = S_FONT.render(screen_text, True, font_color)
elif size.lower() == "m":
text_obj = M_FONT.render(screen_text, True, font_color)
elif size.lower() == "l":
text_obj = L_FONT.render(screen_text, True, font_color)
else:
text_obj = XL_FONT.render(screen_text, True, font_color)
SCREEN.blit(text_obj, (screen_pos_x, screen_pos_y))
# THREADING RECEIVE AT SOCKET FROM ENDPOINT
def conn_rec(t_socket):
global LAST_REC_DATA, LAST_REC_ADDR
t_data, LAST_REC_ADDR = t_socket.recvfrom(1024)
LAST_REC_DATA = t_data.decode("utf-8")
if TRANSMISSION_DEBUG: print(f"In: {LAST_REC_DATA}")
# THREADING SEND FROM SOCKET TO ENDPOINT
def conn_send(t_socket, msg, t_endpoint):
t_socket.sendto(msg.encode("utf-8"), t_endpoint)
if TRANSMISSION_DEBUG: print(f"Out: {msg}")
# INIT BOARD
def init_board(board_pos, board_square_size, board_base_color, board_square_color):
board_tile_matrix = [[None for i in range(8)] for j in range(8)]
for i in range(len(board_tile_matrix)):
for j in range(len(board_tile_matrix[i])):
if (i % 2 == 0) == (j % 2 == 1):
board_tile_matrix[i][j] = BoardTile((i * board_square_size + board_pos[0], (7 - j) * board_square_size + board_pos[1]), board_square_size, board_base_color)
else:
board_tile_matrix[i][j] = BoardTile((i * board_square_size + board_pos[0], (7 - j) * board_square_size + board_pos[1]), board_square_size, board_square_color)
return board_tile_matrix
# CALC POSSIBLE MOVES
def calc_possible_moves(bm, fp, iw, codec = DefaultFigureCodec):
clicked_fig = bm[fp[0]][fp[1]]
pm = []
# WHITE PAWN
if clicked_fig == codec.w_p:
pm += figure.pawn_moves(bm, fp, iw)
# BLACK PAWN
elif clicked_fig == codec.b_p:
pm += figure.pawn_moves(bm, fp, iw)
# CASTLE
elif clicked_fig == codec.w_r or clicked_fig == codec.b_r:
pm += figure.rook_moves(bm, fp, iw)
# HORSE
elif clicked_fig == codec.w_kn or clicked_fig == codec.b_kn:
pm += figure.knight_moves(bm, fp, iw)
# SNIPER
elif clicked_fig == codec.w_b or clicked_fig == codec.b_b:
pm += figure.bishop_moves(bm, fp, iw)
# KING
elif clicked_fig == codec.w_k or clicked_fig == codec.b_k:
pm += figure.king_moves(bm, fp, iw)
# QUEEN
elif clicked_fig == codec.w_q or clicked_fig == codec.b_q:
pm += figure.queen_moves(bm, fp, iw)
return pm
# DRAW BOARD
def draw_board(board_m, figure_d, board_tm, board_pos, board_square_size, playing_as_white, move_able = True):
global CLICKED_TILE, POSSIBLE_MOVES, MOVE_PLAYED
new_click = None
for i in range(len(board_tm)):
for j in range(len(board_tm[i])):
if playing_as_white:
if board_tm[i][j].draw():
new_click = [i, j]
else:
if board_tm[7 - i][7 - j].draw():
new_click = [i, j]
if board_m[i][j] > 0:
tile_text = L_FONT.render(str(board_m[i][j]), True, colors.BLACK)
if playing_as_white:
# SCREEN.blit(tile_text, (i * board_square_size + board_pos[0] + board_square_size * 0.25, (7 - j) * board_square_size + board_pos[1] + board_square_size * 0.2))
SCREEN.blit(figure_d[board_m[i][j]], (i * board_square_size + board_pos[0] + board_square_size * 0.25 - 27, (7 - j) * board_square_size + board_pos[1] + board_square_size * 0.2 - 22))
else:
# SCREEN.blit(tile_text, ((7 - i) * board_square_size + board_pos[0] + board_square_size * 0.25, j * board_square_size + board_pos[1] + board_square_size * 0.2))
SCREEN.blit(figure_d[board_m[i][j]], ((7 - i) * board_square_size + board_pos[0] + board_square_size * 0.25 - 27, j * board_square_size + board_pos[1] + board_square_size * 0.2 - 22))
if new_click and move_able:
if CLICKED_TILE:
if new_click == CLICKED_TILE:
CLICKED_TILE = None
POSSIBLE_MOVES = []
elif new_click in POSSIBLE_MOVES:
board_m[new_click[0]][new_click[1]] = board_m[CLICKED_TILE[0]][CLICKED_TILE[1]]
board_m[CLICKED_TILE[0]][CLICKED_TILE[1]] = 0
MOVE_PLAYED = [CLICKED_TILE, new_click]
CLICKED_TILE = None
POSSIBLE_MOVES = []
else:
if playing_as_white and 0 < board_m[new_click[0]][new_click[1]] < 10 or not playing_as_white and 10 < board_m[new_click[0]][new_click[1]]:
CLICKED_TILE = new_click
POSSIBLE_MOVES = calc_possible_moves(board_m, new_click, playing_as_white)
else:
if playing_as_white and 0 < board_m[new_click[0]][new_click[1]] < 10 or not playing_as_white and 10 < board_m[new_click[0]][new_click[1]]:
CLICKED_TILE = new_click
POSSIBLE_MOVES = calc_possible_moves(board_m, new_click, playing_as_white)
if CLICKED_TILE and move_able:
if playing_as_white:
BoardTile((CLICKED_TILE[0] * board_square_size + board_pos[0],
(7 - CLICKED_TILE[1]) * board_square_size + board_pos[1]), board_square_size, colors.GREEN).sketch()
BoardTile((CLICKED_TILE[0] * board_square_size + board_pos[0] + 5,
(7 - CLICKED_TILE[1]) * board_square_size + board_pos[1] + 5), board_square_size - 10, colors.WHITE if (CLICKED_TILE[0] + CLICKED_TILE[1]) % 2 else colors.DARK_BLUE).sketch()
SCREEN.blit(figure_d[board_m[CLICKED_TILE[0]][CLICKED_TILE[1]]], (CLICKED_TILE[0] * board_square_size + board_pos[0] + board_square_size * 0.25 - 27,
(7 - CLICKED_TILE[1]) * board_square_size + board_pos[1] + board_square_size * 0.2 - 22))
else:
BoardTile(((7 - CLICKED_TILE[0]) * board_square_size + board_pos[0],
CLICKED_TILE[1] * board_square_size + board_pos[1]), board_square_size,
colors.GREEN).sketch()
BoardTile(((7 - CLICKED_TILE[0]) * board_square_size + board_pos[0] + 5,
CLICKED_TILE[1] * board_square_size + board_pos[1] + 5), board_square_size - 10,
colors.WHITE if (CLICKED_TILE[0] + CLICKED_TILE[1]) % 2 else colors.DARK_BLUE).sketch()
SCREEN.blit(figure_d[board_m[CLICKED_TILE[0]][CLICKED_TILE[1]]], ((7 - CLICKED_TILE[0]) * board_square_size + board_pos[0] + board_square_size * 0.25 - 27,
CLICKED_TILE[1] * board_square_size + board_pos[1] + board_square_size * 0.2 - 22))
for tile in POSSIBLE_MOVES:
if playing_as_white:
BoardTile((tile[0] * board_square_size + board_pos[0], (7 - tile[1]) * board_square_size + board_pos[1]), board_square_size, colors.RED).sketch()
BoardTile((tile[0] * board_square_size + board_pos[0] + 5, (7 - tile[1]) * board_square_size + board_pos[1] + 5) , board_square_size - 10, colors.WHITE if (tile[0] + tile[1]) % 2 else colors.DARK_BLUE).sketch()
if board_m[tile[0]][tile[1]] > 0:
# tile_text = L_FONT.render(str(board_m[tile[0]][tile[1]]), True, colors.BLACK)
# SCREEN.blit(tile_text, (tile[0] * board_square_size + board_pos[0] + board_square_size * 0.25, (7 - tile[1]) * board_square_size + board_pos[1] + board_square_size * 0.2))
SCREEN.blit(figure_d[board_m[tile[0]][tile[1]]], (tile[0] * board_square_size + board_pos[0] + board_square_size * 0.25 - 27, (7 - tile[1]) * board_square_size + board_pos[1] + board_square_size * 0.2 - 22))
else:
BoardTile(((7 - tile[0]) * board_square_size + board_pos[0], tile[1] * board_square_size + board_pos[1]), board_square_size, colors.RED).sketch()
BoardTile(((7 - tile[0]) * board_square_size + board_pos[0] + 5, tile[1] * board_square_size + board_pos[1] + 5), board_square_size - 10, colors.WHITE if (tile[0] + tile[1]) % 2 else colors.DARK_BLUE).sketch()
if board_m[tile[0]][tile[1]] > 0:
# tile_text = L_FONT.render(str(board_m[tile[0]][tile[1]]), True, colors.BLACK)
# SCREEN.blit(tile_text, ((7 - tile[0]) * board_square_size + board_pos[0] + board_square_size * 0.25, tile[1] * board_square_size + board_pos[1] + board_square_size * 0.2))
SCREEN.blit(figure_d[board_m[tile[0]][tile[1]]], ((7 - tile[0]) * board_square_size + board_pos[0] + board_square_size * 0.25 - 27, tile[1] * board_square_size + board_pos[1] + board_square_size * 0.2 - 22))
# MAIN LOOP
def main():
# GLOBAL VARS
global LAST_REC_DATA, LAST_REC_ADDR, MOVE_PLAYED, MOVE_REQUESTED
# SOCKET VARS
local_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
conn_socket = None
# MAIN VARS
running = True
game_phase = 0
host_mode = None
# PHASE 1 VARS
local_port = 0
local_port_locked = False
local_port_error = False
conn_port = 0
conn_addr = ""
conn_port_locked = False
conn_addr_locked = False
conn_port_error = False
conn_addr_error = False
conn_started = False
conn_established = False
# PHASE 2 VARS
board_matrix = initialize_board()
board_tile_matrix = None
board_tile_matrix_generated = False
playing_as_white = False
game_ongoing = True
game_moves = 0
game_result = ""
# FIGURE TEXTURES
figure_dict = dict()
figure_dict[1] = pygame.image.load("textures/w_p.png")
figure_dict[2] = pygame.image.load("textures/w_r.png")
figure_dict[3] = pygame.image.load("textures/w_kn.png")
figure_dict[4] = pygame.image.load("textures/w_b.png")
figure_dict[5] = pygame.image.load("textures/w_k.png")
figure_dict[6] = pygame.image.load("textures/w_q.png")
figure_dict[11] = pygame.image.load("textures/b_p.png")
figure_dict[12] = pygame.image.load("textures/b_r.png")
figure_dict[13] = pygame.image.load("textures/b_kn.png")
figure_dict[14] = pygame.image.load("textures/b_b.png")
figure_dict[15] = pygame.image.load("textures/b_k.png")
figure_dict[16] = pygame.image.load("textures/b_q.png")
# GAME LOOP
while running:
# CLEAR AFTER FLIP
SCREEN.fill(colors.BLACK)
# EVENT CATCHES
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
# GAME PHASE 1 KEY OPTIONS
if game_phase == 1:
# Enter Key
if event.dict["key"] == 13:
if not local_port_locked:
if 1024 < local_port < 65535:
local_port_locked = True
elif not conn_port_locked:
if 1024 < conn_port < 65535:
conn_port_locked = True
elif not conn_addr_locked:
conn_addr_parts = conn_addr.split(".")
if len(conn_addr_parts) == 4:
addr_valid = True
for addr_part in conn_addr_parts:
if int(addr_part) > 255:
addr_valid = False
break
if addr_valid:
conn_addr_locked = True
elif len(conn_addr) == 0:
conn_addr = "localhost"
conn_addr_locked = True
elif event.dict["key"] == 8:
if not local_port_locked:
local_port_error = False
if local_port > 10:
local_port = floor(local_port / 10)
else:
local_port = 0
elif not conn_port_locked:
conn_port_error = False
if conn_port > 10:
conn_port = floor(conn_port / 10)
else:
conn_port = 0
elif not conn_addr_locked:
conn_addr_error = False
if len(conn_addr) > 0:
conn_addr = conn_addr[:-1]
if conn_addr[-1:] == ".":
conn_addr = conn_addr[:-1]
elif event.dict["key"] == 46:
if not conn_addr_locked:
conn_addr_error = False
addr_split = conn_addr.split(".")
if len(addr_split) < 4 and len(addr_split[-1]) > 0:
conn_addr += "."
else:
if not local_port_locked:
if event.dict["unicode"].isnumeric() and local_port * 10 + int(event.dict["unicode"]) < 65535:
local_port = local_port * 10 + int(event.dict["unicode"])
local_port_error = False
else:
local_port_error = True
elif not conn_port_locked:
if event.dict["unicode"].isnumeric() and conn_port * 10 + int(event.dict["unicode"]) < 65535:
conn_port = conn_port * 10 + int(event.dict["unicode"])
conn_port_error = False
else:
conn_port_error = True
elif not conn_addr_locked:
addr_split = conn_addr.split(".")
if event.dict["unicode"].isnumeric() and len(conn_addr) < 15 and len(addr_split) < 5:
if not len(addr_split) == 4 or not len(addr_split[-1]) == 3:
if len(addr_split[-1]) == 3:
conn_addr += "."
conn_addr += event.dict["unicode"]
conn_addr_error = False
else:
conn_addr_error = True
else:
conn_addr_error = True
# GAME LOGIC: PHASE SEPARATED
# PHASE 0: CHOOSE HOST/JOIN
if game_phase == 0 and not GAME_DEBUG:
host_btn = Button(SCREEN_W*0.25, SCREEN_H*0.2, SCREEN_W*0.25, SCREEN_H*0.6, "HOST", colors.WHITE, colors.WHITE, 2)
join_btn = Button(SCREEN_W*0.55, SCREEN_H*0.2, SCREEN_W*0.25, SCREEN_H*0.6, "JOIN", colors.WHITE, colors.WHITE, 2)
render_text("CHOOSE CONNECTION MODE:", SCREEN_W*0.33, SCREEN_H*0.1, "L")
if host_btn.draw():
host_mode = True
game_phase = 1
if join_btn.draw():
host_mode = False
game_phase = 1
# PHASE 1: UDP CONN SETUP
elif game_phase == 1 and not GAME_DEBUG:
# HOST CONN SETUP
if host_mode:
if not local_port_locked:
render_text("Enter port for UDP socket (confirm with enter): ", 0.1*SCREEN_W, 0.1*SCREEN_H, "l")
if local_port > 0:
render_text(f"{local_port}", 0.1 * SCREEN_W, 0.2 * SCREEN_H, "l")
if local_port_error:
render_text(f"Possible ports are in range 1024 - 65535", 0.1 * SCREEN_W, 0.2 * SCREEN_H, "l", colors.RED)
elif not conn_established:
if LAST_REC_DATA == "PyChessByNicoConnReq":
LAST_REC_DATA = None
conn_established = True
conn_socket = LAST_REC_ADDR
paw = randint(1, 2)
Thread(target=conn_send, args=(local_socket, f"PyChessByNicoConnAcc.{paw}", conn_socket)).start()
if paw == 1:
playing_as_white = True
else:
playing_as_white = False
game_phase = 2
if not conn_started:
conn_started = True
local_socket.bind(("", local_port))
Thread(target=conn_rec, args=(local_socket,)).start()
render_text("Waiting for a connection...", SCREEN_W*0.33, SCREEN_H*0.1, "L")
# CLIENT CONN SETUP
else:
if not local_port_locked:
render_text("Enter port for UDP socket (confirm with enter): ", 0.1 * SCREEN_W, 0.1 * SCREEN_H, "l")
if local_port > 0:
render_text(f"{local_port}", 0.1 * SCREEN_W, 0.2 * SCREEN_H, "l")
if local_port_error:
render_text(f"Possible ports are in range 1024 - 65535", 0.1 * SCREEN_W, 0.2 * SCREEN_H, "l", colors.RED)
elif not conn_port_locked:
render_text("Enter host port (confirm with enter): ", 0.1 * SCREEN_W, 0.1 * SCREEN_H, "l")
if conn_port > 0:
render_text(f"{conn_port}", 0.1 * SCREEN_W, 0.2 * SCREEN_H, "l")
if conn_port_error:
render_text(f"Possible ports are in range 1024 - 65535", 0.1 * SCREEN_W, 0.2 * SCREEN_H, "l", colors.RED)
elif not conn_addr_locked:
render_text("Enter host ip (confirm with enter): ", 0.1 * SCREEN_W, 0.1 * SCREEN_H, "l")
if conn_addr:
render_text(f"{conn_addr}", 0.1 * SCREEN_W, 0.2 * SCREEN_H, "l")
if conn_addr_error:
render_text(f"IP has to be like ###.###.###.###", 0.1 * SCREEN_W, 0.2 * SCREEN_H, "l", colors.RED)
render_text(f"With each ### < 256", 0.1 * SCREEN_W, 0.3 * SCREEN_H, "l", colors.RED)
elif not conn_established:
if LAST_REC_DATA is not None and LAST_REC_DATA.split(".")[0] == "PyChessByNicoConnAcc":
if LAST_REC_DATA.split(".")[1] == "1":
playing_as_white = False
else:
playing_as_white = True
LAST_REC_DATA = None
conn_established = True
conn_socket = LAST_REC_ADDR
game_phase = 2
if not conn_started:
conn_started = True
local_socket.bind(("", local_port))
Thread(target=conn_send, args=(local_socket, "PyChessByNicoConnReq", (conn_addr, conn_port))).start()
Thread(target=conn_rec, args=(local_socket,)).start()
render_text("Establishing connection...", SCREEN_W * 0.33, SCREEN_H * 0.1, "L")
# PHASE 2: ...
elif game_phase == 2 and not GAME_DEBUG:
# GENERATE BOARD TILES
if not board_tile_matrix_generated:
board_tile_matrix = init_board((368, 32), 100, colors.WHITE, colors.DARK_BLUE)
board_tile_matrix_generated = True
if game_ongoing:
# ROUND IF WHITE
if game_moves % 2 == 0 and playing_as_white or game_moves % 2 == 1 and not playing_as_white:
draw_board(board_matrix, figure_dict, board_tile_matrix, (368, 32), 100, playing_as_white)
if MOVE_PLAYED is not None:
Thread(target=conn_send, args=(local_socket, f"{MOVE_PLAYED[0][0]}.{MOVE_PLAYED[0][1]}-{MOVE_PLAYED[1][0]}.{MOVE_PLAYED[1][1]}", conn_socket)).start()
MOVE_REQUESTED = False
MOVE_PLAYED = None
game_moves += 1
# ROUND IF BLACK
else:
draw_board(board_matrix, figure_dict, board_tile_matrix, (368, 32), 100, playing_as_white, False)
if not MOVE_REQUESTED:
Thread(target=conn_rec, args=(local_socket,)).start()
MOVE_REQUESTED = True
if LAST_REC_DATA is not None:
rds = LAST_REC_DATA.split("-")
ds1 = rds[0].split(".")
ds2 = rds[1].split(".")
board_matrix[int(ds2[0])][int(ds2[1])] = board_matrix[int(ds1[0])][int(ds1[1])]
board_matrix[int(ds1[0])][int(ds1[1])] = 0
LAST_REC_DATA = None
game_moves += 1
# CHECK IF GAME ENDED
bkf = wkf = False
for i in board_matrix:
for j in i:
if j == 5:
wkf = True
elif j == 15:
bkf = True
if not wkf and playing_as_white or not bkf and not playing_as_white:
game_result = "YOU LOST!"
game_ongoing = False
game_phase = 3
elif not wkf and not playing_as_white or not bkf and playing_as_white:
game_result = "YOU WON!"
game_ongoing = False
game_phase = 3
# PHASE 3
elif game_phase == 3 and not GAME_DEBUG:
gg_text = XL_FONT.render(game_result, True, colors.WHITE)
SCREEN.blit(gg_text, (SCREEN_W * 0.4, SCREEN_H * 0.4))
# OUTPUT SCREEN IN 60 FPS
pygame.display.flip()
CLOCK.tick(FPS)
# EXIT
pygame.quit()
exit()
if __name__ == "__main__":
main()