Restarting the game

29 Aug, 2014

Lyna M writes (excerpted):

I bought your book Python for Kids with the intention to teach my son Python later on but have now been using it myself to learn Python as I find your book to be quite comprehensive even if it was 'for kids'.

I've been doing the Bounce tutorial and would like to find a way to add a restart button so that each time the game ends, the player can restart the game if they want to.

I've applied the codes for Game Over but would like to add a restart button after that.

if ball.hit_bottom == True:
    time.sleep
    canvas.itemconfig(game_over_text, state='normal')
    btn = Button(tk, text="Click to Restart Game")
    btn.pack()
tk.update_idletasks()
tk.update()
time.sleep(0.01)

tk = Tk()
btn = Button(tk, text="Click to Restart Game")
btn.pack()

How to tie in the button so that it restarts the game when clicked? I've been trying out codes from forums and blogs but they don't seem to work or are too hard to understand.

There's a few ways you can handle the restart. If you look at the programming puzzles in Chapter 14, the first challenge is to delay the game start using the <Button-1> binding to tie a mouse button click to a function which starts the game. You could adapt that function to also restart the game once it's over. That's probably the most seamless approach.
If you really want to use a button, you need a way to add it just once -- because where you've added it (inside the loop), you'll get one button added to the window for every iteration of the loop. After the loop finishes isn't really the right place either.

First of all you'll need a Game class which will tie all the game objects together. Basically the new class would have object variables to 'store' the paddle, the ball, the score, the game-over-text, and also your restart button (initially the restart button variable would be set to None). So rather than adding the button inside the if-statement, you might do something like this:

    game = Game(canvas, paddle, ball, score, game_over_text)

        --- ✂ ---

    ###
    while 1:###

            --- ✂ ---

        ###
        if ball.hit_bottom == True:
            time.sleep(1)
            canvas.itemconfig(game_over_text, state='normal')###
            if game.restart_button is None:
                game.add_restart()

In the above code, if the game variable restart_button is None (not currently displayed), we call a function add_restart - so that fixes the problem of adding the button more than once. Here's the code for adding the button:

class Game:

        --- ✂ ---

    def add_restart(self):
        self.restart_button = Button(tk, text="Click to Restart Game", command=self.restart)
        self.restart_button.pack()

The command associated with the button is the function on our Game class called restart - this is the guy that takes care of hiding the game over text, putting the ball and paddle back in their starting positions, and so on.

Hope that helps somewhat.