Python飞机大战(2)

飞机大战综合性知识的应用
英雄方和敌方:
我方限定范围通过按键控制飞机发射子弹
敌方限定范围通过随机发射子弹
子弹和飞机通过矩形碰撞检测,发生爆炸,显示爆炸效果,退出游戏
通过类封装
敌方:子弹类、飞机类
英雄方:子弹类、飞机类

import pygame,os,random,time
from pygame.locals import *

#敌方子弹类
class EnemyBiu():
    def __init__(self,x,y):
        self.x=x
        self.y=y
        self.image=pygame.image.load(getImage('bullet1.png'))
    def draw(self):
         window.blit(self.image,(self.x,self.y))
         self.move()
    def move(self):
        self.y+=5
class EnemyPlane():
    def __init__(self,x,y):
        self.x=x
        self.y=y
        self.normalImageList=['enemy1.png']#正常图片列表
        self.normalImageIndex=0#索引号,第一次显示第一张0下标,下次1下标第二张,下次0下标...
        self.biuLIst=[]#子弹列表
        self.direct='左'
        self.bombImageList=['enemy1_down1.png','enemy1_down2.png','enemy1_down3.png','enemy1_down4.png']
        self.bombImageIndex=0
        self.isBomb=False

    def draw(self):
        if self.isBomb == False:
            image=pygame.image.load(getImage(self.normalImageList[self.normalImageIndex]))
            self.normalImageIndex=(self.normalImageIndex+1)%len((self.normalImageList))
            window.blit(image,(self.x,self.y))#画图
        else:
            if self.bombImageIndex == len(self.bombImageList):
                time.sleep(0.8)
                exit(0)
            image = pygame.image.load(getImage(self.bombImageList[self.bombImageIndex]))
            self.bombImageIndex += 1
            window.blit(image, (self.x, self.y))
            time.sleep(0.5)

        for zd in self.biuLIst:#从子弹列表中取出子弹,
            zd.draw()
            self.biuLIst.remove(zd) if zd.y>520 else ''
        self.move()
        self.fire()
    def move(self):
        if self.direct=='左':
            self.x-=5
            if self.x<=0:
                self.direct='右'
        else:
            self.x+=5
            if self.x>=480-69:
                self.direct='左'
    def fire(self):
        x=random.randint(0,100)
        if  x==3 or x==7:
            zd=EnemyBiu(self.x+69/2-9/2,self.y+89)
            self.biuLIst.append(zd)
    def pzjc(self,wfZdList):
        djRect=Rect(self.x,self.y,69,89)
        for zd in wfZdList:
            zdRect=Rect(zd.x,zd.y,22,22)
            if zdRect.colliderect(djRect) :
                self.isBomb=True




class Biu():
    def __init__(self,x,y):
        self.x=x
        self.y=y
        self.image=pygame.image.load(getImage('bullet.png'))
    def draw(self):
         window.blit(self.image,(self.x,self.y))
         self.move()
    def move(self):
        self.y-=5


class HeroPlane():
    def __init__(self,x,y):
        self.x=x
        self.y=y
        self.normalImageList=['hero1.png','hero2.png']#正常图片列表
        self.normalImageIndex=0#索引号,第一次显示第一张0下标,下次1 下标第二张,下次0下标...
        self.biuLIst=[]#子弹列表
        self.bombImageList=['hero_blowup_n1.png','hero_blowup_n2.png','hero_blowup_n3.png','hero_blowup_n4.png']
        self.bombImageIndex=0
        self.isBomb=False
    def draw(self):#显示爆炸效果
        if self.isBomb==False:
            image=pygame.image.load(getImage(self.normalImageList[self.normalImageIndex]))
            self.normalImageIndex=(self.normalImageIndex+1)%len((self.normalImageList))
            window.blit(image,(self.x,self.y))#画图
        else:
            if self.bombImageIndex==len(self.bombImageList):
                time.sleep(0.8)
                exit(0)
            image=pygame.image.load(getImage(self.bombImageList[self.bombImageIndex]))
            self.bombImageIndex+=1
            window.blit(image,(self.x,self.y))
            time.sleep(0.5)
        for zd in self.biuLIst:#从子弹列表中取出子弹,
            zd.draw()
            self.biuLIst.remove(zd) if zd.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-5  if self.x>=5 else 0
                if event.key==K_RIGHT:
                    self.x=self.x+5 if self.x<480-100-5 else 480-100
                if event.key == K_UP:
                    self.y = self.y - 5 if self.y > 5 else 0
                if event.key == K_DOWN:
                    self.y = self.y + 5 if self.y <= 520 - 124 - 5 else 520 - 124
                if event.key==K_SPACE:
                    zd=Biu(self.x+50-22/2,self.y-22)
                    self.biuLIst.append(zd)
    def pzjc(self,dfZdList):
        jtRect=Rect(self.x+36,self.y,28,42)
        jsRect=Rect(self.x,self.y+42,100,124-42)
        for zd in dfZdList:
            zdRect=Rect(zd.x,zd.y,9,21)
            if zdRect.colliderect(jtRect) or zdRect.colliderect(jsRect):
                self.isBomb=True


def getImage(image):#传入图片的文件名
    #下面第一个参数是图片的目录,这样把目录和文件名合成一个完整的绝对路径
    return os.path.join('E:\\Python\\IT研究院-Python\\New_Stydy\\img',image)
window=pygame.display.set_mode((480,520),0,32)
pygame.display.set_caption('战斗机')#设置标题
icon=pygame.image.load(getImage('icon72x72.png'))#导入图片
pygame.display.set_icon(icon)#把图片画到窗口左上角
pygame.key.set_repeat(30,30)
bg=pygame.image.load(getImage('background.png'))
wofang=HeroPlane(480/2-100/2,520-124)#定义一个我方飞机对象
difang=EnemyPlane(480/2-69/2,0)
while True:
    window.blit(bg,(0,0))
    wofang.draw()#调用画图
    difang.draw()
    difang.pzjc(wofang.biuLIst)
    wofang.pzjc(difang.biuLIst)
    wofang.dealEvent(pygame.event.get())

    pygame.display.update()
    pass

在这里插入图片描述
分模块:
三个模块:
子弹模块:BiuClass
飞机模块:PlaneClass
第三个模块:fightPlane
在fightPlane中导入 import pygame
from BiuClass import *
from PlaneClass import *

#BiuClass
import pygame,os
def getImage(image):#传入图片的文件名
    #下面第一个参数是图片的目录,这样把目录和文件名合成一个完整的绝对路径
    return os.path.join('E:\\Python\\IT研究院-Python\\New_Stydy\\img',image)
class BaseBiu():#基类
    def __init__(self,x,y,windows,image):
        self.x=x
        self.y=y
        self.windows=windows
        self.image=pygame.image.load(getImage(image))
    def draw(self):
         self.windows.blit(self.image,(self.x,self.y))
         self.move()
class EnemyBiu(BaseBiu):
    def __init__(self,x,y,windows):
        super().__init__(x,y,windows,'bullet1.png')
    def move(self):
        self.y+=5
class HeroBiu(BaseBiu):
    def __init__(self,x,y,windows):
        super().__init__(x,y,windows,'bullet.png')
    def move(self):
        self.y-=5
#PlaneClass
import pygame,time,random
from pygame.locals import *
from BiuClass import *
class BasePlane():
    def __init__(self,x,y,windows,normalImageList,bombImageList):
        self.x=x
        self.y=y
        self.windows=windows
        self.normalImageList=normalImageList#正常图片列表
        self.normalImageIndex=0
        #索引号,第一次显示第一张0下标,下次1 下标第二张,下次0下标...
        self.biuLIst=[]#子弹列表
        self.bombImageList=bombImageList
        self.bombImageIndex=0
        self.isBomb=False
    def draw(self):
        if self.isBomb==False:
            image=pygame.image.load(getImage(self.normalImageList[self.normalImageIndex]))
            self.normalImageIndex=(self.normalImageIndex+1)%len(self.normalImageList)
            self.windows.blit(image,(self.x,self.y))#画图
        else:
            if self.bombImageIndex==len(self.bombImageList):
                time.sleep(0.8)
                exit(0)
            image = pygame.image.load(getImage(self.bombImageList[self.bombImageIndex]))
            self.bombImageIndex += 1
            self.windows.blit(image, (self.x, self.y))
            time.sleep(0.5)
class HeroPlane(BasePlane):
    def __init__(self,x,y,windows):
        normalImageList = ['hero1.png', 'hero2.png']
        bombImageList = ['hero_blowup_n1.png', 'hero_blowup_n2.png', 'hero_blowup_n3.png', 'hero_blowup_n4.png']
        super().__init__(x,y,windows,normalImageList,bombImageList)
    def draw(self):
        super().draw()
        for zd in self.biuLIst:  # 从子弹列表中取出子弹,
            zd.draw()
            self.biuLIst.remove(zd) if zd.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 - 5 if self.x >= 5 else 0
                if event.key == K_RIGHT:
                    self.x = self.x + 5 if self.x < 480 - 100 - 5 else 480 - 100
                if event.key == K_UP:
                    self.y = self.y - 5 if self.y > 5 else 0
                if event.key == K_DOWN:
                    self.y = self.y + 5 if self.y <= 520 - 124 - 5 else 520 - 124
                if event.key == K_SPACE:
                    zd = HeroBiu(self.x + 50 - 22 / 2, self.y - 22,self.windows)
                    self.biuLIst.append(zd)
    def pzjc(self,dfZdList):
        jtRect=Rect(self.x+36,self.y,28,42)
        jsRect=Rect(self.x,self.y+42,100,124-42)
        for zd in dfZdList:
            zdRect=Rect(zd.x,zd.y,9,21)
            if zdRect.colliderect(jtRect) or zdRect.colliderect(jsRect):
                self.isBomb=True
class EnemyPlane(BasePlane):
    def __init__(self,x,y,windows):
        normalImageList = ['enemy1.png']
        bombImageList = ['enemy1_down1.png', 'enemy1_down2.png', 'enemy1_down3.png', 'enemy1_down4.png']
        self.direct='左'
        super().__init__(x,y,windows,normalImageList,bombImageList)
    def draw(self):
        super().draw()
        for zd in self.biuLIst:#从子弹列表中取出子弹,
            zd.draw()
            self.biuLIst.remove(zd) if zd.y>520 else ''
        self.move()
        self.fire()

    def move(self):
        if self.direct == '左':
            self.x -= 5
            if self.x <= 0:
                self.direct = '右'
        else:
            self.x += 5
            if self.x >= 480 - 69:
                self.direct = '左'

    def fire(self):
        x = random.randint(0, 100)
        if x == 3 or x == 7:
            zd = EnemyBiu(self.x + 69 / 2 - 9 / 2, self.y + 89,self.windows)
            self.biuLIst.append(zd)

    def pzjc(self, wfZdList):
        djRect = Rect(self.x, self.y, 69, 89)
        for zd in wfZdList:
            zdRect = Rect(zd.x, zd.y, 22, 22)
            if zdRect.colliderect(djRect):
                self.isBomb = True



#fightPlane
import pygame
from BiuClass import *
from PlaneClass import *
window=pygame.display.set_mode((480,520),0,32)
pygame.display.set_caption('战斗机')#设置标题
icon=pygame.image.load(getImage('icon72x72.png'))#导入图片
pygame.display.set_icon(icon)#把图片画到窗口左上角
pygame.key.set_repeat(30,30)
bg=pygame.image.load(getImage('background.png'))
wofang=HeroPlane(480/2-100/2,520-124,window)#定义一个我方飞机对象
difang=EnemyPlane(480/2-69/2,0,window)
while True:
    window.blit(bg,(0,0))
    wofang.draw()#调用画图
    difang.draw()
    difang.pzjc(wofang.biuLIst)
    wofang.pzjc(difang.biuLIst)
    wofang.dealEvent(pygame.event.get())

    pygame.display.update()
    pass



  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值