# Get rid of any remaining bullets and aliens.
2 [Link]()
[Link]()
# Create a new fleet and center the ship.
3 self._create_fleet()
[Link].center_ship()
We reset the game statistics 1, which gives the player three new ships.
Then we set game_active to True so the game will begin as soon as the code in
this function finishes running. We empty the aliens and bullets groups 2,
and then we create a new fleet and center the ship 3.
Now the game will reset properly each time you click Play, allowing you
to play it as many times as you want!
Deactivating the Play Button
One issue with our Play button is that the button region on the screen will
continue to respond to clicks even when the Play button isn’t visible. If you
click the Play button area by accident after a game begins, the game will
restart!
To fix this, set the game to start only when game_active is False:
alien def _check_play_button(self, mouse_pos):
_invasion.py """Start a new game when the player clicks Play."""
1 button_clicked = self.play_button.[Link](mouse_pos)
2 if button_clicked and not self.game_active:
# Reset the game statistics.
[Link].reset_stats()
--snip--
The flag button_clicked stores a True or False value 1, and the game will
restart only if Play is clicked and the game is not currently active 2. To test
this behavior, start a new game and repeatedly click where the Play button
should be. If everything works as expected, clicking the Play button area
should have no effect on the gameplay.
Hiding the Mouse Cursor
We want the mouse cursor to be visible when the game is inactive, but
once play begins, it just gets in the way. To fix this, we’ll make it invisible
when the game becomes active. We can do this at the end of the if block in
_check_play_button():
alien_invasion.py def _check_play_button(self, mouse_pos):
"""Start a new game when the player clicks Play."""
button_clicked = self.play_button.[Link](mouse_pos)
if button_clicked and not self.game_active:
--snip--
# Hide the mouse cursor.
[Link].set_visible(False)
282 Chapter 14