solitaire_02.py Diff
solitaire_02.py
--- /home/docs/checkouts/readthedocs.org/user_builds/arcade-library/checkouts/latest/doc/tutorials/card_game/solitaire_01.py
+++ /home/docs/checkouts/readthedocs.org/user_builds/arcade-library/checkouts/latest/doc/tutorials/card_game/solitaire_02.py
@@ -8,6 +8,50 @@
SCREEN_HEIGHT = 768
SCREEN_TITLE = "Drag and Drop Cards"
+# Constants for sizing
+CARD_SCALE = 0.6
+
+# How big are the cards?
+CARD_WIDTH = 140 * CARD_SCALE
+CARD_HEIGHT = 190 * CARD_SCALE
+
+# How big is the mat we'll place the card on?
+MAT_PERCENT_OVERSIZE = 1.25
+MAT_HEIGHT = int(CARD_HEIGHT * MAT_PERCENT_OVERSIZE)
+MAT_WIDTH = int(CARD_WIDTH * MAT_PERCENT_OVERSIZE)
+
+# How much space do we leave as a gap between the mats?
+# Done as a percent of the mat size.
+VERTICAL_MARGIN_PERCENT = 0.10
+HORIZONTAL_MARGIN_PERCENT = 0.10
+
+# The Y of the bottom row (2 piles)
+BOTTOM_Y = MAT_HEIGHT / 2 + MAT_HEIGHT * VERTICAL_MARGIN_PERCENT
+
+# The X of where to start putting things on the left side
+START_X = MAT_WIDTH / 2 + 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"]
+
+
+class Card(arcade.Sprite):
+ """ Card sprite """
+
+ def __init__(self, suit, value, scale=1):
+ """ Card constructor """
+
+ # Attributes for suit and value
+ self.suit = suit
+ self.value = value
+
+ # Image to use for the sprite when face up
+ self.image_file_name = f":resources:images/cards/card{self.suit}{self.value}.png"
+
+ # Call the parent
+ super().__init__(self.image_file_name, scale, hit_box_algorithm="None")
+
class MyGame(arcade.Window):
""" Main application class. """
@@ -15,16 +59,31 @@
def __init__(self):
super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
+ # Sprite list with all the cards, no matter what pile they are in.
+ self.card_list = None
+
self.background_color = arcade.color.AMAZON
def setup(self):
""" Set up the game here. Call this function to restart the game. """
- pass
+
+ # Sprite list with all the cards, no matter what pile they are in.
+ self.card_list = arcade.SpriteList()
+
+ # Create every card
+ for card_suit in CARD_SUITS:
+ for card_value in CARD_VALUES:
+ card = Card(card_suit, card_value, CARD_SCALE)
+ card.position = START_X, BOTTOM_Y
+ self.card_list.append(card)
def on_draw(self):
""" Render the screen. """
# Clear the screen
self.clear()
+
+ # Draw the cards
+ self.card_list.draw()
def on_mouse_press(self, x, y, button, key_modifiers):
""" Called when the user presses a mouse button. """