H a n d s o n, p r o j e c t b a s e d
Deactivating the Play Button
Download 4.21 Mb. Pdf ko'rish
|
Python Crash Course, 2nd Edition
- Bu sahifa navigatsiya:
- Hiding the Mouse Cursor
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 : def _check_play_button(self, mouse_pos): """Start a new game when the player clicks Play.""" u button_clicked = self.play_button.rect.collidepoint(mouse_pos) v if button_clicked and not self.stats.game_active: # Reset the game statistics. self.stats.reset_stats() --snip-- The flag button_clicked stores a True or False value u, and the game will restart only if Play is clicked and the game is not currently active v. 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 to begin play, 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() : def _check_play_button(self, mouse_pos): """Start a new game when the player clicks Play.""" button_clicked = self.play_button.rect.collidepoint(mouse_pos) if button_clicked and not self.stats.game_active: --snip-- # Hide the mouse cursor. pygame.mouse.set_visible(False) alien_invasion.py alien_invasion.py Scoring 285 Passing False to set_visible() tells Pygame to hide the cursor when the mouse is over the game window. We’ll make the cursor reappear once the game ends so the player can click Play again to begin a new game. Here’s the code to do that: def _ship_hit(self): """Respond to ship being hit by alien.""" if self.stats.ships_left > 0: --snip-- else: self.stats.game_active = False pygame.mouse.set_visible(True) We make the cursor visible again as soon as the game becomes inactive, which happens in _ship_hit() . Attention to details like this makes your game more professional looking and allows the player to focus on playing rather than figuring out the user interface. Download 4.21 Mb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling