Write a classic airplane war in Python
Posted May 30, 2020 • 6 min read
When WeChat 5.0 was released that year, the homepage was set as a newly launched mini-game. It was the WeChat version of Aircraft Wars. Once the game was launched, it was extremely popular, and the pencil-style game interface was also liked by many people.
I have recently revisited this little game. Despite years of lapse, the quality and style of the game are still outdated today. In this article, we use Python to implement this small game. The game is mainly implemented by the third-party module pygame, and the installation can be done using
pip install pygame.
- Operating system:Windows
- Python version:3.6
- Involved modules:pygame, sys, random
The composition of the aircraft war is relatively simple, mainly including:the main interface, players, enemies, bullets, scoreboards, etc. Let's take a look at the specific implementation.
First, let's draw a main interface, the main implementation code is as follows:
# Set the width of the screen SCREEN_WIDTH = 450 # Set the height of the screen SCREEN_HEIGHT = 600 # Initialize the window pygame.init() # Set window title pygame.display.set_caption("Aircraft Battle") # Set the screen size screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), 0, 32) # Hide cursor pygame.mouse.set_visible(False) # Set background bg = pygame.image.load("resources/image/bg.png") # Draw the screen screen.fill(0) # Add background image screen.blit(bg,(0, 0)) # Set the picture of the end of the game bg_game_over = pygame.image.load("resources/image/bg_game_over.png") # Load aircraft resource picture img_plane = pygame.image.load("resources/image/shoot.png") img_start = pygame.image.load("resources/image/start.png") img_pause = pygame.image.load("resources/image/pause.png") img_icon = pygame.image.load("resources/image/plane.png"). convert_alpha() # By the way setting window pygame.display.set_icon(img_icon) # Initial position player_pos = [200, 450]
Look at the effect:
Next, let's define the player's attributes and methods. The main implementation code is as follows:
class Player(pygame.sprite.Sprite): def __init __(self, img, rect, pos): pygame.sprite.Sprite .__ init __(self) self.image =  # Separate aircraft pictures for i in range(len(rect)): self.image.append(img.subsurface(rect [i]). convert_alpha()) # Get the area of the plane self.rect = rect  self.rect.topleft = pos self.speed = 8 # Generate sprite group instance self.bullets = pygame.sprite.Group() self.img_index = 0 # Determine if the plane is hit self.is_hit = False def shoot(self, img): bullet = Bullet(img, self.rect.midtop) # Add bullet instances to the player's bullet group self.bullets.add(bullet) def moveUp(self): # When the top is encountered, set the top to 0 if self.rect.top <= 0: self.rect.top = 0 else: self.rect.top-= self.speed def moveDown(self): # When the bottom is encountered, the setting is always constant if self.rect.top> = SCREEN_HEIGHT-self.rect.height: self.rect.top = SCREEN_HEIGHT-self.rect.height else: self.rect.top + = self.speed def moveLeft(self): # When encountering the left side, keep docking on the left side if self.rect.left <= 0: self.rect.left = 0 else: self.rect.left-= self.speed def moveRight(self): # When encountering the right, stop on the right if self.rect.left> = SCREEN_WIDTH-self.rect.width: self.rect.left = SCREEN_WIDTH-self.rect.width else: self.rect.left + = self.speed
Look at the player's aircraft style:
We then define the properties and methods of the bullet, the main implementation code is as follows:
class Bullet(pygame.sprite.Sprite): def __init __(self, img, pos): pygame.sprite.Sprite .__ init __(self) self.image = img # Set the picture area self.rect = self.image.get_rect() self.rect.midbottom = pos self.speed = 10 def move(self): self.rect.top-= self.speed
Look at the style of the bullet:
After defining the player, let's define the attributes and methods of the enemy aircraft. The main implementation code is as follows:
class Enemy(pygame.sprite.Sprite): def __init __(self, img, explosion_img, pos): pygame.sprite.Sprite .__ init __(self) self.image = img self.rect = self.image.get_rect() self.rect.topleft = pos self.explosion_img = explosion_img self.speed = 2 # Set the destruction sequence self.explosion_index = 0 def move(self): # Enemy bullets can only go down self.rect.top + = self.speed
Finally, let's define the corresponding logic of the game operation, such as:hitting an enemy plane, collision between the player and the enemy plane, generating scores, etc. The main implementation code is as follows:
while running: # Set the game frame rate to 60 clock.tick(60) if not is_pause and not is_game_over: if not player.is_hit: # Set continuous shooting, because 60 frames per second, 15/60 = 0.25 seconds to send a bullet if shoot_frequency%15 == 0: player.shoot(bullet_img) shoot_frequency + = 1 # When the set shooting frequency is greater than 15, set to zero if shoot_frequency> = 15: shoot_frequency = 0 # Control the frequency of generating enemy aircraft if enemy_frequency%50 == 0: # Set the position where the enemy plane appears enemy_pos = [random.randint(0, SCREEN_WIDTH-enemy_rect.width), 0] enemy = Enemy(enemy_img, enemy_explosion_imgs, enemy_pos) enemies.add(enemy) enemy_frequency + = 1 if enemy_frequency> = 100: enemy_frequency = 0 # Control bullet display operation for bullet in player.bullets: bullet.move() if bullet.rect.bottom <0: player.bullets.remove(bullet) # Control the operation of enemy aircraft for enemy in enemies: enemy.move() # Determine whether the enemy plane collides with the player's plane if pygame.sprite.collide_circle(enemy, player): enemies_explosion.add(enemy) enemies.remove(enemy) player.is_hit = True # Set player's plane to be destroyed is_game_over = True # Determine whether the enemy plane is on the interface if enemy.rect.top <0: enemies.remove(enemy) # Set the enemy plane to return to the instance of the enemy plane hit when it collides with the player's aircraft bullet enemy_explosion = pygame.sprite.groupcollide(enemies, player.bullets, 1, 1) for enemy in enemy_explosion: enemies_explosion.add(enemy) # Draw the screen screen.fill(0) # Add background image screen.blit(bg,(0, 0)) # Add player aircraft pictures to the screen if not player.is_hit: screen.blit(player.image [int(player.img_index)], player.rect) player.img_index = shoot_frequency/8 else: if player_explosion_index> 47: is_game_over = True else: player.img_index = player_explosion_index/8 screen.blit(player.image [int(player.img_index)], player.rect) player_explosion_index + = 1 # The effect of the enemy aircraft being hit by bullets is displayed for enemy in enemies_explosion: if enemy.explosion_index == 0: pass if enemy.explosion_index> 7: enemies_explosion.remove(enemy) score + = 100 continue # Display pictures when the enemy plane is hit screen.blit(enemy.explosion_img [int(enemy.explosion_index/2)], enemy.rect) enemy.explosion_index + = 1 # Show bullet player.bullets.draw(screen) # Display enemy aircraft enemies.draw(screen) # Score display effect score_font = pygame.font.Font(None, 36) score_text = score_font.render(str(score), True,(128, 128, 128)) # Set text box text_rect = score_text.get_rect() # Where to put the text text_rect.topleft = [20, 10] # Show score screen.blit(score_text, text_rect) left, middle, right = pygame.mouse.get_pressed() # Pause the game if right == True and not is_game_over: is_pause = True if left == True: # Reset game if is_game_over: is_game_over = False player_rect =  player_rect.append(pygame.Rect(0, 99, 102, 126)) player_rect.append(pygame.Rect(165, 360, 102, 126)) player_rect.append(pygame.Rect(165, 234, 102, 126)) player_rect.append(pygame.Rect(330, 624, 102, 126)) player_rect.append(pygame.Rect(330, 498, 102, 126)) player_rect.append(pygame.Rect(432, 624, 102, 126)) player = Player(img_plane, player_rect, player_pos) bullet_rect = pygame.Rect(1004, 987, 9, 21) bullet_img = img_plane.subsurface(bullet_rect) enemy_rect = pygame.Rect(534, 612, 57, 43) enemy_img = img_plane.subsurface(enemy_rect) enemy_explosion_imgs =  enemy_explosion_imgs.append(img_plane.subsurface(pygame.Rect(267, 347, 57, 43))) enemy_explosion_imgs.append(img_plane.subsurface(pygame.Rect(873, 697, 57, 43))) enemy_explosion_imgs.append(img_plane.subsurface(pygame.Rect(267, 296, 57, 43))) enemy_explosion_imgs.append(img_plane.subsurface(pygame.Rect(930, 697, 57, 43))) enemies = pygame.sprite.Group() enemies_explosion = pygame.sprite.Group() score = 0 shoot_frequency = 0 enemy_frequency = 0 player_explosion_index = 16 # Continue the game if is_pause: is_pause = False # game over if is_game_over: font = pygame.font.SysFont("Microsoft Yahei", 48) text = font.render("Score:" + str(score), True,(255, 0, 0)) text_rect = text.get_rect() text_rect.centerx = screen.get_rect(). centerx text_rect.centery = screen.get_rect(). centery + 70 # Display the game end screen screen.blit(bg_game_over,(0, 0)) # Show score screen.blit(text, text_rect) font = pygame.font.SysFont("Microsoft Yahei", 40) text = font.render("Press Left Mouse to Restart", True,(255, 0, 0)) text_rect = text.get_rect() text_rect.centerx = screen.get_rect(). centerx text_rect.centery = screen.get_rect(). centery + 150 screen.blit(text, text_rect) # Refresh the screen pygame.display.update() # Handle game exit for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit() if not is_pause and not is_game_over: key = pygame.key.get_pressed() if key [K_w]or key [K_UP]: player.moveUp() if key [K_s]or key [K_DOWN]: player.moveDown() if key [K_a]or key [K_LEFT]: player.moveLeft() if key [K_d]or key [K_RIGHT]: player.moveRight()
Let's take a look at the final effect:
Source code and material reply in the background of public number 200530 Get it.