solitaire_11.py Full Listing

solitaire_11.py
  1"""
  2Solitaire clone.
  3"""
  4
  5import random
  6
  7import arcade
  8
  9# Screen title and size
 10SCREEN_WIDTH = 1024
 11SCREEN_HEIGHT = 768
 12SCREEN_TITLE = "Drag and Drop Cards"
 13
 14# Constants for sizing
 15CARD_SCALE = 0.6
 16
 17# How big are the cards?
 18CARD_WIDTH = 140 * CARD_SCALE
 19CARD_HEIGHT = 190 * CARD_SCALE
 20
 21# How big is the mat we'll place the card on?
 22MAT_PERCENT_OVERSIZE = 1.25
 23MAT_HEIGHT = int(CARD_HEIGHT * MAT_PERCENT_OVERSIZE)
 24MAT_WIDTH = int(CARD_WIDTH * MAT_PERCENT_OVERSIZE)
 25
 26# How much space do we leave as a gap between the mats?
 27# Done as a percent of the mat size.
 28VERTICAL_MARGIN_PERCENT = 0.10
 29HORIZONTAL_MARGIN_PERCENT = 0.10
 30
 31# The Y of the bottom row (2 piles)
 32BOTTOM_Y = MAT_HEIGHT / 2 + MAT_HEIGHT * VERTICAL_MARGIN_PERCENT
 33
 34# The X of where to start putting things on the left side
 35START_X = MAT_WIDTH / 2 + MAT_WIDTH * HORIZONTAL_MARGIN_PERCENT
 36
 37# The Y of the top row (4 piles)
 38TOP_Y = SCREEN_HEIGHT - MAT_HEIGHT / 2 - MAT_HEIGHT * VERTICAL_MARGIN_PERCENT
 39
 40# The Y of the middle row (7 piles)
 41MIDDLE_Y = TOP_Y - MAT_HEIGHT - MAT_HEIGHT * VERTICAL_MARGIN_PERCENT
 42
 43# How far apart each pile goes
 44X_SPACING = MAT_WIDTH + MAT_WIDTH * HORIZONTAL_MARGIN_PERCENT
 45
 46# Card constants
 47CARD_VALUES = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
 48CARD_SUITS = ["Clubs", "Hearts", "Spades", "Diamonds"]
 49
 50# If we fan out cards stacked on each other, how far apart to fan them?
 51CARD_VERTICAL_OFFSET = CARD_HEIGHT * CARD_SCALE * 0.3
 52
 53# Face down image
 54FACE_DOWN_IMAGE = ":resources:images/cards/cardBack_red2.png"
 55
 56# Constants that represent "what pile is what" for the game
 57PILE_COUNT = 13
 58BOTTOM_FACE_DOWN_PILE = 0
 59BOTTOM_FACE_UP_PILE = 1
 60PLAY_PILE_1 = 2
 61PLAY_PILE_2 = 3
 62PLAY_PILE_3 = 4
 63PLAY_PILE_4 = 5
 64PLAY_PILE_5 = 6
 65PLAY_PILE_6 = 7
 66PLAY_PILE_7 = 8
 67TOP_PILE_1 = 9
 68TOP_PILE_2 = 10
 69TOP_PILE_3 = 11
 70TOP_PILE_4 = 12
 71
 72
 73class Card(arcade.Sprite):
 74    """Card sprite"""
 75
 76    def __init__(self, suit, value, scale=1):
 77        """Card constructor"""
 78
 79        # Attributes for suit and value
 80        self.suit = suit
 81        self.value = value
 82
 83        # Image to use for the sprite when face up
 84        self.image_file_name = f":resources:images/cards/card{self.suit}{self.value}.png"
 85        self.is_face_up = False
 86        super().__init__(FACE_DOWN_IMAGE, scale, hit_box_algorithm="None")
 87
 88    def face_down(self):
 89        """Turn card face-down"""
 90        self.texture = arcade.load_texture(FACE_DOWN_IMAGE)
 91        self.is_face_up = False
 92
 93    def face_up(self):
 94        """Turn card face-up"""
 95        self.texture = arcade.load_texture(self.image_file_name)
 96        self.is_face_up = True
 97
 98    @property
 99    def is_face_down(self):
100        """Is this card face down?"""
101        return not self.is_face_up
102
103
104class MyGame(arcade.Window):
105    """Main application class."""
106
107    def __init__(self):
108        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
109
110        # Sprite list with all the cards, no matter what pile they are in.
111        self.card_list: arcade.SpriteList | None = None
112
113        self.background_color = arcade.color.AMAZON
114
115        # List of cards we are dragging with the mouse
116        self.held_cards = None
117
118        # Original location of cards we are dragging with the mouse in case
119        # they have to go back.
120        self.held_cards_original_position = None
121
122        # Sprite list with all the mats tha cards lay on.
123        self.pile_mat_list = None
124
125        # Create a list of lists, each holds a pile of cards.
126        self.piles = None
127
128    def setup(self):
129        """Set up the game here. Call this function to restart the game."""
130
131        # List of cards we are dragging with the mouse
132        self.held_cards = []
133
134        # Original location of cards we are dragging with the mouse in case
135        # they have to go back.
136        self.held_cards_original_position = []
137
138        # ---  Create the mats the cards go on.
139
140        # Sprite list with all the mats tha cards lay on.
141        self.pile_mat_list: arcade.SpriteList = arcade.SpriteList()
142
143        # Create the mats for the bottom face down and face up piles
144        pile = arcade.SpriteSolidColor(MAT_WIDTH, MAT_HEIGHT, arcade.csscolor.DARK_OLIVE_GREEN)
145        pile.position = START_X, BOTTOM_Y
146        self.pile_mat_list.append(pile)
147
148        pile = arcade.SpriteSolidColor(MAT_WIDTH, MAT_HEIGHT, arcade.csscolor.DARK_OLIVE_GREEN)
149        pile.position = START_X + X_SPACING, BOTTOM_Y
150        self.pile_mat_list.append(pile)
151
152        # Create the seven middle piles
153        for i in range(7):
154            pile = arcade.SpriteSolidColor(MAT_WIDTH, MAT_HEIGHT, arcade.csscolor.DARK_OLIVE_GREEN)
155            pile.position = START_X + i * X_SPACING, MIDDLE_Y
156            self.pile_mat_list.append(pile)
157
158        # Create the top "play" piles
159        for i in range(4):
160            pile = arcade.SpriteSolidColor(MAT_WIDTH, MAT_HEIGHT, arcade.csscolor.DARK_OLIVE_GREEN)
161            pile.position = START_X + i * X_SPACING, TOP_Y
162            self.pile_mat_list.append(pile)
163
164        # --- Create, shuffle, and deal the cards
165
166        # Sprite list with all the cards, no matter what pile they are in.
167        self.card_list = arcade.SpriteList()
168
169        # Create every card
170        for card_suit in CARD_SUITS:
171            for card_value in CARD_VALUES:
172                card = Card(card_suit, card_value, CARD_SCALE)
173                card.position = START_X, BOTTOM_Y
174                self.card_list.append(card)
175
176        # Shuffle the cards
177        for pos1 in range(len(self.card_list)):
178            pos2 = random.randrange(len(self.card_list))
179            self.card_list.swap(pos1, pos2)
180
181        # Create a list of lists, each holds a pile of cards.
182        self.piles = [[] for _ in range(PILE_COUNT)]
183
184        # Put all the cards in the bottom face-down pile
185        for card in self.card_list:
186            self.piles[BOTTOM_FACE_DOWN_PILE].append(card)
187
188        # - Pull from that pile into the middle piles, all face-down
189        # Loop for each pile
190        for pile_no in range(PLAY_PILE_1, PLAY_PILE_7 + 1):
191            # Deal proper number of cards for that pile
192            for j in range(pile_no - PLAY_PILE_1 + 1):
193                # Pop the card off the deck we are dealing from
194                card = self.piles[BOTTOM_FACE_DOWN_PILE].pop()
195                # Put in the proper pile
196                self.piles[pile_no].append(card)
197                # Move card to same position as pile we just put it in
198                card.position = self.pile_mat_list[pile_no].position
199                # Put on top in draw order
200                self.pull_to_top(card)
201
202        # Flip up the top cards
203        for i in range(PLAY_PILE_1, PLAY_PILE_7 + 1):
204            self.piles[i][-1].face_up()
205
206    def on_draw(self):
207        """Render the screen."""
208        # Clear the screen
209        self.clear()
210
211        # Draw the mats the cards go on to
212        self.pile_mat_list.draw()
213
214        # Draw the cards
215        self.card_list.draw()
216
217    def pull_to_top(self, card: arcade.Sprite):
218        """Pull card to top of rendering order (last to render, looks on-top)"""
219
220        # Remove, and append to the end
221        self.card_list.remove(card)
222        self.card_list.append(card)
223
224    def on_key_press(self, symbol: int, modifiers: int):
225        """User presses key"""
226        if symbol == arcade.key.R:
227            # Restart
228            self.setup()
229
230    def on_mouse_press(self, x, y, button, key_modifiers):
231        """Called when the user presses a mouse button."""
232
233        # Get list of cards we've clicked on
234        cards = arcade.get_sprites_at_point((x, y), self.card_list)
235
236        # Have we clicked on a card?
237        if len(cards) > 0:
238            # Might be a stack of cards, get the top one
239            primary_card = cards[-1]
240            assert isinstance(primary_card, Card)
241
242            # Figure out what pile the card is in
243            pile_index = self.get_pile_for_card(primary_card)
244
245            # Are we clicking on the bottom deck, to flip three cards?
246            if pile_index == BOTTOM_FACE_DOWN_PILE:
247                # Flip three cards
248                for i in range(3):
249                    # If we ran out of cards, stop
250                    if len(self.piles[BOTTOM_FACE_DOWN_PILE]) == 0:
251                        break
252                    # Get top card
253                    card = self.piles[BOTTOM_FACE_DOWN_PILE][-1]
254                    # Flip face up
255                    card.face_up()
256                    # Move card position to bottom-right face up pile
257                    card.position = self.pile_mat_list[BOTTOM_FACE_UP_PILE].position
258                    # Remove card from face down pile
259                    self.piles[BOTTOM_FACE_DOWN_PILE].remove(card)
260                    # Move card to face up list
261                    self.piles[BOTTOM_FACE_UP_PILE].append(card)
262                    # Put on top draw-order wise
263                    self.pull_to_top(card)
264
265            elif primary_card.is_face_down:
266                # Is the card face down? In one of those middle 7 piles? Then flip up
267                primary_card.face_up()
268            else:
269                # All other cases, grab the face-up card we are clicking on
270                self.held_cards = [primary_card]
271                # Save the position
272                self.held_cards_original_position = [self.held_cards[0].position]
273                # Put on top in drawing order
274                self.pull_to_top(self.held_cards[0])
275
276                # Is this a stack of cards? If so, grab the other cards too
277                card_index = self.piles[pile_index].index(primary_card)
278                for i in range(card_index + 1, len(self.piles[pile_index])):
279                    card = self.piles[pile_index][i]
280                    self.held_cards.append(card)
281                    self.held_cards_original_position.append(card.position)
282                    self.pull_to_top(card)
283
284        else:
285            # Click on a mat instead of a card?
286            mats = arcade.get_sprites_at_point((x, y), self.pile_mat_list)
287
288            if len(mats) > 0:
289                mat = mats[0]
290                mat_index = self.pile_mat_list.index(mat)
291
292                # Is it our turned over flip mat? and no cards on it?
293                if (
294                    mat_index == BOTTOM_FACE_DOWN_PILE
295                    and len(self.piles[BOTTOM_FACE_DOWN_PILE]) == 0
296                ):
297                    # Flip the deck back over so we can restart
298                    temp_list = self.piles[BOTTOM_FACE_UP_PILE].copy()
299                    for card in reversed(temp_list):
300                        card.face_down()
301                        self.piles[BOTTOM_FACE_UP_PILE].remove(card)
302                        self.piles[BOTTOM_FACE_DOWN_PILE].append(card)
303                        card.position = self.pile_mat_list[BOTTOM_FACE_DOWN_PILE].position
304
305    def remove_card_from_pile(self, card):
306        """Remove card from whatever pile it was in."""
307        for pile in self.piles:
308            if card in pile:
309                pile.remove(card)
310                break
311
312    def get_pile_for_card(self, card):
313        """What pile is this card in?"""
314        for index, pile in enumerate(self.piles):
315            if card in pile:
316                return index
317
318    def move_card_to_new_pile(self, card, pile_index):
319        """Move the card to a new pile"""
320        self.remove_card_from_pile(card)
321        self.piles[pile_index].append(card)
322
323    def on_mouse_release(self, x: float, y: float, button: int, modifiers: int):
324        """Called when the user presses a mouse button."""
325
326        # If we don't have any cards, who cares
327        if len(self.held_cards) == 0:
328            return
329
330        # Find the closest pile, in case we are in contact with more than one
331        pile, distance = arcade.get_closest_sprite(self.held_cards[0], self.pile_mat_list)
332        reset_position = True
333
334        # See if we are in contact with the closest pile
335        if arcade.check_for_collision(self.held_cards[0], pile):
336            # What pile is it?
337            pile_index = self.pile_mat_list.index(pile)
338
339            #  Is it the same pile we came from?
340            if pile_index == self.get_pile_for_card(self.held_cards[0]):
341                # If so, who cares. We'll just reset our position.
342                pass
343
344            # Is it on a middle play pile?
345            elif PLAY_PILE_1 <= pile_index <= PLAY_PILE_7:
346                # Are there already cards there?
347                if len(self.piles[pile_index]) > 0:
348                    # Move cards to proper position
349                    top_card = self.piles[pile_index][-1]
350                    for i, dropped_card in enumerate(self.held_cards):
351                        dropped_card.position = (
352                            top_card.center_x,
353                            top_card.center_y - CARD_VERTICAL_OFFSET * (i + 1),
354                        )
355                else:
356                    # Are there no cards in the middle play pile?
357                    for i, dropped_card in enumerate(self.held_cards):
358                        # Move cards to proper position
359                        dropped_card.position = (
360                            pile.center_x,
361                            pile.center_y - CARD_VERTICAL_OFFSET * i,
362                        )
363
364                for card in self.held_cards:
365                    # Cards are in the right position, but we need to move them to the right list
366                    self.move_card_to_new_pile(card, pile_index)
367
368                # Success, don't reset position of cards
369                reset_position = False
370
371            # Release on top play pile? And only one card held?
372            elif TOP_PILE_1 <= pile_index <= TOP_PILE_4 and len(self.held_cards) == 1:
373                # Move position of card to pile
374                self.held_cards[0].position = pile.position
375                # Move card to card list
376                for card in self.held_cards:
377                    self.move_card_to_new_pile(card, pile_index)
378
379                reset_position = False
380
381        if reset_position:
382            # Where-ever we were dropped, it wasn't valid. Reset the each card's position
383            # to its original spot.
384            for pile_index, card in enumerate(self.held_cards):
385                card.position = self.held_cards_original_position[pile_index]
386
387        # We are no longer holding cards
388        self.held_cards = []
389
390    def on_mouse_motion(self, x: float, y: float, dx: float, dy: float):
391        """User moves mouse"""
392
393        # If we are holding cards, move them with the mouse
394        for card in self.held_cards:
395            card.center_x += dx
396            card.center_y += dy
397
398
399def main():
400    """Main function"""
401    window = MyGame()
402    window.setup()
403    arcade.run()
404
405
406if __name__ == "__main__":
407    main()