solitaire_11.py Full Listing¶
solitaire_11.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 arcade.set_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[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
207 arcade.start_render()
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_key_press(self, symbol: int, modifiers: int):
226 """ User presses key """
227 if symbol == arcade.key.R:
228 # Restart
229 self.setup()
230
231 def on_mouse_press(self, x, y, button, key_modifiers):
232 """ Called when the user presses a mouse button. """
233
234 # Get list of cards we've clicked on
235 cards = arcade.get_sprites_at_point((x, y), self.card_list)
236
237 # Have we clicked on a card?
238 if len(cards) > 0:
239
240 # Might be a stack of cards, get the top one
241 primary_card = cards[-1]
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
286 # Click on a mat instead of a card?
287 mats = arcade.get_sprites_at_point((x, y), self.pile_mat_list)
288
289 if len(mats) > 0:
290 mat = mats[0]
291 mat_index = self.pile_mat_list.index(mat)
292
293 # Is it our turned over flip mat? and no cards on it?
294 if mat_index == BOTTOM_FACE_DOWN_PILE and len(self.piles[BOTTOM_FACE_DOWN_PILE]) == 0:
295 # Flip the deck back over so we can restart
296 temp_list = self.piles[BOTTOM_FACE_UP_PILE].copy()
297 for card in reversed(temp_list):
298 card.face_down()
299 self.piles[BOTTOM_FACE_UP_PILE].remove(card)
300 self.piles[BOTTOM_FACE_DOWN_PILE].append(card)
301 card.position = self.pile_mat_list[BOTTOM_FACE_DOWN_PILE].position
302
303 def remove_card_from_pile(self, card):
304 """ Remove card from whatever pile it was in. """
305 for pile in self.piles:
306 if card in pile:
307 pile.remove(card)
308 break
309
310 def get_pile_for_card(self, card):
311 """ What pile is this card in? """
312 for index, pile in enumerate(self.piles):
313 if card in pile:
314 return index
315
316 def move_card_to_new_pile(self, card, pile_index):
317 """ Move the card to a new pile """
318 self.remove_card_from_pile(card)
319 self.piles[pile_index].append(card)
320
321 def on_mouse_release(self, x: float, y: float, button: int,
322 modifiers: int):
323 """ Called when the user presses a mouse button. """
324
325 # If we don't have any cards, who cares
326 if len(self.held_cards) == 0:
327 return
328
329 # Find the closest pile, in case we are in contact with more than one
330 pile, distance = arcade.get_closest_sprite(self.held_cards[0], self.pile_mat_list)
331 reset_position = True
332
333 # See if we are in contact with the closest pile
334 if arcade.check_for_collision(self.held_cards[0], pile):
335
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 = top_card.center_x, \
352 top_card.center_y - CARD_VERTICAL_OFFSET * (i + 1)
353 else:
354 # Are there no cards in the middle play pile?
355 for i, dropped_card in enumerate(self.held_cards):
356 # Move cards to proper position
357 dropped_card.position = pile.center_x, \
358 pile.center_y - CARD_VERTICAL_OFFSET * i
359
360 for card in self.held_cards:
361 # Cards are in the right position, but we need to move them to the right list
362 self.move_card_to_new_pile(card, pile_index)
363
364 # Success, don't reset position of cards
365 reset_position = False
366
367 # Release on top play pile? And only one card held?
368 elif TOP_PILE_1 <= pile_index <= TOP_PILE_4 and len(self.held_cards) == 1:
369 # Move position of card to pile
370 self.held_cards[0].position = pile.position
371 # Move card to card list
372 for card in self.held_cards:
373 self.move_card_to_new_pile(card, pile_index)
374
375 reset_position = False
376
377 if reset_position:
378 # Where-ever we were dropped, it wasn't valid. Reset the each card's position
379 # to its original spot.
380 for pile_index, card in enumerate(self.held_cards):
381 card.position = self.held_cards_original_position[pile_index]
382
383 # We are no longer holding cards
384 self.held_cards = []
385
386 def on_mouse_motion(self, x: float, y: float, dx: float, dy: float):
387 """ User moves mouse """
388
389 # If we are holding cards, move them with the mouse
390 for card in self.held_cards:
391 card.center_x += dx
392 card.center_y += dy
393
394
395def main():
396 """ Main method """
397 window = MyGame()
398 window.setup()
399 arcade.run()
400
401
402if __name__ == "__main__":
403 main()