solitaire_10.py Full Listing#

solitaire_10.py#
  1"""
  2Solitaire clone.
  3"""
  4import random
  5import arcade
  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
 71class Card(arcade.Sprite):
 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 """
 88        self.texture = arcade.load_texture(FACE_DOWN_IMAGE)
 89        self.is_face_up = False
 90
 91    def face_up(self):
 92        """ Turn card face-up """
 93        self.texture = arcade.load_texture(self.image_file_name)
 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
102class MyGame(arcade.Window):
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
111        self.background_color = arcade.color.AMAZON
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.
139        self.pile_mat_list: arcade.SpriteList = arcade.SpriteList()
140
141        # Create the mats for the bottom face down and face up piles
142        pile = arcade.SpriteSolidColor(MAT_WIDTH, MAT_HEIGHT, arcade.csscolor.DARK_OLIVE_GREEN)
143        pile.position = START_X, BOTTOM_Y
144        self.pile_mat_list.append(pile)
145
146        pile = arcade.SpriteSolidColor(MAT_WIDTH, MAT_HEIGHT, arcade.csscolor.DARK_OLIVE_GREEN)
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):
152            pile = arcade.SpriteSolidColor(MAT_WIDTH, MAT_HEIGHT, arcade.csscolor.DARK_OLIVE_GREEN)
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):
158            pile = arcade.SpriteSolidColor(MAT_WIDTH, MAT_HEIGHT, arcade.csscolor.DARK_OLIVE_GREEN)
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.
165        self.card_list = arcade.SpriteList()
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.swap(pos1, pos2)
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
207        self.clear()
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: arcade.Sprite):
216        """ Pull card to top of rendering order (last to render, looks on-top) """
217
218        # Remove, and append to the end
219        self.card_list.remove(card)
220        self.card_list.append(card)
221
222    def on_mouse_press(self, x, y, button, key_modifiers):
223        """ Called when the user presses a mouse button. """
224
225        # Get list of cards we've clicked on
226        cards = arcade.get_sprites_at_point((x, y), self.card_list)
227
228        # Have we clicked on a card?
229        if len(cards) > 0:
230
231            # Might be a stack of cards, get the top one
232            primary_card = cards[-1]
233            assert isinstance(primary_card, Card)
234
235            # Figure out what pile the card is in
236            pile_index = self.get_pile_for_card(primary_card)
237
238            if primary_card.is_face_down:
239                # Is the card face down? In one of those middle 7 piles? Then flip up
240                primary_card.face_up()
241            else:
242                # All other cases, grab the face-up card we are clicking on
243                self.held_cards = [primary_card]
244                # Save the position
245                self.held_cards_original_position = [self.held_cards[0].position]
246                # Put on top in drawing order
247                self.pull_to_top(self.held_cards[0])
248
249                # Is this a stack of cards? If so, grab the other cards too
250                card_index = self.piles[pile_index].index(primary_card)
251                for i in range(card_index + 1, len(self.piles[pile_index])):
252                    card = self.piles[pile_index][i]
253                    self.held_cards.append(card)
254                    self.held_cards_original_position.append(card.position)
255                    self.pull_to_top(card)
256
257    def remove_card_from_pile(self, card):
258        """ Remove card from whatever pile it was in. """
259        for pile in self.piles:
260            if card in pile:
261                pile.remove(card)
262                break
263
264    def get_pile_for_card(self, card):
265        """ What pile is this card in? """
266        for index, pile in enumerate(self.piles):
267            if card in pile:
268                return index
269
270    def move_card_to_new_pile(self, card, pile_index):
271        """ Move the card to a new pile """
272        self.remove_card_from_pile(card)
273        self.piles[pile_index].append(card)
274
275    def on_mouse_release(self, x: float, y: float, button: int,
276                         modifiers: int):
277        """ Called when the user presses a mouse button. """
278
279        # If we don't have any cards, who cares
280        if len(self.held_cards) == 0:
281            return
282
283        # Find the closest pile, in case we are in contact with more than one
284        pile, distance = arcade.get_closest_sprite(self.held_cards[0], self.pile_mat_list)
285        reset_position = True
286
287        # See if we are in contact with the closest pile
288        if arcade.check_for_collision(self.held_cards[0], pile):
289
290            # What pile is it?
291            pile_index = self.pile_mat_list.index(pile)
292
293            #  Is it the same pile we came from?
294            if pile_index == self.get_pile_for_card(self.held_cards[0]):
295                # If so, who cares. We'll just reset our position.
296                pass
297
298            # Is it on a middle play pile?
299            elif PLAY_PILE_1 <= pile_index <= PLAY_PILE_7:
300                # Are there already cards there?
301                if len(self.piles[pile_index]) > 0:
302                    # Move cards to proper position
303                    top_card = self.piles[pile_index][-1]
304                    for i, dropped_card in enumerate(self.held_cards):
305                        dropped_card.position = top_card.center_x, \
306                                                top_card.center_y - CARD_VERTICAL_OFFSET * (i + 1)
307                else:
308                    # Are there no cards in the middle play pile?
309                    for i, dropped_card in enumerate(self.held_cards):
310                        # Move cards to proper position
311                        dropped_card.position = pile.center_x, \
312                                                pile.center_y - CARD_VERTICAL_OFFSET * i
313
314                for card in self.held_cards:
315                    # Cards are in the right position, but we need to move them to the right list
316                    self.move_card_to_new_pile(card, pile_index)
317
318                # Success, don't reset position of cards
319                reset_position = False
320
321            # Release on top play pile? And only one card held?
322            elif TOP_PILE_1 <= pile_index <= TOP_PILE_4 and len(self.held_cards) == 1:
323                # Move position of card to pile
324                self.held_cards[0].position = pile.position
325                # Move card to card list
326                for card in self.held_cards:
327                    self.move_card_to_new_pile(card, pile_index)
328
329                reset_position = False
330
331        if reset_position:
332            # Where-ever we were dropped, it wasn't valid. Reset the each card's position
333            # to its original spot.
334            for pile_index, card in enumerate(self.held_cards):
335                card.position = self.held_cards_original_position[pile_index]
336
337        # We are no longer holding cards
338        self.held_cards = []
339
340    def on_mouse_motion(self, x: float, y: float, dx: float, dy: float):
341        """ User moves mouse """
342
343        # If we are holding cards, move them with the mouse
344        for card in self.held_cards:
345            card.center_x += dx
346            card.center_y += dy
347
348
349def main():
350    """ Main function """
351    window = MyGame()
352    window.setup()
353    arcade.run()
354
355
356if __name__ == "__main__":
357    main()