Python 7 Levels, L6, Unit 65, Airplane Game, I

Powered By EmbedPress

Exercise:

import pygame
from random import choice

class Plane(pygame.sprite.Sprite):
    def __init__(self,image_file,location):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(image_file)
        self.rect = self.image.get_rect()
        self.rect.left,self.rect.top = location

class Bullet(pygame.sprite.Sprite):
    def __init__(self,image_file,location,speed):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(image_file)
        self.rect = self.image.get_rect()
        self.rect.left,self.rect.top = location
        self.speed = speed
    def move(self):
        self.rect = self.rect.move(self.speed)
        if self.rect.left < 0 or self.rect.right > 590:
            self.speed[0] = -self.speed[0]
        if self.rect.top < 0 or self.rect.bottom > 590:
            self.speed[1] = -self.speed[1]

def initMainWindow():
    pygame.init()
    clock = pygame.time.Clock()
    pygame.key.set_repeat(200,50)
    screen = pygame.display.set_mode([600,600])
    pygame.display.set_caption("Airplane War")
    bg = pygame.image.load("bg.jpg")
    plane = Plane("plane.png",[300,300])
    screen.blit(bg,[0,0])
    pygame.display.update()
    bullets = pygame.sprite.Group()
    for i in range(1,4):
        for j in range(1,4):
            bullet = Bullet("bullet.png",\
                   [300+i*choice([50,100,-50,-100]),300+j*choice([50,100,-50,-100])]\
                   ,[choice([4,6,8,-4,-6,-8]),choice([4,6,8,-4,-6,-8])])
            bullets.add(bullet)
    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_UP:
                    plane.rect.centery -= 10
                elif event.key == pygame.K_DOWN:
                    plane.rect.centery += 10
                elif event.key == pygame.K_LEFT:
                    plane.rect.centerx -= 10
                elif event.key == pygame.K_RIGHT:
                    plane.rect.centerx += 10
        if done == False:
            for bullet in bullets:
                bullet.move()
                screen.blit(bullet.image,bullet.rect)
            screen.blit(plane.image,plane.rect)
            if pygame.sprite.spritecollide(plane,bullets,False):
                bz_place = plane.rect.left,plane.rect.top
                for i in range(1,17):
                    image_file = "e"+str(i)+".gif"
                    bz = pygame.image.load(image_file)
                    screen.blit(bz,bz_place)
                    pygame.display.update()
                    pygame.time.delay(20)
                done = True
        else:
            final_text = "Game over"
            ft_font = pygame.font.Font(None,50)
            ft_surf = ft_font.render(final_text,1,(255,0,0))
            screen.blit(ft_surf,[250,300])
        pygame.display.update()
        clock.tick(20)
        screen.blit(bg,[0,0])
    pygame.quit()

initMainWindow()

dahan1999

1 Comment

Leave a Reply

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

Related Posts