Python 7 Levels, L6, Unit 69, Air Fight, II

Powered By EmbedPress

enemy.py

import pygame
import random
from pathlib import Path
class Enemy(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_enemy.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 = 4

    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)

boss.py

import pygame
import random
from pathlib import Path

class Boss(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_boss.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 = 3
        self.lives = 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.lives = 6
        self.flag = False

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
from pathlib import Path
import random

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#for the generating speed of 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()

dahan1999

1 Comment

Leave a Reply

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

Related Posts