Python3 realizes airplane war game

The examples in this article share the specific code of Python3 to realize the aircraft war game for your reference. The specific content is as follows

1、 Main program: plane_main.py

import pygame
from plane_sprites import*classPlaneGame(object):
# Airplane Wars Main Game
def __init__(self):print("Game initialization")
#1. Create the game window
self.screen = pygame.display.set_mode(SCREEN_RECT.size)
#2. Create a game clock
self.clock = pygame.time.Clock()
#3. Calling private methods to create wizards and wizard groups
self.__create_sprites()
# Set timer event-Create bandit 1s
pygame.time.set_timer(CREATE_ENEMY_EVENT,1000)
pygame.time.set_timer(HERO_FIRE_EVENT,500)
def __create_sprites(self):
# Create background sprites and sprite groups
bg1 =Background()
bg2 =Background(True)
self.back_group = pygame.sprite.Group(bg1,bg2)
# Create a sprite group for the bandit
self.enemy_group = pygame.sprite.Group()
# Create hero's sprites and sprite groups
self.hero =Hero()
self.hero_group = pygame.sprite.Group(self.hero)
def start_game(self):print("Games start...")while True:
#1. Set refresh frame rate
self.clock.tick(FRAME_PER_SEC)
#2. Event monitoring
self.__event_handler()
#3. Impact checking
self.__check_collide()
#4. Update/Draw sprite group
self.__update_sprites()
#5. Update screen display
pygame.display.update()
def __event_handler(self):for event in pygame.event.get():
# Determine whether to exit the game
if event.type == pygame.QUIT:
PlaneGame.__game_over()
elif event.type == CREATE_ENEMY_EVENT:
# print("Enemy plane...")
# Create bandit sprites
enemy =Enemy()
# Add the bandit sprite to the bandit sprite group
self.enemy_group.add(enemy)
elif event.type == HERO_FIRE_EVENT:
self.hero.fire()
# elif event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT:
# print("move to the right....")
# Use the method provided by the keyboard to get the keyboard keys-Key tuple
key_perssed = pygame.key.get_pressed()
# Determine the corresponding key index value in the tuple
if key_perssed[pygame.K_RIGHT]:
self.hero.speed =2
elif key_perssed[pygame.K_LEFT]:
self.hero.speed =-2else:
self.hero.speed =0
def __check_collide(self):
#1. Bullet destroys enemy aircraft
pygame.sprite.groupcollide(self.hero.bullets,self.enemy_group,True,True)
#2. The enemy plane crashes the hero
enemys = pygame.sprite.spritecollide(self.hero,self.enemy_group,True)
#3. Determine whether the list has content
iflen(enemys)0:
# Let the hero sacrifice
self.hero.kill()
# End Game
self.__game_over()
def __update_sprites(self):
self.back_group.update()
self.back_group.draw(self.screen)
self.enemy_group.update()
self.enemy_group.draw(self.screen)
self.hero_group.update()
self.hero_group.draw(self.screen)
self.hero.bullets.update()
self.hero.bullets.draw(self.screen)
@ staticmethod
def __game_over():print("game over")
pygame.quit()exit()if __name__ =='__main__':
# Create game objects
game =PlaneGame()
# Start the game
game.start_game()
  1. Game sprites: plane_sprites.py
import random
import pygame
# Constant for screen size
SCREEN_RECT = pygame.Rect(0,0,480,700)
# Refresh frame rate
FRAME_PER_SEC =60
# Create a timer constant for the bandit
CREATE_ENEMY_EVENT = pygame.USEREVENT
# Hero fired bullet incident
HERO_FIRE_EVENT =pygame.USEREVENT +1classGameSprite(pygame.sprite.Sprite):"""Plane war game wizard"""
def __init__(self, image_name, speed=1):
# Call the initialization method of the parent class
super().__init__()
# Define the properties of the object
self.image = pygame.image.load(image_name)
self.rect = self.image.get_rect()
self.speed = speed
def update(self):
# Move in the vertical direction of the screen
self.rect.y += self.speed
classBackground(GameSprite):"""Game background sprite"""
def __init__(self,is_alt=False):
#1. Call the parent class method to create the wizard(image/rect/speed)
image_name ="./images/background.png"super().__init__(image_name)
#2. Determine whether to alternate images, if it is necessary to set the initial position
if is_alt:
self.rect.y =-self.rect.height
def update(self):
#1. Call the method implementation of the parent class
super().update()
#2. Determine whether to move out of the screen, if it moves out of the screen, it will be set to the top of the screen
if self.rect.y  = SCREEN_RECT.height:
self.rect.y =-self.rect.height
classEnemy(GameSprite):"""Bandit spirit"""
def __init__(self):
#1. Call the parent class method to create the enemy plane sprite and specify the enemy plane picture
super().__init__("./images/enemy1.png")
#2. Set the random initial speed of the bandit
self.speed = random.randint(1,3)
#3. Set the random initial position of the bandit
self.rect.bottom =0
max_x = SCREEN_RECT.width - self.rect.width
self.rect.x = random.randint(0,max_x)
def update(self):
#1. Call the parent method to let the enemy aircraft move in the vertical direction
super().update()
#2. Call whether to fly out of the screen, if so, you need to delete the enemy aircraft from the wizard group
if self.rect.y  = SCREEN_RECT.height:print("The enemy plane flies off the screen...")
# The kill method removes the wizard from all wizard groups
self.kill()
def __del__(self):
pass
# print("The enemy plane hung up%s"% self.rect)classHero(GameSprite):"""Hero elves"""
def __init__(self):
#1. Call the parent class method, set image/speed
super().__init__("./images/me1.png",0)
#2. Set the initial position of the hero
self.rect.centerx = SCREEN_RECT.centerx
self.rect.bottom = SCREEN_RECT.bottom -120
#3. Create a sprite group for bullets
self.bullets = pygame.sprite.Group()
def update(self):
# The hero moves in the horizontal direction
self.rect.x += self.speed
# Control hero cannot leave the screen
if self.rect.x <0:
self.rect.x =0
elif self.rect.right   SCREEN_RECT.right:
self.rect.right = SCREEN_RECT.right
def fire(self):print("Fire a bullet...")for i in(1,2,3):
#1. Create Bullet Wizard
bullet =Bullet()
#2. Set the position of the sprite
bullet.rect.centerx = self.rect.centerx
bullet.rect.bottom = self.rect.y - i*20
#3. Add sprites to the sprite group
self.bullets.add(bullet)classBullet(GameSprite):"""Bullet Wizard"""
def __init__(self):
# Call the parent class method, set the bullet picture, set the initial speed
super().__init__("./images/bullet1.png",-2)
def update(self):
# Call the parent class method to let the bullet fly in the vertical direction
super().update()
# Determine if the bullet flies off the screen
if self.rect.bottom <0:
self.kill()
def __del__(self):
pass
# print("The bullet is destroyed...")

3、 Screenshot of the result:

Attachment: resource pictures of aircraft war

More interesting classic mini game implementation topics, share with you:

C++ classic games summary

Python classic games summary

python tetris game collection

JavaScript classic games are constantly playing

Summary of classic java games

JavaScript classic games summary

The above is the whole content of this article, I hope it will be helpful to everyone's study.

Recommended Posts

Python3 realizes airplane war game
Python realizes spaceship war
Python implements WeChat airplane game
Python realizes apple eating game
Python realizes the guessing game
Use Python to make airplane war games
Use python to realize the aircraft war game
Python implements tic-tac-toe game
Python implements tic-tac-toe game
Python, PyGame game project
Python implements Tetris game
Python implements minesweeper game
Python realizes stitching pictures
Python realizes online translation
Python realizes tank battle
Python implements guessing game
python guess the word game
Python realizes online translation function
Python implements word guessing game
Python implements a guessing game
Python implements digital bomb game
Python implements simple tic-tac-toe game
Python realizes 3D map visualization
Python realizes face sign-in system
Python realizes batch naming photos
Python implements the brick-and-mortar game
Python realizes business card management system
Python simply implements the snake game
Python3 realizes business card management system
Python realizes express price query system
Python realizes online microblog data visualization
Python implements digital bomb game program
Python realizes image recognition car function
Python realizes udp transmission picture function
Python implements the aircraft war project
Python realizes console output color font