Python 7 Levels, L6, Unit 68, Air Fight, I

Powered By EmbedPress

bullet.py

import pygame
from pathlib import Path

class Bullet(pygame.sprite.Sprite):
    def __init__(self,screen,plane):
        pygame.sprite.Sprite.__init__(self)
        self.screen = screen
        self.screenRect = self.screen.get_rect()
        self.plane = plane
        self.image = pygame.image.load(Path("images") / "i_bullet.png")
        self.rect = self.image.get_rect()
        self.rect.centerx = self.plane.rect.centerx
        self.rect.bottom = self.plane.rect.top
        self.speed = 3

    def move(self):
        self.rect.centery -= self.speed
        if self.rect.bottom < 0:
            self.kill()

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

plane.py

import pygame
from pathlib import Path

class Plane(pygame.sprite.Sprite):
    def __init__(self,screen):
        self.screen = screen
        self.screenRect = self.screen.get_rect()
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(Path("images") / "i_plane.png")
        self.rect = self.image.get_rect()
        self.rect.centerx = self.screenRect.centerx
        self.rect.bottom = self.screenRect.bottom
        self.moveLeft = False
        self.moveRight = False
        self.moveUp = False
        self.moveDown = False
        self.speed = 4

    def move(self):
        if self.moveLeft and self.rect.left > 0:
            self.rect.centerx -= self.speed
        if self.moveRight and self.rect.right < self.screenRect.right:
            self.rect.centerx += self.speed
        if self.moveUp and self.rect.top > 0:
            self.rect.centery -= self.speed
        if self.moveDown and self.rect.bottom < self.screenRect.bottom:
            self.rect.centery += self.speed

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

Exercise:

import pygame
from pathlib import Path

pygame.init()
screen= pygame.display.set_mode([320,568])
pygame.display.set_caption("Airplane War")
backgroud= pygame.image.load(Path("images") / "i_bg.jpg")
clock= pygame.time.Clock()
def mainWindows():
    running= True
    while running:
        for event in pygame.event.get():
            if event.type== pygame.QUIT:
                running= False
        pygame.display.update()
        clock.tick(30)
        screen.blit(backgroud,[0,0])
mainWindows()
pygame.quit()

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

dahan1999

1 Comment

Leave a Reply

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

Related Posts