solitaire_11.py Diff

solitaire_11.py
--- /home/docs/checkouts/readthedocs.org/user_builds/arcade-library/checkouts/latest/doc/tutorials/card_game/solitaire_10.py
+++ /home/docs/checkouts/readthedocs.org/user_builds/arcade-library/checkouts/latest/doc/tutorials/card_game/solitaire_11.py
@@ -1,6 +1,8 @@
 """
 Solitaire clone.
 """
+from typing import Optional
+
 import random
 import arcade
 
@@ -106,7 +108,7 @@
         super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
 
         # Sprite list with all the cards, no matter what pile they are in.
-        self.card_list = None
+        self.card_list: Optional[arcade.SpriteList] = None
 
         arcade.set_background_color(arcade.color.AMAZON)
 
@@ -219,6 +221,12 @@
         self.card_list.remove(card)
         self.card_list.append(card)
 
+    def on_key_press(self, symbol: int, modifiers: int):
+        """ User presses key """
+        if symbol == arcade.key.R:
+            # Restart
+            self.setup()
+
     def on_mouse_press(self, x, y, button, key_modifiers):
         """ Called when the user presses a mouse button. """
 
@@ -235,7 +243,27 @@
             # Figure out what pile the card is in
             pile_index = self.get_pile_for_card(primary_card)
 
-            if primary_card.is_face_down:
+            # Are we clicking on the bottom deck, to flip three cards?
+            if pile_index == BOTTOM_FACE_DOWN_PILE:
+                # Flip three cards
+                for i in range(3):
+                    # If we ran out of cards, stop
+                    if len(self.piles[BOTTOM_FACE_DOWN_PILE]) == 0:
+                        break
+                    # Get top card
+                    card = self.piles[BOTTOM_FACE_DOWN_PILE][-1]
+                    # Flip face up
+                    card.face_up()
+                    # Move card position to bottom-right face up pile
+                    card.position = self.pile_mat_list[BOTTOM_FACE_UP_PILE].position
+                    # Remove card from face down pile
+                    self.piles[BOTTOM_FACE_DOWN_PILE].remove(card)
+                    # Move card to face up list
+                    self.piles[BOTTOM_FACE_UP_PILE].append(card)
+                    # Put on top draw-order wise
+                    self.pull_to_top(card)
+
+            elif primary_card.is_face_down:
                 # Is the card face down? In one of those middle 7 piles? Then flip up
                 primary_card.face_up()
             else:
@@ -253,6 +281,25 @@
                     self.held_cards.append(card)
                     self.held_cards_original_position.append(card.position)
                     self.pull_to_top(card)
+
+        else:
+
+            # Click on a mat instead of a card?
+            mats = arcade.get_sprites_at_point((x, y), self.pile_mat_list)
+
+            if len(mats) > 0:
+                mat = mats[0]
+                mat_index = self.pile_mat_list.index(mat)
+
+                # Is it our turned over flip mat? and no cards on it?
+                if mat_index == BOTTOM_FACE_DOWN_PILE and len(self.piles[BOTTOM_FACE_DOWN_PILE]) == 0:
+                    # Flip the deck back over so we can restart
+                    temp_list = self.piles[BOTTOM_FACE_UP_PILE].copy()
+                    for card in reversed(temp_list):
+                        card.face_down()
+                        self.piles[BOTTOM_FACE_UP_PILE].remove(card)
+                        self.piles[BOTTOM_FACE_DOWN_PILE].append(card)
+                        card.position = self.pile_mat_list[BOTTOM_FACE_DOWN_PILE].position
 
     def remove_card_from_pile(self, card):
         """ Remove card from whatever pile it was in. """