It is required to set different difficulty levels in the game alien invasion, and add the function of allowing players to choose the starting difficulty level.
I think the requirements of the exercise can be broken down into the following points:
1. Set several different difficulty levels for players to choose.
2. Players can only choose the difficulty level when the game is stopped. It cannot be changed during the game.
3. After selecting the difficulty level after the game starts, if the game fails and enters the stop state, after clicking the "Play" button, the rhythm of the game is still the initial state under the difficulty level.
4. There is a default difficulty level. If the player directly clicks the "Play" button after running the game, the game will start with the default difficulty level.
Personally, I think this is actually difficult. The main reason is to set a default difficulty level for the game. Once the player selects other difficulty levels, if the game fails and starts the game again, he can continue the game with the difficulty level selected by the player. (if you can't do these two points, I think the game is not intelligent enough)
My idea is to use conditional test statements to solve problems.
First, in the Settings class__ init__ The following difficulty judgment symbols are added to the initial parameters.
#The default game difficulty is normal self.easy = False self.normal = True self.hard = False
Then add the following four methods to the Settings class, which includes a method to call game parameters according to the game difficulty judgment symbol, and three game parameters of different levels to choose from.
def initialize_dynamic_settings(self): """Set different levels of difficulty of the game""" if self.easy: self._easy_settings() elif self.normal: self._normal_settings() elif self.hard: self._hard_settings() def _easy_settings(self): """easy Game difficulty setting""" self.ship_speed = 0.6 self.bullet_color = (0, 0, 255) self.bullet_speed = 2 self.alien_speed = 0.15 self.fleet_drop_speed = 10 def _normal_settings(self): """normal Game difficulty setting""" self.ship_speed = 0.8 self.bullet_color = (0, 0, 0) self.bullet_speed = 2 self.alien_speed = 0.2 self.fleet_drop_speed = 15 def _hard_settings(self): """hard Game difficulty setting""" self.ship_speed = 1 self.bullet_color = (255, 0, 0) self.bullet_speed = 2 self.alien_speed = 0.25 self.fleet_drop_speed = 20
Then delete the Settings class__ init__ Initial parameters for initialize_ dynamic_ Call the Settings method, because the call of the change method can be placed in the game Play button.
The next step is to add the calling method of the game main AlienInvasion class for the game difficulty parameters.
First in_ start_ Calling initialize_ in the game method dynamic_ The settings method can be placed in rest_ After stats. Increased_ start_ The game method is as follows:
def _start_game(self): """Start running the game""" #Reset game statistics. self.stats.rest_stats() self.settings.initialize_dynamic_settings() self.stats.game_active = True #Empty the remaining aliens and bullets. self.aliens.empty() self.bullets.empty() #Create a new group of aliens and center the spacecraft. self._create_fleet() self.ship.center_ship() #Hide the mouse cursor. pygame.mouse.set_visible(False)
Then in response to the key_ check_ keydown_ In the events method, the method of adjusting the value of the game difficulty judgment symbol in settings is added_ check_ keydown_ The events method is as follows:
def _check_keydown_events(self, event): """Response key.""" #Press the left and right keys to control the movement of the spacecraft if event.key == pygame.K_RIGHT: self.ship.moving_right = True elif event.key == pygame.K_LEFT: self.ship.moving_left = True #Press the q key to exit the game elif event.key == pygame.K_q: sys.exit() #Press the space bar to control the spacecraft to launch bullets elif event.key == pygame.K_SPACE: self._fire_bullet() #Press. When the game stops, press the p key to start the game elif event.key == pygame.K_p and not self.stats.game_active: self._start_game() #When the game stops, press the e, n and h keys to adjust the difficulty of the game elif event.key == pygame.K_e and not self.stats.game_active: self.settings.easy = True self.settings.normal = False self.settings.hard = False elif event.key == pygame.K_n and not self.stats.game_active: self.settings.easy = False self.settings.normal = True self.settings.hard = False elif event.key == pygame.K_h and not self.stats.game_active: self.settings.easy = False self.settings.normal = False self.settings.hard = True
It should be noted that I will set initialize_ dynamic_ The call of the settings method is placed in the_ start_ In game instead of_ check_play_button method.
In this way, the function required by the exercise is realized.