面对对象 继承 飞机大战

#飞机大战 面向对象 类

import  os ,time ,random , pygame
from pygame.locals import *
def getPic(path):#创建一个 插入 图片所在的地址
    return os.path.join('D:\\新建文件夹\\新建文件夹\\Python\\python使用软件\\IT研究院-Python\\New_Stydy\\img',path)




class HeroPlane():#战机类
    def __init__(self,x,y,windows):
        self.x=x
        self.y=y
        self.windows=windows
        self.normaImagelist=['hero1.png','hero2.png']
        self.normalIndex=0
        self.bombImagelist=['hero_blowup_n1.png','hero_blowup_n2.png','hero_blowup_n3.png','hero_blowup_n4.png',]
        self.bombIndex=0
        self.BiuList = []
        self.isBomb = False
    def draw(self):#画出飞机
        if self.isBomb==False:
            pic=pygame.image.load(getPic(self.normaImagelist[self.normalIndex]))
            self.windows.blit(pic,(self.x,self.y))
            self.normalIndex=(self.normalIndex+1)%len(self.normaImagelist)#if else 循环
        else:
            if self.bombIndex == len(self.bombImagelist):
                time.sleep(0.3)
                exit(0)
            pic = pygame.image.load(getPic(self.bombImagelist[self.bombIndex]))
            self.windows.blit(pic, (self.x, self.y))
            self.bombIndex = (self.bombIndex + 1)
            time.sleep(0.3)
        for biu in self.BiuList:
            biu.draw()
            self.BiuList.remove(biu) if self.y < 0 else ''
    def dealEvent(self,eventList):
        for event in eventList:
            if event.type==QUIT:
                exit(0) #正常退出
            elif event.type==KEYDOWN:#加入上下左右键
               if event.key==K_LEFT:
                   self.x=self.x-9 if self.x>=9 else 0
               elif event.key==K_RIGHT:
                   self.x=self.x+9 if self.x<=480-100-9 else 480-100
               elif event.key==K_UP:
                   self.y=self.y-9 if self.y>=9 else 0
               elif event.key==K_DOWN:
                   self.y=self.y+9 if self.y<=650-124-9 else 650-124
               elif event.key==K_SPACE:
                   oneBiu=HeroBullet(self.x+50-11,self.y-22,windows)
                   self.BiuList.append(oneBiu)
    def pzjc(self, bList):
        enemyRect = Rect(self.x, self.y, 100, 124)
        for biu in bList:
            biuRect = Rect(biu.x, biu.y, 9, 21)
            if biuRect.colliderect(enemyRect):
                self.isBomb = True




class EnemyPlane():#敌机类
    def __init__(self, x, y, windows):
        self.x = x
        self.y = y
        self.windows = windows
        self.normaImagelist = ['enemy1.png', 'enemy1.png']
        self.normalIndex = 0
        self.bombImagelist = ['enemy1_down1.png', 'enemy1_down2.png', 'enemy1_down3.png', 'enemy1_down4.png', ]
        self.bombIndex = 0
        self.direct='左'
        self.enemyBiuList = []
        self.isBomb = False
    def draw(self):
        if self.isBomb==False:
            pic = pygame.image.load(getPic(self.normaImagelist[self.normalIndex]))
            self.windows.blit(pic, (self.x, self.y))
            self.normalIndex = (self.normalIndex + 1) % len(self.normaImagelist)  # if else 循环
        else:
            if self.bombIndex == len(self.bombImagelist):
                time.sleep(0.3)
                exit(0)
            pic = pygame.image.load(getPic(self.bombImagelist[self.bombIndex]))
            self.windows.blit(pic, (self.x, self.y))
            self.bombIndex = (self.bombIndex + 1)
            time.sleep(0.3)
        self.move()
        self.fire()
        for biu in self.enemyBiuList:
            biu.draw()
            self.enemyBiuList.remove(biu) if self.y < 0 else ''
    def move(self):
        if self.direct == '左':  # 敌机左右动作
            self.x -= 2
            if self.x <= 0:
                self.direct = '右'
        elif self.direct == '右':
            self.x += 2
            if self.x >=480-69:
                self.direct = '左'
    def fire(self):
        x = random.randint(0, 150)
        if x == 5 or x == 75:
            oneBiu = enemyBullet(self.x + 69 // 2 - 9 // 2, self.y + 89, windows)
            self.enemyBiuList.append(oneBiu)
    def pzjc(self,bList):
        enemyRect = Rect(self.x, self.y, 69, 89)
        for biu in bList:
            biuRect = Rect(biu.x, biu.y, 22, 22)
            if biuRect.colliderect(enemyRect):
                self.isBomb = True



class HeroBullet(): #战机子弹
    def __init__(self,x,y,windows):
        self.x=x
        self.y=y
        self.windows=windows
        self.pic=pygame.image.load(getPic('bullet.png'))
    def draw(self):
        self.windows.blit(self.pic,(self.x,self.y))
        self.move()
    def move(self):
        self.y-=3




class enemyBullet():#敌机子弹
    def __init__(self,x,y,windows):
        self.x=x
        self.y=y
        self.windows=windows
        self.pic=pygame.image.load(getPic('bullet2.png'))
    def draw(self):
        self.windows.blit(self.pic,(self.x,self.y))
        self.move()
    def move(self):
        self.y+=3

windows=pygame.display.set_mode((480,650),0,32)#创建窗口
pygame.display.set_caption('雷霆战机')#游戏名字
pygame.display.set_icon(pygame.image.load(getPic('icon72x72.png')))#游戏图标
pygame.key.set_repeat(20,30)#移动连续  毫秒
bj=pygame.image.load(getPic('background.png'))
heroPlane=HeroPlane(480//2-100//2,650-124,windows)#飞机初始位置
enemyPlane=EnemyPlane(480//2-69//2,0,windows)
while True:
    windows.blit(bj,(0,0))#将背景添进窗口
    heroPlane.draw()#调用画函数
    enemyPlane.draw()
    enemyPlane.pzjc(heroPlane.BiuList)
    heroPlane.pzjc(enemyPlane.enemyBiuList)
    heroPlane.dealEvent(pygame.event.get())#调用 飞机移动
    pygame.display.update()#打开帷幕

#飞机大战 面向对象 继承

  import  os ,time ,random , pygame
    from pygame.locals import *
    def getPic(path):#创建一个 插入 图片所在的地址
        return os.path.join('D:\\新建文件夹\\新建文件夹\\Python\\python使用软件\\IT研究院-Python\\New_Stydy\\img',path)
    class bjc():
        def __init__(self,x,y,windows,normaImagelist,bombImagelist):
            self.x = x
            self.y = y
            self.windows = windows
            self.normaImagelist = normaImagelist
            self.normalIndex = 0
            self.bombImagelist = bombImagelist
            self.bombIndex = 0
            self.enemyBiuList = []
            self.isBomb = False
        def draw(self):#画出飞机
            if self.isBomb==False:
                pic=pygame.image.load(getPic(self.normaImagelist[self.normalIndex]))
                self.windows.blit(pic,(self.x,self.y))
                self.normalIndex=(self.normalIndex+1)%len(self.normaImagelist)#if else 循环
            else:
                if self.bombIndex == len(self.bombImagelist):
                    time.sleep(0.3)
                    exit(0)
                pic = pygame.image.load(getPic(self.bombImagelist[self.bombIndex]))
                self.windows.blit(pic, (self.x, self.y))
                self.bombIndex = (self.bombIndex + 1)
                time.sleep(0.3)
        def pzjc(self,bList,m,n,k,v):
            enemyRect = Rect(self.x, self.y, m, n)
            for biu in bList:
                biuRect = Rect(biu.x, biu.y, k, v)
                if biuRect.colliderect(enemyRect):
                    self.isBomb = True
    
    class HeroPlane(bjc):#战机类
        def __init__(self,x,y,windows):
            super().__init__(x,y,windows,['hero1.png','hero2.png'],['hero_blowup_n1.png','hero_blowup_n2.png','hero_blowup_n3.png','hero_blowup_n4.png',])
            self.BiuList = []
        def draw(self):#画出飞机
            super().draw()
            for biu in self.BiuList:
                biu.draw()
                self.BiuList.remove(biu) if self.y < 0 else ''
        def dealEvent(self,eventList):
            for event in eventList:
                if event.type==QUIT:
                    exit(0) #正常退出
                elif event.type==KEYDOWN:#加入上下左右键
                   if event.key==K_LEFT:
                       self.x=self.x-9 if self.x>=9 else 0
                   elif event.key==K_RIGHT:
                       self.x=self.x+9 if self.x<=480-100-9 else 480-100
                   elif event.key==K_UP:
                       self.y=self.y-9 if self.y>=9 else 0
                   elif event.key==K_DOWN:
                       self.y=self.y+9 if self.y<=650-124-9 else 650-124
                   elif event.key==K_SPACE:
                       oneBiu=HeroBullet(self.x+50-11,self.y-22,windows)
                       self.BiuList.append(oneBiu)
        def pzjc(self, bList,m,n,k,v):
            super().pzjc(bList,m,n,k,v)
    
    class EnemyPlane(bjc):#敌机类
        def __init__(self, x, y, windows):
            super().__init__(x, y, windows, ['enemy1.png', 'enemy1.png'],['enemy1_down1.png', 'enemy1_down2.png', 'enemy1_down3.png', 'enemy1_down4.png'])
            self.direct='左'
        def draw(self):
            super().draw()
            self.move()
            self.fire()
            for biu in self.enemyBiuList:
                biu.draw()
                self.enemyBiuList.remove(biu) if self.y < 0 else ''
        def move(self):
            if self.direct == '左':  # 敌机左右动作
                self.x -= 2
                if self.x <= 0:
                    self.direct = '右'
            elif self.direct == '右':
                self.x += 2
                if self.x >=480-69:
                    self.direct = '左'
        def fire(self):
            x = random.randint(0, 150)
            if x == 5 or x == 75:
                oneBiu = enemyBullet(self.x + 69 // 2 - 9 // 2, self.y + 89, windows)
                self.enemyBiuList.append(oneBiu)
        def pzjc(self, bList,m,n,k,v):
            super().pzjc(bList,m,n,k,v)
    
    
    
    
    class bjcBullet():
        def __init__(self,x,y,windows):
            self.x=x
            self.y=y
            self.windows=windows
        def draw(self):
            self.windows.blit(self.pic, (self.x, self.y))
            self.move()
        def move(self):
            self.y -= 3
    
    
    class HeroBullet(bjcBullet): #战机子弹
        def __init__(self,x,y,windows):
            super().__init__(x,y,windows)
            self.pic=pygame.image.load(getPic('bullet.png'))
        def move(self):
            self.y -= 3
    
    class enemyBullet(bjcBullet):#敌机子弹
        def __init__(self,x,y,windows):
            super().__init__(x,y,windows)
            self.pic=pygame.image.load(getPic('bullet2.png'))
        def move(self):
            self.y += 3
    
    windows=pygame.display.set_mode((480,650),0,32)#创建窗口
    pygame.display.set_caption('雷霆战机')#游戏名字
    pygame.display.set_icon(pygame.image.load(getPic('icon72x72.png')))#游戏图标
    pygame.key.set_repeat(20,30)#移动连续  毫秒
    bj=pygame.image.load(getPic('background.png'))
    heroPlane=HeroPlane(480//2-100//2,650-124,windows)#飞机初始位置
    enemyPlane=EnemyPlane(480//2-69//2,0,windows)
    while True:
        windows.blit(bj,(0,0))#将背景添进窗口
        heroPlane.draw()#调用画函数
        enemyPlane.draw()
        enemyPlane.pzjc(heroPlane.BiuList,69,89,22,22)
        heroPlane.pzjc(enemyPlane.enemyBiuList,100,124,9,21)
        heroPlane.dealEvent(pygame.event.get())#调用 飞机移动
        pygame.display.update()#打开帷幕
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值