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