solitaire_03.py Diff

solitaire_03.py
--- /home/docs/checkouts/readthedocs.org/user_builds/arcade-library/checkouts/2.5.7/doc/tutorials/card_game/solitaire_02.py
+++ /home/docs/checkouts/readthedocs.org/user_builds/arcade-library/checkouts/2.5.7/doc/tutorials/card_game/solitaire_03.py
@@ -62,8 +62,22 @@
 
         arcade.set_background_color(arcade.color.AMAZON)
 
+        # List of cards we are dragging with the mouse
+        self.held_cards = None
+
+        # Original location of cards we are dragging with the mouse in case
+        # they have to go back.
+        self.held_cards_original_position = None
+
     def setup(self):
         """ Set up the game here. Call this function to restart the game. """
+
+        # List of cards we are dragging with the mouse
+        self.held_cards = []
+
+        # Original location of cards we are dragging with the mouse in case
+        # they have to go back.
+        self.held_cards_original_position = []
 
         # Sprite list with all the cards, no matter what pile they are in.
         self.card_list = arcade.SpriteList()
@@ -83,18 +97,53 @@
         # Draw the cards
         self.card_list.draw()
 
+    def pull_to_top(self, card):
+        """ 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
+
     def on_mouse_press(self, x, y, button, key_modifiers):
         """ Called when the user presses a mouse button. """
-        pass
+
+        # Get list of cards we've clicked on
+        cards = arcade.get_sprites_at_point((x, y), self.card_list)
+
+        # Have we clicked on a card?
+        if len(cards) > 0:
+
+            # Might be a stack of cards, get the top one
+            primary_card = cards[-1]
+
+            # All other cases, grab the face-up card we are clicking on
+            self.held_cards = [primary_card]
+            # Save the position
+            self.held_cards_original_position = [self.held_cards[0].position]
+            # Put on top in drawing order
+            self.pull_to_top(self.held_cards[0])
 
     def on_mouse_release(self, x: float, y: float, button: int,
                          modifiers: int):
         """ Called when the user presses a mouse button. """
-        pass
+
+        # If we don't have any cards, who cares
+        if len(self.held_cards) == 0:
+            return
+
+        # We are no longer holding cards
+        self.held_cards = []
 
     def on_mouse_motion(self, x: float, y: float, dx: float, dy: float):
         """ User moves mouse """
-        pass
+
+        # If we are holding cards, move them with the mouse
+        for card in self.held_cards:
+            card.center_x += dx
+            card.center_y += dy
 
 
 def main():