solitaire_11.py Full Listing
solitaire_11.py
1"""
2Solitaire clone.
3"""
4from typing import Optional
5
6import random
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: Optional[arcade.SpriteList] = 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
239 # Might be a stack of cards, get the top one
240 primary_card = cards[-1]
241 assert isinstance(primary_card, Card)
242
243 # Figure out what pile the card is in
244 pile_index = self.get_pile_for_card(primary_card)
245
246 # Are we clicking on the bottom deck, to flip three cards?
247 if pile_index == BOTTOM_FACE_DOWN_PILE:
248 # Flip three cards
249 for i in range(3):
250 # If we ran out of cards, stop
251 if len(self.piles[BOTTOM_FACE_DOWN_PILE]) == 0:
252 break
253 # Get top card
254 card = self.piles[BOTTOM_FACE_DOWN_PILE][-1]
255 # Flip face up
256 card.face_up()
257 # Move card position to bottom-right face up pile
258 card.position = self.pile_mat_list[BOTTOM_FACE_UP_PILE].position
259 # Remove card from face down pile
260 self.piles[BOTTOM_FACE_DOWN_PILE].remove(card)
261 # Move card to face up list
262 self.piles[BOTTOM_FACE_UP_PILE].append(card)
263 # Put on top draw-order wise
264 self.pull_to_top(card)
265
266 elif primary_card.is_face_down:
267 # Is the card face down? In one of those middle 7 piles? Then flip up
268 primary_card.face_up()
269 else:
270 # All other cases, grab the face-up card we are clicking on
271 self.held_cards = [primary_card]
272 # Save the position
273 self.held_cards_original_position = [self.held_cards[0].position]
274 # Put on top in drawing order
275 self.pull_to_top(self.held_cards[0])
276
277 # Is this a stack of cards? If so, grab the other cards too
278 card_index = self.piles[pile_index].index(primary_card)
279 for i in range(card_index + 1, len(self.piles[pile_index])):
280 card = self.piles[pile_index][i]
281 self.held_cards.append(card)
282 self.held_cards_original_position.append(card.position)
283 self.pull_to_top(card)
284
285 else:
286
287 # Click on a mat instead of a card?
288 mats = arcade.get_sprites_at_point((x, y), self.pile_mat_list)
289
290 if len(mats) > 0:
291 mat = mats[0]
292 mat_index = self.pile_mat_list.index(mat)
293
294 # Is it our turned over flip mat? and no cards on it?
295 if mat_index == BOTTOM_FACE_DOWN_PILE and len(self.piles[BOTTOM_FACE_DOWN_PILE]) == 0:
296 # Flip the deck back over so we can restart
297 temp_list = self.piles[BOTTOM_FACE_UP_PILE].copy()
298 for card in reversed(temp_list):
299 card.face_down()
300 self.piles[BOTTOM_FACE_UP_PILE].remove(card)
301 self.piles[BOTTOM_FACE_DOWN_PILE].append(card)
302 card.position = self.pile_mat_list[BOTTOM_FACE_DOWN_PILE].position
303
304 def remove_card_from_pile(self, card):
305 """ Remove card from whatever pile it was in. """
306 for pile in self.piles:
307 if card in pile:
308 pile.remove(card)
309 break
310
311 def get_pile_for_card(self, card):
312 """ What pile is this card in? """
313 for index, pile in enumerate(self.piles):
314 if card in pile:
315 return index
316
317 def move_card_to_new_pile(self, card, pile_index):
318 """ Move the card to a new pile """
319 self.remove_card_from_pile(card)
320 self.piles[pile_index].append(card)
321
322 def on_mouse_release(self, x: float, y: float, button: int,
323 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
337 # What pile is it?
338 pile_index = self.pile_mat_list.index(pile)
339
340 # Is it the same pile we came from?
341 if pile_index == self.get_pile_for_card(self.held_cards[0]):
342 # If so, who cares. We'll just reset our position.
343 pass
344
345 # Is it on a middle play pile?
346 elif PLAY_PILE_1 <= pile_index <= PLAY_PILE_7:
347 # Are there already cards there?
348 if len(self.piles[pile_index]) > 0:
349 # Move cards to proper position
350 top_card = self.piles[pile_index][-1]
351 for i, dropped_card in enumerate(self.held_cards):
352 dropped_card.position = top_card.center_x, \
353 top_card.center_y - CARD_VERTICAL_OFFSET * (i + 1)
354 else:
355 # Are there no cards in the middle play pile?
356 for i, dropped_card in enumerate(self.held_cards):
357 # Move cards to proper position
358 dropped_card.position = pile.center_x, \
359 pile.center_y - CARD_VERTICAL_OFFSET * i
360
361 for card in self.held_cards:
362 # Cards are in the right position, but we need to move them to the right list
363 self.move_card_to_new_pile(card, pile_index)
364
365 # Success, don't reset position of cards
366 reset_position = False
367
368 # Release on top play pile? And only one card held?
369 elif TOP_PILE_1 <= pile_index <= TOP_PILE_4 and len(self.held_cards) == 1:
370 # Move position of card to pile
371 self.held_cards[0].position = pile.position
372 # Move card to card list
373 for card in self.held_cards:
374 self.move_card_to_new_pile(card, pile_index)
375
376 reset_position = False
377
378 if reset_position:
379 # Where-ever we were dropped, it wasn't valid. Reset the each card's position
380 # to its original spot.
381 for pile_index, card in enumerate(self.held_cards):
382 card.position = self.held_cards_original_position[pile_index]
383
384 # We are no longer holding cards
385 self.held_cards = []
386
387 def on_mouse_motion(self, x: float, y: float, dx: float, dy: float):
388 """ User moves mouse """
389
390 # If we are holding cards, move them with the mouse
391 for card in self.held_cards:
392 card.center_x += dx
393 card.center_y += dy
394
395
396def main():
397 """ Main function """
398 window = MyGame()
399 window.setup()
400 arcade.run()
401
402
403if __name__ == "__main__":
404 main()