Example #1
0
    def __init__(self, suit, value, canvas):
        """Card constructor.

        Arguments are the card's suit and value, and the canvas widget.

        The card is created at position (0, 0), with its face down
        (adding it to a stack will position it according to that
        stack's rules).

        """
        self.suit = suit
        self.value = value
        self.color = COLOR[suit]
        self.face_shown = 0

        self.x = self.y = 0
        self.canvas = canvas
        self.group = Group(canvas)

        text = "%s  %s" % (VALNAMES[value], suit)
        self.__text = canvas.create_text(CARDWIDTH // 2, 0, anchor=N,
                                         fill=self.color, text=text)
        self.group.addtag_withtag(self.__text)

        self.__rect = canvas.create_rectangle(0, 0, CARDWIDTH, CARDHEIGHT,
                                              outline='black', fill='white')
        self.group.addtag_withtag(self.__rect)

        self.__back = canvas.create_rectangle(MARGIN, MARGIN,
                                              CARDWIDTH - MARGIN,
                                              CARDHEIGHT - MARGIN,
                                              outline='black', fill='blue')
        self.group.addtag_withtag(self.__back)
Example #2
0
    def __init__(self, x, y, game=None):
        """Stack constructor.

        Arguments are the stack's nominal x and y position (the top
        left corner of the first card placed in the stack), and the
        game object (which is used to get the canvas; subclasses use
        the game object to find other stacks).

        """
        self.x = x
        self.y = y
        self.game = game
        self.cards = []
        self.group = Group(self.game.canvas)
        self.group.bind('<1>', self.clickhandler)
        self.group.bind('<Double-1>', self.doubleclickhandler)
        self.group.bind('<B1-Motion>', self.motionhandler)
        self.group.bind('<ButtonRelease-1>', self.releasehandler)
        self.makebottom()
Example #3
0
    def __init__(self, suit, value, canvas):
        """Card constructor.

        Arguments are the card's suit and value, and the canvas widget.

        The card is created at position (0, 0), with its face down
        (adding it to a stack will position it according to that
        stack's rules).

        """
        self.suit = suit
        self.value = value
        self.color = COLOR[suit]
        self.face_shown = 0

        self.x = self.y = 0
        self.canvas = canvas
        self.group = Group(canvas)

        text = "%s  %s" % (VALNAMES[value], suit)
        self.__text = canvas.create_text(CARDWIDTH // 2,
                                         0,
                                         anchor=N,
                                         fill=self.color,
                                         text=text)
        self.group.addtag_withtag(self.__text)

        self.__rect = canvas.create_rectangle(0,
                                              0,
                                              CARDWIDTH,
                                              CARDHEIGHT,
                                              outline='black',
                                              fill='white')
        self.group.addtag_withtag(self.__rect)

        self.__back = canvas.create_rectangle(MARGIN,
                                              MARGIN,
                                              CARDWIDTH - MARGIN,
                                              CARDHEIGHT - MARGIN,
                                              outline='black',
                                              fill='blue')
        self.group.addtag_withtag(self.__back)
Example #4
0
    def __init__(self, x, y, game=None):
        """Stack constructor.

        Arguments are the stack's nominal x and y position (the top
        left corner of the first card placed in the stack), and the
        game object (which is used to get the canvas; subclasses use
        the game object to find other stacks).

        """
        self.x = x
        self.y = y
        self.game = game
        self.cards = []
        self.group = Group(self.game.canvas)
        self.group.bind('<1>', self.clickhandler)
        self.group.bind('<Double-1>', self.doubleclickhandler)
        self.group.bind('<B1-Motion>', self.motionhandler)
        self.group.bind('<ButtonRelease-1>', self.releasehandler)
        self.makebottom()
Example #5
0
class Stack:

    """A generic stack of cards.

    This is used as a base class for all other stacks (e.g. the deck,
    the suit stacks, and the row stacks).

    Public methods:

    add(card) -- add a card to the stack
    delete(card) -- delete a card from the stack
    showtop() -- show the top card (if any) face up
    deal() -- delete and return the top card, or None if empty

    Method that subclasses may override:

    position(card) -- move the card to its proper (x, y) position

        The default position() method places all cards at the stack's
        own (x, y) position.

    userclickhandler(), userdoubleclickhandler() -- called to do
    subclass specific things on single and double clicks

        The default user (single) click handler shows the top card
        face up.  The default user double click handler calls the user
        single click handler.

    usermovehandler(cards) -- called to complete a subpile move

        The default user move handler moves all moved cards back to
        their original position (by calling the position() method).

    Private methods:

    clickhandler(event), doubleclickhandler(event),
    motionhandler(event), releasehandler(event) -- event handlers

        The default event handlers turn the top card of the stack with
        its face up on a (single or double) click, and also support
        moving a subpile around.

    startmoving(event) -- begin a move operation
    finishmoving() -- finish a move operation

    """

    def __init__(self, x, y, game=None):
        """Stack constructor.

        Arguments are the stack's nominal x and y position (the top
        left corner of the first card placed in the stack), and the
        game object (which is used to get the canvas; subclasses use
        the game object to find other stacks).

        """
        self.x = x
        self.y = y
        self.game = game
        self.cards = []
        self.group = Group(self.game.canvas)
        self.group.bind('<1>', self.clickhandler)
        self.group.bind('<Double-1>', self.doubleclickhandler)
        self.group.bind('<B1-Motion>', self.motionhandler)
        self.group.bind('<ButtonRelease-1>', self.releasehandler)
        self.makebottom()

    def makebottom(self):
        pass

    def __repr__(self):
        """Return a string for debug print statements."""
        return "%s(%d, %d)" % (self.__class__.__name__, self.x, self.y)

    # Public methods

    def add(self, card):
        self.cards.append(card)
        card.tkraise()
        self.position(card)
        self.group.addtag_withtag(card.group)

    def delete(self, card):
        self.cards.remove(card)
        card.group.dtag(self.group)

    def showtop(self):
        if self.cards:
            self.cards[-1].showface()

    def deal(self):
        if not self.cards:
            return None
        card = self.cards[-1]
        self.delete(card)
        return card

    # Subclass overridable methods

    def position(self, card):
        card.moveto(self.x, self.y)

    def userclickhandler(self):
        self.showtop()

    def userdoubleclickhandler(self):
        self.userclickhandler()

    def usermovehandler(self, cards):
        for card in cards:
            self.position(card)

    # Event handlers

    def clickhandler(self, event):
        self.finishmoving()             # In case we lost an event
        self.userclickhandler()
        self.startmoving(event)

    def motionhandler(self, event):
        self.keepmoving(event)

    def releasehandler(self, event):
        self.keepmoving(event)
        self.finishmoving()

    def doubleclickhandler(self, event):
        self.finishmoving()             # In case we lost an event
        self.userdoubleclickhandler()
        self.startmoving(event)

    # Move internals

    moving = None

    def startmoving(self, event):
        self.moving = None
        tags = self.game.canvas.gettags('current')
        for i in range(len(self.cards)):
            card = self.cards[i]
            if card.group.tag in tags:
                break
        else:
            return
        if not card.face_shown:
            return
        self.moving = self.cards[i:]
        self.lastx = event.x
        self.lasty = event.y
        for card in self.moving:
            card.tkraise()

    def keepmoving(self, event):
        if not self.moving:
            return
        dx = event.x - self.lastx
        dy = event.y - self.lasty
        self.lastx = event.x
        self.lasty = event.y
        if dx or dy:
            for card in self.moving:
                card.moveby(dx, dy)

    def finishmoving(self):
        cards = self.moving
        self.moving = None
        if cards:
            self.usermovehandler(cards)
Example #6
0
class Card:

    """A playing card.

    A card doesn't record to which stack it belongs; only the stack
    records this (it turns out that we always know this from the
    context, and this saves a ``double update'' with potential for
    inconsistencies).

    Public methods:

    moveto(x, y) -- move the card to an absolute position
    moveby(dx, dy) -- move the card by a relative offset
    tkraise() -- raise the card to the top of its stack
    showface(), showback() -- turn the card face up or down & raise it

    Public read-only instance variables:

    suit, value, color -- the card's suit, value and color
    face_shown -- true when the card is shown face up, else false

    Semi-public read-only instance variables (XXX should be made
    private):

    group -- the Canvas.Group representing the card
    x, y -- the position of the card's top left corner

    Private instance variables:

    __back, __rect, __text -- the canvas items making up the card

    (To show the card face up, the text item is placed in front of
    rect and the back is placed behind it.  To show it face down, this
    is reversed.  The card is created face down.)

    """

    def __init__(self, suit, value, canvas):
        """Card constructor.

        Arguments are the card's suit and value, and the canvas widget.

        The card is created at position (0, 0), with its face down
        (adding it to a stack will position it according to that
        stack's rules).

        """
        self.suit = suit
        self.value = value
        self.color = COLOR[suit]
        self.face_shown = 0

        self.x = self.y = 0
        self.canvas = canvas
        self.group = Group(canvas)

        text = "%s  %s" % (VALNAMES[value], suit)
        self.__text = canvas.create_text(CARDWIDTH // 2, 0, anchor=N,
                                         fill=self.color, text=text)
        self.group.addtag_withtag(self.__text)

        self.__rect = canvas.create_rectangle(0, 0, CARDWIDTH, CARDHEIGHT,
                                              outline='black', fill='white')
        self.group.addtag_withtag(self.__rect)

        self.__back = canvas.create_rectangle(MARGIN, MARGIN,
                                              CARDWIDTH - MARGIN,
                                              CARDHEIGHT - MARGIN,
                                              outline='black', fill='blue')
        self.group.addtag_withtag(self.__back)

    def __repr__(self):
        """Return a string for debug print statements."""
        return "Card(%r, %r)" % (self.suit, self.value)

    def moveto(self, x, y):
        """Move the card to absolute position (x, y)."""
        self.moveby(x - self.x, y - self.y)

    def moveby(self, dx, dy):
        """Move the card by (dx, dy)."""
        self.x = self.x + dx
        self.y = self.y + dy
        self.group.move(dx, dy)

    def tkraise(self):
        """Raise the card above all other objects in its canvas."""
        self.group.tkraise()

    def showface(self):
        """Turn the card's face up."""
        self.tkraise()
        self.canvas.tag_raise(self.__rect)
        self.canvas.tag_raise(self.__text)
        self.face_shown = 1

    def showback(self):
        """Turn the card's face down."""
        self.tkraise()
        self.canvas.tag_raise(self.__rect)
        self.canvas.tag_raise(self.__back)
        self.face_shown = 0
Example #7
0
class Card:
    """A playing card.

    A card doesn't record to which stack it belongs; only the stack
    records this (it turns out that we always know this from the
    context, and this saves a ``double update'' with potential for
    inconsistencies).

    Public methods:

    moveto(x, y) -- move the card to an absolute position
    moveby(dx, dy) -- move the card by a relative offset
    tkraise() -- raise the card to the top of its stack
    showface(), showback() -- turn the card face up or down & raise it

    Public read-only instance variables:

    suit, value, color -- the card's suit, value and color
    face_shown -- true when the card is shown face up, else false

    Semi-public read-only instance variables (XXX should be made
    private):

    group -- the Canvas.Group representing the card
    x, y -- the position of the card's top left corner

    Private instance variables:

    __back, __rect, __text -- the canvas items making up the card

    (To show the card face up, the text item is placed in front of
    rect and the back is placed behind it.  To show it face down, this
    is reversed.  The card is created face down.)

    """
    def __init__(self, suit, value, canvas):
        """Card constructor.

        Arguments are the card's suit and value, and the canvas widget.

        The card is created at position (0, 0), with its face down
        (adding it to a stack will position it according to that
        stack's rules).

        """
        self.suit = suit
        self.value = value
        self.color = COLOR[suit]
        self.face_shown = 0

        self.x = self.y = 0
        self.canvas = canvas
        self.group = Group(canvas)

        text = "%s  %s" % (VALNAMES[value], suit)
        self.__text = canvas.create_text(CARDWIDTH // 2,
                                         0,
                                         anchor=N,
                                         fill=self.color,
                                         text=text)
        self.group.addtag_withtag(self.__text)

        self.__rect = canvas.create_rectangle(0,
                                              0,
                                              CARDWIDTH,
                                              CARDHEIGHT,
                                              outline='black',
                                              fill='white')
        self.group.addtag_withtag(self.__rect)

        self.__back = canvas.create_rectangle(MARGIN,
                                              MARGIN,
                                              CARDWIDTH - MARGIN,
                                              CARDHEIGHT - MARGIN,
                                              outline='black',
                                              fill='blue')
        self.group.addtag_withtag(self.__back)

    def __repr__(self):
        """Return a string for debug print statements."""
        return "Card(%r, %r)" % (self.suit, self.value)

    def moveto(self, x, y):
        """Move the card to absolute position (x, y)."""
        self.moveby(x - self.x, y - self.y)

    def moveby(self, dx, dy):
        """Move the card by (dx, dy)."""
        self.x = self.x + dx
        self.y = self.y + dy
        self.group.move(dx, dy)

    def tkraise(self):
        """Raise the card above all other objects in its canvas."""
        self.group.tkraise()

    def showface(self):
        """Turn the card's face up."""
        self.tkraise()
        self.canvas.tag_raise(self.__rect)
        self.canvas.tag_raise(self.__text)
        self.face_shown = 1

    def showback(self):
        """Turn the card's face down."""
        self.tkraise()
        self.canvas.tag_raise(self.__rect)
        self.canvas.tag_raise(self.__back)
        self.face_shown = 0
Example #8
0
class Stack:
    """A generic stack of cards.

    This is used as a base class for all other stacks (e.g. the deck,
    the suit stacks, and the row stacks).

    Public methods:

    add(card) -- add a card to the stack
    delete(card) -- delete a card from the stack
    showtop() -- show the top card (if any) face up
    deal() -- delete and return the top card, or None if empty

    Method that subclasses may override:

    position(card) -- move the card to its proper (x, y) position

        The default position() method places all cards at the stack's
        own (x, y) position.

    userclickhandler(), userdoubleclickhandler() -- called to do
    subclass specific things on single and double clicks

        The default user (single) click handler shows the top card
        face up.  The default user double click handler calls the user
        single click handler.

    usermovehandler(cards) -- called to complete a subpile move

        The default user move handler moves all moved cards back to
        their original position (by calling the position() method).

    Private methods:

    clickhandler(event), doubleclickhandler(event),
    motionhandler(event), releasehandler(event) -- event handlers

        The default event handlers turn the top card of the stack with
        its face up on a (single or double) click, and also support
        moving a subpile around.

    startmoving(event) -- begin a move operation
    finishmoving() -- finish a move operation

    """
    def __init__(self, x, y, game=None):
        """Stack constructor.

        Arguments are the stack's nominal x and y position (the top
        left corner of the first card placed in the stack), and the
        game object (which is used to get the canvas; subclasses use
        the game object to find other stacks).

        """
        self.x = x
        self.y = y
        self.game = game
        self.cards = []
        self.group = Group(self.game.canvas)
        self.group.bind('<1>', self.clickhandler)
        self.group.bind('<Double-1>', self.doubleclickhandler)
        self.group.bind('<B1-Motion>', self.motionhandler)
        self.group.bind('<ButtonRelease-1>', self.releasehandler)
        self.makebottom()

    def makebottom(self):
        pass

    def __repr__(self):
        """Return a string for debug print statements."""
        return "%s(%d, %d)" % (self.__class__.__name__, self.x, self.y)

    # Public methods

    def add(self, card):
        self.cards.append(card)
        card.tkraise()
        self.position(card)
        self.group.addtag_withtag(card.group)

    def delete(self, card):
        self.cards.remove(card)
        card.group.dtag(self.group)

    def showtop(self):
        if self.cards:
            self.cards[-1].showface()

    def deal(self):
        if not self.cards:
            return None
        card = self.cards[-1]
        self.delete(card)
        return card

    # Subclass overridable methods

    def position(self, card):
        card.moveto(self.x, self.y)

    def userclickhandler(self):
        self.showtop()

    def userdoubleclickhandler(self):
        self.userclickhandler()

    def usermovehandler(self, cards):
        for card in cards:
            self.position(card)

    # Event handlers

    def clickhandler(self, event):
        self.finishmoving()  # In case we lost an event
        self.userclickhandler()
        self.startmoving(event)

    def motionhandler(self, event):
        self.keepmoving(event)

    def releasehandler(self, event):
        self.keepmoving(event)
        self.finishmoving()

    def doubleclickhandler(self, event):
        self.finishmoving()  # In case we lost an event
        self.userdoubleclickhandler()
        self.startmoving(event)

    # Move internals

    moving = None

    def startmoving(self, event):
        self.moving = None
        tags = self.game.canvas.gettags('current')
        for i in range(len(self.cards)):
            card = self.cards[i]
            if card.group.tag in tags:
                break
        else:
            return
        if not card.face_shown:
            return
        self.moving = self.cards[i:]
        self.lastx = event.x
        self.lasty = event.y
        for card in self.moving:
            card.tkraise()

    def keepmoving(self, event):
        if not self.moving:
            return
        dx = event.x - self.lastx
        dy = event.y - self.lasty
        self.lastx = event.x
        self.lasty = event.y
        if dx or dy:
            for card in self.moving:
                card.moveby(dx, dy)

    def finishmoving(self):
        cards = self.moving
        self.moving = None
        if cards:
            self.usermovehandler(cards)