943,954 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 8094
  • Python RSS
Oct 25th, 2006
0

Python - Card Class - Graphics Win

Expand Post »
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.

[php]# 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[/php]
Rgds
macca1111
Last edited by macca1111; Oct 25th, 2006 at 10:17 am. Reason: additional info
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
macca1111 is offline Offline
36 posts
since Oct 2006
Oct 25th, 2006
0

Re: Python - Card Class - Graphics Win

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?
Reputation Points: 10
Solved Threads: 2
Newbie Poster
DarkFlash is offline Offline
13 posts
since Oct 2006
Oct 25th, 2006
0

Re: Python - Card Class - Graphics Win

For how to show the card as an image take a look at:
http://www.daniweb.com/techtalkforum...257347-78.html

This example uses the Tkinter canvas and the canvas.create_image() function.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Oct 26th, 2006
0

Re: Python - Card Class - Graphics Win

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)

[php]
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!"
[/php]

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.
Last edited by Zonr_0; Oct 26th, 2006 at 10:41 pm. Reason: Plugged the book I referenced
Reputation Points: 10
Solved Threads: 3
Newbie Poster
Zonr_0 is offline Offline
16 posts
since Oct 2006
Oct 26th, 2006
0

Re: Python - Card Class - Graphics Win

Click to Expand / Collapse  Quote originally posted by Zonr_0 ...
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)

[php]
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!"
[/php]

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:
Reputation Points: 10
Solved Threads: 0
Light Poster
macca1111 is offline Offline
36 posts
since Oct 2006
Oct 29th, 2006
0

Re: Python - Card Class - Graphics Win

Click to Expand / Collapse  Quote originally posted by macca1111 ...
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
Reputation Points: 10
Solved Threads: 0
Light Poster
macca1111 is offline Offline
36 posts
since Oct 2006
Oct 30th, 2006
0

Re: Python - Card Class - Graphics Win

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?
Reputation Points: 10
Solved Threads: 3
Newbie Poster
Zonr_0 is offline Offline
16 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: tribute project for daniweb
Next Thread in Python Forum Timeline: Python - Importing Data with a Class





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC