-
Notifications
You must be signed in to change notification settings - Fork 0
/
set.py
697 lines (587 loc) · 23.6 KB
/
set.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
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
# Ken Schroeder
# Python Set Game
'''Next To-Dos
Save Game Feature
What types of sets there are (color, #, shade, symbol)
'''
import pygame
import math
from pygame.locals import *
import random
import time
from abc import ABCMeta, abstractmethod
import planes
import planes.gui
from class_utils import Button
from class_utils import ScreenText
####################
# DEFINE CONSTANTS #
####################
WINDOW_WIDTH = 1000
WINDOW_HEIGHT = 700
CARD_WIDTH = 200
CARD_HEIGHT = 100
top_margin = 50
left_margin = 50
space_horiz = ((3*WINDOW_WIDTH/4)-2*left_margin-3*CARD_WIDTH)/2
BLACK = (255, 105, 180)
GREEN = (0, 255, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
RED = (255,0,0)
MODE_HOME = 0
MODE_GAME = 1
HINT_DEDUC = 2000
WRONG_DEDUC = 3000
FONT_BIG = pygame.font.SysFont ("Arial", 40)
FONT_SMALL = pygame.font.SysFont ("Arial", 20)
colors = ['green', 'red', 'purple']
shapes = ['oval', 'diamond', 'squiggle']
numbers = [1,2,3]
shades = ['filled','shaded', 'empty']
'''
Given three cards, checks whether they form a Set
Args: card1, card2, card3 - objects of type Card
Returns: True if cards form a Set, False otherwise
'''
def check_set (card1, card2, card3):
color_check = all_same_or_all_diff (card1.color, card2.color, card3.color)
shape_check = all_same_or_all_diff (card1.shape, card2.shape, card3.shape)
num_check = all_same_or_all_diff (card1.number, card2.number, card3.number)
shade_check = all_same_or_all_diff (card1.shade, card2.shade, card3.shade)
return color_check and shape_check and num_check and shade_check
'''
Given three attributes (one from each of three cards), checks whether the
they are either all the same, or all different
Functions as a helper method to check_set
Args: attr1, attr2, attr3 - attributes of a card (color, shape, number, shade)
Returns: True if all three attributes are equal OR none of them are equal, False otherwise
'''
def all_same_or_all_diff (attr1, attr2, attr3):
if attr1 == attr2 and attr2 == attr3:
return True
elif (attr1 != attr2) and (attr2 != attr3) and (attr3 != attr1):
return True
else:
return False
'''
Helper function, takes a game time in seconds and formats it into a human-readable string
Returns string in format, for example: "1m 20s"
'''
def format_secs (secs):
minutes = secs / 60
seconds = secs % 60
return str (minutes) + "m " + str (seconds) + "s"
'''
a Card has attributes of color, shape, number, and shade
'''
class Card (planes.Plane):
def __init__ (self, name, color, shape, number, shade):
planes.Plane.__init__ (self, name, pygame.Rect (0,0,CARD_WIDTH,CARD_HEIGHT), False, False)
self.color = color
self.shape = shape
self.number = number
self.shade = shade
self.been_clicked = False
def __eq__ (self, other):
return self.color == other.color and \
self.shape == other.shape and \
self.number == other.number and \
self.shade == other.shade
def __ne__ (self, other):
return not self.__eq__(other)
def clicked (self, button_name):
self.been_clicked = not self.been_clicked
def update (self):
pass
##################
# BUTTON CLASSES #
##################
# GAME BUTTON
### When clicked, adds three new cards if no Set on the board
class AddThreeCardsButton (Button):
def __init__(self, name, rect, callback, model):
Button.__init__ (self, name, rect, callback, model)
self.image = pygame.image.load ("img/plus3_icon.png")
def clicked (self, button_name):
if self.model.check_in_play():
if not self.model.check_if_any_sets:
self.model.add_new_cards (3)
# GAME BUTTON
### When clicked, gives a hint
### If Set on the board: highlights the next card in the Set
### If no Set on board: adds three new cards
### Ignores cards already clicked, so user should not click when cards are highlighted
class HintButton (Button):
def __init__(self, name, rect, callback, model):
Button.__init__ (self, name, rect, callback, model)
self.image = pygame.image.load ("img/hint_icon.png")
def clicked (self, button_name):
if self.model.check_in_play():
self.model.hints_used += 1
for card1 in self.model.in_play_cards:
for card2 in self.model.in_play_cards:
for card3 in self.model.in_play_cards:
if card1 != card2 and card2 != card3 and card1 != card3:
if check_set (card1, card2, card3):
if not card1.been_clicked:
card1.been_clicked = True
return
elif not card2.been_clicked:
card2.been_clicked = True
return
else:
card3.been_clicked = True
return
self.model.add_new_cards (3)
# GAME BUTTON
### When clicked, pauses time in game
class PauseButton (Button):
def __init__(self, name, rect, callback, model):
Button.__init__ (self, name, rect, callback, model)
self.image = pygame.image.load ("img/pause_icon.png")
def clicked (self, button_name):
if not self.model.check_if_won():
if self.model.paused_time_at != 0: # game is already paused, act as play button
self.model.pause_time += pygame.time.get_ticks () - self.model.paused_time_at
self.model.paused_time_at = 0
else:
self.model.paused_time_at = pygame.time.get_ticks ()
# GAME BUTTON (pause screen)
### When clicked, return to Homescreen
class BackButton (Button):
def __init__(self, name, rect, callback, model):
Button.__init__ (self, name, rect, callback, model)
self.image = pygame.image.load ("img/back_icon.png")
def clicked (self, button_name):
self.model.model.game = None
self.model.model.mode = MODE_HOME
# GAME BUTTON (pause screen)
### When clicked, resume game time
class PlayButton (Button):
def __init__(self, name, rect, callback, model):
Button.__init__ (self, name, rect, callback, model)
self.image = pygame.image.load ("img/start_icon.png")
def clicked (self, button_name):
if self.model.check_if_won (): # if game over act as restart button
self.model.model.game = None
self.model.model.game = Game (self.model.model)
else:
self.model.pause_time += pygame.time.get_ticks () - self.model.paused_time_at
self.model.paused_time_at = 0
# GAME BUTTON (pause screen)
### Restarts the game by creating a new Game object
class RestartButton (Button):
def __init__(self, name, rect, callback, model):
Button.__init__ (self, name, rect, callback, model)
self.image = pygame.image.load ("img/restart_icon.png")
def clicked (self, button_name):
self.model.model.game = None
self.model.model.game = Game (self.model.model)
# HOME BUTTON
### Starts a new game by creating a new Game object
class StartButton (Button):
def __init__(self, name, rect, callback, model):
Button.__init__ (self, name, rect, callback, model)
self.image = pygame.image.load ("img/start_icon.png")
self.clickbox = False # all home screen buttons have a clickbox option
# which shows up as blakc box to indicate selection
def clicked (self, button_name):
self.model.mode = MODE_GAME
self.model.game = Game (self.model)
# HOME BUTTON
### When clicked, display game statistics
### Number of games, best time, average time
class StatsButton (Button):
def __init__(self, name, rect, callback, model):
Button.__init__ (self, name, rect, callback, model)
self.image = pygame.image.load ("img/stats_icon.png")
self.clickbox = False
def clicked (self, button_name):
if len (self.model.show_stats) > 0: # we are already showing stats, unshow
self.model.show_stats = []
else:
num_games = str (len (self.model.times))
best_time = "No Time Data Yet"
avg_time = "No Time Data Yet"
if len(self.model.times) > 0:
best_time = format_secs (min (self.model.times))
avg_time = format_secs (sum (self.model.times) / len (self.model.times))
message_box = planes.Plane ('message_box',
pygame.Rect (left_margin, top_margin, 13*WINDOW_WIDTH/16, (WINDOW_HEIGHT-300)))
message_box.image.fill ((0,0,0))
win_stats = "Game Stats \n" + "Number of Games: " + num_games + "\nBest time: " + best_time + "\nAverage time: " + avg_time
message_texts = []
lines = win_stats.split ("\n")
box_width = 13*WINDOW_WIDTH/16
for line in lines:
message_texts.append (ScreenText (line, line,
pygame.Rect(left_margin, top_margin + 60*(lines.index(line)+1) ,box_width, 45), FONT_BIG))
#message_text.background_color = (255,0,0) #fixthis not transparent
self.model.show_stats.append (message_box)
self.model.show_stats += message_texts
'''
A Game is a single game that ends when won or cancelled
'''
class Game ():
def __init__(self, model):
########################
# GAME SCREEN ELEMENTS #
########################
self.deck = []
self.model = model
self.pause_time = 0
self.paused_time_at = 0
self.start_time = pygame.time.get_ticks ()
self.end_time = 0 # time game ended at
#make 81 unique cards, add to deck
for color in colors:
for shape in shapes:
for number in numbers:
for shade in shades:
card_to_add = Card (color + shape + shade + str (number),
color, shape, number, shade)
self.deck.append (card_to_add)
card_to_add.image = pygame.image.load ("img/" + card_to_add.name + ".png")
self.actors = []
self.in_play_cards = []
self.clicked_cards = []
self.out_of_play_cards = []
self.sets_found = 0
self.sets_wrong = 0 # should we take off points for these?
self.hints_used = 0
# tells if we have already added the game time to the times []
# prevents from adding the time on every update loop
self.added_time = False
#### Elements of a game ####
self.sets_found_label = ScreenText ("sets_found_label",
"Sets: " + str (self.sets_found),
pygame.Rect (3*WINDOW_WIDTH/4, 290, WINDOW_WIDTH/4, 50),
FONT_BIG)
self.time_label = ScreenText ("time_label",
"Time: " + format_secs (self.start_time / 1000),
pygame.Rect (3*WINDOW_WIDTH/4, 220, WINDOW_WIDTH/4, 100),
FONT_BIG)
self.left_in_deck_label = ScreenText ("left_in_deck_label",
"Deck: " + str (len (self.deck) - (len (self.in_play_cards) + len (self.out_of_play_cards))),
pygame.Rect (3*WINDOW_WIDTH/4, 505, WINDOW_WIDTH/4, 25),
FONT_SMALL)
self.sets_on_table_label = ScreenText("sets_on_table_label","Sets On Table" + str(0),pygame.Rect(3*WINDOW_WIDTH/4,535,WINDOW_WIDTH/4, 25),FONT_SMALL)
self.add3_button = AddThreeCardsButton ("add_three_cards_button",
pygame.Rect (3*WINDOW_WIDTH/4 + (WINDOW_WIDTH/4 - 200)/2, 360, 100, 100),
AddThreeCardsButton.clicked,
self)
self.hint_button = HintButton ("hint_button",
pygame.Rect (3*WINDOW_WIDTH/4 + (WINDOW_WIDTH/4 - 200)/2 + 100, 360, 100, 100),
HintButton.clicked,
self)
self.pause_button = PauseButton ("pause_button",
pygame.Rect (3*WINDOW_WIDTH/4 + (WINDOW_WIDTH/4 - 200)/2 + 50, WINDOW_HEIGHT - 120, 100, 100),
PauseButton.clicked,
self)
self.hints_used_label = ScreenText ("hints_used_label",
"Hints Used: " + str (self.hints_used),
pygame.Rect (3*WINDOW_WIDTH/4, 475, WINDOW_WIDTH/4, 25),
FONT_SMALL)
self.logo = planes.Plane ("setlogo",
pygame.Rect (3*WINDOW_WIDTH/4, 50, 240, 162),
False, False)
self.logo.image = pygame.image.load ("img/set.jpg")
#### PAUSE SCREEN BUTTONS ####
message_width = 3*CARD_WIDTH + 2*space_horiz # width of playing field
self.play_button = PlayButton ("play_button",
pygame.Rect (2*message_width/5 - 50, WINDOW_HEIGHT - 300, 100, 100),
PlayButton.clicked,
self)
self.restart_button = RestartButton ("restart_button",
pygame.Rect (3*message_width/5 - 50, WINDOW_HEIGHT - 300, 100, 100),
RestartButton.clicked,
self)
self.back_button = BackButton ("back_button",
pygame.Rect (4*message_width/5 - 50, WINDOW_HEIGHT - 300, 100, 100),
BackButton.clicked,
self)
#### CATEGORIES ####
self.gamebuttons = [self.add3_button, self.hint_button, self.pause_button, self.logo]
self.gamelabels = [self.sets_found_label, self.time_label, self.hints_used_label, self.left_in_deck_label,self.sets_on_table_label]
self.pausebuttons = [self.play_button, self.restart_button, self.back_button]
# start the game
self.add_new_cards (12)
# Add cards to the in-play cards
# Number = number of cards to add
# Index allows adding 1 card in the same position as a removed card
# Does not check whether we SHOULD because assumes we have checked that before calling
def add_new_cards (self, number, index=0):
if not len (self.in_play_cards) + len (self.out_of_play_cards) == len (self.deck):
i = 0
while i < number:
num = random.randint (0,len (self.deck)-1)
card = self.deck[num]
if card not in self.in_play_cards and card not in self.out_of_play_cards:
self.in_play_cards.insert (index, card)
i += 1
# Checks if any sets on the board
def check_if_any_sets (self):
for card1 in self.in_play_cards:
for card2 in self.in_play_cards:
for card3 in self.in_play_cards:
if card1 != card2 and card2 != card3 and card1 != card3:
if check_set (card1, card2, card3):
return True
return False
# Checks if game is won
def check_if_won (self):
return (not self.check_if_any_sets ()) and \
(len (self.in_play_cards) + len (self.out_of_play_cards) == len (self.deck))
# Game can only be lost if playing in time mode
def check_in_play (self):
return not self.check_if_won() and self.paused_time_at == 0
# Number of sets on board
def numSets(self):
self.setsOnTable = 0
for first in range(0,len(self.in_play_cards)):
for second in range(first+1,len(self.in_play_cards)):
for third in range(second+1,len(self.in_play_cards)):
card1 = self.in_play_cards[first]
card2 = self.in_play_cards[second]
card3 = self.in_play_cards[third]
if check_set(card1,card2,card3):
self.setsOnTable += 1
# Called infinitely
def update (self):
self.numSets()
# if game not in play, display messages, not cards
if not self.check_in_play():
self.actors = []
self.actors += self.gamelabels + self.gamebuttons
self.hints_used_label.update_text ("Hints Remaining: " + str (self.hints_used))
self.left_in_deck_label.update_text ("Deck: " + str (len (self.deck) - (len (self.in_play_cards) + len (self.out_of_play_cards))))
self.sets_on_table_label.update_text("Sets On Table: " + str(self.setsOnTable))
message_box = planes.Plane ('message_box',
pygame.Rect (left_margin,
top_margin,
3*CARD_WIDTH + 2*space_horiz,
4*CARD_HEIGHT + 3*((WINDOW_HEIGHT - 4*CARD_HEIGHT - 2*top_margin) / 3)))
message_box.image.fill ((0,0,0))
message_texts = []
# if game won or lost, note time game ended
if self.check_if_won ():
if self.end_time == 0:
self.end_time = pygame.time.get_ticks ()
total_time = self.end_time - self.start_time - self.pause_time
if self.check_if_won () and not self.added_time:
self.model.add_time((total_time+(self.sets_wrong*WRONG_DEDUC))/ 1000)
self.added_time = True
best_time = ""
if len(self.model.times) == 0:
best_time = format_secs (total_time/ 1000)
else:
best_time = format_secs (min (self.model.times))
win_stats = "Game Complete! \n" + \
"Total time: " + format_secs ((self.end_time - self.start_time - self.pause_time)/ 1000) + "\n" +\
"Best time: " + best_time
win_stats_with_loss = "Game Complete! \n" + \
"Total time: " + format_secs (total_time/ 1000) + "\n" +\
"Incorrect Sets: " + str(self.sets_wrong) + "\n" +\
"Adjusted Time: " + format_secs ((total_time+(self.sets_wrong*WRONG_DEDUC))/ 1000) + "\n" +\
"Best time: " + best_time
stats = win_stats_with_loss
lines = stats.split ("\n")
box_width = 3*CARD_WIDTH + 2*space_horiz
for line in lines:
message_texts.append (ScreenText (line, line,
pygame.Rect(left_margin, top_margin + 50*(lines.index(line)+1) ,box_width, 45), FONT_BIG))
elif self.paused_time_at != 0: #game is paused
message_texts.append (ScreenText ("message_text", "Game Paused",
pygame.Rect (left_margin,
top_margin,
3*CARD_WIDTH + 2*space_horiz,
4*CARD_HEIGHT + 3*((WINDOW_HEIGHT - 4*CARD_HEIGHT - 2*top_margin) / 3)),
FONT_BIG))
self.actors.append (message_box)
self.actors += message_texts
self.actors += self.pausebuttons
# game in play
else:
#check which cards are clicked
self.clicked_cards = []
self.actors = []
for card in self.in_play_cards:
self.actors.append (card)
if card.been_clicked:
self.clicked_cards.append (card)
card.update ()
#add click boxes
for card in self.clicked_cards:
clicked_box = planes.Plane ("box" + card.name,
pygame.Rect (card.rect.x-5,
card.rect.y-5,
card.rect.width + 10,
card.rect.height + 10),
False, False)
clicked_box.image = pygame.image.load ("img/clickbox.png")
self.actors.insert (0, clicked_box)
#check for sets
if len (self.clicked_cards) == 3:
is_set = check_set (self.clicked_cards[0],
self.clicked_cards[1],
self.clicked_cards[2])
if is_set:
self.sets_found += 1
self.sets_found_label.update_text ("Sets: " + str (self.sets_found))
#remove cards and add new ones
for card in self.clicked_cards:
self.out_of_play_cards.append (card)
index = self.in_play_cards.index (card)
self.in_play_cards.remove (card)
if len (self.in_play_cards) < 12:
self.add_new_cards (1, index)
else:
self.sets_wrong += 1
for card in self.clicked_cards:
card.been_clicked = False
self.actors += self.gamelabels + self.gamebuttons
self.time_label.update_text ("Time: " + format_secs ((pygame.time.get_ticks () - self.start_time - self.pause_time)/ 1000))
self.hints_used_label.update_text ("Hints Used: " + str (self.hints_used))
self.left_in_deck_label.update_text ("Deck: " + str (len (self.deck) - (len (self.in_play_cards) + len (self.out_of_play_cards))))
self.sets_on_table_label.update_text("Sets On Table: " + str(self.setsOnTable))
'''
The Model is the overall object in controlling the entire program
It instantiates Game objects as needed but also contains home screen
'''
class Model:
def __init__ (self):
self.background = (20,20,20)
self.mode = MODE_HOME
self.game = None
self.actors = []
try:
times_file = open("times_file.txt","r")
except:
times_file = open("times_file.txt", "w+")
self.times = [int(score.strip()) for score in times_file.readlines()]
times_file.close()
self.show_stats = [] # a list of things for stats screen
########################
# HOME SCREEN ELEMENTS #
########################
self.title = planes.Plane("title", pygame.Rect (left_margin, top_margin, 13*WINDOW_WIDTH/16, (WINDOW_HEIGHT-300)))
self.start_button = StartButton ("start_button",
pygame.Rect (200,500,100,100),
StartButton.clicked,
self)
self.stats_button = StatsButton ("stats_button",
pygame.Rect (500,500,100,100),
StatsButton.clicked,
self)
self.homebuttons = [self.start_button,self.stats_button]
# Opens the times file and writes a new time score to the end
def add_time(self, time):
times_file = open("times_file.txt", "a")
times_file.write(str(time)+"\n")
self.times.append(time)
times_file.close()
# update model - either update homescreen or update game
def update (self):
if self.mode == MODE_HOME:
self.actors = [self.title] + self.homebuttons[:]
if self.show_stats != None:
self.actors += self.show_stats
clicked_button = None
#add click box
for button in self.homebuttons:
if button.clickbox:
clicked_button = button
if clicked_button != None:
clicked_box = planes.Plane ("box" + clicked_button.name,
pygame.Rect (clicked_button.rect.x-5,
clicked_button.rect.y-5,
clicked_button.rect.width + 10,
clicked_button.rect.height + 10),
False, False)
self.actors.insert (1, clicked_box)
else:
self.game.numSets()
self.game.update ()
self.actors = self.game.actors[:]
'''
Draw elements of Model actors onto screen
'''
class View:
def __init__ (self, model, screen):
self.model = model
self.screen = screen
def draw (self):
screen.remove_all ()
if isinstance (self.model.background, str):
self.screen.image = pygame.transform.scale (pygame.image.load (self.model.background),
(WINDOWWIDTH,WINDOWHEIGHT))
else:
self.screen.image.fill (self.model.background)
#put cards in play into a grid:
if model.game != None:
space_vert = 50
# space_vert changes so that cards adjust themselves if more than 12
# never more than 21, any collection of 20 cards must contain a Set
if len (self.model.game.in_play_cards) == 12:
space_vert = (WINDOW_HEIGHT - 4*CARD_HEIGHT - 2*top_margin) / 3
elif len (self.model.game.in_play_cards) == 15:
space_vert = (WINDOW_HEIGHT - 5*CARD_HEIGHT - 2*top_margin) / 4
elif len (self.model.game.in_play_cards) == 18:
space_vert = (WINDOW_HEIGHT - 6*CARD_HEIGHT - 2*top_margin) / 5
elif len (self.model.game.in_play_cards) == 21:
space_vert = (WINDOW_HEIGHT - 7*CARD_HEIGHT - 2*top_margin) / 6
# create positions of cards
positions =[(left_margin, top_margin),
(left_margin + CARD_WIDTH + space_horiz, top_margin),
(left_margin + 2*CARD_WIDTH + 2*space_horiz, top_margin),
(left_margin, top_margin + CARD_HEIGHT + space_vert),
(left_margin + CARD_WIDTH + space_horiz, top_margin + CARD_HEIGHT + space_vert),
(left_margin + 2*CARD_WIDTH + 2*space_horiz, top_margin + CARD_HEIGHT + space_vert),
(left_margin, top_margin + 2*CARD_HEIGHT + 2*space_vert),
(left_margin + CARD_WIDTH + space_horiz, top_margin + 2*CARD_HEIGHT + 2*space_vert),
(left_margin + 2*CARD_WIDTH + 2*space_horiz, top_margin + 2*CARD_HEIGHT + 2*space_vert),
(left_margin, top_margin + 3*CARD_HEIGHT + 3*space_vert),
(left_margin + CARD_WIDTH + space_horiz, top_margin + 3*CARD_HEIGHT + 3*space_vert),
(left_margin + 2*CARD_WIDTH + 2*space_horiz, top_margin + 3*CARD_HEIGHT + 3*space_vert),
(left_margin, top_margin + 4*CARD_HEIGHT + 4*space_vert),
(left_margin + CARD_WIDTH + space_horiz, top_margin + 4*CARD_HEIGHT + 4*space_vert),
(left_margin + 2*CARD_WIDTH + 2*space_horiz, top_margin + 4*CARD_HEIGHT + 4*space_vert),
(left_margin, top_margin + 5*CARD_HEIGHT + 5*space_vert),
(left_margin + CARD_WIDTH + space_horiz, top_margin + 5*CARD_HEIGHT + 5*space_vert),
(left_margin + 2*CARD_WIDTH + 2*space_horiz, top_margin + 5*CARD_HEIGHT + 5*space_vert),
(left_margin, top_margin + 6*CARD_HEIGHT + 6*space_vert),
(left_margin + CARD_WIDTH + space_horiz, top_margin + 6*CARD_HEIGHT + 6*space_vert),
(left_margin + 2*CARD_WIDTH + 2*space_horiz, top_margin + 6*CARD_HEIGHT + 6*space_vert) ]
# assign positions to cards in play
for i in range (len (self.model.game.in_play_cards)):
self.model.game.in_play_cards[i].rect.x = positions[i][0]
self.model.game.in_play_cards[i].rect.y = positions[i][1]
# add all actors to screen
for actor in self.model.actors:
self.screen.sub (actor)
# THE MAIN LOOP
if __name__ == "__main__":
pygame.init ()
size = (WINDOW_WIDTH, WINDOW_HEIGHT)
screen = planes.Display (size)
screen.grab = False
screen.image.fill (RED)
model = Model ()
view = View (model, screen)
running = True
while running:
events = pygame.event.get ()
for event in events:
if event.type == pygame.QUIT:
raise SystemExit
#pressed = pygame.key.get_pressed()
#if pressed[pygame.K_q]: pygame.quit()
screen.process (events)
model.update ()
screen.update ()
screen.render ()
view.draw ()
pygame.display.flip ()
time.sleep (.001)
pygame.quit ()