Python - Card Class - Graphics Win

Reply

Join Date: Oct 2006
Posts: 36
Reputation: macca1111 is an unknown quantity at this point 
Solved Threads: 0
macca1111 macca1111 is offline Offline
Light Poster

Python - Card Class - Graphics Win

 
0
  #1
Oct 25th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 13
Reputation: DarkFlash is an unknown quantity at this point 
Solved Threads: 2
DarkFlash's Avatar
DarkFlash DarkFlash is offline Offline
Newbie Poster

Re: Python - Card Class - Graphics Win

 
0
  #2
Oct 25th, 2006
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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,972
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 920
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Python - Card Class - Graphics Win

 
0
  #3
Oct 25th, 2006
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.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 16
Reputation: Zonr_0 is an unknown quantity at this point 
Solved Threads: 3
Zonr_0 Zonr_0 is offline Offline
Newbie Poster

Re: Python - Card Class - Graphics Win

 
0
  #4
Oct 26th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 36
Reputation: macca1111 is an unknown quantity at this point 
Solved Threads: 0
macca1111 macca1111 is offline Offline
Light Poster

Re: Python - Card Class - Graphics Win

 
0
  #5
Oct 26th, 2006
Originally Posted by Zonr_0 View Post
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:
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 36
Reputation: macca1111 is an unknown quantity at this point 
Solved Threads: 0
macca1111 macca1111 is offline Offline
Light Poster

Re: Python - Card Class - Graphics Win

 
0
  #6
Oct 29th, 2006
Originally Posted by macca1111 View Post
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 16
Reputation: Zonr_0 is an unknown quantity at this point 
Solved Threads: 3
Zonr_0 Zonr_0 is offline Offline
Newbie Poster

Re: Python - Card Class - Graphics Win

 
0
  #7
Oct 30th, 2006
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?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC