Python implements the aircraft war project

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

Quoting the framework of the small turtle and adding some new features to it

1、 Our aircraft module

import pygame
 
classMyPlane(pygame.sprite.Sprite):
 def __init__(self,bg_size):
 pygame.sprite.Sprite.__init__(self)
 
 self.image1 = pygame.image.load(r'E:\learn pygame\image\me1.png').convert_alpha()
 self.image2 = pygame.image.load(r'E:\learn pygame\image\me2.png').convert_alpha()
 self.image3 = pygame.image.load(r'E:\learn pygame\image\me_protect.png').convert_alpha()
 self.destroy_images =[]
 self.destroy_images.extend([\
 pygame.image.load(r'E:\learn pygame\image\me_destroy_1.png').convert_alpha(), \
 pygame.image.load(r'E:\learn pygame\image\me_destroy_2.png').convert_alpha(), \
 pygame.image.load(r'E:\learn pygame\image\me_destroy_3.png').convert_alpha(), \
 pygame.image.load(r'E:\learn pygame\image\me_destroy_4.png').convert_alpha() \
  ])
 self.rect = self.image1.get_rect()
 self.width,self.height = bg_size[0],bg_size[1]
 self.rect.left,self.rect.top =(self.width - self.rect.width)// 2,self.height - self.rect.height - 60
 self.speed =10
 self.active = True
 self.invincible = False
 self.mask = pygame.mask.from_surface(self.image1)
 
 def moveUp(self):if self.rect.top   0:
 self.rect.top -= self.speed
 else:
 self.rect.top =0
  
 def moveDown(self):if self.rect.bottom < self.height -30:
 self.rect.top += self.speed
 else:
 self.rect.bottom = self.height -30
  
 def moveLeft(self):if self.rect.left   0:
 self.rect.left -= self.speed
 else:
 self.rect.left =0
  
 def moveRight(self):if self.rect.right < self.width:
 self.rect.left += self.speed
 else:
 self.rect.right = self.width
  
 def reset(self):
 self.rect.left,self.rect.top =(self.width - self.rect.width)// 2,self.height - self.rect.height - 30
 self.active = True 
 self.invincible = True

2、 Enemy aircraft and background animation module

import pygame
from random import*classSmallEnemy(pygame.sprite.Sprite):
energy =1
def __init__(self,bg_size):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(r'E:\learn pygame\image\enemy1.png').convert_alpha()
self.destroy_images =[]
self.destroy_images.extend([\
pygame.image.load(r'E:\learn pygame\image\enemy1_down1.png').convert_alpha(), \
pygame.image.load(r'E:\learn pygame\image\enemy1_down2.png').convert_alpha(), \
pygame.image.load(r'E:\learn pygame\image\enemy1_down3.png').convert_alpha(), \
pygame.image.load(r'E:\learn pygame\image\enemy1_down4.png').convert_alpha() \
])
self.rect = self.image.get_rect()
self.width,self.height = bg_size[0],bg_size[1]
self.speed =2
self.active = True
self.rect.left,self.rect.top = \
randint(0,self.width - self.rect.width), \
randint(-5* self.height,0)
self.mask = pygame.mask.from_surface(self.image)
self.energy = SmallEnemy.energy
self.hit = False
def move(self):if self.rect.top < self.height:
self.rect.top += self.speed
else:
self.reset()
def reset(self):
self.active = True
self.energy = SmallEnemy.energy
self.rect.left,self.rect.top = \
randint(0,self.width - self.rect.width), \
randint(-5* self.height,0)classMidEnemy(pygame.sprite.Sprite):
energy =4 
def __init__(self,bg_size):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(r'E:\learn pygame\image\enemy2.png').convert_alpha()
self.image_hit = pygame.image.load(r'E:\learn pygame\image\enemy2_hit.png').convert_alpha()
self.destroy_images =[]
self.destroy_images.extend([\
pygame.image.load(r'E:\learn pygame\image\enemy2_down1.png').convert_alpha(), \
pygame.image.load(r'E:\learn pygame\image\enemy2_down2.png').convert_alpha(), \
pygame.image.load(r'E:\learn pygame\image\enemy2_down3.png').convert_alpha(), \
pygame.image.load(r'E:\learn pygame\image\enemy2_down4.png').convert_alpha() \
])
self.rect = self.image.get_rect()
self.width,self.height = bg_size[0],bg_size[1]
self.speed =2
self.active = True
self.rect.left,self.rect.top = \
randint(0,self.width - self.rect.width), \
randint(-10* self.height,-self.height)
self.mask = pygame.mask.from_surface(self.image)
self.energy = MidEnemy.energy
self.hit = False
def move(self):if self.rect.top < self.height:
self.rect.top += self.speed
else:
self.reset()
def reset(self):
self.active = True
self.energy = MidEnemy.energy
self.rect.left,self.rect.top = \
randint(0,self.width - self.rect.width), \
randint(-10* self.height,-self.height)classBigEnemy(pygame.sprite.Sprite):
energy =8
def __init__(self,bg_size):
pygame.sprite.Sprite.__init__(self)
self.image1 = pygame.image.load(r'E:\learn pygame\image\enemy3_n1.png').convert_alpha()
self.image2 = pygame.image.load(r'E:\learn pygame\image\enemy3_n2.png').convert_alpha()
self.image_hit = pygame.image.load(r'E:\learn pygame\image\enemy3_hit.png').convert_alpha()
self.destroy_images =[]
self.destroy_images.extend([\
pygame.image.load(r'E:\learn pygame\image\enemy3_down1.png').convert_alpha(), \
pygame.image.load(r'E:\learn pygame\image\enemy3_down2.png').convert_alpha(), \
pygame.image.load(r'E:\learn pygame\image\enemy3_down3.png').convert_alpha(), \
pygame.image.load(r'E:\learn pygame\image\enemy3_down4.png').convert_alpha(), \
pygame.image.load(r'E:\learn pygame\image\enemy3_down5.png').convert_alpha(), \
pygame.image.load(r'E:\learn pygame\image\enemy3_down6.png').convert_alpha() \
])
self.rect = self.image1.get_rect()
self.width,self.height = bg_size[0],bg_size[1]
self.speed =2
self.active = True
self.rect.left,self.rect.top = \
randint(0,self.width - self.rect.width), \
randint(-15* self.height,-5* self.height)
self.mask = pygame.mask.from_surface(self.image1)
self.energy = BigEnemy.energy
self.hit = False
def move(self):if self.rect.top < self.height:
self.rect.top += self.speed
else:
self.reset()
def reset(self):
self.active = True
self.energy = BigEnemy.energy
self.rect.left,self.rect.top = \
randint(0,self.width - self.rect.width), \
randint(-15* self.height,-5* self.height)classCloud1(pygame.sprite.Sprite):
energy =2
def __init__(self,bg_size):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(r'E:\learn pygame\image\cloud1.png').convert_alpha()
self.rect = self.image.get_rect()
self.width,self.height = bg_size[0],bg_size[1]
self.speed =1
self.active = True
self.rect.left,self.rect.top = \
randint(0,self.width - self.rect.width), \
randint(-5* self.height,0)
self.energy = Cloud1.energy
def move(self):if self.rect.top < self.height:
self.rect.top += self.speed
else:
self.reset()
def reset(self):
self.active = True
self.energy = Cloud1.energy
self.rect.left,self.rect.top = \
randint(0,self.width - self.rect.width), \
randint(-5* self.height,0)classCloud2(pygame.sprite.Sprite):
energy =2
def __init__(self,bg_size):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(r'E:\learn pygame\image\cloud1.png').convert_alpha()
self.rect = self.image.get_rect()
self.width,self.height = bg_size[0],bg_size[1]
self.speed =1
self.active = True
self.rect.left,self.rect.top = \
randint(0,self.width - self.rect.width), \
randint(-5* self.height,0)
self.energy = Cloud2.energy
def move(self):if self.rect.top < self.height:
self.rect.top += self.speed
else:
self.reset()
def reset(self):
self.active = True
self.energy = Cloud2.energy
self.rect.left,self.rect.top = \
randint(0,self.width - self.rect.width), \
randint(-5* self.height,0)classBombEnemy(pygame.sprite.Sprite):
energy =10
def __init__(self,bg_size):
pygame.sprite.Sprite.__init__(self)
self.image1 = pygame.image.load(r'E:\learn pygame\image\bomb_enemy1.png').convert_alpha()
self.image2 = pygame.image.load(r'E:\learn pygame\image\bomb_enemy2.png').convert_alpha()
self.rect = self.image1.get_rect()
self.destroy_images =[]
self.destroy_images.extend([ \
pygame.image.load(r'E:\learn pygame\image\bomb_enemy_down1.png').convert_alpha(), \
pygame.image.load(r'E:\learn pygame\image\bomb_enemy_down2.png').convert_alpha(), \
pygame.image.load(r'E:\learn pygame\image\bomb_enemy_down3.png').convert_alpha(), \
pygame.image.load(r'E:\learn pygame\image\bomb_enemy_down4.png').convert_alpha(), \
])
self.width,self.height = bg_size[0],bg_size[1]
self.speed =2
self.active = True
self.rect.left,self.rect.top = \
randint(0,self.width - self.rect.width), \
randint(-15* self.height,-5* self.height)
self.mask = pygame.mask.from_surface(self.image1)
self.energy = BombEnemy.energy
self.hit = False
def move(self):if self.rect.top < self.height:
self.rect.top += self.speed
else:
self.reset()
def reset(self):
self.active = True
self.energy = BombEnemy.energy
self.rect.left,self.rect.top = \
randint(0,self.width - self.rect.width), \
randint(-15* self.height,-5* self.height)classBackGround1(pygame.sprite.Sprite):
def __init__(self,bg_size):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(r'E:\learn pygame\image\background1.png').convert_alpha()
self.rect = self.image.get_rect()
self.width,self.height = bg_size[0],bg_size[1]
self.speed =1
self.active = True
self.rect.center =((self.width // 2), (self.height // 2))
def move(self):if self.rect.top < self.height:
self.rect.top += self.speed
else:
self.reset()
def reset(self):
self.active = True
self.rect.center =((self.width // 2), -(self.height // 2))classBackGround2(pygame.sprite.Sprite):
def __init__(self,bg_size):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(r'E:\learn pygame\image\background1.png').convert_alpha()
self.rect = self.image.get_rect()
self.width,self.height = bg_size[0],bg_size[1]
self.speed =1
self.active = True
self.rect.center =((self.width // 2), -(self.height // 2))
def move(self):if self.rect.top < self.height:
self.rect.top += self.speed
else:
self.reset()
def reset(self):
self.active = True
self.rect.center =((self.width // 2), -(self.height // 2))

3、 Bullet module

import pygame
classBullet1(pygame.sprite.Sprite):
def __init__(self,position):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(r'E:\learn pygame\image\bullet1.png').convert_alpha()
self.rect = self.image.get_rect()
self.rect.left,self.rect.top = position
self.speed =18
self.active = False
self.mask = pygame.mask.from_surface(self.image)
def move(self):
self.rect.top -= self.speed
if self.rect.top <0:
self.active = False
def reset(self,position):
self.rect.left,self.rect.top = position
self.active = True
classBullet2(pygame.sprite.Sprite):
def __init__(self,position):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(r'E:\learn pygame\image\bullet2.png').convert_alpha()
self.rect = self.image.get_rect()
self.rect.left,self.rect.top = position
self.speed =18
self.active = False
self.mask = pygame.mask.from_surface(self.image)
def move(self):
self.rect.top -= self.speed
if self.rect.top <0:
self.active = False
def reset(self,position):
self.rect.left,self.rect.top = position
self.active = True
classBullet3(pygame.sprite.Sprite):
def __init__(self,position):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(r'E:\learn pygame\image\bullet3.png').convert_alpha()
self.rect = self.image.get_rect()
self.rect.left,self.rect.top = position
self.speed =120
self.active = False
self.mask = pygame.mask.from_surface(self.image)
def move(self):
self.rect.top -= self.speed
if self.rect.top <0:
self.active = False
def reset(self,position):
self.rect.left,self.rect.top = position
self.active = True
classBullet4(pygame.sprite.Sprite):
def __init__(self,position,bg_size):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(r'E:\learn pygame\image\bullet4.png').convert_alpha()
self.rect = self.image.get_rect()
self.rect.left,self.rect.top = position
self.width,self.height = bg_size[0],bg_size[1]
self.speed =9
self.active = False
self.mask = pygame.mask.from_surface(self.image)
def move(self):
self.rect.top -= self.speed
if self.rect.top <0:
self.active = False
def reset(self,position):
self.rect.left,self.rect.top = position
self.active = True

4、 Supply module

import pygame
from random import*classBullet_Supply(pygame.sprite.Sprite):
def __init__(self,bg_size):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(r'E:\learn pygame\image\bullet_supply.png').convert_alpha()
self.rect = self.image.get_rect()
self.width,self.height = bg_size[0],bg_size[1]
self.rect.left,self.rect.bottom = \
randint(0,self.width - self.rect.width),-100
self.speed =5
self.active = False
self.mask = pygame.mask.from_surface(self.image)
def move(self):if self.rect.top < self.height:
self.rect.top += self.speed
else:
self.active = False
def reset(self):
self.active = True
self.rect.left,self.rect.bottom = \
randint(0,self.width - self.rect.width),-100classBomb_Supply(pygame.sprite.Sprite):
def __init__(self,bg_size):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(r'E:\learn pygame\image\bomb_supply.png').convert_alpha()
self.rect = self.image.get_rect()
self.width,self.height = bg_size[0],bg_size[1]
self.rect.left,self.rect.bottom = \
randint(0,self.width - self.rect.width),-100
self.speed =5
self.active = False
self.mask = pygame.mask.from_surface(self.image)
def move(self):if self.rect.top < self.height:
self.rect.top += self.speed
else:
self.active = False
def reset(self):
self.active = True
self.rect.left,self.rect.bottom = \
randint(0,self.width - self.rect.width),-100classLaser_Supply(pygame.sprite.Sprite):
def __init__(self,bg_size):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(r'E:\learn pygame\image\laser_supply.png').convert_alpha()
self.rect = self.image.get_rect()
self.width,self.height = bg_size[0],bg_size[1]
self.rect.left,self.rect.bottom = \
randint(0,self.width - self.rect.width),-100
self.speed =5
self.active = False
self.mask = pygame.mask.from_surface(self.image)
def move(self):if self.rect.top < self.height:
self.rect.top += self.speed
else:
self.active = False
def reset(self):
self.active = True
self.rect.left,self.rect.bottom = \
randint(0,self.width - self.rect.width),-100classFire_Supply(pygame.sprite.Sprite):
def __init__(self,bg_size):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(r'E:\learn pygame\image\fire_supply.png').convert_alpha()
self.rect = self.image.get_rect()
self.width,self.height = bg_size[0],bg_size[1]
self.rect.left,self.rect.bottom = \
randint(0,self.width - self.rect.width),-100
self.speed =5
self.active = False
self.mask = pygame.mask.from_surface(self.image)
def move(self):if self.rect.top < self.height:
self.rect.top += self.speed
else:
self.active = False
def reset(self):
self.active = True
self.rect.left,self.rect.bottom = \
randint(0,self.width - self.rect.width),-100classMe_Protect_Supply(pygame.sprite.Sprite):
def __init__(self,bg_size):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(r'E:\learn pygame\image\me_protect_supply.png').convert_alpha()
self.rect = self.image.get_rect()
self.width,self.height = bg_size[0],bg_size[1]
self.rect.left,self.rect.bottom = \
randint(0,self.width - self.rect.width),-100
self.speed =5
self.active = False
self.mask = pygame.mask.from_surface(self.image)
def move(self):if self.rect.top < self.height:
self.rect.top += self.speed
else:
self.active = False
def reset(self):
self.active = True
self.rect.left,self.rect.bottom = \
randint(0,self.width - self.rect.width),-100classLife_Supply(pygame.sprite.Sprite):
def __init__(self,bg_size):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(r'E:\learn pygame\image\life_supply.png').convert_alpha()
self.rect = self.image.get_rect()
self.width,self.height = bg_size[0],bg_size[1]
self.rect.left,self.rect.bottom = \
randint(0,self.width - self.rect.width),-100
self.speed =5
self.active = False
self.mask = pygame.mask.from_surface(self.image)
def move(self):if self.rect.top < self.height:
self.rect.top += self.speed
else:
self.active = False
def reset(self):
self.active = True
self.rect.left,self.rect.bottom = \
randint(0,self.width - self.rect.width),-100

5、 Falling stars module (not perfect yet)

import pygame
classStar(pygame.sprite.Sprite):
energy =1
def __init__(self,bg_size,star_left,star_top):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(r'E:\learn pygame\image\star_bling1.png').convert_alpha()
self.rect = self.image.get_rect()
self.star_images =[]
self.star_images.extend([pygame.image.load(r'E:\learn pygame\image\star_bling1.png').convert_alpha(),pygame.image.load(r'E:\learn pygame\image\star_bling2.png').convert_alpha(),pygame.image.load(r'E:\learn pygame\image\star_bling3.png').convert_alpha(),pygame.image.load(r'E:\learn pygame\image\star_bling4.png').convert_alpha(),pygame.image.load(r'E:\learn pygame\image\star_bling5.png').convert_alpha(),pygame.image.load(r'E:\learn pygame\image\star_bling6.png').convert_alpha(),pygame.image.load(r'E:\learn pygame\image\star_bling7.png').convert_alpha(),pygame.image.load(r'E:\learn pygame\image\star_bling8.png').convert_alpha(),pygame.image.load(r'E:\learn pygame\image\star_bling9.png').convert_alpha(),pygame.image.load(r'E:\learn pygame\image\star_bling10.png').convert_alpha()]) 
self.width,self.height = bg_size[0],bg_size[1]
self.rect.left,self.rect.top = star_left,star_top
self.speed =1
self.active = True
self.is_star = False
self.star_is_touch = False
def move(self):if self.rect.top < self.height:
self.rect.top += self.speed
else:
self.reset()
def reset(self):
self.active = True

6、 Main module

import pygame
import sys
import traceback
import time
import myplane
import enemy
import bullet
import supply
import star
from random import*from pygame.locals import*
pygame.init()
pygame.mixer.init()
bg_size = width, height =450,675
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption('Aircraft war')
background = pygame.image.load(r'E:\learn pygame\image\background1.png').convert()
background_rect = background.get_rect()
BLACK =(0,0,0)
WHITE =(255,255,255)
GREEN =(0,255,0)
RED =(255,0,0)
# Load game music
pygame.mixer.music.load(r'E:\learn pygame\music\bg_music.ogg')
pygame.mixer.music.set_volume(0.2)
bomb_enemy_fly_sound = pygame.mixer.Sound(r'E:\learn pygame\music\bomb_enemy_flying.wav')
bomb_enemy_fly_sound.set_volume(0.1)
enemy3_fly_sound = pygame.mixer.Sound(r'E:\learn pygame\music\enemy3_flying.wav')
enemy3_fly_sound.set_volume(0.1)
enemy1_down_sound = pygame.mixer.Sound(r'E:\learn pygame\music\enemy1_down.wav')
enemy1_down_sound.set_volume(0.5)
enemy2_down_sound = pygame.mixer.Sound(r'E:\learn pygame\music\enemy2_down.wav')
enemy2_down_sound.set_volume(0.5)
enemy3_down_sound = pygame.mixer.Sound(r'E:\learn pygame\music\bomb_enemy_down.wav')
enemy3_down_sound.set_volume(0.5)
bomb_enemy_down_sound = pygame.mixer.Sound(r'E:\learn pygame\music\bomb_enemy_down.wav')
bomb_enemy_down_sound.set_volume(0.5)
me_down_sound = pygame.mixer.Sound(r'E:\learn pygame\music\me_down.wav')
me_down_sound.set_volume(0.5)
upgrade_sound = pygame.mixer.Sound(r'E:\learn pygame\music\upgrade.wav')
upgrade_sound.set_volume(0.5)
bomb_sound = pygame.mixer.Sound(r'E:\learn pygame\music\use_bomb.wav')
bomb_sound.set_volume(0.5)
supply_sound = pygame.mixer.Sound(r'E:\learn pygame\music\supply.wav')
supply_sound.set_volume(0.5)
get_bomb_sound = pygame.mixer.Sound(r'E:\learn pygame\music\get_bomb.wav')
get_bomb_sound.set_volume(0.5)
get_bullet_sound = pygame.mixer.Sound(r'E:\learn pygame\music\get_bullet.wav')
get_bullet_sound.set_volume(0.5)
get_laser_sound = pygame.mixer.Sound(r'E:\learn pygame\music\get_laser.wav')
get_laser_sound.set_volume(0.5)
get_fire_sound = pygame.mixer.Sound(r'E:\learn pygame\music\get_fire.wav')
get_fire_sound.set_volume(0.5)
get_me_protect_sound = pygame.mixer.Sound(r'E:\learn pygame\music\get_me_protect.wav')
get_me_protect_sound.set_volume(0.5)
get_life_sound = pygame.mixer.Sound(r'E:\learn pygame\music\get_life.wav')
get_life_sound.set_volume(0.5)
bullet_sound = pygame.mixer.Sound(r'E:\learn pygame\music\bullet.wav')
bullet_sound.set_volume(0.2)
fire_bullet_sound = pygame.mixer.Sound(r'E:\learn pygame\music\fire_bullet.wav')
fire_bullet_sound.set_volume(0.2)
def add_small_enemies(group1,group2,num):for i inrange(num):
e1 = enemy.SmallEnemy(bg_size)
group1.add(e1)
group2.add(e1)
def add_mid_enemies(group1,group2,num):for i inrange(num):
e2 = enemy.MidEnemy(bg_size)
group1.add(e2)
group2.add(e2)
def add_big_enemies(group1,group2,num):for i inrange(num):
e3 = enemy.BigEnemy(bg_size)
group1.add(e3)
group2.add(e3)
def add_bomb_enemies(group1,group2,num):for i inrange(num):
e3 = enemy.BombEnemy(bg_size)
group1.add(e3)
group2.add(e3)
def add_cloud1_enemies(group1,group2,num):for i inrange(num):
e4 = enemy.Cloud1(bg_size)
group1.add(e4)
group2.add(e4)
def add_cloud2_enemies(group1,group2,num):for i inrange(num):
e5 = enemy.Cloud2(bg_size)
group1.add(e5)
group2.add(e5)
def add_background1(group1,group2,num):for i inrange(num):
e6 = enemy.BackGround1(bg_size)
group1.add(e6)
group2.add(e6)
def add_background2(group1,group2,num):for i inrange(num):
e7 = enemy.BackGround2(bg_size)
group1.add(e7)
group2.add(e7)
def add_star(group1,group2,star_left,star_top,num):for i inrange(num):
e8 = star.Star(bg_size,star_left,star_top)
group1.add(e8)
group2.add(e8)
def inc_speed(inc,*targets):for each in targets:for every in each:
every.speed += inc
def inc_speed_background(inc,target):for each in target:
each.speed += inc 
def inc_speed_me(inc,target):
target.speed += inc 
def inc_speeds(inc,*targets):for each in targets:for every in each:
every.speed += inc
def main():
pygame.mixer.music.play(-1)
# Generate classes of our aircraft, background, enemy aircraft, clouds, and stars for collision detection
me = myplane.MyPlane(bg_size)
backgrounds = pygame.sprite.Group()
enemies = pygame.sprite.Group()
clouds = pygame.sprite.Group()
stars = pygame.sprite.Group()
bullets = pygame.sprite.Group()
# Generate background 1
background1 = pygame.sprite.Group()add_background1(background1,backgrounds,1)
# Generate background 2
background2 = pygame.sprite.Group()add_background2(background2,backgrounds,1)
# Spawn enemy small aircraft
small_enemies = pygame.sprite.Group()add_small_enemies(small_enemies,enemies,15)
# Spawn enemy medium aircraft
mid_enemies = pygame.sprite.Group()add_mid_enemies(mid_enemies,enemies,4)
# Spawn enemy large aircraft
big_enemies = pygame.sprite.Group()add_big_enemies(big_enemies,enemies,2)
# Generate enemy super bombs
bomb_enemies = pygame.sprite.Group()add_bomb_enemies(bomb_enemies,enemies,1)
# Generate clouds 1
cloud1_enemies = pygame.sprite.Group()add_cloud1_enemies(cloud1_enemies,clouds,1)
# Generate clouds 2
cloud2_enemies = pygame.sprite.Group()add_cloud2_enemies(cloud2_enemies,clouds,1)
# Generate stars
star = pygame.sprite.Group()
# Generate ordinary bullets
bullet1 =[]
bullet1_index =0
BULLET1_NUM =6for i inrange(BULLET1_NUM):
bullet1.append(bullet.Bullet1(me.rect.midtop))
clock = pygame.time.Clock()
# Generate super bullet
bullet2 =[]
bullet2_index =0
BULLET2_NUM =12for i inrange(BULLET2_NUM // 2):
bullet2.append(bullet.Bullet2((me.rect.centerx -33,me.rect.centery)))
bullet2.append(bullet.Bullet2((me.rect.centerx +30,me.rect.centery)))
clock = pygame.time.Clock()
# Generate laser
bullet3 =[]
bullet3_index =0
BULLET3_NUM =1for i inrange(BULLET3_NUM):
bullet3.append(bullet.Bullet3((me.rect.midtop)))
clock = pygame.time.Clock()
# Fire bomb
bullet4 =[]
bullet4_index =0
BULLET4_NUM =4for i inrange(BULLET4_NUM):
bullet4.append(bullet.Bullet4((me.rect.midtop),bg_size))
clock = pygame.time.Clock()
# Shot index
e1_destroy_index =0
e2_destroy_index =0
e3_destroy_index =0
e4_destroy_index =0
me_destroy_index =0
# Statistics score and stars
score =0
score_font = pygame.font.Font(r'E:\learn pygame\font\font.ttf',20)
# Count stars and load star pictures
star_score =0
star_score_font = pygame.font.Font(r'E:\learn pygame\font\font.ttf',20)
star_score_image = pygame.image.load(r'E:\learn pygame\image\star_score.png').convert_alpha()
star_score_rect = star_score_image.get_rect()
# Flag whether to pause
paused = False
paused_nor_image = pygame.image.load(r'E:\learn pygame\image\pause_nor.png').convert_alpha()
paused_pressed_image = pygame.image.load(r'E:\learn pygame\image\pause_pressed.png').convert_alpha()
resume_nor_image = pygame.image.load(r'E:\learn pygame\image\resume_nor.png').convert_alpha()
resume_pressed_image = pygame.image.load(r'E:\learn pygame\image\resume_pressed.png').convert_alpha()
paused_rect = paused_nor_image.get_rect()
paused_rect.left,paused_rect.top = width - paused_rect.width -10,10
paused_image = paused_nor_image
# Game pause screen
continues_image = pygame.image.load(r'E:\learn pygame\image\continues.png').convert_alpha()
continues_rect = continues_image.get_rect() 
continues_rect.left,continues_rect.top = \
( width - continues_rect.width)// 2, \(height - continues_rect.height)// 2
# Set difficulty level
level =1
# Full screen bomb
bomb_image = pygame.image.load(r'E:\learn pygame\image\bomb.png').convert_alpha()
bomb_rect = bomb_image.get_rect()
bomb_font = pygame.font.Font(r'E:\learn pygame\font\font.ttf',30)
bomb_num =3
# Distribute a supply pack every 30 seconds
bullet_supply = supply.Bullet_Supply(bg_size)
bomb_supply = supply.Bomb_Supply(bg_size)
laser_supply = supply.Laser_Supply(bg_size)
fire_supply = supply.Fire_Supply(bg_size)
me_protect_supply = supply.Me_Protect_Supply(bg_size)
life_supply = supply.Life_Supply(bg_size)
SUPPLY_TIME = USEREVENT
pygame.time.set_timer(SUPPLY_TIME,18*1000)
# Whether the sign uses normal bullets
is_normal_bullet = True
# Super bullet timer
DOUBLE_BULLET_TIME = USEREVENT +1
# Whether the logo uses super bullets
is_double_bullet = False
# Disable our invincibility timer
INVINCIBLE_TIME = USEREVENT +2
# Laser timer
LASER_BULLET_TIME = USEREVENT +3
# Whether the logo uses laser
is_laser_bullet = False
# Flame bomb timer
FIRE_BULLET_TIME = USEREVENT +4
# Whether the logo uses flame bombs
is_fire_bullet = False
# Protective cover timer
ME_PROTECT_BULLET_TIME = USEREVENT +5
# Whether to use a protective cover
is_me_protect_bullet = False
# Death generates star image index
s1_star_index =0
# Number of lives
life_image = pygame.image.load(r'E:\learn pygame\image\life.png').convert_alpha()
life_rect = life_image.get_rect()
life_num =3
# Game start screen
logo_image = pygame.image.load(r'E:\learn pygame\image\logo.png').convert_alpha()
logo_rect = logo_image.get_rect()
logo_rect.left,logo_rect.top = \
( width - logo_rect.width)// 2, \(height - logo_rect.height)// 2
begingame_image = pygame.image.load(r'E:\learn pygame\image\begingame.png').convert_alpha()
begingame_rect = begingame_image.get_rect()
begingame_rect.left,begingame_rect.top = \
( width - begingame_rect.width)// 2, \(logo_rect.bottom +10)
# Game over screen
gameover_font = pygame.font.Font(r'E:\learn pygame\font\font.ttf',48)
again_image = pygame.image.load(r'E:\learn pygame\image\again.png').convert_alpha()
again_rect = again_image.get_rect()
gameover_image = pygame.image.load(r'E:\learn pygame\image\gameover.png').convert_alpha()
gameover_rect = gameover_image.get_rect()
# Used to prevent repeated opening of log files
recorded = False
# Used to switch pictures
switch_image = True
# For delay
delay =100
# Games start
running = True
# Officially start the game
start = False
while running:for event in pygame.event.get():if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == MOUSEBUTTONDOWN:if event.button ==1 and paused_rect.collidepoint(event.pos):
paused = not paused
if paused:
pygame.time.set_timer(SUPPLY_TIME,0)
pygame.mixer.music.pause()
pygame.mixer.pause()else:
pygame.time.set_timer(SUPPLY_TIME,30*1000)
pygame.mixer.music.unpause()
pygame.mixer.unpause()   
elif event.type == MOUSEMOTION:if paused_rect.collidepoint(event.pos):if paused:
paused_image = resume_pressed_image
else:
paused_image = paused_pressed_image
else:if paused:
paused_image = resume_nor_image
else:
paused_image = paused_nor_image
elif event.type == KEYDOWN:if event.key == K_SPACE:if bomb_num:
bomb_num -=1
bomb_sound.play()for each in enemies:if each.rect.bottom   0:
each.active = False
elif event.type == SUPPLY_TIME:
supply_sound.play()ifchoice([1,2,3,4,5,6])==1:
bomb_supply.reset()
elif choice([1,2,3,4,5,6])==2:
bullet_supply.reset()
elif choice([1,2,3,4,5,6])==3:
laser_supply.reset()
elif choice([1,2,3,4,5,6])==4:
fire_supply.reset()
elif choice([1,2,3,4,5,6])==5:
me_protect_supply.reset()else:
life_supply.reset()
elif event.type == DOUBLE_BULLET_TIME:
is_double_bullet = False
pygame.time.set_timer(DOUBLE_BULLET_TIME,0)
elif event.type == INVINCIBLE_TIME:
me.invincible = False
pygame.time.set_timer(INVINCIBLE_TIME,0)
elif event.type == LASER_BULLET_TIME:
is_laser_bullet = False
pygame.time.set_timer(LASER_BULLET_TIME,0)
elif event.type == FIRE_BULLET_TIME:
is_fire_bullet = False
is_normal_bullet = True
pygame.time.set_timer(FIRE_BULLET_TIME,0) 
elif event.type == ME_PROTECT_BULLET_TIME:
is_me_protect_bullet = False
pygame.time.set_timer(ME_PROTECT_BULLET_TIME,0) 
# Draw moving background
for each1 in background1:
screen.blit(each1.image,each1.rect)
each1.move()if each1.rect.height   0:for each2 in background2: 
screen.blit(each2.image,each2.rect)
each2.move()
# Increase difficulty based on user score
if level ==1 and score   100000:
level =2
upgrade_sound.play()
# Add 3 small enemy aircraft, 2 medium enemy aircraft and 1 large enemy aircraft
add_small_enemies(small_enemies,enemies,3)add_mid_enemies(mid_enemies,enemies,2)add_big_enemies(big_enemies,enemies,1)
# Increase enemy speed
inc_speed(1,small_enemies)
# Increase background speed
elif level ==2 and score   300000:
level =3
upgrade_sound.play()
# Added 5 small enemy aircraft, 3 medium enemy aircraft and 2 large enemy aircraft
add_small_enemies(small_enemies,enemies,5)add_mid_enemies(mid_enemies,enemies,3)add_big_enemies(big_enemies,enemies,2)
# Increase speed of enemy aircraft, clouds, background, and stars
inc_speeds(1,enemies,clouds,star)
# inc_speed_background(1,background1)
# inc_speed_background(1,background2)inc_speed_me(3,me)
elif level ==3 and score   8000000:
level =4
upgrade_sound.play()
# Added 5 small enemy aircraft, 3 medium enemy aircraft, 2 large enemy aircraft and 1 super bomb
add_small_enemies(small_enemies,enemies,5)add_mid_enemies(mid_enemies,enemies,3)add_big_enemies(big_enemies,enemies,2)add_bomb_enemies(big_enemies,enemies,1)
# Increase enemy speed
inc_speeds(1,enemies,clouds,star)inc_speed_background(1,background1)inc_speed_background(1,background2)inc_speed_me(3,me)
elif level ==4 and score   1200000:
level =5
upgrade_sound.play()
# Added 5 small enemy aircraft, 3 medium enemy aircraft, 2 large enemy aircraft and 1 super bomb
add_small_enemies(small_enemies,enemies,5)add_mid_enemies(mid_enemies,enemies,3)add_big_enemies(big_enemies,enemies,2)add_bomb_enemies(big_enemies,enemies,1)
# Increase enemy speed
inc_speeds(1,enemies,clouds,star)inc_speed_background(1,background1)inc_speed_background(1,background2)inc_speed_me(3,me)
# Draw start interface
if not start:
screen.blit(logo_image,logo_rect)    
screen.blit(begingame_image,begingame_rect)
# Detect user&#39;s mouse operation
# If the user presses the left mouse button
if pygame.mouse.get_pressed()[0]:
# Get mouse coordinates
pos = pygame.mouse.get_pos()
# If the user clicks to restart
if begingame_rect.left < pos[0]< begingame_rect.right and \
begingame_rect.top < pos[1]< begingame_rect.bottom:
start = True
elif life_num and not paused and start: 
# Detect user&#39;s keyboard operation
key_pressed = pygame.key.get_pressed()if key_pressed[K_w] or key_pressed[K_UP]:
me.moveUp()if key_pressed[K_s] or key_pressed[K_DOWN]:
me.moveDown()if key_pressed[K_a] or key_pressed[K_LEFT]:
me.moveLeft()if key_pressed[K_d] or key_pressed[K_RIGHT]:
me.moveRight()
# Draw clouds
for each in cloud1_enemies:
each.move()
screen.blit(each.image,each.rect)for each in cloud2_enemies:
each.move()
screen.blit(each.image,each.rect)
# Draw full-screen bomb supplies and check if they are obtained
if bomb_supply.active:
bomb_supply.move()
screen.blit(bomb_supply.image,bomb_supply.rect)if pygame.sprite.collide_mask(bomb_supply,me):
get_bomb_sound.play()if bomb_num <3:
bomb_num +=1
bomb_supply.active = False
# Draw super bullet supplies and check if they are obtained
if bullet_supply.active:
bullet_supply.move()
screen.blit(bullet_supply.image,bullet_supply.rect)if pygame.sprite.collide_mask(bullet_supply,me):
get_bullet_sound.play()
is_double_bullet = True
pygame.time.set_timer(DOUBLE_BULLET_TIME,18*1000)
bullet_supply.active = False
# Draw laser supplies and check if they are obtained
if laser_supply.active:
laser_supply.move()
screen.blit(laser_supply.image,laser_supply.rect)if pygame.sprite.collide_mask(laser_supply,me):
get_laser_sound.play()
is_laser_bullet = True
pygame.time.set_timer(LASER_BULLET_TIME,18*1000)
laser_supply.active = False
# Draw fire bomb supplies and check if they are obtained
if fire_supply.active:
fire_supply.move()
screen.blit(fire_supply.image,fire_supply.rect)if pygame.sprite.collide_mask(fire_supply,me):
get_fire_sound.play()
is_fire_bullet = True
is_normal_bullet = False
pygame.time.set_timer(FIRE_BULLET_TIME,18*1000)
fire_supply.active = False
# Draw the protective cover supply and check if it is obtained
if me_protect_supply.active:
me_protect_supply.move()
screen.blit(me_protect_supply.image,me_protect_supply.rect)if pygame.sprite.collide_mask(me_protect_supply,me):
get_me_protect_sound.play()
is_me_protect_bullet = True
pygame.time.set_timer(ME_PROTECT_BULLET_TIME,15*1000)
me_protect_supply.active = False
# Draw life supplies and check if they are obtained
if life_supply.active:
life_supply.move()
screen.blit(life_supply.image,life_supply.rect)if pygame.sprite.collide_mask(life_supply,me):
get_life_sound.play()if life_num <3:
life_num +=1
life_supply.active = False
# Fire ordinary bullets, lasers, double bullets
ifnot(delay %10):
bullet_sound.play()if is_double_bullet:
bullets = bullet2
bullets[bullet2_index].reset((me.rect.centerx -33,me.rect.centery))
bullets[bullet2_index +1].reset((me.rect.centerx +33,me.rect.centery))  
bullet2_index =(bullet2_index +2)% BULLET2_NUM
elif is_laser_bullet:
bullets = bullet3
bullets[bullet3_index].reset((me.rect.centerx -20,me.rect.top -70))
bullet3_index =(bullet3_index +1)% BULLET3_NUM
else:
bullets = bullet1
bullets[bullet1_index].reset(me.rect.midtop)
bullet1_index =(bullet1_index +1)% BULLET1_NUM
# Ordinary bullets, flame bombs occur
ifnot(delay %10):if is_normal_bullet:
bullet_sound.play()else:
fire_bullet_sound.play()if is_fire_bullet:
bullets = bullet4
bullets[bullet4_index].reset((me.rect.centerx -20,me.rect.top -70))
bullet4_index =(bullet4_index +1)% BULLET4_NUM
# Check if the bullet hits the enemy aircraft
for b in bullets:if b.active:
b.move()
screen.blit(b.image,b.rect)
enemy_hit = pygame.sprite.spritecollide(b,enemies,False,pygame.sprite.collide_mask)if enemy_hit:
b.active = False
for e in enemy_hit:   
# Fireball hits 2 HP
if b == bullet4:if e in mid_enemies or e in big_enemies or e in bomb_enemies:
e.hit = True
e.energy -=2
# Enemy plane dead
if e.energy ==0:
e.active = False
# Record the enemy&#39;s death position and generate stars
enemies_death_down_left,enemies_death_down_top = e.rect.left,e.rect.top
print(enemies_death_down_left,enemies_death_down_top)add_star(star,stars,starenemies_death_down_left,enemies_death_down_top,1)else:
e.active = False
# Normal bullet hits 1 HP
else:if e in mid_enemies or e in big_enemies or e in bomb_enemies:
e.hit = True
e.energy -=1
# Enemy plane dead
if e.energy ==0:
e.active = False
# Record the enemy&#39;s death position and generate stars
enemies_death_down_left,enemies_death_down_top = e.rect.left,e.rect.top
print(enemies_death_down_left,enemies_death_down_top)add_star(star,stars,enemies_death_down_left,enemies_death_down_top,1)else:
e.active = False
# Draw the generated stars
for each in star:if each.active:
each.move()ifnot(delay %3):
screen.blit(each.image,each.rect)
screen.blit(each.star_images[s1_star_index],each.rect)
s1_star_index =(s1_star_index +1)%10
star_touch = pygame.sprite.spritecollide(me,star,False,pygame.sprite.collide_mask)if star_touch:for s in star_touch:
s.active = False
elif each.rect.top   height:
each.active = False
# Draw the enemy super bomb
for each in bomb_enemies:if each.active:
each.move()if switch_image:
screen.blit(each.image1,each.rect)else:
screen.blit(each.image2,each.rect)
# Draw blood tank
pygame.draw.line(screen,BLACK, \
( each.rect.left,each.rect.top -5), \
( each.rect.right,each.rect.top -5), \
2)
# When life is greater than 20%Display green, otherwise display red
energy_remain = each.energy / enemy.BombEnemy.energy
if energy_remain   0.2:
energy_color = GREEN
else:
energy_color = RED
pygame.draw.line(screen,energy_color, \
( each.rect.left,each.rect.top -5),\
( each.rect.left + each.rect.width * energy_remain, \
each.rect.top -5),2)
# Will appear on the screen, playing sound effects
if(height +50)   each.rect.bottom   -50:
bomb_enemy_fly_sound.play(-1)if each.rect.bottom(height +50):
bomb_enemy_fly_sound.stop()
each.reset()else:
# destroy
ifnot(delay %3):if e4_destroy_index ==0:
bomb_enemy_down_sound.play()  
screen.blit(each.destroy_images[e4_destroy_index],each.rect)
e4_destroy_index =(e4_destroy_index +1)%4if e4_destroy_index ==0:
bomb_enemy_fly_sound.stop()
score +=12000
each.reset()
# Draw a large enemy plane
for each in big_enemies:if each.active:
each.move()if each.hit:
# Draw the hit special effects
screen.blit(each.image_hit,each.rect)
each.hit = False
else:if switch_image:
screen.blit(each.image1,each.rect)else:
screen.blit(each.image2,each.rect)
# Draw blood tank
pygame.draw.line(screen,BLACK, \
( each.rect.left,each.rect.top -5), \
( each.rect.right,each.rect.top -5), \
2)
# When life is greater than 20%Display green, otherwise display red
energy_remain = each.energy / enemy.BigEnemy.energy
if energy_remain   0.2:
energy_color = GREEN
else:
energy_color = RED
pygame.draw.line(screen,energy_color, \
( each.rect.left,each.rect.top -5),\
( each.rect.left + each.rect.width * energy_remain, \
each.rect.top -5),2)
# Will appear on the screen, playing sound effects
if height   each.rect.bottom   -50:
enemy3_fly_sound.play(-1)if each.rect.bottom   height:
enemy3_fly_sound.stop()
each.active = False
each.reset()else:
# destroy
ifnot(delay %3):if e3_destroy_index ==0:
enemy3_down_sound.play(-1)
screen.blit(each.destroy_images[e3_destroy_index],each.rect)
e3_destroy_index =(e3_destroy_index +1)%6if e3_destroy_index ==0:
enemy3_fly_sound.stop()
score +=10000
each.reset()
# Draw medium enemy aircraft
for each in mid_enemies:if each.active:
each.move()if each.hit:
# Draw the hit special effects
screen.blit(each.image_hit,each.rect)
each.hit = False
else:
screen.blit(each.image,each.rect)
# Draw blood tank
pygame.draw.line(screen,BLACK, \
( each.rect.left,each.rect.top -5), \
( each.rect.right,each.rect.top -5), \
2)
# When life is greater than 20%Display green, otherwise display red
energy_remain = each.energy / enemy.MidEnemy.energy
if energy_remain   0.2:
energy_color = GREEN
else:
energy_color = RED
pygame.draw.line(screen,energy_color, \
( each.rect.left,each.rect.top -5),\
( each.rect.left + each.rect.width * energy_remain, \
each.rect.top -5),2)else:
# destroy
ifnot(delay %3):if e2_destroy_index ==0:
enemy2_down_sound.play()
screen.blit(each.destroy_images[e2_destroy_index],each.rect)
e2_destroy_index =(e2_destroy_index +1)%4if e2_destroy_index ==0:
score +=6000
each.reset()
# Drawing small enemy aircraft
for each in small_enemies:if each.active:
each.move()
screen.blit(each.image,each.rect)else:
# destroy
ifnot(delay %3):if e1_destroy_index ==0:
enemy1_down_sound.play()  
screen.blit(each.destroy_images[e1_destroy_index],each.rect)
e1_destroy_index =(e1_destroy_index +1)%4if e1_destroy_index ==0:
score +=1000
each.reset()
# Check if our plane has been hit
enemies_down = pygame.sprite.spritecollide(me,enemies,False,pygame.sprite.collide_mask)if enemies_down and not me.invincible:
# Turn on the protective cover protection function
if is_me_protect_bullet:
me.active = True
else:
me.active = False
for e in enemies_down:
e.active = False
# Draw the protective cover
if is_me_protect_bullet:if switch_image:
screen.blit(me.image1,me.rect)
screen.blit(me.image3,me.rect)else:
screen.blit(me.image2,me.rect)
# Draw our plane
elif me.active:if switch_image:
screen.blit(me.image1,me.rect)else:
screen.blit(me.image2,me.rect)
# destroy
else:ifnot(delay %3):if me_destroy_index ==0:
me_down_sound.play()
screen.blit(me.destroy_images[me_destroy_index],me.rect)
me_destroy_index =(me_destroy_index +1)%4if me_destroy_index ==0:
life_num -=1
me.reset()
pygame.time.set_timer(INVINCIBLE_TIME,3*1000)
# Draw the number of full-screen bombs
bomb_text = bomb_font.render('x %d'% bomb_num,True,WHITE)
text_rect = bomb_text.get_rect()
screen.blit(bomb_image,(0,height - bomb_rect.height))
screen.blit(bomb_text,(bomb_rect.width -10,height -10- text_rect.height))
# Plot the remaining lives
if life_num:for i inrange(life_num):
screen.blit(life_image, \
( width -10-(i +1)* life_rect.width, \
height -10- life_rect.height))
# Draw score
score_text = score_font.render('Score:%s'%str(score),True,WHITE)
screen.blit(score_text,(10,5))
star_score_text = star_score_font.render('Stars:%s'%str(star_score),True,WHITE)
screen.blit(star_score_text,(10,25))
# Draw pause button
screen.blit(paused_image,paused_rect)
# Draw pause game interface
elif paused:
screen.blit(continues_image,continues_rect)
# Detect user&#39;s mouse operation
# Such as-If the user presses the left mouse button
if pygame.mouse.get_pressed()[0]:
# Get mouse coordinates
pos = pygame.mouse.get_pos()
# If the user clicks to continue the game
if continues_rect.left < pos[0]< continues_rect.right and \
continues_rect.top < pos[1]< continues_rect.bottom:
paused = not paused
pygame.time.set_timer(SUPPLY_TIME,30*1000)
pygame.mixer.music.unpause()
pygame.mixer.unpause()
# Draw the game end interface
elif life_num ==0:
# Background music stopped
pygame.mixer.music.stop()
# Stop all sound effects
pygame.mixer.stop()
# Stop issuing supplies
pygame.time.set_timer(SUPPLY_TIME,0)if not recorded:
recorded = True
# Read the highest score in history
withopen(r'E:\learn pygame\record.txt','r')as f:
record_score =int(f.readline())
record_star_score =int(f.readline())
# If the player score is higher than the highest score in history, save
if score   record_score or star_score   record_star_score:withopen(r'E:\learn pygame\record.txt','w')as f:if score   record_score and star_score <= record_star_score:
f.write('{0}{1}{2}'.format(str(score),'\n',str(record_star_score)))
elif score <= record_score and star_score   record_star_score :
f.write('{0}{1}{2}'.format(str(score),'\n',str(star_score)))
elif score   record_score and star_score   record_star_score:
f.write('{0}{1}{2}'.format(str(score),'\n',str(star_score)))
# Draw the end screen
record_score_text = score_font.render('Best : %d'% record_score,True,WHITE)
screen.blit(record_score_text,(20,20))
star_score_text = star_score_font.render('Total : %d'% record_star_score,True,WHITE)
screen.blit(star_score_text,(20,60))
gameover_text1 = gameover_font.render('Your Score',True,WHITE)
gameover_text1_rect = gameover_text1.get_rect()
gameover_text1_rect.left,gameover_text1_rect.top = \
( width - gameover_text1_rect.width)// 2, \(height - gameover_text1_rect.bottom)// 2
screen.blit(gameover_text1,gameover_text1_rect)
gameover_text2 = gameover_font.render(str(score),True,WHITE)
gameover_text2_rect = gameover_text2.get_rect()
gameover_text2_rect.left,gameover_text2_rect.top = \
( width - gameover_text2_rect.width)// 2, \
gameover_text1_rect.bottom +10
screen.blit(gameover_text2,gameover_text2_rect)
again_rect.left,again_rect.top = \
( width - again_rect.width)// 2, \
gameover_text2_rect.bottom +50
screen.blit(again_image,again_rect)
gameover_rect.left,gameover_rect.top = \
( width - again_rect.width)// 2, \
again_rect.bottom +10
screen.blit(gameover_image,gameover_rect)
# Detect user&#39;s mouse operation
# If the user presses the left mouse button
if pygame.mouse.get_pressed()[0]:
# Get mouse coordinates
pos = pygame.mouse.get_pos()
# If the user clicks to restart
if again_rect.left < pos[0]< again_rect.right and \
again_rect.top < pos[1]<again_rect.bottom:
# Call the main function to restart the game
main()
# If the user clicks to end the game
elif gameover_rect.left < pos[0]< gameover_rect.right and \
gameover_rect.top < pos[1]<gameover_rect.bottom:
# exit the game
pygame.quit()
sys.exit(0)
# Switch picture
ifnot(delay %5):
switch_image = not switch_image
delay -=1if not delay:
delay =100
pygame.display.flip()
clock.tick(60)if __name__ =='__main__':try:main()
except SystemError:
pass
except:
traceback.print_exc()
pygame.quit()input()

7、 The results are as follows

I downloaded the texture of the Teng National Aircraft War on the Internet, and changed the original texture. The modification process of the P picture has gone through a lot of hard work. I worked as an artist part-time...

Purely for self-entertainment and learning the pygame module, this process is a relatively comprehensive understanding of the pygame module haha, but the actual knowledge used is still very basic python knowledge.

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

Recommended Posts

Python implements the aircraft war project
Use python to realize the aircraft war game
Python implements the brick-and-mortar game
How Python implements the mail function
Python simply implements the snake game
Python3 implements the singleton design pattern
Python implements the steepest descent method
Python implements the actual banking system
How Python implements the timer function
python implements the gradient method python the fastest descent method
python3 simply implements the combined design pattern
2.1 The Python Interpreter (python interpreter)
Python implements the source code of the snake game
500 lines of python code to achieve aircraft war
Python generates the dependent files required for the project
Python implements Super Mario
Python realizes spaceship war
Python implements tic-tac-toe game
Python implements tic-tac-toe game
Python implements man-machine gobang
Python, PyGame game project
Python implements Tetris game
Python implements image stitching
Python implements minesweeper game
Python implements scanning tools
Consolidate the Python foundation (2)
Python implements minesweeper games
Python implements electronic dictionary
Python implements guessing game
Consolidate the foundation of Python (4)
Python implements simple tank battle
Python3 realizes airplane war game
Consolidate the foundation of Python(7)
Python implements udp chat window
python guess the word game
Python implements WeChat airplane game
Python implements word guessing game
Consolidate the foundation of Python(6)
Python implements a guessing game
Python implements parking management system
Python implements digital bomb game
Python implements TCP file transfer
Python realizes the guessing game
The difference between Python extensions
Python numpy implements rolling case
OpenCV Python implements puzzle games
Python implements simple tic-tac-toe game
Consolidate the foundation of Python(5)
Python implements password strength verification
Python implements car management system
Python implements code block folding
Python implements panoramic image stitching
Python implements SMTP mail sending
Python implements multi-dimensional array sorting
Python implements mean-shift clustering algorithm
Python implements verification code recognition
Python implements gradient descent method
Python implements text version minesweeper
Consolidate the foundation of Python (3)
Python implements image stitching function