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()

Recommended Answers

All 3 Replies

You can just make dictionary from card values to file names fo the cards. Then just show the card's images for the chosen cards from deck (pop from deck and add to hand).

Here is the program written in Tkinter, It was originally posted using Python24 about 5 years ago somewhere in "Starting Python" together with the card image zip file you have ...

# using Tkinter to display a hand of 5 random card images
# each time you click the canvas
# (images are in GIF format for Tkinter to display properly)
# tested with Python32

import tkinter as tk
import random

root = tk.Tk()
root.title("Click me!")

def create_cards():
    """
    create a list of 52 cards
    suit: club=C, diamond=D, heart=H spade=S
    rank: ace=A, 10=T, jack=J, queen=Q, king=K, numbers=2..9
    ace of spade would be SA, 8 of heart would be H8 and so on ...
    """
    return [ suit + rank for suit in "CDHS" for rank in "A23456789TJQK" ]

def shuffle_cards(card_list):
    """random shuffle a list of cards"""
    # make a copy of the original list
    card_list1 = card_list[:]
    random.shuffle(card_list1)
    return card_list1

def pick_5cards(card_list):
    """pick five cards from the shuffled list"""
    return card_list[:5]

def create_images():
    """create all card images as a card_name:image_object dictionary"""
    card_list = create_cards()
    image_dict = {}
    for card in card_list:
        # all images have filenames that match the card_list names + extension .gif
        image_dict[card] = tk.PhotoImage(file=image_dir+card+".gif")
        #print image_dir+card+".gif"  # test
    return image_dict

def next_hand(event):
    """create the card list, shuffle, pick five cards and display them"""
    card_list = create_cards()
    card_list = shuffle_cards(card_list)
    card_list = pick_5cards(card_list)
    root.title(card_list)  # test

    # now display the card images at the proper location on the canvas
    x = 10
    y = 10
    for card in card_list:
        #print card, x, y  # test
        canvas1.create_image(x, y, image=image_dict[card], anchor='nw')
        # calculate each NW corner x, y
        x += 90

# change this to the directory your card GIFs are in
image_dir = "C:/Python32/Atest32/tk/Cards_gif/"

# load a sample card to get the size
photo1 = tk.PhotoImage(file=image_dir+"C2.gif")

# make canvas 5 times the width of a card + 100
width1 = 5 * photo1.width() + 100
height1 = photo1.height() + 20
canvas1 = tk.Canvas(width=width1, height=height1)
canvas1.pack()

# now load all card images into a dictionary
image_dict = create_images()
#print image_dict  # test

# bind left mouse click on canvas to next_hand display
canvas1.bind('<Button-1>', next_hand)

root.mainloop()

Since the module graphics is written in Tkinter, you can hopefully exract some needed helpful hints.
Sorry, the new DaniWeb format makes referencing these things pretty merdie

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.