Pythonzh makes childhood games - aircraft war with code

1. Destroy the enemy plane

(1) Add enemy aircraft: Set the enemy aircraft to randomly appear from the top of the window, move to the bottom, and randomly appear from the top of the window. Just set the initial ordinate to a fixed value at the top:

#敌机初始化k = 0enemy_x1 = 0enemy_y1 = 0

(2) The enemy plane moves

  • Enemy planes appear randomly
  • set bandit speed

The bandit appears randomly, you can set the initial position at y = -24 (slightly higher than the y = 0 position at the top of the window). Finally, if you are not very time constrained and want to quickly improve python, the most important The one who is not afraid of hardships, I suggest you use the price: 762459510, that is really good, many people are making rapid progress, you need not be afraid of hardships! You can go and add it~

To set the speed of the enemy aircraft, to determine the speed, you need to set the time t and the distance s. The time is set to t0 each time the program is executed, and the time parameter k is set to move the pixel for each execution of the program to 5, and adjust the parameters according to the actual effect. , the total running time of the aircraft from top to bottom is (1000 + 90)/10 = 109 t0, and the total distance s = (109 t0)*5:

    #随机出现敌人    
#敌人1    
if k == 0:        
enemy_x1 = random.randint(-24, 426)        
enemy_y1 = -24    
elif k == 90:        
k = -1000    
k += 10    
enemy_y1 += 5

In the same way, you can add another enemy aircraft, but the initial position of the aircraft needs to be different from the previous one:

    #敌人2    
if l == 80:        
enemy_x2 = random.randint(-24, 426)        
enemy_y2 = -24    
elif l == 170:        
l = -1170    
l += 10    
enemy_y2 += 5

2. Collision detection

As long as it is detected that the abscissa of the "left edge" of the bullet is between the "left edge" and the "right edge" of the enemy aircraft and the ordinate of the "top" of the bullet is above the "bottom coordinate" of the enemy aircraft, or the abscissa of the "right edge" of the bullet is in Between the "left edge" and the "right edge" of the enemy plane and the ordinate of the bullet "top" is above the "bottom coordinate" of the enemy plane, it is determined that the bullet will collide with the enemy plane: (Because the set bullet speed is very fast, For the time being, it has not been judged that the "top" ordinate of the bullet is above the "bottom coordinate" of the enemy aircraft)

Pythonzh makes childhood games - Plane Wars!  enclosed code

#检测碰撞def collide(button_x1, enemy_x1, enemy_y1, score_count):       
 # x1子弹中心横坐标,X2敌机中心横坐标    
collide_x1 = enemy_x1    
collide_y1 = enemy_y1    
if ((enemy_x1 - 15) <= (button_x1 - 4) <= (enemy_x1 + 15)) 
or ((enemy_x1 - 15) <= (button_x1 + 4) <= (enemy_x1 + 15)):        
collide_x1 = random.randint(-24, 426)        
collide_y1 = -65        
score_count +=1    
print(score_count)    
return collide_x1, 
collide_y1, score_count

3. Increase the sound

Initialize and load:

#音乐初始化及加载pygame.mixer.init()pygame.mixer_music.load('button.wav')

Show bullet sounds:

    #显示子弹声音    pygame.mixer_music.play()

4. Calculate the score

It is relatively simple to calculate the score. Only three digits are selected here, that is, the maximum is 999, but you need to prepare a picture of numbers 1-9 in advance:

    #计算并显示分数    
units = score_count//1%10    
tens = score_count//10%10    
hundreds = score_count//100%10     
units_picture = score_picture_list[units]    
tens_picture = score_picture_list[tens]    hundreds_picture = score_picture_list[hundreds]

Friends who need the complete code can follow the editor and private message "01" in the background to get it!


The text and pictures in this article come from the Internet and their own ideas, which are only for learning and communication, and do not have any commercial use. The copyright belongs to the original author. If you have any questions, please contact us in time for processing.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326801686&siteId=291194637