Python 7 Levels, L6, Unit 70, Air Fight, III

Powered By EmbedPress

allied.py

import pygame
import random
from pathlib import Path

class Allied(pygame.sprite.Sprite):
    def __init__(self,screen):
        pygame.sprite.Sprite.__init__(self)
        self.screen = screen
        self.screenRect = self.screen.get_rect()
        self.image = pygame.image.load(Path("images") / "i_allied.png")
        self.rect = self.image.get_rect()
        self.rect.left = random.randint(0,self.screenRect.right - self.rect.right)
        self.rect.bottom = self.screenRect.top
        self.speed = 6
        self.flag = False

    def move(self):
        self.rect.centery += self.speed

    def blit(self):
        self.screen.blit(self.image,self.rect)

    def reset(self):
        self.rect.left = random.randint(0,self.screenRect.right - self.rect.right)
        self.rect.bottom = self.screenRect.top
        self.flag = False

bossBullet.py

import pygame
from pathlib import Path
class BossBullet(pygame.sprite.Sprite):
    def __init__(self,screen,boss):
        pygame.sprite.Sprite.__init__(self)
        self.screen = screen
        self.screenRect = self.screen.get_rect()
        self.boss = boss
        self.image = pygame.image.load(Path("images") / "i_bullet.png")
        self.rect = self.image.get_rect()
        self.rect.top = self.boss.rect.bottom
        self.rect.centerx = self.boss.rect.centerx
        self.speed = 5

    def move(self):
        self.rect.centery += self.speed
        if self.rect.top > self.screenRect.bottom:
            self.kill()

    def blit(self):
        self.screen.blit(self.image,self.rect)

planeBattle1.py

import pygame
from plane import Plane
from bullet import Bullet
from pathlib import Path

pygame.init()
screen = pygame.display.set_mode([320,568])
pygame.display.set_caption("Air Fight")
backgroud = pygame.image.load(Path("images") / "i_bg.jpg")
clock = pygame.time.Clock()

def mainWindows():
    plane = Plane(screen)
    bullets = pygame.sprite.Group()
    rates = 0#for the generating speed of bullets
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    plane.moveLeft = True
                if event.key == pygame.K_RIGHT:
                    plane.moveRight = True
                if event.key == pygame.K_UP:
                    plane.moveUp = True
                if event.key == pygame.K_DOWN:
                    plane.moveDown = True
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT:
                    plane.moveLeft = False
                if event.key == pygame.K_RIGHT:
                    plane.moveRight = False
                if event.key == pygame.K_UP:
                    plane.moveUp = False
                if event.key == pygame.K_DOWN:
                    plane.moveDown = False
        rates += 1
        if rates % 20 == 0:
            newBullet = Bullet(screen,plane)
            bullets.add(newBullet)
        for bullet in bullets:
            bullet.move()
            bullet.blit()
        plane.move()
        plane.blit()
        pygame.display.update()
        clock.tick(30)
        screen.blit(backgroud,[0,0])
        
mainWindows()
pygame.quit()

planeBattle2.py

import pygame
from plane import Plane
from bullet import Bullet
from enemy import Enemy
from boss import Boss
import random
from pathlib import Path
pygame.init()
screen = pygame.display.set_mode([320,568])
pygame.display.set_caption("Air Fight")
backgroud = pygame.image.load(Path("images") / "i_bg.jpg")
clock = pygame.time.Clock()
pygame.time.set_timer(pygame.USEREVENT,5000)

def mainWindows():
    plane = Plane(screen)
    bullets = pygame.sprite.Group()
    enemys = pygame.sprite.Group()
    boss = Boss(screen)
    rates = 0#The speed of creating bullets
    score = 0
    lives = 3
    done = False
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    plane.moveLeft = True
                if event.key == pygame.K_RIGHT:
                    plane.moveRight = True
                if event.key == pygame.K_UP:
                    plane.moveUp = True
                if event.key == pygame.K_DOWN:
                    plane.moveDown = True
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT:
                    plane.moveLeft = False
                if event.key == pygame.K_RIGHT:
                    plane.moveRight = False
                if event.key == pygame.K_UP:
                    plane.moveUp = False
                if event.key == pygame.K_DOWN:
                    plane.moveDown = False
                    
            if event.type == pygame.USEREVENT:
                if boss.flag == False:
                    boss.flag = True
        if not done:
            rates += 1
            if rates % 20 == 0:
                newBullet = Bullet(screen,plane)
                bullets.add(newBullet)
            for bullet in bullets:
                bullet.move()
                bullet.blit()
            if rates % 15 == 0:
                newEnemy = Enemy(screen)
                enemys.add(newEnemy)
            for enemy in enemys:
                enemy.move()
                enemy.blit()
            plane.move()
            plane.blit()
            if pygame.sprite.groupcollide(enemys,bullets,True,True):
                score += 1
            if pygame.sprite.spritecollide(plane,enemys,True):
                lives -= 1
                if lives == 0:
                    done = True
                    
            if boss.flag == True:
                boss.blit()
                boss.move()
                if pygame.sprite.spritecollide(boss,bullets,True):
                    boss.lives -= 1
                    if boss.lives == 0:
                        score += 10
                        boss.reset()
                if pygame.sprite.collide_rect(plane,boss):
                    lives -= 1
                    if lives == 0:
                        done = True
                    boss.reset()

                if boss.rect.top > 568:
                    lives -= 1
                    if lives == 0:
                        done = True
                    boss.reset()
    
        else:
            over_ft = pygame.font.Font(None,40)#创建分数字体
            over_txt = over_ft.render("Game over",1,[255,0,0])
            screen.blit(over_txt,[100,280])
        score_ft = pygame.font.Font(None,40)#创建分数字体
        score_txt = score_ft.render("score"+str(score),1,[255,0,0])
        screen.blit(score_txt,[0,0])
        for num in range(1,lives):
            screen.blit(plane.image,[320-num*30,0])
        pygame.display.update()
        clock.tick(30)
        screen.blit(backgroud,[0,0])
        
mainWindows()
pygame.quit()

planeBattle3.py

import pygame
from plane import Plane
from bullet import Bullet
from enemy import Enemy
from boss import Boss
import random
from bossBullet import BossBullet
from allied import Allied
from pathlib import Path

'''Initialize'''
pygame.init()
siz = width,height, = 320,568
screen = pygame.display.set_mode(siz)
pygame.display.set_caption("Air Fight")
backgroud = pygame.image.load(Path("images") / "i_bg.jpg")
clock = pygame.time.Clock()
pygame.time.set_timer(pygame.USEREVENT,15000)

'''设置声音'''
pygame.mixer.init()
pygame.mixer.music.load(Path("sounds") / "s_bg.mp3")
pygame.mixer.music.set_volume(0.05)
#pygame.mixer.music.play(-1)
s_allied = pygame.mixer.Sound(Path("sounds") / "s_allied.wav")
s_allied.set_volume(0.5)
s_live = pygame.mixer.Sound(Path("sounds") / "s_live.wav")
s_live.set_volume(0.5)
s_boss = pygame.mixer.Sound(Path("sounds") / "s_boss.wav")
s_boss.set_volume(0.02)
s_bullet = pygame.mixer.Sound(Path("sounds") / "s_bullet.wav")
s_bullet.set_volume(0.1)
s_bz1 = pygame.mixer.Sound(Path("sounds") / "s_bz1.wav")
s_bz1.set_volume(0.1)
s_bz2 = pygame.mixer.Sound(Path("sounds") / "s_bz2.wav")
s_bz2.set_volume(0.9)

def mainWindows():
    plane = Plane(screen)
    bullets = pygame.sprite.Group()
    enemys = pygame.sprite.Group()
    bossBullets = pygame.sprite.Group()
    boss = Boss(screen)
    allied = Allied(screen)
    rates = 0#speed of generating bullets and enemy planes
    rates_2 = 0#speed of generating boss bullets
    rates_3 = 0#speed of generating ally
    score = 0#game score
    lives = 3#health value
    done = False
    running = True
    while running:
        for event in pygame.event.get():
            '''Event handle'''
            if event.type == pygame.QUIT:
                running = False
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    plane.moveLeft = True
                if event.key == pygame.K_RIGHT:
                    plane.moveRight = True
                if event.key == pygame.K_UP:
                    plane.moveUp = True
                if event.key == pygame.K_DOWN:
                    plane.moveDown = True
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT:
                    plane.moveLeft = False
                if event.key == pygame.K_RIGHT:
                    plane.moveRight = False
                if event.key == pygame.K_UP:
                    plane.moveUp = False
                if event.key == pygame.K_DOWN:
                    plane.moveDown = False     
            if event.type == pygame.USEREVENT:
                if boss.flag == False:
                    boss.flag = True
                rates_3 += 1
                if rates_3 % 3 == 0:
                    allied.flag = True
         
        if not done:
            rates += 1
            
            '''Player's plane'''
            plane.move()
            plane.blit()
            

            '''player bullet speed'''
            if rates % 20 == 0:
                s_bullet.play()
                newBullet = Bullet(screen,plane)
                bullets.add(newBullet)
            for bullet in bullets:
                bullet.move()
                bullet.blit()
                
            '''Enemy plane'''
            if rates % 15 == 0:
                newEnemy = Enemy(screen)
                enemys.add(newEnemy)
            for enemy in enemys:
                enemy.move()
                enemy.blit()
            

            '''Player's bullet collision with enemy plane'''
            result = pygame.sprite.groupcollide(enemys,bullets,True,True)
            if result:
                s_bz1.play()
                #print(result.keys())
                bz_place = 0,0
                for e in result.keys():
                    bz_place = e.rect.left,e.rect.top
                for i in range(1,17):#Explosion of enemy plane
                    image_file_name = "e"+str(i)+".gif"
                    bz = pygame.image.load(Path("bz") / image_file_name)
                    screen.blit(bz,bz_place)
                    pygame.display.update()
                    pygame.time.delay(10)   
                score += 1

            '''Collision between enemy plane and player's plane'''
            if pygame.sprite.spritecollide(plane,enemys,True):
                s_live.play()
                lives -= 1
                if lives == 0:
                    done = True

            '''Large enemy plane, enemy boss'''
            if boss.flag == True:
                s_boss.play()
                boss.blit()
                boss.move()
                '''collision between boss enemy and player's bullet'''
                if pygame.sprite.spritecollide(boss,bullets,True):
                    boss.lives -= 1
                    if boss.lives == 0:
                        s_bz2.play()
                        bz_place = boss.rect.left,boss.rect.top
                        for i in range(1,17):#Explosion of enemy plane
                            image_file_name = "e"+str(i)+".gif"
                            bz = pygame.image.load(Path("bz") / image_file_name)
                            screen.blit(bz,bz_place)
                            pygame.display.update()
                            pygame.time.delay(10) 
                        score += 10
                        boss.reset()      
                '''collision between boss and player plane'''
                if pygame.sprite.collide_rect(plane,boss):
                    s_live.play(1)
                    lives -= 1
                    if lives == 0:
                        done = True
                    boss.reset()
                '''boss enemy fall on the bottom of the screen'''
                if boss.rect.top > height:
                    s_live.play(1)
                    lives -= 1
                    if lives == 0:
                        done = True
                    boss.reset()
                '''Generate boss enemy bullets'''
                rates_2 += 1
                if rates_2 % 20 == 0:
                    newBossBullet = BossBullet(screen,boss)
                    bossBullets.add(newBossBullet)
                    
            '''Movement of boss enemy bullets'''
            for bossBullet in bossBullets:
                bossBullet.move()
                bossBullet.blit()
            '''Collision between boss-bullet and player plane'''
            if pygame.sprite.spritecollide(plane,bossBullets,True):
                s_live.play(1)
                lives -= 1
                if lives == 0:
                    done = True

            '''Ally'''
            if allied.flag == True:
                allied.move()
                allied.blit()
                if pygame.sprite.collide_rect(plane,allied):
                    s_allied.play()
                    lives += 1
                    allied.reset()
                if allied.rect.top > height:
                    allied.reset()
                    
        else:
            '''Display Game Over'''
            over_ft = pygame.font.Font(None,40)#Create font for score
            over_txt = over_ft.render("Game over",1,[255,0,0])
            screen.blit(over_txt,[100,280])
            
        '''Display game score and health value'''
        score_ft = pygame.font.Font(None,40)#create font for score
        score_txt = score_ft.render("score "+str(score),1,[255,0,0])
        screen.blit(score_txt,[0,0])
        for num in range(1,lives):
            screen.blit(plane.image,[320-num*30,0])
            
        pygame.display.update()
        clock.tick(30)
        screen.blit(backgroud,[0,0])
        
mainWindows()
pygame.quit()

dahan1999

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts