solitaire_03.py Full Listing#

solitaire_03.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# Card constants
 35CARD_VALUES = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
 36CARD_SUITS = ["Clubs", "Hearts", "Spades", "Diamonds"]
 37
 38
 39class Card(arcade.Sprite):
 40    """ Card sprite """
 41
 42    def __init__(self, suit, value, scale=1):
 43        """ Card constructor """
 44
 45        # Attributes for suit and value
 46        self.suit = suit
 47        self.value = value
 48
 49        # Image to use for the sprite when face up
 50        self.image_file_name = f":resources:images/cards/card{self.suit}{self.value}.png"
 51
 52        # Call the parent
 53        super().__init__(self.image_file_name, scale, hit_box_algorithm="None")
 54
 55
 56class MyGame(arcade.Window):
 57    """ Main application class. """
 58
 59    def __init__(self):
 60        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
 61
 62        # Sprite list with all the cards, no matter what pile they are in.
 63        self.card_list = None
 64
 65        self.background_color = arcade.color.AMAZON
 66
 67        # List of cards we are dragging with the mouse
 68        self.held_cards = None
 69
 70        # Original location of cards we are dragging with the mouse in case
 71        # they have to go back.
 72        self.held_cards_original_position = None
 73
 74    def setup(self):
 75        """ Set up the game here. Call this function to restart the game. """
 76
 77        # List of cards we are dragging with the mouse
 78        self.held_cards = []
 79
 80        # Original location of cards we are dragging with the mouse in case
 81        # they have to go back.
 82        self.held_cards_original_position = []
 83
 84        # Sprite list with all the cards, no matter what pile they are in.
 85        self.card_list = arcade.SpriteList()
 86
 87        # Create every card
 88        for card_suit in CARD_SUITS:
 89            for card_value in CARD_VALUES:
 90                card = Card(card_suit, card_value, CARD_SCALE)
 91                card.position = START_X, BOTTOM_Y
 92                self.card_list.append(card)
 93
 94    def on_draw(self):
 95        """ Render the screen. """
 96        # Clear the screen
 97        self.clear()
 98
 99        # Draw the cards
100        self.card_list.draw()
101
102    def pull_to_top(self, card: arcade.Sprite):
103        """ Pull card to top of rendering order (last to render, looks on-top) """
104
105        # Remove, and append to the end
106        self.card_list.remove(card)
107        self.card_list.append(card)
108
109    def on_mouse_press(self, x, y, button, key_modifiers):
110        """ Called when the user presses a mouse button. """
111
112        # Get list of cards we've clicked on
113        cards = arcade.get_sprites_at_point((x, y), self.card_list)
114
115        # Have we clicked on a card?
116        if len(cards) > 0:
117
118            # Might be a stack of cards, get the top one
119            primary_card = cards[-1]
120
121            # All other cases, grab the face-up card we are clicking on
122            self.held_cards = [primary_card]
123            # Save the position
124            self.held_cards_original_position = [self.held_cards[0].position]
125            # Put on top in drawing order
126            self.pull_to_top(self.held_cards[0])
127
128    def on_mouse_release(self, x: float, y: float, button: int,
129                         modifiers: int):
130        """ Called when the user presses a mouse button. """
131
132        # If we don't have any cards, who cares
133        if len(self.held_cards) == 0:
134            return
135
136        # We are no longer holding cards
137        self.held_cards = []
138
139    def on_mouse_motion(self, x: float, y: float, dx: float, dy: float):
140        """ User moves mouse """
141
142        # If we are holding cards, move them with the mouse
143        for card in self.held_cards:
144            card.center_x += dx
145            card.center_y += dy
146
147
148def main():
149    """ Main function """
150    window = MyGame()
151    window.setup()
152    arcade.run()
153
154
155if __name__ == "__main__":
156    main()