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