# solitaire_04.py Full Listing¶

solitaire_04.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# The Y of the top row (4 piles)
35TOP_Y = SCREEN_HEIGHT - MAT_HEIGHT / 2 - MAT_HEIGHT * VERTICAL_MARGIN_PERCENT
36
37# The Y of the middle row (7 piles)
38MIDDLE_Y = TOP_Y - MAT_HEIGHT - MAT_HEIGHT * VERTICAL_MARGIN_PERCENT
39
40# How far apart each pile goes
41X_SPACING = MAT_WIDTH + MAT_WIDTH * HORIZONTAL_MARGIN_PERCENT
42
43# Card constants
44CARD_VALUES = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
45CARD_SUITS = ["Clubs", "Hearts", "Spades", "Diamonds"]
46
48    """ Card sprite """
49
50    def __init__(self, suit, value, scale=1):
51        """ Card constructor """
52
53        # Attributes for suit and value
54        self.suit = suit
55        self.value = value
56
57        # Image to use for the sprite when face up
58        self.image_file_name = f":resources:images/cards/card{self.suit}{self.value}.png"
59
60        # Call the parent
61        super().__init__(self.image_file_name, scale, hit_box_algorithm="None")
62
64    """ Main application class. """
65
66    def __init__(self):
67        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
68
69        # Sprite list with all the cards, no matter what pile they are in.
70        self.card_list = None
71
73
74        # List of cards we are dragging with the mouse
75        self.held_cards = None
76
77        # Original location of cards we are dragging with the mouse in case
78        # they have to go back.
79        self.held_cards_original_position = None
80
81        # Sprite list with all the mats tha cards lay on.
82        self.pile_mat_list = None
83
84    def setup(self):
85        """ Set up the game here. Call this function to restart the game. """
86
87        # List of cards we are dragging with the mouse
88        self.held_cards = []
89
90        # Original location of cards we are dragging with the mouse in case
91        # they have to go back.
92        self.held_cards_original_position = []
93
94        # ---  Create the mats the cards go on.
95
96        # Sprite list with all the mats tha cards lay on.
98
99        # Create the mats for the bottom face down and face up piles
101        pile.position = START_X, BOTTOM_Y
102        self.pile_mat_list.append(pile)
103
105        pile.position = START_X + X_SPACING, BOTTOM_Y
106        self.pile_mat_list.append(pile)
107
108        # Create the seven middle piles
109        for i in range(7):
111            pile.position = START_X + i * X_SPACING, MIDDLE_Y
112            self.pile_mat_list.append(pile)
113
114        # Create the top "play" piles
115        for i in range(4):
117            pile.position = START_X + i * X_SPACING, TOP_Y
118            self.pile_mat_list.append(pile)
119
120        # Sprite list with all the cards, no matter what pile they are in.
121        self.card_list = arcade.SpriteList()
122
123        # Create every card
124        for card_suit in CARD_SUITS:
125            for card_value in CARD_VALUES:
126                card = Card(card_suit, card_value, CARD_SCALE)
127                card.position = START_X, BOTTOM_Y
128                self.card_list.append(card)
129
130    def on_draw(self):
131        """ Render the screen. """
132        # Clear the screen
134
135        # Draw the mats the cards go on to
136        self.pile_mat_list.draw()
137
138        # Draw the cards
139        self.card_list.draw()
140
141    def pull_to_top(self, card):
142        """ Pull card to top of rendering order (last to render, looks on-top) """
143        # Find the index of the card
144        index = self.card_list.index(card)
145        # Loop and pull all the other cards down towards the zero end
146        for i in range(index, len(self.card_list) - 1):
147            self.card_list[i] = self.card_list[i + 1]
148        # Put this card at the right-side/top/size of list
149        self.card_list[len(self.card_list) - 1] = card
150
151    def on_mouse_press(self, x, y, button, key_modifiers):
152        """ Called when the user presses a mouse button. """
153
154        # Get list of cards we've clicked on
155        cards = arcade.get_sprites_at_point((x, y), self.card_list)
156
157        # Have we clicked on a card?
158        if len(cards) > 0:
159
160            # Might be a stack of cards, get the top one
161            primary_card = cards[-1]
162
163            # All other cases, grab the face-up card we are clicking on
164            self.held_cards = [primary_card]
165            # Save the position
166            self.held_cards_original_position = [self.held_cards[0].position]
167            # Put on top in drawing order
168            self.pull_to_top(self.held_cards[0])
169
170    def on_mouse_release(self, x: float, y: float, button: int,
171                         modifiers: int):
172        """ Called when the user presses a mouse button. """
173
174        # If we don't have any cards, who cares
175        if len(self.held_cards) == 0:
176            return
177
178        # We are no longer holding cards
179        self.held_cards = []
180
181    def on_mouse_motion(self, x: float, y: float, dx: float, dy: float):
182        """ User moves mouse """
183
184        # If we are holding cards, move them with the mouse
185        for card in self.held_cards:
186            card.center_x += dx
187            card.center_y += dy
188
189
190def main():
191    """ Main method """
192    window = MyGame()
193    window.setup()