Python 7 Levels, L6, Unit 66, Airplane Game, II

Powered By EmbedPress

Exercise:

import pygame
from random import choice

class Plane(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]

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()
    pygame.time.set_timer(pygame.USEREVENT,1000)
    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],[0,0])
    plane2 = Plane("plane2.png",[choice([100,300,500]),choice([100,300,500])],\
                   [choice([4,6,8,-4,-6,-8]),choice([4,6,8,-4,-6,-8])])
    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
    score = 0
    lives = 3
    plane2_flag = False
    add_time = 0
    t = 0
    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 event.type == pygame.USEREVENT:
                if done == False:
                    score += 1
                    if plane2_flag == False:
                        add_time += 1
                        if add_time == 10:
                            plane2_flag = True
                            add_time = 0
                    else:
                        t += 1
                        if t == 10:
                            plane2_flag = False
                
        if done == False:
            if plane2_flag == True:
                screen.blit(plane2.image,plane2.rect)
                plane2.move()
                if pygame.sprite.collide_rect(plane,plane2):
                    lives += 1
                    plane2_flag = False
                    
            for num in range(1,lives):
                screen.blit(plane.image,[600-num*30,0])
                
            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)
                plane.rect.left,plane.rect.top = [300,300]
                del bullets#清除原来的精灵组
                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)  
                lives -= 1
                if lives == 0:
                    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])
        score_text = "score:"+str(score)
        score_font = pygame.font.Font(None,40)
        score_surf = score_font.render(score_text,1,(255,0,0))
        screen.blit(score_surf,[0,0])
        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