Python Realizes Aircraft War Project
- 2021-11-14 06:23:19
- OfStack
In this paper, we share the specific code of Python to realize aircraft war for your reference. The specific contents are as follows
plane_main.py
import pygame
from Aircraft war .plane_sprites import *
class PlaneGame(object):
""" Aircraft battle main program """
def __init__(self):
print(" Game initialization ")
# 1. Create a game window
self.screen = pygame.display.set_mode(SCREEN_RECT.size)
# 2. Create a game clock
self.clock = pygame.time.Clock()
# 3. Create game private methods, wizards and wizard groups
self.__create_sprites()
# 4. Set timer time - Create enemy planes
pygame.time.set_timer(CREATE_ENEMY_EVENT, 1000)
pygame.time.set_timer(HERO_FIRE_EVENT, 500)
def __create_sprites(self):
# Create background wizards and wizard groups
bg1 = Background()
bg2 = Background(True)
self.back_group = pygame.sprite.Group(bg1, bg2)
# Creating Elves for Enemy Planes
self.enemy_group = pygame.sprite.Group()
# Creating Hero Elves and Elf Groups
self.hero = Hero()
self.hero_group = pygame.sprite.Group(self.hero)
def start_game(self):
print(" The game begins ----")
while True:
# 1. Set the refresh frame rate
self.clock.tick(FRAME_PER_SEC)
# 2. Time monitoring
self.__event_handler()
# 3. Collision detection
self.__check_collide()
# 4. Update / Draw wizard group
self.__update_sprites()
# 5. Update display
pygame.display.update()
def __event_handler(self):
for event in pygame.event.get():
# Determine whether to quit the game
if event.type == pygame.QUIT:
PlaneGame.__game_over()
elif event.type == CREATE_ENEMY_EVENT:
print(" Enemy planes come out ----")
# Create Enemy Plane Elves
enemy = Enemy()
# Add Enemy Elves to Enemy Elves 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
keys_pressed = pygame.key.get_pressed()
# Determining the corresponding key index value in the tuple
if keys_pressed[pygame.K_RIGHT]:
self.hero.speed = 10
elif keys_pressed[pygame.K_LEFT]:
self.hero.speed = -10
else:
self.hero.speed = 0
def __check_collide(self):
# 1. Bullets destroy enemy planes
pygame.sprite.groupcollide(self.hero.bullets, self.enemy_group, True, True)
# 2. Enemy planes crashed into heroes
enemies = pygame.sprite.spritecollide(self.hero, self.enemy_group, True)
# Determine whether the list has content
if len(enemies) > 0:
# Let the hero die
self.hero.kill()
# End the game
PlaneGame.__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__':
# Creating Game Objects
game = PlaneGame()
# Start the game
game.start_game()
plane_sprites.py
import random
import pygame
# Constant of screen size
SCREEN_RECT = pygame.Rect(0, 0, 512, 768)
# Refresh frame rate
FRAME_PER_SEC = 60
# Create a timer constant for enemy planes
CREATE_ENEMY_EVENT = pygame.USEREVENT
# Hero firing bullets
HERO_FIRE_EVENT = pygame.USEREVENT + 1
class GameSprite(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 object properties
self.image = pygame.image.load(image_name)
self.rect = self.image.get_rect()
self.speed = speed
def update(self):
# Move perpendicular to the screen
self.rect.y += self.speed
class Background(GameSprite):
""" Game background wizard """
def __init__(self, is_alt=False):
# 1. Call the parent class method to realize the creation of the wizard
super().__init__("./images/img_bg_level_5.jpg")
# 2. Judge whether to alternate images
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 off the screen
if self.rect.y >= SCREEN_RECT.height:
self.rect.y = -self.rect.height
class Enemy(GameSprite):
""" Enemy Elf """
def __init__(self):
# 1. Call the parent class method, create the enemy plane wizard, and specify the enemy plane picture at the same time
super().__init__("./images/img-plane_2.png")
# 2. Specify the initial random velocity of enemy aircraft
self.speed = random.randint(2, 5)
# 3. Specify the initial random position of enemy aircraft
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 class method and keep flying vertically
super().update()
# 2. Determine whether to fly out of the screen, and if so, delete enemy planes from the elf group
if self.rect.y >= SCREEN_RECT.height:
print(" The plane flies off the screen and needs to be deleted from the wizard group ---")
# kill Method to remove a wizard from all wizard groups, and the wizard is automatically destroyed
self.kill()
def __del__(self):
# print(" Enemy planes hang up %s" % self.rect)
pass
class Hero(GameSprite):
""" Hero Elf """
def __init__(self):
# 1. Call the parent class method and set the image&speed
super().__init__("./images/hero2.png", 0)
# 2. Set the initial position of the hero
self.rect.centerx = SCREEN_RECT.centerx
self.rect.bottom = SCREEN_RECT.bottom - 20
# 3. Create Bullet Elves
self.bullets = pygame.sprite.Group()
def update(self):
# Heroes move horizontally
self.rect.x += self.speed
# Heroes can't 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):
for i in (0, 1, 2):
# 1. Create a bullet elf
bullet = Bullet()
# 2. Set the wizard position
bullet.rect.bottom = self.rect.y - i*40
bullet.rect.centerx = self.rect.centerx
# 3. Add Bullets to the Elves Group
self.bullets.add(bullet)
class Bullet(GameSprite):
""" Bullet Elf """
def __init__(self):
# Call the parent class method, set the bullet picture and set the initial speed
super().__init__("./images/bullet_11.png", -5)
def update(self):
# Call the parent class method to let the bullet fly out of the screen vertically
super().update()
# Determine if the bullet flies off the screen
if self.rect.bottom < 0:
self.kill()
def __del__(self):
print(" The bullets were destroyed ")