solitaire_03.py Diff
solitaire_03.py
--- /home/docs/checkouts/readthedocs.org/user_builds/arcade-library/checkouts/latest/doc/tutorials/card_game/solitaire_02.py
+++ /home/docs/checkouts/readthedocs.org/user_builds/arcade-library/checkouts/latest/doc/tutorials/card_game/solitaire_03.py
@@ -64,8 +64,22 @@
self.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()
@@ -85,18 +99,50 @@
# Draw the cards
self.card_list.draw()
+ def pull_to_top(self, card: arcade.Sprite):
+ """ Pull card to top of rendering order (last to render, looks on-top) """
+
+ # 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. """
- 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():