-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame_objects.py
More file actions
461 lines (396 loc) · 15.3 KB
/
Copy pathgame_objects.py
File metadata and controls
461 lines (396 loc) · 15.3 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
"""Game Objects for the Solitaire game"""
import pygame
import random
pygame.init()
my_font = pygame.font.Font(None, 36)
rank_icon_dict = {
1: "A",
11: "J",
12: "Q",
13: "K",
}
suit_icon_dict = {
1: "D",
2:"C",
3:"H",
4:"S"
}
class Card:
"""class for a card"""
def __init__(self, suit: int, rank: int, shown: bool = False):
self.suit = suit
self.rank = rank
self.shown = shown
self.card_width = 35
self.card_height = 50
self.selected = False
self.card_rect = pygame.Rect(10, 10, self.card_width, self.card_height)
self.set_icons()
if self.suit % 2 == 0:
self.color = "black"
else:
self.color = "red"
def draw(self, surface: pygame.Surface) -> None:
border_rect = pygame.rect.Rect(
self.card_rect.left,
self.card_rect.top,
self.card_rect.width,
self.card_rect.height,
)
border_rect.left -= 1
border_rect.top -= 1
border_rect.width += 2
border_rect.height += 2
if self.selected:
border_color = "yellow"
else:
border_color = "black"
pygame.draw.rect(surface, border_color, border_rect)
if self.shown:
pygame.draw.rect(surface, "white", self.card_rect)
text = my_font.render(str(self.rank_icon), True, self.color)
surface.blit(text, (self.card_rect.x, self.card_rect.y))
text = my_font.render(self.suit_icon, True, self.color)
surface.blit(text,(self.card_rect.centerx, self.card_rect.centery))
else:
pygame.draw.rect(surface, "red", self.card_rect)
def set_icons(self) -> None:
if self.rank in [1, 11, 12, 13]:
self.rank_icon = rank_icon_dict[self.rank]
else:
self.rank_icon = str(self.rank)
self.suit_icon = suit_icon_dict[self.suit]
class Column:
def __init__(self, x_location: int, y_location: int, surface: pygame.Surface):
self.x_location = x_location
self.y_location = y_location
self.surface = surface
self.cards: list[Card] = []
self.empty_rect = pygame.rect.Rect(x_location, y_location, 35, 50)
def draw_column(self) -> None:
self.refresh()
pygame.draw.rect(self.surface, "yellow", self.empty_rect)
for card in self.cards:
card.draw(self.surface)
def refresh(self) -> None:
y_offset = 0
for card in self.cards:
card.card_rect = pygame.Rect(
self.x_location,
(self.y_location + y_offset),
card.card_width,
card.card_height,
)
y_offset += 15
if self.cards:
if self.cards[-1].shown == False:
self.cards[-1].shown = True
class Cell:
def __init__(self, x_location, y_location, surface: pygame.Surface):
self.x_location = x_location
self.y_location = y_location
self.surface = surface
self.cards: list[Card] = []
self.cell_rect = pygame.rect.Rect(x_location, y_location, 35, 50)
def refresh(self):
for card in self.cards:
card.card_rect = pygame.Rect(
self.x_location,
self.y_location,
card.card_width,
card.card_height,
)
def draw_cell(self):
pygame.draw.rect(self.surface, "yellow", self.cell_rect)
self.draw_cards()
def draw_cards(self):
for card in self.cards:
card.draw(self.surface)
class Deck(Cell):
def get_cards(self):
if self.cards:
return self.cards.pop()
class DrawPile(Cell):
pass
class Foundation(Cell):
def __init__(self, suit, x_location, y_location, surface):
super().__init__(x_location, y_location, surface)
self.suit = suit
class Board:
def __init__(self, surface: pygame.Surface):
self.surface: pygame.Surface = surface
self.deck: list[Card] = []
self.columns: list[Column] = []
self.top_row: list[Cell | Deck] = []
self.cells = []
self.create_deck()
self.setup_columns()
self.create_top_row()
def new_game(self):
self.deck = []
self.columns = []
self.top_row = []
self.cells = []
self.create_deck()
self.setup_columns()
self.create_top_row()
def refresh_all(self):
for cell in self.cells:
cell.refresh()
for column in self.columns:
column.refresh()
def create_deck(self) -> None:
suits = [1, 2, 3, 4]
for suit in suits:
for rank in range(1, 14):
self.deck.append(Card(rank=rank, suit=suit))
random.shuffle(self.deck)
def create_top_row(self) -> None:
self.cells.append(Deck(self.border_size + (self.column_space * 1), 10, self.surface))
self.cells.append(DrawPile(self.border_size + (self.column_space * 2), 10, self.surface))
for i in range(1,5):
self.cells.append(Foundation(i, self.border_size + (self.column_space * (i + 3)), 10, self.surface))
self.cells[0].cards = self.deck
self.cells[0].refresh()
def setup_columns(self) -> None:
board_width = self.surface.get_width()
board_height = self.surface.get_height()
y_value = board_height/6
self.border_size = board_width / 4
columns_area = board_width - (self.border_size * 2)
self.column_space = columns_area / 8
for i in range(1, 8):
self.columns.append(
Column(int(self.border_size + (self.column_space * i)), y_value, self.surface)
)
count = 1
for column in self.columns:
cards = self.deck[-count:]
del self.deck[-count:]
column.cards += cards
count += 1
column.refresh()
def draw_board(self) -> None:
self.surface.fill("green")
for column in self.columns:
column.draw_column()
for cell in self.cells:
cell.draw_cell()
def check_win(self) -> None:
cards = 0
for cell in self.cells:
if isinstance(cell, Foundation):
cards += len(cell.cards)
if cards == 52:
return True
else:
return False
class Cursor:
def __init__(self, board: Board) -> None:
self.board = board
self.current_column = 0
self.current_upper_column = 0
self.cursor_rect = pygame.rect.Rect(1, 1, 1, 1)
self.selection: Card | None = None
self.selection_column: int | None = None
self.selection_column_upper = None
self.cursor_height: int = -1
self.upper = False
self.update_column()
def draw_cursor(self) -> None:
pygame.draw.rect(self.board.surface, "black", self.cursor_rect)
def update_column(self) -> None:
if self.upper:
if self.board.cells[self.current_upper_column].cards:
self.cursor_rect = pygame.rect.Rect(
self.board.cells[self.current_upper_column].cards[-1].card_rect.left,
self.board.cells[self.current_upper_column].cards[-1].card_rect.top,
5,
5,
)
else:
self.cursor_rect = pygame.rect.Rect(
self.board.cells[self.current_upper_column].cell_rect.left,
self.board.cells[self.current_upper_column].cell_rect.top,
5,
5,
)
elif self.board.columns[self.current_column].cards:
self.cursor_rect = pygame.rect.Rect(
self.board.columns[self.current_column].cards[self.cursor_height].card_rect.left,
self.board.columns[self.current_column].cards[self.cursor_height].card_rect.top,
5,
5,
)
else:
self.cursor_rect = pygame.rect.Rect(
self.board.columns[self.current_column].empty_rect.left,
self.board.columns[self.current_column].empty_rect.top,
5,
5,
)
def move_right(self) -> None:
if self.upper:
self.current_upper_column += 1
self.cursor_height = -1
if self.current_upper_column > len(self.board.cells) -1:
self.current_upper_column = 0
else:
self.current_column += 1
self.cursor_height = -1
if self.current_column > len(self.board.columns) - 1:
self.current_column = 0
self.update_column()
def move_left(self) -> None:
if self.upper:
self.current_upper_column -= 1
self.cursor_height = -1
if self.current_upper_column < 0:
self.current_upper_column = len(self.board.cells) - 1
else:
self.current_column -= 1
self.cursor_height = -1
if self.current_column < 0:
self.current_column = len(self.board.columns) - 1
self.update_column()
def move_up(self) -> None:
if abs(self.cursor_height - 1) <= len(self.board.columns[self.current_column].cards) \
and self.board.columns[self.current_column].cards[self.cursor_height -1].shown == True:
self.cursor_height -= 1
elif not self.upper:
if self.selection:
if len(self.selection) > 1:
return
self.upper = True
elif self.upper:
self.upper = False
self.update_column()
def move_down(self) -> None:
if (self.cursor_height + 1) <= -1:
self.cursor_height += 1
elif not self.upper:
if self.selection:
if len(self.selection) > 1:
return
self.upper = True
elif self.upper:
self.upper = False
self.update_column()
def interact(self) -> None:
def move_cards(self):
if self.selection_column is not None:
column = self.board.columns[self.selection_column]
else:
column = self.board.cells[self.selection_column_upper]
for i in range(len(self.selection)):
column.cards.pop()
if self.upper:
self.board.cells[self.current_upper_column].cards += self.selection
else:
self.board.columns[self.current_column].cards += self.selection
for i in self.selection:
i.selected=False
self.selection = None
if self.upper:
self.board.cells[self.current_upper_column].refresh()
else:
self.board.columns[self.current_column].refresh()
self.update_column()
self.selection_column = None
self.selection_column_upper = None
if self.upper:
cell = self.board.cells[self.current_upper_column]
#Interacting with Deck
if isinstance(cell, Deck):
if cell.cards:
flipped_card = cell.get_cards()
flipped_card.shown = True
self.board.cells[1].cards.append(flipped_card)
else:
if self.board.cells[1].cards:
for i in self.board.cells[1].cards:
i.shown = False
cell.cards += self.board.cells[1].cards
cell.cards.reverse()
self.board.cells[1].cards = []
if self.selection:
for i in self.selection:
i.selected=False
self.selection = None
self.selection_column = None
self.selection_column_upper = None
cell.refresh()
self.board.cells[1].refresh()
return
#Interacting with DrawPile
if isinstance(cell, DrawPile):
if self.selection:
for i in self.selection:
i.selected=False
self.selection = None
self.selection_column = None
self.selection_column_upper = None
if cell.cards:
self.selection = cell.cards[-1:]
self.selection_column_upper = self.current_upper_column
for i in self.selection:
i.selected = True
elif not self.selection and cell.cards:
if isinstance(cell, Foundation):
self.selection = cell.cards[-1:]
self.selection_column_upper = self.current_upper_column
for i in self.selection:
i.selected = True
elif self.selection:
if self.validate_move(self.selection, self.board.cells[self.current_upper_column]):
move_cards(self)
elif not self.selection and self.board.columns[self.current_column].cards:
self.selection = self.board.columns[self.current_column].cards[self.cursor_height:]
self.selection_column = self.current_column
for i in self.selection:
i.selected = True
elif self.selection and (self.selection_column is not None or self.selection_column_upper is not None):
move_spot = self.board.columns[self.current_column] if self.current_column is not None else self.board.cells[self.current_upper_column]
if self.validate_move(self.selection, move_spot):
move_cards(self)
def deselect(self):
for i in self.selection:
i.selected = False
if self.upper:
self.board.cells[self.current_upper_column].refresh()
else:
self.board.columns[self.current_column].refresh()
if self.selection_column:
self.board.columns[self.selection_column].refresh()
elif self.selection_column_upper:
self.board.cells[self.selection_column_upper].refresh()
self.selection = None
self.selection_column = None
self.selection_column_upper = None
def validate_move(self, selected: list[Card], target: Column | Cell):
if isinstance(target, Foundation):
if len(selected) > 1:
return False
elif not target.cards:
if selected[0].rank == 1:
return True
else:
return False
elif (selected[0].rank - target.cards[-1].rank) == 1 and selected[0].suit == target.cards[-1].suit:
return True
else:
return False
elif isinstance(target, Column):
if target.cards:
if target.cards[-1].rank - selected[0].rank == 1 \
and abs(target.cards[-1].suit % 2 - selected[0].suit % 2) == 1:
return True
else:
self.deselect()
return False
elif selected[0].rank == 13:
return True
else:
return False
else:
return False