solitaire_06.py Diff#

solitaire_06.py#
--- /home/docs/checkouts/readthedocs.org/user_builds/arcade-library/checkouts/stable/doc/tutorials/card_game/solitaire_05.py
+++ /home/docs/checkouts/readthedocs.org/user_builds/arcade-library/checkouts/stable/doc/tutorials/card_game/solitaire_06.py
@@ -1,6 +1,7 @@
 """
 Solitaire clone.
 """
+import random
 import arcade
 
 # Screen title and size
@@ -118,6 +119,8 @@
             pile = arcade.SpriteSolidColor(MAT_WIDTH, MAT_HEIGHT, arcade.csscolor.DARK_OLIVE_GREEN)
             pile.position = START_X + i * X_SPACING, TOP_Y
             self.pile_mat_list.append(pile)
+
+        # --- Create, shuffle, and deal the cards
 
         # Sprite list with all the cards, no matter what pile they are in.
         self.card_list = arcade.SpriteList()
@@ -129,6 +132,11 @@
                 card.position = START_X, BOTTOM_Y
                 self.card_list.append(card)
 
+        # Shuffle the cards
+        for pos1 in range(len(self.card_list)):
+            pos2 = random.randrange(len(self.card_list))
+            self.card_list.swap(pos1, pos2)
+
     def on_draw(self):
         """ Render the screen. """
         # Clear the screen
@@ -140,15 +148,12 @@
         # Draw the cards
         self.card_list.draw()
 
-    def pull_to_top(self, card):
+    def pull_to_top(self, card: arcade.Sprite):
         """ Pull card to top of rendering order (last to render, looks on-top) """
-        # Find the index of the card
-        index = self.card_list.index(card)
-        # Loop and pull all the other cards down towards the zero end
-        for i in range(index, len(self.card_list) - 1):
-            self.card_list[i] = self.card_list[i + 1]
-        # Put this card at the right-side/top/size of list
-        self.card_list[len(self.card_list) - 1] = card
+
+        # Remove, and append to the end
+        self.card_list.remove(card)
+        self.card_list.append(card)
 
     def on_mouse_press(self, x, y, button, key_modifiers):
         """ Called when the user presses a mouse button. """
@@ -192,7 +197,6 @@
             # Success, don't reset position of cards
             reset_position = False
 
-            # Release on top play pile? And only one card held?
         if reset_position:
             # Where-ever we were dropped, it wasn't valid. Reset the each card's position
             # to its original spot.