solitaire_02.py Full Listing

solitaire_02.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
 38class Card(arcade.Sprite):
 39    """ Card sprite """
 40
 41    def __init__(self, suit, value, scale=1):
 42        """ Card constructor """
 43
 44        # Attributes for suit and value
 45        self.suit = suit
 46        self.value = value
 47
 48        # Image to use for the sprite when face up
 49        self.image_file_name = f":resources:images/cards/card{self.suit}{self.value}.png"
 50
 51        # Call the parent
 52        super().__init__(self.image_file_name, scale, hit_box_algorithm="None")
 53
 54class MyGame(arcade.Window):
 55    """ Main application class. """
 56
 57    def __init__(self):
 58        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
 59
 60        # Sprite list with all the cards, no matter what pile they are in.
 61        self.card_list = None
 62
 63        arcade.set_background_color(arcade.color.AMAZON)
 64
 65    def setup(self):
 66        """ Set up the game here. Call this function to restart the game. """
 67
 68        # Sprite list with all the cards, no matter what pile they are in.
 69        self.card_list = arcade.SpriteList()
 70
 71        # Create every card
 72        for card_suit in CARD_SUITS:
 73            for card_value in CARD_VALUES:
 74                card = Card(card_suit, card_value, CARD_SCALE)
 75                card.position = START_X, BOTTOM_Y
 76                self.card_list.append(card)
 77
 78    def on_draw(self):
 79        """ Render the screen. """
 80        # Clear the screen
 81        arcade.start_render()
 82
 83        # Draw the cards
 84        self.card_list.draw()
 85
 86    def on_mouse_press(self, x, y, button, key_modifiers):
 87        """ Called when the user presses a mouse button. """
 88        pass
 89
 90    def on_mouse_release(self, x: float, y: float, button: int,
 91                         modifiers: int):
 92        """ Called when the user presses a mouse button. """
 93        pass
 94
 95    def on_mouse_motion(self, x: float, y: float, dx: float, dy: float):
 96        """ User moves mouse """
 97        pass
 98
 99
100def main():
101    """ Main method """
102    window = MyGame()
103    window.setup()
104    arcade.run()
105
106
107if __name__ == "__main__":
108    main()