solitaire_05.py Full Listing
solitaire_05.py
1"""
2Solitaire clone.
3"""
4import arcade
5
6# Screen title and size
7SCREEN_WIDTH = 1024
8SCREEN_HEIGHT = 768
9SCREEN_TITLE = "Drag and Drop Cards"
10
11# Constants for sizing
12CARD_SCALE = 0.6
13
14# How big are the cards?
15CARD_WIDTH = 140 * CARD_SCALE
16CARD_HEIGHT = 190 * CARD_SCALE
17
18# How big is the mat we'll place the card on?
19MAT_PERCENT_OVERSIZE = 1.25
20MAT_HEIGHT = int(CARD_HEIGHT * MAT_PERCENT_OVERSIZE)
21MAT_WIDTH = int(CARD_WIDTH * MAT_PERCENT_OVERSIZE)
22
23# How much space do we leave as a gap between the mats?
24# Done as a percent of the mat size.
25VERTICAL_MARGIN_PERCENT = 0.10
26HORIZONTAL_MARGIN_PERCENT = 0.10
27
28# The Y of the bottom row (2 piles)
29BOTTOM_Y = MAT_HEIGHT / 2 + MAT_HEIGHT * VERTICAL_MARGIN_PERCENT
30
31# The X of where to start putting things on the left side
32START_X = MAT_WIDTH / 2 + MAT_WIDTH * HORIZONTAL_MARGIN_PERCENT
33
34# The Y of the top row (4 piles)
35TOP_Y = SCREEN_HEIGHT - MAT_HEIGHT / 2 - MAT_HEIGHT * VERTICAL_MARGIN_PERCENT
36
37# The Y of the middle row (7 piles)
38MIDDLE_Y = TOP_Y - MAT_HEIGHT - MAT_HEIGHT * VERTICAL_MARGIN_PERCENT
39
40# How far apart each pile goes
41X_SPACING = MAT_WIDTH + MAT_WIDTH * HORIZONTAL_MARGIN_PERCENT
42
43# Card constants
44CARD_VALUES = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
45CARD_SUITS = ["Clubs", "Hearts", "Spades", "Diamonds"]
46
47
48class Card(arcade.Sprite):
49 """ Card sprite """
50
51 def __init__(self, suit, value, scale=1):
52 """ Card constructor """
53
54 # Attributes for suit and value
55 self.suit = suit
56 self.value = value
57
58 # Image to use for the sprite when face up
59 self.image_file_name = f":resources:images/cards/card{self.suit}{self.value}.png"
60
61 # Call the parent
62 super().__init__(self.image_file_name, scale, hit_box_algorithm="None")
63
64
65class MyGame(arcade.Window):
66 """ Main application class. """
67
68 def __init__(self):
69 super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
70
71 # Sprite list with all the cards, no matter what pile they are in.
72 self.card_list = None
73
74 self.background_color = arcade.color.AMAZON
75
76 # List of cards we are dragging with the mouse
77 self.held_cards = None
78
79 # Original location of cards we are dragging with the mouse in case
80 # they have to go back.
81 self.held_cards_original_position = None
82
83 # Sprite list with all the mats tha cards lay on.
84 self.pile_mat_list = None
85
86 def setup(self):
87 """ Set up the game here. Call this function to restart the game. """
88
89 # List of cards we are dragging with the mouse
90 self.held_cards = []
91
92 # Original location of cards we are dragging with the mouse in case
93 # they have to go back.
94 self.held_cards_original_position = []
95
96 # --- Create the mats the cards go on.
97
98 # Sprite list with all the mats tha cards lay on.
99 self.pile_mat_list: arcade.SpriteList = arcade.SpriteList()
100
101 # Create the mats for the bottom face down and face up piles
102 pile = arcade.SpriteSolidColor(MAT_WIDTH, MAT_HEIGHT, arcade.csscolor.DARK_OLIVE_GREEN)
103 pile.position = START_X, BOTTOM_Y
104 self.pile_mat_list.append(pile)
105
106 pile = arcade.SpriteSolidColor(MAT_WIDTH, MAT_HEIGHT, arcade.csscolor.DARK_OLIVE_GREEN)
107 pile.position = START_X + X_SPACING, BOTTOM_Y
108 self.pile_mat_list.append(pile)
109
110 # Create the seven middle piles
111 for i in range(7):
112 pile = arcade.SpriteSolidColor(MAT_WIDTH, MAT_HEIGHT, arcade.csscolor.DARK_OLIVE_GREEN)
113 pile.position = START_X + i * X_SPACING, MIDDLE_Y
114 self.pile_mat_list.append(pile)
115
116 # Create the top "play" piles
117 for i in range(4):
118 pile = arcade.SpriteSolidColor(MAT_WIDTH, MAT_HEIGHT, arcade.csscolor.DARK_OLIVE_GREEN)
119 pile.position = START_X + i * X_SPACING, TOP_Y
120 self.pile_mat_list.append(pile)
121
122 # Sprite list with all the cards, no matter what pile they are in.
123 self.card_list = arcade.SpriteList()
124
125 # Create every card
126 for card_suit in CARD_SUITS:
127 for card_value in CARD_VALUES:
128 card = Card(card_suit, card_value, CARD_SCALE)
129 card.position = START_X, BOTTOM_Y
130 self.card_list.append(card)
131
132 def on_draw(self):
133 """ Render the screen. """
134 # Clear the screen
135 self.clear()
136
137 # Draw the mats the cards go on to
138 self.pile_mat_list.draw()
139
140 # Draw the cards
141 self.card_list.draw()
142
143 def pull_to_top(self, card):
144 """ Pull card to top of rendering order (last to render, looks on-top) """
145 # Find the index of the card
146 index = self.card_list.index(card)
147 # Loop and pull all the other cards down towards the zero end
148 for i in range(index, len(self.card_list) - 1):
149 self.card_list[i] = self.card_list[i + 1]
150 # Put this card at the right-side/top/size of list
151 self.card_list[len(self.card_list) - 1] = card
152
153 def on_mouse_press(self, x, y, button, key_modifiers):
154 """ Called when the user presses a mouse button. """
155
156 # Get list of cards we've clicked on
157 cards = arcade.get_sprites_at_point((x, y), self.card_list)
158
159 # Have we clicked on a card?
160 if len(cards) > 0:
161
162 # Might be a stack of cards, get the top one
163 primary_card = cards[-1]
164
165 # All other cases, grab the face-up card we are clicking on
166 self.held_cards = [primary_card]
167 # Save the position
168 self.held_cards_original_position = [self.held_cards[0].position]
169 # Put on top in drawing order
170 self.pull_to_top(self.held_cards[0])
171
172 def on_mouse_release(self, x: float, y: float, button: int,
173 modifiers: int):
174 """ Called when the user presses a mouse button. """
175
176 # If we don't have any cards, who cares
177 if len(self.held_cards) == 0:
178 return
179
180 # Find the closest pile, in case we are in contact with more than one
181 pile, distance = arcade.get_closest_sprite(self.held_cards[0], self.pile_mat_list)
182 reset_position = True
183
184 # See if we are in contact with the closest pile
185 if arcade.check_for_collision(self.held_cards[0], pile):
186
187 # For each held card, move it to the pile we dropped on
188 for i, dropped_card in enumerate(self.held_cards):
189 # Move cards to proper position
190 dropped_card.position = pile.center_x, pile.center_y
191
192 # Success, don't reset position of cards
193 reset_position = False
194
195 # Release on top play pile? And only one card held?
196 if reset_position:
197 # Where-ever we were dropped, it wasn't valid. Reset the each card's position
198 # to its original spot.
199 for pile_index, card in enumerate(self.held_cards):
200 card.position = self.held_cards_original_position[pile_index]
201
202 # We are no longer holding cards
203 self.held_cards = []
204
205 def on_mouse_motion(self, x: float, y: float, dx: float, dy: float):
206 """ User moves mouse """
207
208 # If we are holding cards, move them with the mouse
209 for card in self.held_cards:
210 card.center_x += dx
211 card.center_y += dy
212
213
214def main():
215 """ Main function """
216 window = MyGame()
217 window.setup()
218 arcade.run()
219
220
221if __name__ == "__main__":
222 main()