O'Reilly logo

Tkinter GUI Programming by Example by David Love

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The Game class and main loop

We will define the game's main loop within the class __init__ method so that to begin playing, we simply need to create an instance of this class:

class Game:    def __init__(self):        playing = True        while playing:            self.deck = Deck()            self.deck.shuffle()            self.player_hand = Hand()            self.dealer_hand = Hand(dealer=True)            for i in range(2):                self.player_hand.add_card(self.deck.deal())                self.dealer_hand.add_card(self.deck.deal())            print("Your hand is:")            self.player_hand.display()            print()            print("Dealer's hand is:")            self.dealer_hand.display()

We start off our loop with a Boolean which will be used to track whether or not we are still playing the game.

If we are, we need a shuffled Deck and two Hand instances—one for the dealer and ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required