daily pastebin goal
37%
SHARE
TWEET

Untitled

a guest Dec 10th, 2018 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Donald Quick
  2. #Cptr 215 - Lab13
  3. from unoCard import UnoCard
  4. #from unoPlayer import UnoPlayer
  5. import random
  6. from tkinter import ttk
  7. import tkinter as tk
  8. import tkinter.messagebox as messagebox
  9.  
  10. class UnoPlayer:
  11.     def __init__(self):
  12.         self.hand = []
  13.        
  14.     def __str__(self):
  15.         #return cards in hand
  16.         return f'{self.hand}'
  17.    
  18.     def __repr__(self):
  19.         return 'UnoPlayer()'
  20.  
  21.     def __add__(self, card):
  22.         self.hand.append(card)
  23.    
  24.     def remove(self, card):
  25.         self.hand.remove(card)
  26.  
  27. class gameWindow(tk.Frame):
  28.     def __init__(self, parent, players):
  29.         tk.Frame.__init__(self, parent)
  30.         self.parent = parent
  31.         self.buttons = []
  32.         self.column = 0
  33.         self.turnLabel = ttk.Label(self, text=f"Player 1 turn")
  34.         self.turnLabel.grid(row=1, column=1)
  35.  
  36.         self.currentCardLabel = ttk.Label(self, text='Current card')
  37.         self.currentCardFrame = ttk.Frame(self)
  38.         self.currentCardLabel.grid(row=2, column=1)
  39.         self.currentCardFrame.grid(row=3, column=1)
  40.  
  41.         cardLabel = ttk.Label(self, text="Cards in your hand")
  42.         cardLabel.grid(row=4, column=1)
  43.         self.buttonFrame = ttk.Frame(self)
  44.         self.buttonFrame.grid(row=5, column=1)
  45.  
  46.         self.players = players
  47.         self.deck = create_deck()
  48.         self.currentCard = self.drawCard(self.deck)
  49.         self.currentPlayer = self.players[0]
  50.  
  51.         self.passButton = tk.Button(self, text='Skip your turn', command=lambda: self.nextTurn())
  52.         self.passButton.grid(row=6, column=1)
  53.  
  54.         self.changeCurrentCard(self.currentCard)
  55.         for i in range(len(self.players)):
  56.             self.initHands(players[i])
  57.         self.generateCardButtons(self.currentPlayer.hand)
  58.  
  59.     def initHands(self, player): #init multi hands - pass in players
  60.         for i in range(7):
  61.             player + (self.drawCard(self.deck))
  62.  
  63.     def generateCardButtons(self, range):
  64.         if self.buttons != []:
  65.             self.clearButtons(range)
  66.         else:
  67.             for card in range:
  68.                 cardButton = tk.Button(self.buttonFrame, text=str(card), fg=card.color, bg='White', width=10,
  69.                                        command=lambda currentCard=card: self.buttonAction(currentCard))
  70.                 self.buttons.append(cardButton)
  71.                 cardButton.grid(row=0, column=self.column, sticky='ew')
  72.                 self.column += 1
  73.  
  74.     def clearButtons(self, passthrough):
  75.         for i in self.buttons:
  76.                 i.destroy()
  77.                 self.buttons.remove(i)
  78.         self.generateCardButtons(passthrough)
  79.  
  80.     def buttonAction(self, card):
  81.         #Check for wild
  82.         if card.playedOn(self.currentCard):
  83.             self.currentPlayer.remove(card)    
  84.             self.changeCurrentCard(card)
  85.             button = self.getButton(card)
  86.             button.destroy()
  87.             self.buttons.remove(button)
  88.             self.nextTurn()
  89.         else:
  90.             messagebox.showinfo("Incorrect match", "Incorrect card match! Please match by card type or color. "
  91.                                     + "If you can't, skip your turn")
  92.  
  93.     def changeCurrentCard(self, card):
  94.         #Mabye add the changed button to a list for reshuffling (discardPile)
  95.         cardButton = tk.Button(self.currentCardFrame, text=str(card), fg=card.color)
  96.         cardButton.grid(row=0, column=0, sticky='ew')
  97.         self.currentCard = card
  98.  
  99.     def addCard(self):
  100.         self.currentPlayer + self.drawCard(self.deck)
  101.         self.generateCardButtons(self.currentPlayer.hand)
  102.  
  103.     def getButton(self, card):
  104.         for button in self.buttons:
  105.             if button['text'] == str(card) and button['fg'].startswith(card.color):
  106.                 return button
  107.  
  108.     def nextTurn(self):
  109.         self.addCard
  110.         if self.players.index(self.currentPlayer) + 1 == len(self.players):
  111.             self.currentPlayer = self.players[0]
  112.             self.turnLabel.config(text = f"Player 1 turn")
  113.         else:
  114.             self.currentPlayer = self.players[self.players.index(self.currentPlayer) + 1]
  115.             self.turnLabel.config(text = f"Player {self.players.index(self.currentPlayer) + 1} turn")
  116.         self.generateCardButtons(self.currentPlayer.hand)
  117.    
  118.     def drawCard(self, deck):
  119.         card = deck[0]
  120.         deck.pop(0)
  121.         return card
  122.  
  123.  
  124. def create_deck():
  125.     deck = []
  126.     for c in ['Red', 'Green', 'Blue', 'Gold']:
  127.         for r in ['0','1','2','3','4','5','6','7','8','9', 'Skip', 'Reverse', 'Draw']:
  128.             if r == '0':
  129.                 deck.append(UnoCard(c, r))
  130.             else:
  131.                 for i in range(2):
  132.                     deck.append(UnoCard(c, r))
  133.         deck.append(UnoCard('Black', 'Wild'))
  134.         deck.append(UnoCard('Black', 'Draw4'))
  135.     random.shuffle(deck)
  136.     return deck
  137.  
  138. player1 = UnoPlayer()
  139. player2 = UnoPlayer()
  140. player3 = UnoPlayer()
  141. player4 = UnoPlayer()
  142.  
  143. players = [player1, player2, player3, player4]
  144.  
  145. if __name__ == "__main__":
  146.     #https://stackoverflow.com/questions/17466561/best-way-to-structure-a-tkinter-application
  147.     root = tk.Tk()
  148.     # Ask how many players
  149.     gameWindow(root, players).pack(side="top", fill="both", expand=True)
  150.     root.mainloop()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top