Initializing the GameWindow class

Let's look at the new GameWindow class and how it manages our application's main window and widgets. A lot of this code will feel familiar from our previous GameScreen class:

class GameWindow(tk.Tk):
def __init__(self):
super().__init__()
self.title("Blackjack")
self.geometry("800x640")
self.resizable(False, False)

self.bottom_frame = tk.Frame(self, width=800, height=140, bg="red")
self.bottom_frame.pack_propagate(0)

self.hit_button = tk.Button(self.bottom_frame, text="Hit",
width=25, command=self.hit)
self.stick_button = tk.Button(self.bottom_frame, text="Stick",
width=25, command=self.stick)

self.next_round_button = tk.Button(self.bottom_frame,
text="Next Round", width=25, command=self.next_round)
self.quit_button = tk.Button(self.bottom_frame, text="Quit",
width=25, command=self.destroy)

self.new_game_button = tk.Button(self.bottom_frame,
text="New Game", width=25, command=self.new_game)

self.bottom_frame.pack(side=tk.BOTTOM, fill=tk.X)

You should see that the majority of the initialization code has remained from our earlier GameScreen class.

One new button is now added, the new_game_button, which will allow the user to start a new game when they have run out of money.

Our game_screen attribute is no longer a stock Canvas widget, but a new subclass, which we will call GameScreen. This will allow us to handle animations separately and separate the game logic from the game's main window.

Instead of displaying the table at the end of this function, we now pass over to the game_screen instance to display the opening animation:

self.game_screen = GameScreen(self, bg="white", width=800, height=500)
self.game_screen.pack(side=tk.LEFT, anchor=tk.N)
self.game_screen.setup_opening_animation()

Since the rest of the methods in this class revolve around packing and unpacking certain buttons, let's jump to the new GameScreen class to see where our GameWindow class is leading us.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset