# solitaire_03.py Full Listing¶

solitaire_03.py
```  1"""
2Solitaire clone.
3"""
5
6# Screen title and size
7SCREEN_WIDTH = 1024
8SCREEN_HEIGHT = 768
9SCREEN_TITLE = "Drag and Drop Cards"
10
11# Constants for sizing
12CARD_SCALE = 0.6
13
14# How big are the cards?
15CARD_WIDTH = 140 * CARD_SCALE
16CARD_HEIGHT = 190 * CARD_SCALE
17
18# How big is the mat we'll place the card on?
19MAT_PERCENT_OVERSIZE = 1.25
20MAT_HEIGHT = int(CARD_HEIGHT * MAT_PERCENT_OVERSIZE)
21MAT_WIDTH = int(CARD_WIDTH * MAT_PERCENT_OVERSIZE)
22
23# How much space do we leave as a gap between the mats?
24# Done as a percent of the mat size.
25VERTICAL_MARGIN_PERCENT = 0.10
26HORIZONTAL_MARGIN_PERCENT = 0.10
27
28# The Y of the bottom row (2 piles)
29BOTTOM_Y = MAT_HEIGHT / 2 + MAT_HEIGHT * VERTICAL_MARGIN_PERCENT
30
31# The X of where to start putting things on the left side
32START_X = MAT_WIDTH / 2 + MAT_WIDTH * HORIZONTAL_MARGIN_PERCENT
33
34# Card constants
35CARD_VALUES = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
36CARD_SUITS = ["Clubs", "Hearts", "Spades", "Diamonds"]
37
39    """ Card sprite """
40
41    def __init__(self, suit, value, scale=1):
42        """ Card constructor """
43
44        # Attributes for suit and value
45        self.suit = suit
46        self.value = value
47
48        # Image to use for the sprite when face up
49        self.image_file_name = f":resources:images/cards/card{self.suit}{self.value}.png"
50
51        # Call the parent
52        super().__init__(self.image_file_name, scale, hit_box_algorithm="None")
53
55    """ Main application class. """
56
57    def __init__(self):
58        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
59
60        # Sprite list with all the cards, no matter what pile they are in.
61        self.card_list = None
62
64
65        # List of cards we are dragging with the mouse
66        self.held_cards = None
67
68        # Original location of cards we are dragging with the mouse in case
69        # they have to go back.
70        self.held_cards_original_position = None
71
72    def setup(self):
73        """ Set up the game here. Call this function to restart the game. """
74
75        # List of cards we are dragging with the mouse
76        self.held_cards = []
77
78        # Original location of cards we are dragging with the mouse in case
79        # they have to go back.
80        self.held_cards_original_position = []
81
82        # Sprite list with all the cards, no matter what pile they are in.
84
85        # Create every card
86        for card_suit in CARD_SUITS:
87            for card_value in CARD_VALUES:
88                card = Card(card_suit, card_value, CARD_SCALE)
89                card.position = START_X, BOTTOM_Y
90                self.card_list.append(card)
91
92    def on_draw(self):
93        """ Render the screen. """
94        # Clear the screen
96
97        # Draw the cards
98        self.card_list.draw()
99
100    def pull_to_top(self, card):
101        """ Pull card to top of rendering order (last to render, looks on-top) """
102        # Find the index of the card
103        index = self.card_list.index(card)
104        # Loop and pull all the other cards down towards the zero end
105        for i in range(index, len(self.card_list) - 1):
106            self.card_list[i] = self.card_list[i + 1]
107        # Put this card at the right-side/top/size of list
108        self.card_list[len(self.card_list) - 1] = card
109
110    def on_mouse_press(self, x, y, button, key_modifiers):
111        """ Called when the user presses a mouse button. """
112
113        # Get list of cards we've clicked on
114        cards = arcade.get_sprites_at_point((x, y), self.card_list)
115
116        # Have we clicked on a card?
117        if len(cards) > 0:
118
119            # Might be a stack of cards, get the top one
120            primary_card = cards[-1]
121
122            # All other cases, grab the face-up card we are clicking on
123            self.held_cards = [primary_card]
124            # Save the position
125            self.held_cards_original_position = [self.held_cards[0].position]
126            # Put on top in drawing order
127            self.pull_to_top(self.held_cards[0])
128
129    def on_mouse_release(self, x: float, y: float, button: int,
130                         modifiers: int):
131        """ Called when the user presses a mouse button. """
132
133        # If we don't have any cards, who cares
134        if len(self.held_cards) == 0:
135            return
136
137        # We are no longer holding cards
138        self.held_cards = []
139
140    def on_mouse_motion(self, x: float, y: float, dx: float, dy: float):
141        """ User moves mouse """
142
143        # If we are holding cards, move them with the mouse
144        for card in self.held_cards:
145            card.center_x += dx
146            card.center_y += dy
147
148
149def main():
150    """ Main method """
151    window = MyGame()
152    window.setup()