I have two types of score in the game to record my game's statistic.
The first score is the score a user earn playing. In the code it's called as "score"
The second score (high_score) is the highest score of the all plays a user does.
It stores in "game_stats.py" module in the self.high_score
attribute until a user closes the game.
Here's how it works. The peace of code is stored in the "scoreboard.oy" module.
def check_high_score(self):
"""Check to see if there's a new high score."""
if self.stats.score > self.stats.high_score:
self.stats.high_score = self.stats.score
self.prep_high_score()
self.prep_high_score()
-- that's the method in the "scoreboard.py" module which displays the high_score on the display surface.
def prep_high_score(self):
"""Turn the high score into a rendered image."""
rounded_high_score = round(self.stats.high_score, -1) # round a number to the neariest integer.
high_score_str = "{:,}".format(rounded_high_score)
self.high_score_image = self.font.render(high_score_str, True,
self.text_color, self.settings.bg_color)
# Center the high score at the bottom of the screen
self.high_score_rect = self.high_score_image.get_rect()
self.high_score_rect.centerx = self.screen_rect.centerx
self.high_score_rect.top = self.score_rect.top
I have dynamic settings such as: ship's speed, aliens' speed, and bullet's speed that increase after a user leveled up.
Here's the method from the settings.py module.
def _check_play_button(self, mouse_pos):
"""Start a new game when a player clicks the "Play" button."""
# Every time the game restarted the settings of the game are reseted.
button_clicked = self.play_button.rect.collidepoint(mouse_pos)
if button_clicked and not self.stats.game_active: # game_active = False
# Reset the game settings.
self.settings.initialize_dynamic_settings()
# Reset the game statistics (a user can restart the game after died).
self.stats.reset_stats()
self.stats.game_active = True
# self.sb.prep_score()
# self.sb.prep_level()
# Ged rid of any remaining aliens and bullets.
self.aliens.empty()
self.bullets.empty()
# Create a new fleet and center the ship.
self._create_fleet()
self.ship.center_ship()
# Hide the mouse cursor.
pygame.mouse.set_visible(False)
# Hide the cursor when game_active = False
elif self.stats.game_active:
pygame.mouse.set_visible(True)
After a uses lost the game, and clicked the "Play" button the game resets those dynamic settings and the score that was made during the game.
Here's the method of the main file (alien_invasion.py) that resets the dynamic settings.
def _check_play_button(self, mouse_pos):
"""Start a new game when a player clicks the "Play" button."""
# Every time the game restarted the settings of the game are reseted.
button_clicked = self.play_button.rect.collidepoint(mouse_pos)
if button_clicked and not self.stats.game_active: # game_active = False
# Reset the game settings.
self.settings.initialize_dynamic_settings()
# Reset the game statistics (a user can restart the game after died).
self.stats.reset_stats()
self.stats.game_active = True
# self.sb.prep_score()
# self.sb.prep_level()
# Ged rid of any remaining aliens and bullets.
self.aliens.empty()
self.bullets.empty()
# Create a new fleet and center the ship.
self._create_fleet()
self.ship.center_ship()
# Hide the mouse cursor.
pygame.mouse.set_visible(False)
# Hide the cursor when game_active = False
elif self.stats.game_active:
pygame.mouse.set_visible(True)
self.stats = GameStats(self) self.stats.reset_stats() -- that's the method of the game_stats.py module that resets the dynamic settings.
But the problem is that when a user clicks the button only the score are reseted.