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 GameState class

We will put the code which needs to run at the beginning of any game into the GameState __init__ method. This code will then run any time we make a new game:

class GameState:    def __init__(self):        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())        self.has_winner = ''

Inside the __init__ method, we create and shuffle our Deck, assign a Hand to our player and dealer, and initialize the winner as nobody (using an empty string).

Our player_is_over method now sits in this class. Copy it over if you are using a new file.

We will slightly modify our old check_for_blackjack ...

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