# solitaire_06.py Full Listing¶

solitaire_06.py
```  1"""
2Solitaire clone.
3"""
4import random
6
7# Screen title and size
8SCREEN_WIDTH = 1024
9SCREEN_HEIGHT = 768
10SCREEN_TITLE = "Drag and Drop Cards"
11
12# Constants for sizing
13CARD_SCALE = 0.6
14
15# How big are the cards?
16CARD_WIDTH = 140 * CARD_SCALE
17CARD_HEIGHT = 190 * CARD_SCALE
18
19# How big is the mat we'll place the card on?
20MAT_PERCENT_OVERSIZE = 1.25
21MAT_HEIGHT = int(CARD_HEIGHT * MAT_PERCENT_OVERSIZE)
22MAT_WIDTH = int(CARD_WIDTH * MAT_PERCENT_OVERSIZE)
23
24# How much space do we leave as a gap between the mats?
25# Done as a percent of the mat size.
26VERTICAL_MARGIN_PERCENT = 0.10
27HORIZONTAL_MARGIN_PERCENT = 0.10
28
29# The Y of the bottom row (2 piles)
30BOTTOM_Y = MAT_HEIGHT / 2 + MAT_HEIGHT * VERTICAL_MARGIN_PERCENT
31
32# The X of where to start putting things on the left side
33START_X = MAT_WIDTH / 2 + MAT_WIDTH * HORIZONTAL_MARGIN_PERCENT
34
35# The Y of the top row (4 piles)
36TOP_Y = SCREEN_HEIGHT - MAT_HEIGHT / 2 - MAT_HEIGHT * VERTICAL_MARGIN_PERCENT
37
38# The Y of the middle row (7 piles)
39MIDDLE_Y = TOP_Y - MAT_HEIGHT - MAT_HEIGHT * VERTICAL_MARGIN_PERCENT
40
41# How far apart each pile goes
42X_SPACING = MAT_WIDTH + MAT_WIDTH * HORIZONTAL_MARGIN_PERCENT
43
44# Card constants
45CARD_VALUES = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
46CARD_SUITS = ["Clubs", "Hearts", "Spades", "Diamonds"]
47
49    """ Card sprite """
50
51    def __init__(self, suit, value, scale=1):
52        """ Card constructor """
53
54        # Attributes for suit and value
55        self.suit = suit
56        self.value = value
57
58        # Image to use for the sprite when face up
59        self.image_file_name = f":resources:images/cards/card{self.suit}{self.value}.png"
60
61        # Call the parent
62        super().__init__(self.image_file_name, scale, hit_box_algorithm="None")
63
65    """ Main application class. """
66
67    def __init__(self):
68        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
69
70        # Sprite list with all the cards, no matter what pile they are in.
71        self.card_list = None
72
74
75        # List of cards we are dragging with the mouse
76        self.held_cards = None
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 = None
81
82        # Sprite list with all the mats tha cards lay on.
83        self.pile_mat_list = None
84
85    def setup(self):
86        """ Set up the game here. Call this function to restart the game. """
87
88        # List of cards we are dragging with the mouse
89        self.held_cards = []
90
91        # Original location of cards we are dragging with the mouse in case
92        # they have to go back.
93        self.held_cards_original_position = []
94
95        # ---  Create the mats the cards go on.
96
97        # Sprite list with all the mats tha cards lay on.
99
100        # Create the mats for the bottom face down and face up piles
102        pile.position = START_X, BOTTOM_Y
103        self.pile_mat_list.append(pile)
104
106        pile.position = START_X + X_SPACING, BOTTOM_Y
107        self.pile_mat_list.append(pile)
108
109        # Create the seven middle piles
110        for i in range(7):
112            pile.position = START_X + i * X_SPACING, MIDDLE_Y
113            self.pile_mat_list.append(pile)
114
115        # Create the top "play" piles
116        for i in range(4):
118            pile.position = START_X + i * X_SPACING, TOP_Y
119            self.pile_mat_list.append(pile)
120
121        # --- Create, shuffle, and deal the cards
122
123        # Sprite list with all the cards, no matter what pile they are in.
125
126        # Create every card
127        for card_suit in CARD_SUITS:
128            for card_value in CARD_VALUES:
129                card = Card(card_suit, card_value, CARD_SCALE)
130                card.position = START_X, BOTTOM_Y
131                self.card_list.append(card)
132
133        # Shuffle the cards
134        for pos1 in range(len(self.card_list)):
135            pos2 = random.randrange(len(self.card_list))
136            self.card_list[pos1], self.card_list[pos2] = self.card_list[pos2], self.card_list[pos1]
137
138
139    def on_draw(self):
140        """ Render the screen. """
141        # Clear the screen
143
144        # Draw the mats the cards go on to
145        self.pile_mat_list.draw()
146
147        # Draw the cards
148        self.card_list.draw()
149
150    def pull_to_top(self, card):
151        """ Pull card to top of rendering order (last to render, looks on-top) """
152        # Find the index of the card
153        index = self.card_list.index(card)
154        # Loop and pull all the other cards down towards the zero end
155        for i in range(index, len(self.card_list) - 1):
156            self.card_list[i] = self.card_list[i + 1]
157        # Put this card at the right-side/top/size of list
158        self.card_list[len(self.card_list) - 1] = card
159
160    def on_mouse_press(self, x, y, button, key_modifiers):
161        """ Called when the user presses a mouse button. """
162
163        # Get list of cards we've clicked on
164        cards = arcade.get_sprites_at_point((x, y), self.card_list)
165
166        # Have we clicked on a card?
167        if len(cards) > 0:
168
169            # Might be a stack of cards, get the top one
170            primary_card = cards[-1]
171
172            # All other cases, grab the face-up card we are clicking on
173            self.held_cards = [primary_card]
174            # Save the position
175            self.held_cards_original_position = [self.held_cards[0].position]
176            # Put on top in drawing order
177            self.pull_to_top(self.held_cards[0])
178
179    def on_mouse_release(self, x: float, y: float, button: int,
180                         modifiers: int):
181        """ Called when the user presses a mouse button. """
182
183        # If we don't have any cards, who cares
184        if len(self.held_cards) == 0:
185            return
186
187        # Find the closest pile, in case we are in contact with more than one
188        pile, distance = arcade.get_closest_sprite(self.held_cards[0], self.pile_mat_list)
189        reset_position = True
190
191        # See if we are in contact with the closest pile
193
194            # For each held card, move it to the pile we dropped on
195            for i, dropped_card in enumerate(self.held_cards):
196                # Move cards to proper position
197                dropped_card.position = pile.center_x, pile.center_y
198
199            # Success, don't reset position of cards
200            reset_position = False
201
202        if reset_position:
203            # Where-ever we were dropped, it wasn't valid. Reset the each card's position
204            # to its original spot.
205            for pile_index, card in enumerate(self.held_cards):
206                card.position = self.held_cards_original_position[pile_index]
207
208        # We are no longer holding cards
209        self.held_cards = []
210
211    def on_mouse_motion(self, x: float, y: float, dx: float, dy: float):
212        """ User moves mouse """
213
214        # If we are holding cards, move them with the mouse
215        for card in self.held_cards:
216            card.center_x += dx
217            card.center_y += dy
218
219
220def main():
221    """ Main method """
222    window = MyGame()
223    window.setup()