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
 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    def setup(self):
 68        """ Set up the game here. Call this function to restart the game. """
 69
 70        # Sprite list with all the cards, no matter what pile they are in.
 71        self.card_list = arcade.SpriteList()
 72
 73        # Create every card
 74        for card_suit in CARD_SUITS:
 75            for card_value in CARD_VALUES:
 76                card = Card(card_suit, card_value, CARD_SCALE)
 77                card.position = START_X, BOTTOM_Y
 78                self.card_list.append(card)
 79
 80    def on_draw(self):
 81        """ Render the screen. """
 82        # Clear the screen
 83        self.clear()
 84
 85        # Draw the cards
 86        self.card_list.draw()
 87
 88    def on_mouse_press(self, x, y, button, key_modifiers):
 89        """ Called when the user presses a mouse button. """
 90        pass
 91
 92    def on_mouse_release(self, x: float, y: float, button: int,
 93                         modifiers: int):
 94        """ Called when the user presses a mouse button. """
 95        pass
 96
 97    def on_mouse_motion(self, x: float, y: float, dx: float, dy: float):
 98        """ User moves mouse """
 99        pass
100
101
102def main():
103    """ Main function """
104    window = MyGame()
105    window.setup()
106    arcade.run()
107
108
109if __name__ == "__main__":
110    main()