solitaire_06.py Full Listing#

solitaire_06.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
 49class Card(arcade.Sprite):
 50    """ Card sprite """
 51
 52    def __init__(self, suit, value, scale=1):
 53        """ Card constructor """
 54
 55        # Attributes for suit and value
 56        self.suit = suit
 57        self.value = value
 58
 59        # Image to use for the sprite when face up
 60        self.image_file_name = f":resources:images/cards/card{self.suit}{self.value}.png"
 61
 62        # Call the parent
 63        super().__init__(self.image_file_name, scale, hit_box_algorithm="None")
 64
 65
 66class MyGame(arcade.Window):
 67    """ Main application class. """
 68
 69    def __init__(self):
 70        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
 71
 72        # Sprite list with all the cards, no matter what pile they are in.
 73        self.card_list = None
 74
 75        self.background_color = arcade.color.AMAZON
 76
 77        # List of cards we are dragging with the mouse
 78        self.held_cards = None
 79
 80        # Original location of cards we are dragging with the mouse in case
 81        # they have to go back.
 82        self.held_cards_original_position = None
 83
 84        # Sprite list with all the mats tha cards lay on.
 85        self.pile_mat_list = None
 86
 87    def setup(self):
 88        """ Set up the game here. Call this function to restart the game. """
 89
 90        # List of cards we are dragging with the mouse
 91        self.held_cards = []
 92
 93        # Original location of cards we are dragging with the mouse in case
 94        # they have to go back.
 95        self.held_cards_original_position = []
 96
 97        # ---  Create the mats the cards go on.
 98
 99        # Sprite list with all the mats tha cards lay on.
100        self.pile_mat_list: arcade.SpriteList = arcade.SpriteList()
101
102        # Create the mats for the bottom face down and face up piles
103        pile = arcade.SpriteSolidColor(MAT_WIDTH, MAT_HEIGHT, arcade.csscolor.DARK_OLIVE_GREEN)
104        pile.position = START_X, BOTTOM_Y
105        self.pile_mat_list.append(pile)
106
107        pile = arcade.SpriteSolidColor(MAT_WIDTH, MAT_HEIGHT, arcade.csscolor.DARK_OLIVE_GREEN)
108        pile.position = START_X + X_SPACING, BOTTOM_Y
109        self.pile_mat_list.append(pile)
110
111        # Create the seven middle piles
112        for i in range(7):
113            pile = arcade.SpriteSolidColor(MAT_WIDTH, MAT_HEIGHT, arcade.csscolor.DARK_OLIVE_GREEN)
114            pile.position = START_X + i * X_SPACING, MIDDLE_Y
115            self.pile_mat_list.append(pile)
116
117        # Create the top "play" piles
118        for i in range(4):
119            pile = arcade.SpriteSolidColor(MAT_WIDTH, MAT_HEIGHT, arcade.csscolor.DARK_OLIVE_GREEN)
120            pile.position = START_X + i * X_SPACING, TOP_Y
121            self.pile_mat_list.append(pile)
122
123        # --- Create, shuffle, and deal the cards
124
125        # Sprite list with all the cards, no matter what pile they are in.
126        self.card_list = arcade.SpriteList()
127
128        # Create every card
129        for card_suit in CARD_SUITS:
130            for card_value in CARD_VALUES:
131                card = Card(card_suit, card_value, CARD_SCALE)
132                card.position = START_X, BOTTOM_Y
133                self.card_list.append(card)
134
135        # Shuffle the cards
136        for pos1 in range(len(self.card_list)):
137            pos2 = random.randrange(len(self.card_list))
138            self.card_list.swap(pos1, pos2)
139
140    def on_draw(self):
141        """ Render the screen. """
142        # Clear the screen
143        self.clear()
144
145        # Draw the mats the cards go on to
146        self.pile_mat_list.draw()
147
148        # Draw the cards
149        self.card_list.draw()
150
151    def pull_to_top(self, card: arcade.Sprite):
152        """ Pull card to top of rendering order (last to render, looks on-top) """
153
154        # Remove, and append to the end
155        self.card_list.remove(card)
156        self.card_list.append(card)
157
158    def on_mouse_press(self, x, y, button, key_modifiers):
159        """ Called when the user presses a mouse button. """
160
161        # Get list of cards we've clicked on
162        cards = arcade.get_sprites_at_point((x, y), self.card_list)
163
164        # Have we clicked on a card?
165        if len(cards) > 0:
166
167            # Might be a stack of cards, get the top one
168            primary_card = cards[-1]
169
170            # All other cases, grab the face-up card we are clicking on
171            self.held_cards = [primary_card]
172            # Save the position
173            self.held_cards_original_position = [self.held_cards[0].position]
174            # Put on top in drawing order
175            self.pull_to_top(self.held_cards[0])
176
177    def on_mouse_release(self, x: float, y: float, button: int,
178                         modifiers: int):
179        """ Called when the user presses a mouse button. """
180
181        # If we don't have any cards, who cares
182        if len(self.held_cards) == 0:
183            return
184
185        # Find the closest pile, in case we are in contact with more than one
186        pile, distance = arcade.get_closest_sprite(self.held_cards[0], self.pile_mat_list)
187        reset_position = True
188
189        # See if we are in contact with the closest pile
190        if arcade.check_for_collision(self.held_cards[0], pile):
191
192            # For each held card, move it to the pile we dropped on
193            for i, dropped_card in enumerate(self.held_cards):
194                # Move cards to proper position
195                dropped_card.position = pile.center_x, pile.center_y
196
197            # Success, don't reset position of cards
198            reset_position = False
199
200        if reset_position:
201            # Where-ever we were dropped, it wasn't valid. Reset the each card's position
202            # to its original spot.
203            for pile_index, card in enumerate(self.held_cards):
204                card.position = self.held_cards_original_position[pile_index]
205
206        # We are no longer holding cards
207        self.held_cards = []
208
209    def on_mouse_motion(self, x: float, y: float, dx: float, dy: float):
210        """ User moves mouse """
211
212        # If we are holding cards, move them with the mouse
213        for card in self.held_cards:
214            card.center_x += dx
215            card.center_y += dy
216
217
218def main():
219    """ Main function """
220    window = MyGame()
221    window.setup()
222    arcade.run()
223
224
225if __name__ == "__main__":
226    main()