def hit(self): ''' Appends a card to the players hand and then displays that card. If the player busts gameover() is called''' self._player.append(self._deck.deal(1)[0]) self.hits = CardLabel(root) self.hits.grid(row=3, column=len(self._player) - 1) self.hits.display('front', self._player[-1].num()) #print(self._player) if (BlackJack.total(self._player)) > 21: BlackJack.gameover(self, 0, 'You Busted!')
def deal(self): ''' Adds cards to the game: Creates a deck, shuffles the deck, deals cards to the dealer and then the player ''' BlackJack.__init__( self) # Reinstantiates the class to reset the game space self._deck = Deck.Deck() # 1) Create Deck self._deck.shuffle() # 2) shuffle deck self._dealer = self._deck.deal(2) # 3) Create Dealers Hand self._dealercard1 = CardLabel(root) self._dealercard1.grid(row=1, column=0) self._dealercard1.display('back', self._dealer[0].num()) self._dealercard2 = CardLabel(root) self._dealercard2.grid(row=1, column=1) self._dealercard2.display('front', self._dealer[1].num()) self._player = self._deck.deal(2) # 4) Create Players hand for i in range(len(self._player)): self._playercards = CardLabel(root) self._playercards.grid(row=3, column=i) self._playercards.display('front', self._player[i].num())
def stand(self): ''' this function is called when the player clicks the pass button. To stand in blackjack means you pass priority and allow the dealer to play out their hand ''' self._dealercard1.display('front', self._dealer[0].num()) while (BlackJack.total(self._dealer) < 22): if (BlackJack.total(self._dealer)) < 17: self._dealer.append(self._deck.deal(1)[0]) self.dealerhits = CardLabel(root) #self.dealerhits.after(2000) self.dealerhits.grid(row=1, column=len(self._dealer) - 1) self.dealerhits.display('front', self._dealer[-1].num()) else: return self.compare(self._dealer, self._player) return BlackJack.gameover(self, 1, 'Dealer Busted!')
def display_cards(hand,restart=False,show_hand=False): ''' This creates the labels for each user and makes the first card of the comp uters hand visible if show hand is true. If restart is true then all lables are reset. ''' for i in range(len(hand)): a = CardLabel(root) if i == 0 and hand == computer_hand and not show_hand: a.display('back',hand[i].n) else: a.display('front',hand[i].n) if hand == computer_hand: a.grid(row=0,column=i+1) else: a.grid(row=1,column=i+1) if restart == True: for i in range(len(hand),6): a = CardLabel(root) a.display(1) if hand == computer_hand: a.grid(row=0,column=i+1) else: a.grid(row=1,column=i+1)
############################################################################################################### root = Tk() CardLabel.load_images() # Call after creating top level app ############################################################################################################### # Card labels dealer_label = [0]*6 player_label = [0]*6 for card in range(6): # Create labels for dealer dealer_label[card] = CardLabel(root) dealer_label[card].grid(row=0, column=card, padx=10, pady=10) dealer_label[card].display('blank') # Create labels for player player_label[card] = CardLabel(root) player_label[card].grid(row=1, column=card, padx=10, pady=10) player_label[card].display('blank') ############################################################################################################### # Score update label dealer_win = Label(root, text='Dealer\'s score: ') dealer_win.grid(row=0, column=6, sticky=W, padx=20, pady=10) player_win = Label(root, text='Player\'s score: ')
n has a pattern of 0,1,2,0,1,2,... in order to alternate between cards every click """ global a, n, card a = (a + 1) % 9 w = a // 3 n = a % 3 #print(a,n,w) card[n].display(side[w], randint(0, 51)) root = Tk() CardLabel.load_images() card = [] for i in range(3): card.append(CardLabel(root)) card[i].grid(row=0, column=i) root.rowconfigure(0, minsize=115) root.columnconfigure(0, minsize=85) button = Button(root, text="Flip", command=flip) button.grid(row=1, column=1, pady=10) if __name__ == "__main__": root.mainloop()
elif counter1 != counter2 and counter2 == counter3: #If first card flipped, then second card flips counter2 = (counter2 + 1) % 3 elif counter2 != counter3: #If the first and second card is flipped, the third card is counter3 = (counter3 + 1) % 3 if counter1 == 0 and counter2 == 0 and counter3 == 0: #Assigns new cards when cards flipped to back again rand_num1 = randint(0,51) rand_num2 = randint(0,51) rand_num3 = randint(0,51) a.display(flipping_status[counter1], rand_num1)#Displays first card b.display(flipping_status[counter2], rand_num2)#Displays second card c.display(flipping_status[counter3], rand_num3)#Displays third card CardLabel.load_images() #Loads the images of the cards a = CardLabel(root) #Makes 'a' a label of class CardLabel a.grid(row=0, column=0) b = CardLabel(root) #Makes 'b' a label of class CardLabel b.grid(row=0,column=1) c = CardLabel(root) #Makes 'c' a label of class CardLabel c.grid(row=0,column=2) button = Button(root,text='Flip', command=flipping_cards) #Flips the cards button.grid(row=1,column=1) if __name__ == '__main__': root.mainloop() #Calls mainloop
from tkinter import * from random import randint from CardLabel import * side = ["back", "front", "blank"] n = 0 def flip(): global n, label n = (n + 1) % 3 label.display(side[n], randint(0, 51)) root = Tk() CardLabel.load_images() label = CardLabel(root) label.grid(row=0, column=0) root.rowconfigure(0, minsize=115) root.columnconfigure(0, minsize=85) button = Button(root, text="Flip", command=flip) button.grid(row=1, column=0, pady=10) if __name__ == "__main__": root.mainloop()
elif dealer_score < player_total or dealer_score > 21: showinfo("Game Over", "Player 1 Wins!") else: showinfo("Game Over", "Its a Tie!") total() """ This next section is filled with display info for the tkinter. The card labels are made with [Cardlabel(root) for i in range(6)] for better usability. """ root = Tk() CardLabel.load_images() Dcard = [CardLabel(root) for i in range(6)] Pcard = [CardLabel(root) for i in range(6)] dealerpos = 0 for x in Dcard: x.grid(row=0, column = dealerpos) x.display(side = 'blank') dealerpos += 1 playerpos = 0 for y in Pcard: y.grid(row=1, column = playerpos) y.display(side = 'blank') playerpos += 1
else: a.grid(row=1,column=i+1) if restart == True: for i in range(len(hand),6): a = CardLabel(root) a.display(1) if hand == computer_hand: a.grid(row=0,column=i+1) else: a.grid(row=1,column=i+1) CardLabel.load_images() root.bind("<Key>", key) #Key events deck_lbl = CardLabel(root) deck_lbl.grid(row=0,column=0) deck_lbl.display('back',0) deal_btn = Button(root,text="Deal",command=deal) hit_btn = Button(root,text="Hit",command=hit) hit_btn['state'] = 'disabled' pass_btn = Button(root,text="Pass",command=end_turn) pass_btn['state'] = 'disabled' deal_btn.grid(row=3,column=1) hit_btn.grid(row=3, column=2) pass_btn.grid(row=3,column=3) if __name__ == '__main__': root.mainloop()
from tkinter import * from random import randint from CardLabel import * from Card import * flipper_app = Tk() CardLabel.load_images() box = Frame(flipper_app) box.pack() card1 = CardLabel(box) card1.grid(row = 0, column = 1) card2 = CardLabel(box) card2.grid(row = 0, column = 2) card3 = CardLabel(box) card3.grid(row = 0, column = 3) move = 0 def flip(): global move side = ['front', 'front', 'front', 'blank', 'blank', 'blank', 'back', 'back', 'back'] label = [card1, card2, card3] * 3 if move < 9: label[move].display(side[move], randint(0,51)) flipper_app.update()
while result > 21 and numAces > 0: result -= 10 numAces -= 1 return result root = Tk() CardLabel.load_images() p_display = [] d_display = [] for i in range(6): d_display.append(CardLabel(root)) d_display[i].grid(row = 0, column = i) for i in range(6): p_display.append(CardLabel(root)) p_display[i].grid(row = 1, column = i) deal() root.rowconfigure(0, minsize = 115) root.columnconfigure(0, minsize=85) button = Button(root, text = "Deal", command = deal) button.grid(row= 3,column=0, pady =10)
def __init__(self, parent): """ Buttons and card labels initialized. """ Frame.__init__(self, parent) self.configure(background = 'white') self._cards = Deck(BlackjackCard) self._cards.shuffle() self.pack() CardLabel.load_images() self._dc1 = CardLabel(self) self._dc1.grid(row = 0, column = 0) self._dc2 = CardLabel(self) self._dc2.grid(row = 0, column = 1) self._dc3 = CardLabel(self) self._dc3.grid(row = 0, column = 2) self._dc4 = CardLabel(self) self._dc4.grid(row = 0, column = 3) self._dc5 = CardLabel(self) self._dc5.grid(row = 0, column = 4) self._dc6 = CardLabel(self) self._dc6.grid(row = 0, column = 5) self._pc1 = CardLabel(self) self._pc1.grid(row = 1, column = 0) self._pc2 = CardLabel(self) self._pc2.grid(row = 1, column = 1) self._pc3 = CardLabel(self) self._pc3.grid(row = 1, column = 2) self._pc4 = CardLabel(self) self._pc4.grid(row = 1, column = 3) self._pc5 = CardLabel(self) self._pc5.grid(row = 1, column = 4) self._pc6 = CardLabel(self) self._pc6.grid(row = 1, column = 5) self._deal = Button(self, text = 'Deal', command = self.dealcb) self._deal.grid(row = 2, column = 0, padx = 10, pady = 10) self._hit = Button(self, text = 'Hit', command = self.hitcb) self._hit.grid(row = 2, column = 2, padx = 10, pady = 10) self._stand = Button(self, text = 'Stand', command = self.standcb) self._stand.grid(row = 2, column = 4, padx = 10, pady = 10) self.dealcb()