solitaire_04.py Diff
solitaire_04.py
--- /home/docs/checkouts/readthedocs.org/user_builds/arcade-library/checkouts/latest/doc/tutorials/card_game/solitaire_03.py
+++ /home/docs/checkouts/readthedocs.org/user_builds/arcade-library/checkouts/latest/doc/tutorials/card_game/solitaire_04.py
@@ -31,6 +31,15 @@
# The X of where to start putting things on the left side
START_X = MAT_WIDTH / 2 + MAT_WIDTH * HORIZONTAL_MARGIN_PERCENT
+# The Y of the top row (4 piles)
+TOP_Y = SCREEN_HEIGHT - MAT_HEIGHT / 2 - MAT_HEIGHT * VERTICAL_MARGIN_PERCENT
+
+# The Y of the middle row (7 piles)
+MIDDLE_Y = TOP_Y - MAT_HEIGHT - MAT_HEIGHT * VERTICAL_MARGIN_PERCENT
+
+# How far apart each pile goes
+X_SPACING = MAT_WIDTH + MAT_WIDTH * HORIZONTAL_MARGIN_PERCENT
+
# Card constants
CARD_VALUES = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
CARD_SUITS = ["Clubs", "Hearts", "Spades", "Diamonds"]
@@ -71,6 +80,9 @@
# they have to go back.
self.held_cards_original_position = None
+ # Sprite list with all the mats tha cards lay on.
+ self.pile_mat_list = None
+
def setup(self):
""" Set up the game here. Call this function to restart the game. """
@@ -80,6 +92,32 @@
# Original location of cards we are dragging with the mouse in case
# they have to go back.
self.held_cards_original_position = []
+
+ # --- Create the mats the cards go on.
+
+ # Sprite list with all the mats tha cards lay on.
+ self.pile_mat_list: arcade.SpriteList = arcade.SpriteList()
+
+ # Create the mats for the bottom face down and face up piles
+ pile = arcade.SpriteSolidColor(MAT_WIDTH, MAT_HEIGHT, arcade.csscolor.DARK_OLIVE_GREEN)
+ pile.position = START_X, BOTTOM_Y
+ self.pile_mat_list.append(pile)
+
+ pile = arcade.SpriteSolidColor(MAT_WIDTH, MAT_HEIGHT, arcade.csscolor.DARK_OLIVE_GREEN)
+ pile.position = START_X + X_SPACING, BOTTOM_Y
+ self.pile_mat_list.append(pile)
+
+ # Create the seven middle piles
+ for i in range(7):
+ pile = arcade.SpriteSolidColor(MAT_WIDTH, MAT_HEIGHT, arcade.csscolor.DARK_OLIVE_GREEN)
+ pile.position = START_X + i * X_SPACING, MIDDLE_Y
+ self.pile_mat_list.append(pile)
+
+ # Create the top "play" piles
+ for i in range(4):
+ pile = arcade.SpriteSolidColor(MAT_WIDTH, MAT_HEIGHT, arcade.csscolor.DARK_OLIVE_GREEN)
+ pile.position = START_X + i * X_SPACING, TOP_Y
+ self.pile_mat_list.append(pile)
# Sprite list with all the cards, no matter what pile they are in.
self.card_list = arcade.SpriteList()
@@ -96,15 +134,21 @@
# Clear the screen
self.clear()
+ # Draw the mats the cards go on to
+ self.pile_mat_list.draw()
+
# Draw the cards
self.card_list.draw()
- def pull_to_top(self, card: arcade.Sprite):
+ def pull_to_top(self, card):
""" Pull card to top of rendering order (last to render, looks on-top) """
-
- # Remove, and append to the end
- self.card_list.remove(card)
- self.card_list.append(card)
+ # Find the index of the card
+ index = self.card_list.index(card)
+ # Loop and pull all the other cards down towards the zero end
+ for i in range(index, len(self.card_list) - 1):
+ self.card_list[i] = self.card_list[i + 1]
+ # Put this card at the right-side/top/size of list
+ self.card_list[len(self.card_list) - 1] = card
def on_mouse_press(self, x, y, button, key_modifiers):
""" Called when the user presses a mouse button. """