Starting the game

Finally, we have built the functionality needed to run the game loop—the logic required to update the ball's position according to the rebounds, and restart the game if the player loses one life.

Now we can add the following methods to our Game class to complete the development of our game:

 def start_game(self): self.canvas.unbind('<space>') self.canvas.delete(self.text) self.paddle.ball = None self.game_loop() def game_loop(self): self.check_collisions() num_bricks = len(self.canvas.find_withtag('brick')) if num_bricks == 0: self.ball.speed = None self.draw_text(300, 200, 'You win!') elif self.ball.get_position()[3] >= self.height: self.ball.speed = None self.lives -= 1 if self.lives < 0: self.draw_text(300, 200, 'Game Over') ...

Get Python Game Programming By Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.