solitaire_10.py Full Listing¶

solitaire_10.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
48# If we fan out cards stacked on each other, how far apart to fan them?
49CARD_VERTICAL_OFFSET = CARD_HEIGHT * CARD_SCALE * 0.3
50
51# Face down image
52FACE_DOWN_IMAGE = ":resources:images/cards/cardBack_red2.png"
53
54# Constants that represent "what pile is what" for the game
55PILE_COUNT = 13
56BOTTOM_FACE_DOWN_PILE = 0
57BOTTOM_FACE_UP_PILE = 1
58PLAY_PILE_1 = 2
59PLAY_PILE_2 = 3
60PLAY_PILE_3 = 4
61PLAY_PILE_4 = 5
62PLAY_PILE_5 = 6
63PLAY_PILE_6 = 7
64PLAY_PILE_7 = 8
65TOP_PILE_1 = 9
66TOP_PILE_2 = 10
67TOP_PILE_3 = 11
68TOP_PILE_4 = 12
69
70
72    """ Card sprite """
73
74    def __init__(self, suit, value, scale=1):
75        """ Card constructor """
76
77        # Attributes for suit and value
78        self.suit = suit
79        self.value = value
80
81        # Image to use for the sprite when face up
82        self.image_file_name = f":resources:images/cards/card{self.suit}{self.value}.png"
83        self.is_face_up = False
84        super().__init__(FACE_DOWN_IMAGE, scale, hit_box_algorithm="None")
85
86    def face_down(self):
87        """ Turn card face-down """
89        self.is_face_up = False
90
91    def face_up(self):
92        """ Turn card face-up """
94        self.is_face_up = True
95
96    @property
97    def is_face_down(self):
98        """ Is this card face down? """
99        return not self.is_face_up
100
101
103    """ Main application class. """
104
105    def __init__(self):
106        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
107
108        # Sprite list with all the cards, no matter what pile they are in.
109        self.card_list = None
110
112
113        # List of cards we are dragging with the mouse
114        self.held_cards = None
115
116        # Original location of cards we are dragging with the mouse in case
117        # they have to go back.
118        self.held_cards_original_position = None
119
120        # Sprite list with all the mats tha cards lay on.
121        self.pile_mat_list = None
122
123        # Create a list of lists, each holds a pile of cards.
124        self.piles = None
125
126    def setup(self):
127        """ Set up the game here. Call this function to restart the game. """
128
129        # List of cards we are dragging with the mouse
130        self.held_cards = []
131
132        # Original location of cards we are dragging with the mouse in case
133        # they have to go back.
134        self.held_cards_original_position = []
135
136        # ---  Create the mats the cards go on.
137
138        # Sprite list with all the mats tha cards lay on.
140
141        # Create the mats for the bottom face down and face up piles
143        pile.position = START_X, BOTTOM_Y
144        self.pile_mat_list.append(pile)
145
147        pile.position = START_X + X_SPACING, BOTTOM_Y
148        self.pile_mat_list.append(pile)
149
150        # Create the seven middle piles
151        for i in range(7):
153            pile.position = START_X + i * X_SPACING, MIDDLE_Y
154            self.pile_mat_list.append(pile)
155
156        # Create the top "play" piles
157        for i in range(4):
159            pile.position = START_X + i * X_SPACING, TOP_Y
160            self.pile_mat_list.append(pile)
161
162        # --- Create, shuffle, and deal the cards
163
164        # Sprite list with all the cards, no matter what pile they are in.
166
167        # Create every card
168        for card_suit in CARD_SUITS:
169            for card_value in CARD_VALUES:
170                card = Card(card_suit, card_value, CARD_SCALE)
171                card.position = START_X, BOTTOM_Y
172                self.card_list.append(card)
173
174        # Shuffle the cards
175        for pos1 in range(len(self.card_list)):
176            pos2 = random.randrange(len(self.card_list))
177            self.card_list[pos1], self.card_list[pos2] = self.card_list[pos2], self.card_list[pos1]
178
179        # Create a list of lists, each holds a pile of cards.
180        self.piles = [[] for _ in range(PILE_COUNT)]
181
182        # Put all the cards in the bottom face-down pile
183        for card in self.card_list:
184            self.piles[BOTTOM_FACE_DOWN_PILE].append(card)
185
186        # - Pull from that pile into the middle piles, all face-down
187        # Loop for each pile
188        for pile_no in range(PLAY_PILE_1, PLAY_PILE_7 + 1):
189            # Deal proper number of cards for that pile
190            for j in range(pile_no - PLAY_PILE_1 + 1):
191                # Pop the card off the deck we are dealing from
192                card = self.piles[BOTTOM_FACE_DOWN_PILE].pop()
193                # Put in the proper pile
194                self.piles[pile_no].append(card)
195                # Move card to same position as pile we just put it in
196                card.position = self.pile_mat_list[pile_no].position
197                # Put on top in draw order
198                self.pull_to_top(card)
199
200        # Flip up the top cards
201        for i in range(PLAY_PILE_1, PLAY_PILE_7 + 1):
202            self.piles[i][-1].face_up()
203
204    def on_draw(self):
205        """ Render the screen. """
206        # Clear the screen
208
209        # Draw the mats the cards go on to
210        self.pile_mat_list.draw()
211
212        # Draw the cards
213        self.card_list.draw()
214
215    def pull_to_top(self, card):
216        """ Pull card to top of rendering order (last to render, looks on-top) """
217        # Find the index of the card
218        index = self.card_list.index(card)
219        # Loop and pull all the other cards down towards the zero end
220        for i in range(index, len(self.card_list) - 1):
221            self.card_list[i] = self.card_list[i + 1]
222        # Put this card at the right-side/top/size of list
223        self.card_list[len(self.card_list) - 1] = card
224
225    def on_mouse_press(self, x, y, button, key_modifiers):
226        """ Called when the user presses a mouse button. """
227
228        # Get list of cards we've clicked on
229        cards = arcade.get_sprites_at_point((x, y), self.card_list)
230
231        # Have we clicked on a card?
232        if len(cards) > 0:
233
234            # Might be a stack of cards, get the top one
235            primary_card = cards[-1]
236            # Figure out what pile the card is in
237            pile_index = self.get_pile_for_card(primary_card)
238
239            if primary_card.is_face_down:
240                # Is the card face down? In one of those middle 7 piles? Then flip up
241                primary_card.face_up()
242            else:
243                # All other cases, grab the face-up card we are clicking on
244                self.held_cards = [primary_card]
245                # Save the position
246                self.held_cards_original_position = [self.held_cards[0].position]
247                # Put on top in drawing order
248                self.pull_to_top(self.held_cards[0])
249
250                # Is this a stack of cards? If so, grab the other cards too
251                card_index = self.piles[pile_index].index(primary_card)
252                for i in range(card_index + 1, len(self.piles[pile_index])):
253                    card = self.piles[pile_index][i]
254                    self.held_cards.append(card)
255                    self.held_cards_original_position.append(card.position)
256                    self.pull_to_top(card)
257
258    def remove_card_from_pile(self, card):
259        """ Remove card from whatever pile it was in. """
260        for pile in self.piles:
261            if card in pile:
262                pile.remove(card)
263                break
264
265    def get_pile_for_card(self, card):
266        """ What pile is this card in? """
267        for index, pile in enumerate(self.piles):
268            if card in pile:
269                return index
270
271    def move_card_to_new_pile(self, card, pile_index):
272        """ Move the card to a new pile """
273        self.remove_card_from_pile(card)
274        self.piles[pile_index].append(card)
275
276    def on_mouse_release(self, x: float, y: float, button: int,
277                         modifiers: int):
278        """ Called when the user presses a mouse button. """
279
280        # If we don't have any cards, who cares
281        if len(self.held_cards) == 0:
282            return
283
284        # Find the closest pile, in case we are in contact with more than one
285        pile, distance = arcade.get_closest_sprite(self.held_cards[0], self.pile_mat_list)
286        reset_position = True
287
288        # See if we are in contact with the closest pile
290
291            # What pile is it?
292            pile_index = self.pile_mat_list.index(pile)
293
294            #  Is it the same pile we came from?
295            if pile_index == self.get_pile_for_card(self.held_cards[0]):
296                # If so, who cares. We'll just reset our position.
297                pass
298
299            # Is it on a middle play pile?
300            elif PLAY_PILE_1 <= pile_index <= PLAY_PILE_7:
301                # Are there already cards there?
302                if len(self.piles[pile_index]) > 0:
303                    # Move cards to proper position
304                    top_card = self.piles[pile_index][-1]
305                    for i, dropped_card in enumerate(self.held_cards):
306                        dropped_card.position = top_card.center_x, \
307                                                top_card.center_y - CARD_VERTICAL_OFFSET * (i + 1)
308                else:
309                    # Are there no cards in the middle play pile?
310                    for i, dropped_card in enumerate(self.held_cards):
311                        # Move cards to proper position
312                        dropped_card.position = pile.center_x, \
313                                                pile.center_y - CARD_VERTICAL_OFFSET * i
314
315                for card in self.held_cards:
316                    # Cards are in the right position, but we need to move them to the right list
317                    self.move_card_to_new_pile(card, pile_index)
318
319                # Success, don't reset position of cards
320                reset_position = False
321
322            # Release on top play pile? And only one card held?
323            elif TOP_PILE_1 <= pile_index <= TOP_PILE_4 and len(self.held_cards) == 1:
324                # Move position of card to pile
325                self.held_cards[0].position = pile.position
326                # Move card to card list
327                for card in self.held_cards:
328                    self.move_card_to_new_pile(card, pile_index)
329
330                reset_position = False
331
332        if reset_position:
333            # Where-ever we were dropped, it wasn't valid. Reset the each card's position
334            # to its original spot.
335            for pile_index, card in enumerate(self.held_cards):
336                card.position = self.held_cards_original_position[pile_index]
337
338        # We are no longer holding cards
339        self.held_cards = []
340
341    def on_mouse_motion(self, x: float, y: float, dx: float, dy: float):
342        """ User moves mouse """
343
344        # If we are holding cards, move them with the mouse
345        for card in self.held_cards:
346            card.center_x += dx
347            card.center_y += dy
348
349
350def main():
351    """ Main method """
352    window = MyGame()
353    window.setup()