Compile a plane hitting game in Python (8)
Now the plane has been able to take a burst of blood to let players abuse, so we give users a sense of accomplishment-scoring system and increasing difficulty mechanism.
1. Scoring System
First, we add the global variable (score) in the main () function and initialize it to zero to calculate the current user's score:
Score = 0 # Count user scores
Next, we only need to accumulate the score when the enemy plane is destroyed. Here we will mark the price of a small enemy plane with a value of 500 points and a medium-sized enemy plane with a value of 2000 points, A large enemy server has a value of 6000 points. When the enemy server is damaged, add more or less points based on the type of the enemy server. For small enemy servers:
For each in small_enemies: # draw small enemy planes and automatically move if each. active: # Draw a small enemy else: if eincludestroy_index = 0: # if not (delay % 3): # play a damaged video if eincludestroy_index = 0: score + = 500 # reset ()
Medium-sized enemy planes:
For each in mid_enemies: # Draw medium enemy planes and automatically move if each. active: # draw medium-sized enemy servers # Draw a blood tank else: if e2_destroy_index = 0: # play a damaged sound effect if not (delay % 3): # Draw a damaged image if e2_destroy_index = 0: score + = 2000 # reset ()
Large enemy planes:
For each in big_enemies: # draw large enemy planes and automatically move if each. active: # if the plane exists normally # drawing large enemy planes # drawing blood tanks if each. rect. bottom =-50: # play the sound effects of large aircraft (loop playback) else: # if the plane has crashed # Stop playing the sound effects if e3_destroy_index = 0: # Play plane crash sound effects if not (delay % 3): # play a damaged picture every three frames # There are six damaged pictures on large enemy planes if e3_destroy_index = 0: # If the damaged image is played back, reset the airplane attribute score + = 6000 # reset ()
It should be noted that the reason why "if e3_destroy_index = 0:" is added here is that the entire plane's damage process is completed by playback of four (or six) frames, if this restriction is not applied, a score is added for each frame played during the aircraft damage process. In this way, a score of four points will be added for each frame damaged by the minicomputer and the middleware, an additional six points is required for a single damage to the mainframe. Therefore, it is required that extra points, reset, and sound playback be performed only after the last frame of the damaged aircraft is played.
2. Display scores
During the game, you need to display the player score in the upper left corner of the interface in real time, which involves the application of the font module of Pygame. First, call the font module member function in the main function initialization process (before the while LOOP) to create the system font object:
Score_font = pygame. font. SysFont (arial, 48) # define the score font
The score_font object uses the system font "arial" and the font size is 48. Then, the current score is saved in the font object by calling the render () member function of the font object, and the surface format object of the font is returned for subsequent screen printing:
score_text = score_font.render(Score : %s % str(score), True, color_white) screen.blit(score_text, (10, 5))
Here, the current Score is converted to a string using the str () function, and then formatted to the "Score:" string. The second parameter indicates that the current font will automatically enable the anti-aliasing function when printing, the third parameter is the font color. We recommend that you place the two sentences in front of the while loop to make the code more clean. Run the program, and the scores can be correctly and displayed on the screen in real time. Next we will increase the difficulty of the game-the increasing difficulty level.
3. Classification Criteria of design difficulty
Our goal is to increase the difficulty of the game as the user score increases. First, we need to add a global flag level in the main () function to indicate the difficulty of the current game:
Level = 1 # game difficulty level
Next, we will briefly set the following difficulty thresholds: The score is at () the first level of difficulty, the score is at (,) the second level of difficulty, and the score is at (,) the third level of difficulty:
#===================================================== ===== If level = 1 and score> 5000: # If the second difficulty level is reached, three smaller enemy planes, two medium-sized enemy planes, and one large enemy machine will be added, and improves the speed of small enemy planes. level = 2 pass elif level = 2 and score> 30000: # If the third difficulty level is reached, level = 3 pass elif level = 3 and score> 60000: # If the level reaches the fourth difficulty level = 4 pass
The next step is to increase the difficulty of the game based on the corresponding difficulty level. How to increase the difficulty is simply to increase the number of enemy hosts and increase the speed of the enemy machine. Therefore, we define two functions to accomplish this function, first, define the function for increasing the number of aircraft. We have defined this function before:
#================================= Generation control functions of enemy aircraft ================== ====== def add_small_enemies (group1, group2, num): def add_mid_enemies (group1, group2, num): def add_big_enemies (group1, group2, num ):
For more information about the specific functions of these functions, see the previous blog post. The next step is to define the speed increase function. The principle is to increase the value of the speed member variable inside the enemy machine object:
#================================================ ==== Def inc_speed (target, inc): for each in target: each. speed + = inc
Target is the enemy plane genie group to be accelerated, and inc is the degree of acceleration (which can be an acceleration ).
4. Improve the difficulty grading and increasing Mechanism
After defining operation functions such as "add_small_enemies" and "inc_speed", we will provide the complete code for the progressive increase mechanism of difficulty (we suggest placing it at the beginning of the while LOOP ):
#===================================================== ===== If level = 1 and score> 5000: # If the second difficulty level is reached, three small enemy hosts, two medium-sized enemy hosts, and one large enemy server will be added, and the speed of the small enemy machine will be improved. level = 2 level_up_sound.play () add_small_enemies, enemies, 3) add_mid_enemies (mid_enemies, enemies, 2) add_big_enemies (big_enemies, enemies, 1) inc_speed (small_enemies, 1) elif level = 2 and score> 30000: # If the third difficulty level is reached, level = 3 lower () lower (small_enemies, enemies, 3) lower (mid_enemies, enemies, 2) lower (big_enemies, enemies, 1) inc_speed (small_enemies, 1) inc_speed (mid_enemies, 1) elif level = 3 and score> 60000: # If the level reaches the fourth difficulty level = 4 level_up_sound.play () add_small_enemies (small_enemies, enemies, 3) values (mid_enemies, enemies, 2) values (big_enemies, enemies, 1) inc_speed (small_enemies, 1) inc_speed (mid_enemies, 1) inc_speed (big_enemies, 1)
The classification mechanism of this code is very simple. for each level of difficulty added, three small enemy hosts, two medium-sized enemy hosts and one large enemy machine are added on the basis of the previous steps, at the same time, the speed of each model is increased, and special sound effects are also played when the difficulty is improved. The OK program should be able to run correctly here. After a trial, we found that when the score was over 30000, the enemy plane was already flying to the ground to survive the players, in the next blog, we will add super weapons: Super bullets and full-screen bombs to our aircraft.