| | |
Python - Card Class - Graphics Win
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2006
Posts: 36
Reputation:
Solved Threads: 0
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
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
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.
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!
•
•
Join Date: Oct 2006
Posts: 16
Reputation:
Solved Threads: 3
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.
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
•
•
Join Date: Oct 2006
Posts: 36
Reputation:
Solved Threads: 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.
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:
•
•
Join Date: Oct 2006
Posts: 36
Reputation:
Solved Threads: 0
•
•
•
•
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:
draw(self, win, center)
and use this to call a bmp picture of a card?
macca1111
![]() |
Similar Threads
- Card Class - Reading from a File (Python)
- Python - Importing Data with a Class (Python)
- graphics card (Monitors, Displays and Video Cards)
Other Threads in the Python Forum
- Previous Thread: tribute project for daniweb
- Next Thread: Python - Importing Data with a Class
| Thread Tools | Search this Thread |
accessdenied advanced application argv beginner change color command convert csv cursor def dictionary digital dynamic dynamically edit editing enter event examples excel file float format frange function google gui homework i/o import input jaunty java keyboard lapse line linux list lists loop microphone mouse movingimageswithpygame newb number numbers numeric obexftp output parameters parsing path port prime programming projects py2exe pygame pygtk pyopengl python random recursion remote return reverse scrolledtext session simple skinning smtp sprite stderr string strings subprocess syntax table tennis terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode unit urllib urllib2 variable voip web-scrape windows wxpython






