Hi,

I have the following code working fine. What I would like to try is when n cards are randomly selected, to print the cards in a graphics window.

I want to add a new class draw(self,win,center) that displays the card.

I have a set of bmp cards.

# Import random
from random import*

# Create the Card class
class Card:
       def __init__(self, rank, suit):

        self.rank = rank
        self.suit = suit
        self.ranks = [None, "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"]
        self.suits = ["Spades","Diamonds","clubs","hearts"]
        self.BJ = [None, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
        

    def getRank(self):
            return self.rank

    def getSuit(self):
               return self.suit

    def BJValue(self):
           return 0 ## temporary

    def __str__(self):
              return "%s of %s (%s)" % (self.ranks[self.rank],self.suits[self.suit], self.BJ[self.rank])

def main():
    print __doc__
       n = input("Enter the number of cards to draw >>")

    for i in range(n):
        rankCard = randrange(1,13)
        suitCard = randrange(0,3)

       
        NumCards = Card(rankCard,suitCard)

      
        if rankCard == 1:
            print NumCards,"but it can be (1) also"
                 
        else:
            print NumCards

Rgds
macca1111

I've only been using python for about a month, so I'm not sure how to do this with python itself, but have you thought about using PyGame for projects like this?

In this case, a module such as tkinter would be much more useful since it doesn't contain any animation. TKinter is probably your best bet, sicne you can create a simple GUI interface for the whole program with it.

I'm guessing you're trying to create a blackjack game, correct? Instead of creating a totally random card, you can create hand and deck classes so that you have a set of 52 cards in play without duplicates.

It would look something like this:

(Inspired by code by in Python Programming for the absolute beginner by Micheal Dawson, chapter nine)

class Hand(object):
    """ A hand of playing cards. """
    def __init__(self):
            self.cards = []

    def __str__(self):
        if self.cards:
            rep = ""
            for card in self.cards:
                rep += str(card) + "\t"
        else:
            rep = "<empty>"
        return rep

    def clear(self):
        self.cards = []

    def add(self, card):
        self.cards.append(card)

    def give(self, card, other_hand):
        self.cards.remove(card)
        other_hand.add(card)

#The Deck class is inherited from the Hand class. So it retains all #methods and attributes except for those that it overrides.
class Deck(Hand):
    def populate(self):
        for suit in Card.SUITS:
            for rank in Card.RANKS:
                self.add(Card(rank, suit))

    def shuffle(self):
        import random
        random.shuffle(self.cards)

    def deal(self, hands, per_hand = 1):
        for rounds in range(per_hand):
            for hand in hands:
                if self.cards:
                    top_card = self.cards[0]
                    self.give(top_card, hand)
                else:
                    print "Can't continue deal. Out of cards!"

The project for the chapter in that book is in fact a text based but rather in depth black jack game. (and is a rather dang good book besides) so it's well worth it to pick it up.

In this case, a module such as tkinter would be much more useful since it doesn't contain any animation. TKinter is probably your best bet, sicne you can create a simple GUI interface for the whole program with it.

I'm guessing you're trying to create a blackjack game, correct? Instead of creating a totally random card, you can create hand and deck classes so that you have a set of 52 cards in play without duplicates.

It would look something like this:

(Inspired by code by in Python Programming for the absolute beginner by Micheal Dawson, chapter nine)

class Hand(object):
    """ A hand of playing cards. """
    def __init__(self):
            self.cards = []
 
    def __str__(self):
        if self.cards:
            rep = ""
            for card in self.cards:
                rep += str(card) + "\t"
        else:
            rep = "<empty>"
        return rep
 
    def clear(self):
        self.cards = []
 
    def add(self, card):
        self.cards.append(card)
 
    def give(self, card, other_hand):
        self.cards.remove(card)
        other_hand.add(card)
 
#The Deck class is inherited from the Hand class. So it retains all #methods and attributes except for those that it overrides.
class Deck(Hand):
    def populate(self):
        for suit in Card.SUITS:
            for rank in Card.RANKS:
                self.add(Card(rank, suit))
 
    def shuffle(self):
        import random
        random.shuffle(self.cards)
 
    def deal(self, hands, per_hand = 1):
        for rounds in range(per_hand):
            for hand in hands:
                if self.cards:
                    top_card = self.cards[0]
                    self.give(top_card, hand)
                else:
                    print "Can't continue deal. Out of cards!"

The project for the chapter in that book is in fact a text based but rather in depth black jack game. (and is a rather dang good book besides) so it's well worth it to pick it up.

Zonr_0,

I happened to go to a bookshop yesterday and purchased the book 'Python Programming, for the absolute beginner' given the chapter on classes is much easier to understand than the text book I'm using now.

Your comments are easier now to understand given I am starting to understand the language.

Thanks Zonr_0:cheesy: :cheesy: :cheesy:

Zonr_0,

I happened to go to a bookshop yesterday and purchased the book 'Python Programming, for the absolute beginner' given the chapter on classes is much easier to understand than the text book I'm using now.

Your comments are easier now to understand given I am starting to understand the language.

Thanks Zonr_0:cheesy: :cheesy: :cheesy:

Is it easier to create a new class with the following in it:

draw(self, win, center)

and use this to call a bmp picture of a card?


macca1111

I haven't done much GUI work to be honest, but I'm PRETTY sure tkinter has a canvas tool, and you could put that into your card class. Anybody else want to chime in on that?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.