Hi everyone, I'm new to python and I have been trying to get this program to word but I don't know how to get the image cards to shuffle randomly and then appear on the graphics screen. Can anyone help me?
The question was to create a program that displays 5 random cards in a deck.
I already have a file with the card images called Cards_gif.zip.
I just don't understand how to suffle the pictures inside the file and then display 5 random cards. I have tested it and I can display 5 cards just not randomly. (Also I am using Python 3.2)
Here is my program:
from graphics import *
from button import Button
import random
def deck(rank,suits):
rank = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "J", "Q", "K"]
suits = ["C","D", "H", "S"]
rs = [rank + suit for ranks in rank for suit in suits]
return rs
def draw_cards(win,cards_list):
random.shuffle(cards_list)
return [cards_list.pop() for i in range(5)]
cards_list = deck()
hand = draw_cards(cards_list)
click = win.getMouse()
# This is what I was trying to get it to display, after I clicked the shuffleButton
if shuffleButton.clicked(click):
if hand == "C2":
I = Image(Point(0,4),"C2.gif")
I.draw(win)
def main():
win = GraphWin("Card Graphics", 500,400)
win.setCoords(-10, -10, 10, 10)
shuffleButton = Button(win, Point(0,-2),6,1,"Shuffle Cards")
shuffleButton.activate()
click=win.getMouse()
#This is my exapmle of being able to display 5 cards, just not randomly
if shuffleButton.clicked(click):
I = Image(Point(0,4),"C2.gif")
I.draw(win)
K = Image(Point(4,4),"S9.gif")
K.draw(win)
C = Image(Point(8,4),"HA.gif")
C.draw(win)
L = Image(Point(-4,4),"D3.gif")
L.draw(win)
A = Image(Point(-8,4),"CJ.gif")
A.draw(win)
main()