@@ -131,6 +131,12 @@ def on_key_press(self, symbol: int, modifiers: int):
131
131
class WinningView (arcade .View ):
132
132
'''Winning Screen'''
133
133
134
+ def __init__ (self , time_taken : int = 0 , moves : int = 0 ):
135
+ '''Initialize the view'''
136
+ super ().__init__ ()
137
+ self .time_taken = time_taken
138
+ self .moves = moves
139
+
134
140
def on_show_view (self ):
135
141
'''Called when view is activated'''
136
142
# Reset the viewport, necessary if we have a scrolling game and we need
@@ -140,12 +146,29 @@ def on_show_view(self):
140
146
def on_draw (self ):
141
147
'''Draw the view'''
142
148
self .clear ()
149
+ # Draw the timer inside a gray rectangle
150
+ minutes = int (self .time_taken // 60 )
151
+ seconds = int (self .time_taken % 60 )
152
+ timer_text = f"{ minutes :02d} :{ seconds :02d} "
153
+
143
154
arcade .set_background_color (arcade .color .GRAY )
144
- arcade .draw_text ("You Won!" , self .window .width / 2 , self .window .height / 2 ,
145
- arcade .color .BLACK , font_size = 50 , anchor_x = "center" )
146
- arcade .draw_text ("Press R to restart" , self .window .width / 2 , self .window .height / 2 - 75 ,
147
- arcade .color .BLACK , font_size = 20 , anchor_x = "center" )
148
-
155
+ arcade .draw_text ("Somehow you finished the game..." , self .window .width / 2 , self .window .height / 2 ,
156
+ arcade .color .BLACK , font_size = 40 , anchor_x = "center" )
157
+ arcade .draw_text ("You have successfully wasted..." , self .window .width / 2 , self .window .height / 2 - 50 ,
158
+ arcade .color .BLACK , font_size = 30 , anchor_x = "center" )
159
+ # Show the time taken to win the game
160
+ arcade .draw_text ("Time: " , self .window .width / 2 , self .window .height / 2 - 100 ,
161
+ arcade .color .BLACK , font_size = 30 , anchor_x = "center" )
162
+ arcade .draw_text (timer_text , self .window .width / 2 + 140 , self .window .height / 2 - 100 ,
163
+ arcade .color .BLACK , font_size = 30 , anchor_x = "center" )
164
+ # Show the number of moves to win the game
165
+ arcade .draw_text ("Moves: " , self .window .width / 2 , self .window .height / 2 - 150 ,
166
+ arcade .color .BLACK , font_size = 30 , anchor_x = "center" )
167
+ arcade .draw_text (str (self .moves ), self .window .width / 2 + 140 , self .window .height / 2 - 150 ,
168
+ arcade .color .BLACK , font_size = 30 , anchor_x = "center" )
169
+ arcade .draw_text ("Note: Press R to restart" , self .window .width / 2 , self .window .height / 2 - 350 ,
170
+ arcade .color .LIGHT_GRAY , font_size = 20 , anchor_x = "center" )
171
+
149
172
def on_key_press (self , symbol : int , modifiers : int ):
150
173
""" If the user presses the mouse button, start the game. """
151
174
if symbol == arcade .key .R :
@@ -479,7 +502,7 @@ def on_mouse_release(self, x, y, button, key_modifiers):
479
502
pile , distance = arcade .get_closest_sprite (self .held_cards [0 ], self .pile_mat_list )
480
503
reset_position = True
481
504
482
- # Check if we are in contact with the closest mat
505
+ # Check if we are in contact with the closest mat or the cards in it
483
506
if arcade .check_for_collision (self .held_cards [0 ], pile ):
484
507
485
508
# Get the index of the pile we are over
@@ -552,6 +575,32 @@ def on_mouse_release(self, x, y, button, key_modifiers):
552
575
else :
553
576
# Reset position of cards
554
577
reset_position = True
578
+ else :
579
+ # Check if we are over a card pile
580
+ for pile_index in range (PILE_COUNT ):
581
+ if len (self .piles [pile_index ]) > 0 :
582
+ top_card = self .piles [pile_index ][- 1 ]
583
+ if arcade .check_for_collision (self .held_cards [0 ], top_card ):
584
+ if pile_index >= PLAY_PILE_1 and pile_index <= PLAY_PILE_7 :
585
+ if top_card .colour != self .held_cards [0 ].colour and \
586
+ CARD_VALUES .index (top_card .value ) - 1 == CARD_VALUES .index (self .held_cards [0 ].value ):
587
+ # Move cards to proper position
588
+ for i , dropped_card in enumerate (self .held_cards ):
589
+ dropped_card .position = top_card .center_x , \
590
+ top_card .center_y - CARD_VERTICAL_OFFSET * (i + 1 )
591
+ reset_position = False
592
+ self .moves += 1
593
+ for card in self .held_cards :
594
+ self .move_card_to_new_pile (card , pile_index )
595
+ break
596
+ elif pile_index >= TOP_PILE_1 and pile_index <= TOP_PILE_4 and len (self .held_cards ) == 1 :
597
+ if top_card .suit == self .held_cards [0 ].suit and \
598
+ CARD_VALUES .index (top_card .value ) + 1 == CARD_VALUES .index (self .held_cards [0 ].value ):
599
+ self .move_card_to_new_pile (self .held_cards [0 ], pile_index )
600
+ self .held_cards [0 ].position = top_card .position
601
+ reset_position = False
602
+ self .moves += 1
603
+ break
555
604
if reset_position :
556
605
# Reset position of the cards
557
606
for i , card in enumerate (self .held_cards ):
@@ -563,7 +612,7 @@ def on_mouse_release(self, x, y, button, key_modifiers):
563
612
# --- Win check
564
613
if self .check_winning ():
565
614
# Show the winning window
566
- view = WinningView ()
615
+ view = WinningView (self . elapsed_time , self . moves )
567
616
self .window .show_view (view )
568
617
569
618
def on_mouse_motion (self , x : float , y : float , dx : float , dy : float ):
@@ -586,6 +635,8 @@ def on_key_press(self, symbol: int, modifiers: int):
586
635
587
636
def check_winning (self ):
588
637
'''Check if the player has won the game'''
638
+ if len (self .piles [TOP_PILE_1 ]) > 0 :
639
+ return True
589
640
for pile in self .piles [TOP_PILE_1 :]:
590
641
if len (pile ) != 13 :
591
642
return False
0 commit comments