Replace canvas with new canvas

Post here if you need help with creating a Graphical User Interface in Python.

Replace canvas with new canvas

Postby Raj13 » Tue Sep 03, 2013 10:01 am

I am a happy amateur when it comes in Python and I need help. I am about to create a card game. As it is now, then draws a card out when the program is run. When the user clicks on the "högre" (higher) or "lägre" (lower) - randomly selected new short back and painted the window. From right to left. However, I want the first card, removed and replaced with card number 2. At the same time, the third card to the place where short 2 existed before.

So I just want to have two cards on the table at a time.

Code: Select all
Rounds Column1 Column2
  1     Card1   Card2 
  2     Card2   Card3
  3     Card3   Card4 


Hope you understand what I'm after and can give me a good solution! Tips are appreciated, questioning about my way of programming please.

Code: Select all
class Card(object):

    RANKS = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]

    SUIT = ["Python/projekt/bilder/hearts.png", "Python/projekt/bilder/spades.png", "Python/projekt/bilder/diamond.png", "Python/projekt/bilder/clubs.png"]

    def __init__(self, rank, suit):
        self.rank = rank
        self.suit = suit

    def __str__(self):
        rep = self.rank + self.suit
        return rep

    def draw(self):

        bg = ImageTk.PhotoImage(Image.open(self.suit).resize((10, 10)))

        cardGraph = Canvas(win, width=70, height=100, bg="White", bd=1, relief='solid', highlightthickness=2)
        cardGraph.photo=bg

        cardGraph.create_image(10,10, image=bg, anchor=CENTER) #left/up
        cardGraph.create_image(53,93, image=bg, anchor=CENTER) #right/down

        cardGraph.create_text(20, 10, text=self.rank, font=("Helvetica", 8, "bold")) #left/up
        cardGraph.create_text(63, 93, text=self.rank, font=("Helvetica", 8, "bold")) #right/down
        cardGraph.create_text(36, 50, text=self.rank, font=("Helvetica", 12, "bold")) #middle

        cardGraph.pack(side=LEFT, anchor=NW)

class Hand(object):
    def __init__(self):
        self.cards = []

    def __str__ (self):
        if self.cards:
            rep = ""
            for card in self.cards:
                rep += str(card) + " "
        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)

class Deck(Hand):

    global CardValueHist
    CardValueHist = []

    def populate(self):
        for suit in Card.SUIT:
            for rank in Card.RANKS:
                self.add(Card(rank, suit))

    def shuffle(self):
        import random
        random.shuffle(self.cards)
        DrawCard = self.cards[0]
        DrawCard.draw()


    def deal(self, hands, per_hand = 0):
        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("Cant continue deck. Out of cards!!")


    def setValue(self):
        global CardValue
        CardValue = 0
        rank = self.cards[0].rank
        if rank == 'K':
            CardValue += 13
            CardValueHist.append(CardValue)
        elif rank == 'Q':
            CardValue += 12
            CardValueHist.append(CardValue)
        elif rank == 'J':
            CardValue += 11
            CardValueHist.append(CardValue)
        elif rank == 'A':
            CardValue += 1
            CardValueHist.append(CardValue)
        else:
            CardValue += int(rank)
            CardValueHist.append(CardValue)

    def higher(self):
        decket.shuffle()
        decket.setValue()

        rank = CardValue
        compare = CardValueHist[-2]

        if (compare <= rank):
            mode.set("Bra jobbat! Fortsätt!")
            infoText.config(bg="Green")
        else:
            mode.set("Tyvärr, du gissade fel")
            infoText.config(bg="Red")
            higher.config(state='disable')
            lower.config(state='disable')

    def lower(self):
        decket.shuffle()
        decket.setValue()

        rank = CardValue
        compare = CardValueHist[-2]

        if (compare >= rank):
            mode.set("Bra jobbat! Fortsätt!")
            infoText.config(bg="Green")
        else:
            mode.set("Tyvärr, du gissade fel")
            infoText.config(bg="Red")
            higher.config(state='disable')
            lower.config(state='disable')

BannerBg = ImageTk.PhotoImage(Image.open("Python/projekt/bilder/card_bg.jpg"))
Banner = Label(win, text="Detta är ett spel", image=BannerBg)
Banner.pack(fill=BOTH)

mode = StringVar()
mode.set("Välkommen till High or Low Game")

infoText = Label(win, textvariable=mode, font=("Helvetica", 12, "bold"), bg="Yellow", bd=0.5, relief='solid')
infoText.pack(fill=BOTH)

decket = Deck()
decket.populate()
decket.shuffle()
decket.setValue()
decket.higher
decket.lower
Last edited by Mekire on Tue Sep 03, 2013 10:06 am, edited 1 time in total.
Reason: First post lock.
Raj13
 
Posts: 4
Joined: Tue Sep 03, 2013 9:57 am

Re: Replace canvas with new canvas

Postby wuf » Tue Sep 03, 2013 6:24 pm

Hi Raj13

Have made some modifications to your code. Sorry i had to replaced your Swedish text by english text because i did get some encoding errors. Look at my inserted marks '#New by wuf'. To handel the card canvas i used a stack like structure named 'self.draw_card_stack':
Code: Select all
#!/usr/bin/python
# -*- codieren: utf-8 -*-

# New by wuf
from Tkinter import *
# New by wuf
from PIL import ImageTk, Image

class Card(object):

    RANKS = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]

    SUIT = ["Python/projekt/bilder/hearts.png", "Python/projekt/bilder/spades.png", "Python/projekt/bilder/diamond.png", "Python/projekt/bilder/clubs.png"]
    #SUIT = ["hearts.png", "spades.png", "diamond.png", "clubs.png"]
   
    def __init__(self, rank, suit):
        self.rank = rank
        self.suit = suit

    def __str__(self):
        rep = self.rank + self.suit
        return rep

    def draw(self):

        bg = ImageTk.PhotoImage(Image.open(self.suit).resize((10, 10)))

        cardGraph = Canvas(win, width=70, height=100, bg="White", bd=1, relief='solid', highlightthickness=2)
        cardGraph.photo=bg

        cardGraph.create_image(10,10, image=bg, anchor=CENTER) #left/up
        cardGraph.create_image(53,93, image=bg, anchor=CENTER) #right/down

        cardGraph.create_text(20, 10, text=self.rank, font=("Helvetica", 8, "bold")) #left/up
        cardGraph.create_text(63, 93, text=self.rank, font=("Helvetica", 8, "bold")) #right/down
        cardGraph.create_text(36, 50, text=self.rank, font=("Helvetica", 12, "bold")) #middle

        cardGraph.pack(side=LEFT, anchor=NW)
       
        # New by wuf
        return cardGraph

class Hand(object):
    def __init__(self):
        self.cards = []
       
        # New by wuf
        self.draw_card_stack = []

    def __str__ (self):
        if self.cards:
            rep = ""
            for card in self.cards:
                rep += str(card) + " "
        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)

class Deck(Hand):

    global CardValueHist
    CardValueHist = []

    def populate(self):
        for suit in Card.SUIT:
            for rank in Card.RANKS:
                self.add(Card(rank, suit))

    def shuffle(self, event):
        import random
        random.shuffle(self.cards)
        DrawCard = self.cards[0]
        card_graph = DrawCard.draw()
       
        # New by wuf
        self.draw_card_stack.append(card_graph)
        print(len(self.draw_card_stack))
        if len(self.draw_card_stack) > 2:
            self.draw_card_stack[0].destroy()
            del self.draw_card_stack[0]
       

    def deal(self, hands, per_hand = 0):
        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("Cant continue deck. Out of cards!!")


    def setValue(self):
        global CardValue
        CardValue = 0
        rank = self.cards[0].rank
        if rank == 'K':
            CardValue += 13
            CardValueHist.append(CardValue)
        elif rank == 'Q':
            CardValue += 12
            CardValueHist.append(CardValue)
        elif rank == 'J':
            CardValue += 11
            CardValueHist.append(CardValue)
        elif rank == 'A':
            CardValue += 1
            CardValueHist.append(CardValue)
        else:
            CardValue += int(rank)
            CardValueHist.append(CardValue)

    def higher(self):
        decket.shuffle()
        decket.setValue()

        rank = CardValue
        compare = CardValueHist[-2]

        if (compare <= rank):
            mode.set("Good job! Go on!")
            infoText.config(bg="Green")
        else:
            mode.set("Sorry, you guessed wrong")
            infoText.config(bg="Red")
            higher.config(state='disable')
            lower.config(state='disable')

    def lower(self):
        decket.shuffle()
        decket.setValue()

        rank = CardValue
        compare = CardValueHist[-2]

        if (compare >= rank):
            mode.set("Good job! Go on!")
            infoText.config(bg="Green")
        else:
            mode.set("Sorry, you guessed wrong")
            infoText.config(bg="Red")
            higher.config(state='disable')
            lower.config(state='disable')

# New by wuf
win = Tk()

BannerBg = ImageTk.PhotoImage(Image.open("Python/projekt/bilder/card_bg.jpg"))
#BannerBg = ImageTk.PhotoImage(Image.open("card_bg.jpg"))

Banner = Label(win, text="This is a game", image=BannerBg)
Banner.pack(fill=BOTH)

mode = StringVar()
mode.set("Welcome to High or Low Game")

decket = Deck()
decket.populate()
# New by wuf
#decket.shuffle()
decket.setValue()
decket.higher
decket.lower

infoText = Label(win, textvariable=mode, font=("Helvetica", 12, "bold"), bg="Yellow", bd=0.5, relief='solid')
infoText.pack(fill=BOTH)

# New by wuf
infoText.bind('<Button-1>', decket.shuffle)

# New by wuf
win.mainloop()

wuf ;)
wuf
 
Posts: 37
Joined: Fri Feb 08, 2013 6:42 am

Re: Replace canvas with new canvas

Postby Raj13 » Tue Sep 03, 2013 8:39 pm

You made my day... Thank you very much :D :D :D :mrgreen:
Raj13
 
Posts: 4
Joined: Tue Sep 03, 2013 9:57 am

Re: Replace canvas with new canvas

Postby Raj13 » Thu Sep 05, 2013 3:46 pm

How do I do to remove all cards from the table? I have to make resetbutton :oops:
Raj13
 
Posts: 4
Joined: Tue Sep 03, 2013 9:57 am


Return to GUI

Who is online

Users browsing this forum: No registered users and 1 guest

cron