2018-10-21 Python27 飞机大战

feijidazhan.gif

 
 

飞机基类:

#支持2D游戏
import pygame, sys, random, time
from pygame.locals import *


class Plane(object):
    png0 = None
    png1 = None
    png2 = None
    png3 = None
    png4 = None
    rate = 0
    bullet_type = 0
    def set_broken(self, get_bullet_nums):
        pass
    def select_png(self):
        return {0:self.png0, 1:self.png1, 2:self.png2, 3:self.png3, 4:self.png4}.get(self.broken, 4)
    def __init__(self, screen_obj):
        self.screen = screen_obj
        self.get_bullet_nums = 0
        self.broken = 0
        self.image = pygame.image.load(self.select_png())
        self.x = screen_obj.get_width()/2-self.image.get_width()/2
        self.y = screen_obj.get_height()-self.image.get_height()
        self.bullet_list = []
    def display(self):
        self.image = pygame.image.load(self.select_png())
        self.screen.blit(self.image, (self.x, self.y)) #贴到窗口上
        self.teardown()             #拆掉打到边界外的子弹
        for b in self.bullet_list:  #显示子弹
            b.display()
            b.move()

    def is_rightSide(self):
        return self.x >= (self.screen.get_width()-self.image.get_width()) 
    def is_leftSide(self):
        return self.x <= 0

    def move_left(self):
        if not self.is_leftSide():
            self.x -= self.rate
    def move_right(self):
        if not self.is_rightSide():
            self.x += self.rate

    def top_point(self):
        pass
    def fire(self):
        b = Bullet(self.screen, self.top_point(), self.bullet_type)
        self.bullet_list.append(b)
    def teardown(self):
        c = []
        for b in self.bullet_list:
            if b.y<-10 or b.y>self.screen.get_height():
                c.append(b)
        for bu in c:
            self.bullet_list.remove(bu)
        c.clear()

 

英雄(玩家):

class Hero(Plane):
    png0 = "./feiji/hero1.png"
    png1 = "./feiji/hero_blowup_n1.png"
    png2 = "./feiji/hero_blowup_n2.png"
    png3 = "./feiji/hero_blowup_n3.png"
    png4 = "./feiji/hero_blowup_n4.png"
    rate = 10
    def set_broken(self, get_bullet_nums):
        self.get_bullet_nums += get_bullet_nums
        if self.broken <= 4:
            n = self.get_bullet_nums//30
        else:
            n += 1 
        self.broken = int(n)

    def top_point(self):
        return (self.x+self.image.get_width()/2, self.y-22)
    def left_point(self):
        return (self.x+10, self.y+20)
    def right_point(self):
        return (self.x+70, self.y+20)

 

敌机:

class Enemy(Plane):
    png = None
    rate = 1  #飞机左右移动速度
    bullet_type = 1
    def __init__(self, screen_obj):
        super().__init__(screen_obj)
        self.x = random.randint(0,self.screen.get_width()-self.image.get_width())
        self.y = random.randint(0, 30)
        self.towards_left = True        #初始移动朝向
    def top_point(self):
        return (self.x+self.image.get_width()/2, self.y+self.image.get_height())

class Enemy0(Enemy):
    png0 = "./feiji/enemy0.png"
    png1 = "./feiji/enemy0_down1.png"
    png2 = "./feiji/enemy0_down2.png"
    png3 = "./feiji/enemy0_down3.png"
    png4 = "./feiji/enemy0_down4.png"
    def set_broken(self, get_bullet_nums):
        self.get_bullet_nums += get_bullet_nums
        if self.broken <= 4:
            n = self.get_bullet_nums//3
        else:
            n += 1 
        self.broken = int(n)

class Enemy1(Enemy):
    png0 = "./feiji/enemy1.png"
    png1 = "./feiji/enemy1_down1.png"
    png2 = "./feiji/enemy1_down2.png"
    png3 = "./feiji/enemy1_down3.png"
    png4 = "./feiji/enemy1_down4.png"
    def set_broken(self, get_bullet_nums):
        self.get_bullet_nums += get_bullet_nums
        if self.broken <= 4:
            n = self.get_bullet_nums//8
        else:
            n += 1 
        self.broken = int(n)

 

子弹:

class Bullet(object):
    png0 = "./feiji/bullet.png"
    png1 = "./feiji/bullet1.png"
    png2 = "./feiji/bullet2.png"
    def select_type(self, bullet_type):
        return {0:self.png0, 1:self.png1, 2:self.png2}[bullet_type]
    def __init__(self, screen_obj, xy_tuple, bullet_type):
        if bullet_type==0:
            self.rate = 15 #子弹移动速度
        else:
            self.rate = 3
        self.screen = screen_obj
        self.bullet_type = bullet_type
        self.image = pygame.image.load(self.select_type(bullet_type))
        self.x,self.y = xy_tuple
        self.x -= self.image.get_width()/2
    def display(self):
        self.screen.blit(self.image, (self.x, self.y))
    def move(self):
        if self.bullet_type==0:
            self.y -= self.rate
        else:
            self.y += self.rate

 

主程序:

def key_control(hero):
    for event in pygame.event.get():
        if event.type == QUIT:
            print("exit")
            exit()
    keys = pygame.key.get_pressed()
    if keys[K_SPACE]:
        hero.fire()
    if keys[K_LEFT]:
        hero.move_left()
    if keys[K_RIGHT]:
        hero.move_right()


def auto_control_enemy(enemy_list):
    for enemy in enemy_list:
        if enemy!=None:
            n = random.randint(1,100)
            if n==2 or n==78:   #敌机开火
                enemy.fire()

            if enemy.towards_left:    #左右移动
                enemy.move_left()
                if enemy.is_leftSide():
                    enemy.towards_left = False
            else:
                enemy.move_right()
                if enemy.is_rightSide():
                    enemy.towards_left = True


def broke_up(hero, enemy_list):
    n = 0
    for enemy in enemy_list:
        if enemy!=None:
            for bullet in enemy.bullet_list:
                if (bullet.x>hero.x and bullet.xhero.y and bullet.yenemy.x and b.xenemy.y and b.y= 20:
                    enemy_list[i].broken=5
                if enemy_list[i].broken>4:
                    enemy_list[i]=None
                    broke_secs[i]=0
    else:
        enemy_list.clear()      
      
def create_enemy(screen, enemy_list):
    #创建敌机
    enemy0 = Enemy0(screen)
    enemy1 = Enemy0(screen)
    enemy2 = Enemy0(screen)
    enemy3 = Enemy1(screen)
    enemy4 = Enemy1(screen)
    enemy_list.append(enemy0)
    enemy_list.append(enemy1)
    enemy_list.append(enemy2)
    enemy_list.append(enemy3)
    enemy_list.append(enemy4)


def enemy_display(screen, enemy_list):
    if len(enemy_list)>0:
        for enemy in enemy_list:
            if enemy != None:
                enemy.display()
    else:
        create_enemy(screen, enemy_list)    


def main():
    screen = pygame.display.set_mode((430,770),0,30)  #创建游戏窗口(宽,高),FLAG,DEPTH
    bgPNG = pygame.image.load("./feiji/background.png") #加载背景图片
    #创建玩家飞机
    hero = Hero(screen)
    #创建敌机
    enemy_list = []
    create_enemy(screen, enemy_list)

    broke_sec = 0
    broke_secs = [0 for i in range(len(enemy_list))]
    #使程序不结束
    while True:
        screen.blit(bgPNG, (0,0))  #将背景图片贴到窗口
        if hero.broken == 4:
            broke_sec+=1
        if broke_sec >= 20:
            hero.broken=5
        if hero.broken<=4:           #显示英雄
            hero.display()
        else:
            hero = Hero(screen)
            broke_sec = 0

        enemy_display(screen,enemy_list)  #显示敌机

        key_control(hero)
        auto_control_enemy(enemy_list)

        broke_up(hero, enemy_list)  #判断是否摧毁飞机
        cleaning(enemy_list,broke_secs)

        pygame.display.update()     #刷新显示
        time.sleep(0.01)   #对于电脑配置不高的,可以使用sleep延缓刷新,但是睡眠时间越长,游戏显示越卡



#测试:
if __name__=="__main__":
    main()

你可能感兴趣的:(2018-10-21 Python27 飞机大战)