H a n d s o n, p r o j e c t b a s e d
Download 4.21 Mb. Pdf ko'rish
|
Python Crash Course, 2nd Edition
Displaying the Level
To display the player’s level in the game, we first need an attribute in GameStats representing the current level. To reset the level at the start of each new game, initialize it in reset_stats() : def reset_stats(self): """Initialize statistics that can change during the game.""" self.ships_left = self.settings.ship_limit alien_invasion.py game_stats.py Scoring 297 self.score = 0 self.level = 1 To have Scoreboard display the current level, we call a new method, prep _level() , from __init__() : def __init__(self, ai_game): --snip-- self.prep_high_score() self.prep_level() Here’s prep_level() : def prep_level(self): """Turn the level into a rendered image.""" level_str = str(self.stats.level) u self.level_image = self.font.render(level_str, True, self.text_color, self.settings.bg_color) # Position the level below the score. self.level_rect = self.level_image.get_rect() v self.level_rect.right = self.score_rect.right w self.level_rect.top = self.score_rect.bottom + 10 The prep_level() method creates an image from the value stored in stats.level u and sets the image’s right attribute to match the score’s right attribute v. It then sets the top attribute 10 pixels beneath the bottom of the score image to leave space between the score and the level w. We also need to update show_score() : def show_score(self): """Draw scores and level to the screen.""" self.screen.blit(self.score_image, self.score_rect) self.screen.blit(self.high_score_image, self.high_score_rect) self.screen.blit(self.level_image, self.level_rect) This new line draws the level image to the screen. We’ll increment stats.level and update the level image in _check_bullet _alien_collisions() : def _check_bullet_alien_collisions(self): --snip-- if not self.aliens: # Destroy existing bullets and create new fleet. self.bullets.empty() self._create_fleet() self.settings.increase_speed() # Increase level. self.stats.level += 1 self.sb.prep_level() scoreboard.py scoreboard.py scoreboard.py alien_invasion.py 298 Chapter 14 If a fleet is destroyed, we increment the value of stats.level and call prep_level() to make sure the new level displays correctly. To ensure the level image updates properly at the start of a new game, we also call prep_level() when the player clicks the Play button: def _check_play_button(self, mouse_pos): --snip-- if button_clicked and not self.stats.game_active: --snip-- self.sb.prep_score() self.sb.prep_level() --snip-- We call prep_level() right after calling prep_score() . Now you’ll see how many levels you’ve completed, as shown in Figure 14-5. Figure 14-5: The current level appears just below the current score. n o T e In some classic games, the scores have labels, such as Score, High Score, and Level. We’ve omitted these labels because the meaning of each number becomes clear once you’ve played the game. To include these labels, add them to the score strings just before the calls to font.render() in Scoreboard . 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