-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.py
More file actions
754 lines (616 loc) · 24.1 KB
/
Copy pathgame.py
File metadata and controls
754 lines (616 loc) · 24.1 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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
from operator import le
import re
import pygame
import random
from word_checker import WordChecker
# === CONSTANS ===
PLAYERS = ['Player 1']
BLACK = ( 0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = ( 0, 0, 255)
# MAIN_THEME = (246, 197, 233)
# X3_WORD_COLOR = (134, 87, 255)
# X2_WORD_COLOR = (140, 155, 194)
MAIN_THEME = (188, 197, 220)
X3_WORD_COLOR = (13, 112, 75)
X2_WORD_COLOR = (132, 214, 127)
X2_LETTER_COLOR = (134, 87, 255)
X3_LETTER_COLOR = (234, 207, 150)
START_COLOR = (246, 197, 233)
SCREEN_WIDTH = 700
SCREEN_HEIGHT = 600
TILESIZE = 30
TILE_MARGIN = 1
LETTER_SCORE = {
'A': 1,
'B': 9,
'C': 1,
'D': 2,
'E': 1,
'F': 8,
'G': 9,
'H': 10,
'I': 1,
'J': 10,
'L': 1,
'M': 4,
'N': 1,
'O': 1,
'P': 2,
'R': 1,
'S': 1,
'T': 1,
'U': 1,
'V': 8,
'X': 10,
':)': 0
}
LETTER_COUNT = {
'A': 11,
'B': 2,
'C': 5,
'D': 4,
'E': 9,
'F': 2,
'G': 2,
'H': 1,
'I': 10,
'J': 1,
'L': 4,
'M': 3,
'N': 6,
'O': 5,
'P': 4,
'R': 7,
'S': 5,
'T': 7,
'U': 6,
'V': 2,
'X': 1,
':)': 2
}
BOARD_POS = (10, 10)
PLAYER_BOARD_POS = (10, 500)
SUBMIT_BUTTON_POS = (300, 505)
SCORE_BOARD_POS = (500, 10)
X2_WORD_SPOTS = [(1, 1), (2, 2), (3, 3), (4, 4), (13, 1), (12, 2), (11, 3), (10, 4), (1, 13), (2, 12), (3, 11), (4, 10), (10, 10), (11, 11), (12, 12), (13, 13)]
X3_WORD_SPOTS = [(0, 0), (7, 0), (14, 0), (0, 7), (14, 7), (0, 14), (7, 14), (14, 14)]
X2_LETTER_SPOTS = [(3, 0), (0, 3), (6, 2), (2, 6), (7, 3), (3, 7), (8, 2), (2, 8), (11, 0), (0, 11), (6, 6), (8, 8), (6, 8), (8, 6), (3, 14), (14, 3), (6, 12), (12, 6), (7, 11), (11, 7), (8, 12), (12, 8), (11, 14), (14, 11)]
X3_LETTER_SPOTS = [(5, 1), (1, 5), (1, 9), (9, 1), (5, 5), (9, 9), (5, 9), (9, 5), (13, 5), (5, 13), (9, 13), (13, 9)]
START_TILE = (7, 7)
# === CLASSES ===
buttons = []
class Button:
def __init__(self,text,width,height,pos,elevation,font,click_function):
#Core attributes
self.pressed = False
self.elevation = elevation
self.dynamic_elecation = elevation
self.original_y_pos = pos[1]
self.font = font
self.click_function = click_function
# top rectangle
self.top_rect = pygame.Rect(pos,(width,height))
self.top_color = '#475F77'
# bottom rectangle
self.bottom_rect = pygame.Rect(pos,(width,height))
self.bottom_color = '#354B5E'
#text
self.text = text
self.text_surf = self.font.render(text,True,'#FFFFFF')
self.text_rect = self.text_surf.get_rect(center = self.top_rect.center)
buttons.append(self)
def change_text(self, newtext):
self.text_surf = self.font.render(newtext, True,'#FFFFFF')
self.text_rect = self.text_surf.get_rect(center = self.top_rect.center)
def draw(self):
# elevation logic
self.top_rect.y = self.original_y_pos - self.dynamic_elecation
self.text_rect.center = self.top_rect.center
self.bottom_rect.midtop = self.top_rect.midtop
self.bottom_rect.height = self.top_rect.height + self.dynamic_elecation
pygame.draw.rect(screen,self.bottom_color, self.bottom_rect,border_radius = 12)
pygame.draw.rect(screen,self.top_color, self.top_rect,border_radius = 12)
screen.blit(self.text_surf, self.text_rect)
self.check_click()
def check_click(self):
mouse_pos = pygame.mouse.get_pos()
if self.top_rect.collidepoint(mouse_pos):
self.top_color = '#D74B4B'
if pygame.mouse.get_pressed()[0]:
self.dynamic_elecation = 0
self.pressed = True
self.change_text(f"{self.text}")
else:
self.dynamic_elecation = self.elevation
if self.pressed == True:
print('clicked')
self.pressed = False
self.change_text(self.text)
self.click_function(board, player_board)
else:
self.dynamic_elecation = self.elevation
self.top_color = '#475F77'
# === FUNCTIONS ===
# Creates the board surface
def create_board_surf(board):
board_surf = pygame.Surface((TILESIZE*15, TILESIZE*15))
for y in range(15):
for x in range(15):
rect = pygame.Rect(x*TILESIZE+TILE_MARGIN, y*TILESIZE+TILE_MARGIN, TILESIZE-TILE_MARGIN*2, TILESIZE-TILE_MARGIN*2)
(tile_type, tile) = board[y][x]
if (tile_type == 'X3Word'):
pygame.draw.rect(board_surf, X3_WORD_COLOR, rect)
continue
if (tile_type == 'X2Word'):
pygame.draw.rect(board_surf, X2_WORD_COLOR, rect)
continue
if (tile_type == 'X2Letter'):
pygame.draw.rect(board_surf, X2_LETTER_COLOR, rect)
continue
if (tile_type == 'X3Letter'):
pygame.draw.rect(board_surf, X3_LETTER_COLOR, rect)
continue
if (tile_type == 'Start'):
pygame.draw.rect(board_surf, START_COLOR, rect)
continue
pygame.draw.rect(board_surf, MAIN_THEME, rect)
return board_surf
# Creates an empty board with the right tile multipliers in place
def create_board():
board = []
for y in range(15):
board.append([])
for x in range(15):
board[y].append(('Normal', None))
for (x, y) in X2_WORD_SPOTS:
board[y][x] = (('X2Word', None))
for (x, y) in X3_WORD_SPOTS:
board[y][x] = (('X3Word', None))
for (x, y) in X2_LETTER_SPOTS:
board[y][x] = (('X2Letter', None))
for (x, y) in X3_LETTER_SPOTS:
board[y][x] = (('X3Letter', None))
(x, y) = START_TILE
board[y][x] = (('Start', None))
return board
# Checks if a spot on the board is free
def is_free_board_spot(board, x, y):
(tile_tipe, letter) = board[y][x]
return letter == None
def get_free_player_board_spot(player_board, x):
for i in range(x, 8):
if player_board[i] == None:
return i
for i in range(x-1, -1, -1):
if player_board[i] == None:
return i
# places selected piece to x position and fixes the other pieces
def place_selected_piece_fix_player_board(player_board, x, selected_x, selected_piece, direction):
position_to_move = x
piece_to_move = player_board[position_to_move]
player_board[position_to_move] = selected_piece
while piece_to_move is not None:
position_to_move += direction
next_piece = player_board[position_to_move]
player_board[position_to_move] = piece_to_move
piece_to_move = next_piece
# Clears a tile (either on the board or the player board)
def clear_tile(board, player_board, board_type, x, y):
if board_type is not None:
if board_type == 'Player Board':
letter = player_board[x]
player_board[x] = None
if board_type == 'Board':
(mult, letter) = board[y][x]
board[y][x] = (mult, None)
return letter
# Puts the tile on the board
def put_tile_on_board(board, x, y, tile):
(mult, crt_tile) = board[y][x]
board[y][x] = (mult, tile)
# Returns the tile under the mouse (either on the main board or on the player board)
def get_tile_under_mouse(board, player_board):
mouse_pos_on_board = pygame.Vector2(pygame.mouse.get_pos()) - BOARD_POS
x, y = [int(v // TILESIZE) for v in mouse_pos_on_board]
if x >= 0 and y >= 0 and x < 15 and y < 15: return ('Board', board[y][x][1], x, y)
mouse_pos_on_player_board = pygame.Vector2(pygame.mouse.get_pos()) - PLAYER_BOARD_POS
x, y = [int(v // TILESIZE) for v in mouse_pos_on_player_board]
if x >= 0 and y == 0 and x < 8: return ('Player Board', player_board[x], x, y)
return None, None, None, None
# draws the selected tile at the mouse position
def draw_drag_tile(screen, board, player_board, selected_tile, font):
if selected_tile and selected_tile[0]:
selected_piece = selected_tile[1]
# board_type, piece, x, y = get_tile_under_mouse(board, player_board)
# if x != None:
# rect = (BOARD_POS[0] + x * TILESIZE, BOARD_POS[1] + y * TILESIZE, TILESIZE, TILESIZE)
# pygame.draw.rect(screen, (0, 255, 0, 50), rect, 2)
pos = pygame.Vector2(pygame.mouse.get_pos()) # Mouse position
letter_render = font.render(selected_piece, True, BLACK) # Letter
letter_rect = pygame.Rect(0, 0, TILESIZE-TILE_MARGIN*2, TILESIZE-TILE_MARGIN*2)
letter_rect.center = pos
pygame.draw.rect(screen, WHITE, letter_rect)
screen.blit(letter_render, letter_render.get_rect(center=letter_rect.center))
# Creates a surface for an empty player board with 8 tiles
def create_player_board_surf():
player_pieces_surf = pygame.Surface((TILESIZE*8, TILESIZE))
for x in range(8):
rect = pygame.Rect(x*TILESIZE, 0, TILESIZE, TILESIZE)
pygame.draw.rect(player_pieces_surf, MAIN_THEME, rect)
return player_pieces_surf
# Creates the initial pieces for a player
def create_player_pieces(shuffled_letters: list):
player_pieces = [None] * 8
for x in range(7):
player_pieces[x] = shuffled_letters.pop()
return player_pieces
# Shuffle letters in random order and return a list from which to take letters
def shuffle_letters():
letters = []
for letter in LETTER_COUNT.keys():
for i in range(LETTER_COUNT[letter]):
letters.append(letter)
random.shuffle(letters)
# print ('Shuffled letters are:', letters)
return letters
# Get a list of the newly placed letter positions
def get_placed_letters_positions():
placed_letters_positions = []
for y in range(15):
for x in range(15):
(tile_tipe, tile) = board[y][x]
if tile is not None and tile_tipe != 'Fixed':
placed_letters_positions.append((x, y))
print (f'Placed letters positions are {placed_letters_positions}')
return placed_letters_positions
def has_adjacent_vertical_letters(board, position):
x, y = position
tile_type, tile = board[y][x]
for (adjacent_x, adjacent_y) in [(x, y-1), (x, y+1)]:
tile_type, tile = board[adjacent_y][adjacent_x]
if tile is not None:
return True
return False
def has_adjacent_horizontal_letters(board, position):
x, y = position
tile_type, tile = board[y][x]
for (adjacent_x, adjacent_y) in [(x-1, y), (x+1, y)]:
tile_type, tile = board[adjacent_y][adjacent_x]
if tile is not None:
return True
return False
# Return true if position is next to a fixed letter or position is a start tile
def has_adjacent_fixed_letters_or_is_start(board, position):
x, y = position
tile_type, tile = board[y][x]
if tile_type == 'Start':
print ('Start position')
return True
for (adjacent_x, adjacent_y) in [(x-1, y), (x+1, y), (x, y-1), (x, y+1)]:
tile_type, tile = board[adjacent_y][adjacent_x]
if tile_type == 'Fixed':
return True
return False
def has_no_empty_spots(board, direction, stable_coordinate, min, max):
if direction == 'Vertical':
print(f'Checking for empty spots x={stable_coordinate}, y=[{min},{max}]')
# X is stable, check Y
for y in range(min, max+1):
tile_type, tile = board[y][stable_coordinate]
if tile is None:
return False
return True
if direction == 'Horizontal':
# Y is stable, check X
print(f'Checking for empty spots y={stable_coordinate}, x=[{min},{max}]')
for x in range(min, max+1):
tile_type, tile = board[stable_coordinate][x]
if tile is None:
return False
return True
# Check if positions are in a row / column
def check_placed_letters_positions(board, placed_letters_positions):
flag = False
(min_x, min_y) = (max_x, max_y) = placed_letters_positions[0]
for (x, y) in placed_letters_positions:
min_x = min(x, min_x)
min_y = min(y, min_y)
max_x = max(x, max_x)
max_y = max(y, max_y)
flag = flag or has_adjacent_fixed_letters_or_is_start(board, (x, y))
if flag == False:
return False, None
print(f'x=[{min_x},{max_x}] y=[{min_y}, {max_y}]')
if min_x == max_x:
return has_no_empty_spots(board, 'Vertical', min_x, min_y, max_y), 'Vertical'
if min_y == max_y:
return has_no_empty_spots(board, 'Horizontal', min_y, min_x, max_x), 'Horizontal'
return False, None
def get_horizontal_word(board, pos):
x, y = pos
score = 0
word = ''
word_multiplier = 1
while board[y][x][1] is not None:
tile_type, tile = board[y][x]
letter_score = LETTER_SCORE[tile]
if tile_type == 'X2Letter':
letter_score *= 2
if tile_type == 'X3Letter':
letter_score *= 3
if (tile_type == 'X2Word'):
word_multiplier *= 2
if (tile_type == 'X2Word'):
word_multiplier *= 3
word = tile + word
score += letter_score
x -= 1
x, y = pos
x += 1
while board[y][x][1] is not None:
tile_type, tile = board[y][x]
letter_score = LETTER_SCORE[tile]
if tile_type == 'X2Letter':
letter_score *= 2
if tile_type == 'X3Letter':
letter_score *= 3
if (tile_type == 'X2Word'):
word_multiplier *= 2
if (tile_type == 'X2Word'):
word_multiplier *= 3
word = word + tile
score += letter_score
x += 1
score *= word_multiplier
return (word, score)
def get_vertical_word(board, pos):
x, y = pos
score = 0
word = ''
word_multiplier = 1
while board[y][x][1] is not None:
tile_type, tile = board[y][x]
letter_score = LETTER_SCORE[tile]
if tile_type == 'X2Letter':
letter_score *= 2
if tile_type == 'X3Letter':
letter_score *= 3
if (tile_type == 'X2Word'):
word_multiplier *= 2
if (tile_type == 'X2Word'):
word_multiplier *= 3
word = tile + word
score += letter_score
y -= 1
x, y = pos
y += 1
while board[y][x][1] is not None:
tile_type, tile = board[y][x]
letter_score = LETTER_SCORE[tile]
if tile_type == 'X2Letter':
letter_score *= 2
if tile_type == 'X3Letter':
letter_score *= 3
if (tile_type == 'X2Word'):
word_multiplier *= 2
if (tile_type == 'X2Word'):
word_multiplier *= 3
word = word + tile
score += letter_score
y += 1
score *= word_multiplier
return (word, score)
def get_newly_created_words(board, placed_letters_positions, direction):
words = []
total_score = 0
first_pos = placed_letters_positions[0]
if direction == 'Horizontal':
main_word, main_score = get_horizontal_word(board, first_pos)
words.append(main_word)
total_score += main_score
for pos in placed_letters_positions:
if has_adjacent_vertical_letters(board, pos):
word, score = get_vertical_word(board, pos)
words.append(word)
total_score += score
if direction == 'Vertical':
main_word, main_score = get_vertical_word(board, first_pos)
words.append(main_word)
total_score += main_score
for pos in placed_letters_positions:
if has_adjacent_horizontal_letters(board, pos):
word, score = get_horizontal_word(board, pos)
words.append(word)
total_score += score
print(f'Words {words} => {total_score}')
return words, total_score
def return_letters(player_board, board, placed_letters_positions):
player_board_x = 0
while len(placed_letters_positions) > 0:
if player_board[player_board_x] is None:
x, y = placed_letters_positions.pop()
tile_type, tile = board[y][x]
board[y][x] = (tile_type, None)
player_board[player_board_x] = tile
player_board_x += 1
def get_score(placed_letters_positions):
# TODO: implement this:
return 0
def get_new_letters(player_board, number_of_letters):
x = 0
while number_of_letters > 0 and len(shuffled_letters) > 0 and x < 8:
if player_board[x] == None:
player_board[x] = shuffled_letters.pop()
number_of_letters -= 1
x += 1
def mark_letters_as_fixed(board, placed_letters_positions):
for (x, y) in placed_letters_positions:
tile_type, tile = board[y][x]
board[y][x] = ('Fixed', tile)
def click_submit_button(board, player_board):
global turn_count
placed_letters_positions = get_placed_letters_positions()
if placed_letters_positions == []:
return
is_in_line, direction = check_placed_letters_positions(board, placed_letters_positions)
if is_in_line == False:
print('Letters can not be placed like this!')
return_letters(player_board, board, placed_letters_positions)
return
new_words, score = get_newly_created_words(board, placed_letters_positions, direction)
if word_checker.check_words(new_words) == False:
score = 0
# return letters to player
return_letters(player_board, board, placed_letters_positions)
return
else:
number_of_placed_letters = len(placed_letters_positions)
get_new_letters(player_board, number_of_placed_letters)
mark_letters_as_fixed(board, placed_letters_positions)
# Add score to player
player_number = turn_count % len(PLAYERS)
player_score[player_number] += score
# Increment turn count
turn_count += 1
def draw_player_pieces(screen, player_pieces, font, selected_tile):
if selected_tile:
selected_board_type = selected_tile[0]
selected_x = selected_tile[2]
for x in range(8):
letter = player_pieces[x]
if letter is not None:
# print (f'Position {x}: letter: {letter}')
if selected_tile and selected_board_type == 'Player Board' and x == selected_x:
continue
letter_render = font.render(letter, True, BLACK) # Letter
letter_rect = pygame.Rect(PLAYER_BOARD_POS[0] + x*TILESIZE+TILE_MARGIN, PLAYER_BOARD_POS[1]+TILE_MARGIN, TILESIZE-TILE_MARGIN*2, TILESIZE-TILE_MARGIN*2)
pygame.draw.rect(screen, WHITE, letter_rect)
screen.blit(letter_render, letter_render.get_rect(center=letter_rect.center))
def draw_board_pieces(screen, board, font, selected_tile):
if selected_tile:
selected_board_type = selected_tile[0]
selected_x = selected_tile[2]
selected_y = selected_tile[3]
for y in range(15):
for x in range(15):
letter = board[y][x][1]
if letter is not None:
# print (f'Position {x}, {y}: letter: {letter}')
if selected_tile and selected_board_type == 'Board' and x == selected_x and y == selected_y:
continue
letter_render = font.render(letter, True, BLACK) # Letter
letter_rect = pygame.Rect(BOARD_POS[0] + x*TILESIZE+TILE_MARGIN, BOARD_POS[1] + y*TILESIZE+TILE_MARGIN, TILESIZE-TILE_MARGIN*2, TILESIZE-TILE_MARGIN*2)
pygame.draw.rect(screen, WHITE, letter_rect)
screen.blit(letter_render, letter_render.get_rect(center=letter_rect.center))
def draw_buttons():
for b in buttons:
b.draw()
def draw_score_board(font):
column_width = 20
x, y = SCORE_BOARD_POS
for i in range(len(PLAYERS)):
score_table = font.render(("{} {}".format(PLAYERS[i].ljust(column_width), str(player_score[i]).ljust(column_width))), True, BLACK)
screen.blit(score_table, (x, y))
x += column_width
# === MAIN ===
# --- (global) variables ---
turn_count = 0
player_score = [0] * len(PLAYERS)
word_checker = WordChecker('resources/dictionary/loc-flexiuni-6.0.txt')
# --- init ---
pygame.init()
letter_font = pygame.font.SysFont('', 24)
bonus_font = pygame.font.SysFont('', 20)
button_font = pygame.font.SysFont('', 100)
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
screen_rect = screen.get_rect()
board = create_board()
board_surf = create_board_surf(board)
shuffled_letters = shuffle_letters()
player_board_surf = create_player_board_surf()
player_board = create_player_pieces(shuffled_letters)
submit_button = Button('Submit', 80, 20, SUBMIT_BUTTON_POS, 1, letter_font, click_submit_button)
# --- objects ---
selected = None
# --- mainloop ---
clock = pygame.time.Clock()
is_running = True
selected_tile = None
while is_running:
tile = get_tile_under_mouse(board, player_board)
board_type, piece, x, y = tile
# --- events ---
for event in pygame.event.get():
# --- global events ---
if event.type == pygame.QUIT:
is_running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
is_running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
if piece is not None:
if (board_type == 'Board' and board[y][x][0] != 'Fixed') or board_type == 'Player Board':
selected_tile = tile
elif event.type == pygame.MOUSEBUTTONUP:
if event.button == 1:
if selected_tile is not None:
(selected_tile_board, selected_piece, selected_x, selected_y) = selected_tile
if board_type == 'Player Board':
if selected_tile_board == 'Player Board':
# Move from player board to player board
print ('Move piece to player board')
if piece is None:
# Free space => Just put it there
player_board[selected_x] = None
player_board[x] = selected_piece
else:
# Put the selected piece on position x and shift the other pieces to the left/right to make space
if selected_x < x:
direction = -1
else:
direction = 1
player_board[selected_x] = None
place_selected_piece_fix_player_board(player_board, x, selected_x, selected_piece, direction)
if selected_tile_board == 'Board':
# Move from board to player board
clear_tile(board, player_board, selected_tile_board, selected_x, selected_y)
if piece is None:
# Free space => Just put it there
player_board[x] = selected_piece
else:
free_x = get_free_player_board_spot(player_board, x)
if free_x < x:
direction = -1
else:
direction = 1
place_selected_piece_fix_player_board(player_board, x, selected_x, selected_piece, direction)
if board_type == 'Board':
# Move to board
print ('Move piece to board')
if is_free_board_spot(board, x, y):
print("Move to free spot")
clear_tile(board, player_board, selected_tile_board, selected_x, selected_y)
put_tile_on_board(board, x, y, selected_piece)
selected_tile = None
# --- updates ---
# --- draws ---
screen.fill(WHITE)
screen.blit(board_surf, BOARD_POS)
screen.blit(player_board_surf, PLAYER_BOARD_POS)
# draw pieces
draw_player_pieces(screen, player_board, letter_font, selected_tile)
draw_board_pieces(screen, board, letter_font, selected_tile)
# draw the tile as it is dragged
draw_drag_tile(screen, board, player_board, selected_tile, letter_font)
# draw buttons
draw_buttons()
# draw score board
draw_score_board(letter_font)
pygame.display.update()
# --- FPS ---
clock.tick(60)
# --- the end ---
pygame.quit()