飞机大战编程

博客内容主要探讨了飞机大战游戏的编程实现,详细阐述了在没有封装前的代码状态,并逐步介绍了如何进行代码封装,最终实现更高效、可维护的游戏代码结构。
摘要由CSDN通过智能技术生成

飞机大战没有封装的前的代码

import pygame
from pygame.locals import *
import random
import time
class HeroBullet():#定义一个战机子弹的类
    def __init__(self,x,y,windows):
        self.x=x
        self.y=y
        self.windows=windows
        self.pic=pygame.image.load('D:\\python使用软件\\IT研究院-Python\\New_Stydy\\img\\bullet.png')
    def draw(self):#用来画子弹
        self.windows.blit(self.pic,(self.x,self.y))
        self.move()
    def move(self):
        self.y-=5
class EnemyBullet():
    def __init__(self,x,y,windows):
        self.x=x
        self.y=y
        self.windows=windows
        self.pic=pygame.image.load('D:\\python使用软件\\IT研究院-Python\\New_Stydy\\img\\bullet1.png')
    def draw(self):
        self.windows.blit(self.pic,(self.x,self.y))
        self.move()
    def move(self):
        self.y+=5

windows=pygame.display.set_mode((480,650),0,32)#创建窗口
bg=pygame.image.load('D:\\python使用软件\\IT研究院-Python\\New_Stydy\\img\\background.png')
pygame.display.set_caption('灰机大战')
icon=pygame.image.load('D:\\python使用软件\\IT研究院-Python\\New_Stydy\\img\\icon72x72.png')
pygame.display.set_icon(icon)
heroPlane1=pygame.image.load('D:\\python使用软件\\IT研究院-Python\\New_Stydy\\img\\hero1.png')
heroPlane2=pygame.image.load('D:\\python使用软件\\IT研究院-Python\\New_Stydy\\img\\hero2.png')
enemyPlane=pygame.image.load('D:\\python使用软件\\IT研究院-Python\\New_Stydy\\img\\enemy1.png')
enemyBombList=[ 'D:\\python使用软件\\IT研究院-Python\\New_Stydy\\img\\enemy1_down1.png',
              'D:\\python使用软件\\IT研究院-Python\\New_Stydy\\img\\enemy1_down2.png',
              'D:\\python使用软件\\IT研究院-Python\\New_Stydy\\img\\enemy1_down3.png',
              'D:\\python使用软件\\IT研究院-Python\\New_Stydy\\img\\enemy1_down4.png',]
heroIndexShift=0
direct='左'
pygame.key.set_repeat(20,30)#重复按键指令
heroPlaneX=190
heroPlaneY=526
enemyPlaneX=480//2-69//2
enemyPlaneY=0
BiuList=[]
enemyBiulist=[]
enemy_isBomb=False#敌机爆炸条件
enemy_BombIndex=0#爆炸图片索引
while True:
    windows.blit(bg,(0,0))#把图片贴上去!
    if heroIdexShift==0:
        windows.blit(heroPlane1,(heroPlaneX,heroPlaneY))
        heroIdexShift+=1
    else:
        windows.blit(heroPlane2, (heroPlaneX, heroPlaneY))
        heroIdexShift=0
    if direct=='左':#敌机移动
        enemyPlaneX-=5
        if enemyPlaneX<=0:
            direct='右'
    elif direct=='右':
        enemyPlaneX+=5
        if enemyPlaneX>=480-69:
            direct='左'
    if enemy_isBomb==False:#敌机爆炸
        windows.blit(enemyPlane,(enemyPlaneX,enemyPlaneY))
    else:
        if enemy_BombIndex==len(enemyBombList):
            time.sleep(0.1)
            exit(0)
        pic=pygame.image.load(enemyBombList[enemy_BombIndex])
        windows.blit(pic,(enemyPlaneX,enemyPlaneY))
        enemy_BombIndex=(enemy_BombIndex+1)
        time.sleep(0.1)
    for biu in BiuList:
        biu.draw()
   
        BiuList.remove(biu) if biu.y<0 else '' #让子弹到最上边的时候,不至于消失
    for biu in EnemyBiulist:
        biu.draw()
        EnemyBiulist.remove(biu) if biu.y>650-79 else''
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            print('关闭了')
            exit(0)
        if event.type==pygame.KEYDOWN:
            if event.key==pygame.K_LEFT
               heroPlaneX=heroPlaneX-5  if heroPlaneX>=5 else 0
            elif event.key==pygame.K_RIGHT:
               heroPlaneX=heroPlaneX+5  if heroPlaneX<=375 else 380
            elif event.key==pygame.K_DOWN:
              heroPlaneY=heroPlaneY+5   if heroPlaneY<=521 else 526
            elif event.key==pygame.K_UP:
              heroPlaneY=heroPlaneY-5   if heroPlaneY>=5  else 0
            elif event.key==K_SPACE:
                oneBiu=HeroBullet(heroPlaneX+50-11,heroPlaneY-22,windows)
                BiuList.append(oneBiu)
    x=random.randint(0,100)
    if x==5 or x==79:
        oneBiu=EnemyBullet(enemyPlaneX+69//2-9//2,enemyPlaneY+89,windows)
        EnemyBiulist.append(oneBiu)
    enemyRect=Rect(enemyPlaneX,enemyPlaneY,69,89)
    for biu in BiuList:
        biuRect=Rect(biu.x,biu.y,22,22)
        if biuRect.colliderect(enemyRect):#当战机子弹和敌机重叠时爆炸
            print('爆炸')
            enemy_isBomb=True
            BiuList.remove(biu)
    pygame.display.update()

第一封装:

import pygame,random,time,os
from pygame.locals import *
def getPic(path):
    #path只是文件名称,在这里是图片名称。加入到绝对路径上,写此函数易于不同环境下的修改
    return os.path.join('D:\\python使用软件\\IT研究院-Python\\New_Stydy\\img',path)
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-=5
class EnemyBullet():
    def __init__(self,x,y,windows):
         self.x=x
         self.y=y
         self.windows=windows
         self.pic=pygame.image.load(getPic('bullet1.png'))
    def draw(self):
        self.windows.blit(self.pic,(self.x,self.y))
        self.move()
    def move(self):
        self.y+=5
class EnemyPlane():
    def __init__(self,x,y,windows):
        self.x=x
        self.y=y
        self.windows=windows
        self.normalImageList=['enemy1.png']
        self.normalImageIndex=0
        self.bombImageList=['enemy1_down1.png','enemy1_down2.png','enemy1_down3.png','enemy1_down4.png',]
        self.bombImageIndex=0
        self.isbomb=False
        self.biuList=[]
        self.direct='左'
    def draw(self):
        if self.isbomb==False:
            pic=pygame.image.load(getPic(self.normalImageList[self.normalImageIndex]))
            self.windows.blit(pic,(self.x,self.y))
            self.normalImageIndex=(self.normalImageIndex+1)%len(self.normalImageList)#是让两张照片无线循环
        else:
            if self.bombImageIndex==len(self.bombImageList):
                time.sleep(0.8)
                exit(0)
            pic = pygame.image.load(getPic(self.bombImageList[self.bombImageIndex]))
            self.windows.blit(pic,(self.x, self.y))
            self.bombImageIndex=(self.bombImageIndex + 1)
            time.sleep(0.6)
        self.move()
        self.fire()
        for biu in self.biuList:
            biu.draw()
            self.biuList.remove(biu) if self.y>650 else''
    def move(self):
        if self.direct =='左':
            self.x-=5
            if  self.x<=0:
                self.direct='右'
        elif self.direct =='右':
            self.x+=5
            if self.x>=480-69:
                self.direct='左'
    def fire(self):
        x=random.randint(0,100)
        if x==5 or x==79:
            oneBiu=EnemyBullet(self.x+69//2-9//2, self.y+89, windows)
            self.biuList.append(oneBiu)
    def pzjc(self,bList):#敌机爆炸检测条件
        eRect=Rect(self.x,self.y,69,89)
        for biu in bList:
            bRect=Rect(biu.x,biu.y,22,22)
            if bRect.colliderect(eRect):
                self.isbomb=True
class HeroPlane():
    def __init__(self,x,y,windows):
        self.x=x
        self.y=y
        self.windows=windows
        self.normalImageList=['hero1.png','hero2.png']#为爆炸的图片列表
        self.normaIndex=0
        self.bombImageList=['hero_blowup_n1.png','hero_blowup_n2.png','hero_blowup_n3.png','hero_blowup_n4.png']
        self.bombIamgeIndex=0#显示图片列表索引
        self.isBomb=False
        self.biuList=[]
    def draw(self):
        if self.isBomb==False:
            pic=pygame.image.load(getPic(self.normalImageList[self.normaIndex]))
            self.windows.blit(pic,(self.x,self.y))
            self.normaIndex=(self.normaIndex+1)%len(self.normalImageList)#将战机两张照片循环出现
        else:
            if self.bombImageIndex == len(self.bombImageList):
                time.sleep(0.8)
                exit(0)
            pic=pygame.image.load(getPic(self.bombImageList[self.bombImageIndex]))
            self.windows.blit(pic, (self.x, self.y))
            self.bombImageIndex = (self.bombImageIndex + 1)
            time.sleep(0.6)
        for biu in self.biuList:
            biu.draw()
            self.biuList.remove(biu)if biu.y<0 else''
    def dealEvent(self,eventList):
        for event in eventList:
            if event.type==QUIT:
                exit(0)
            if event.type==KEYDOWN:
                if event.key==K_LEFT:
                    self.x=self.x-5 if self.x>=5 else 0
                elif event.key==K_RIGHT:
                    self.x=self.x+5 if self.x<=480-5-100 else 380
                elif event.key==K_UP:
                    self.y=self.y-5 if self.y>=5 else 0
                elif event.key==K_DOWN :
                    self.y=self.y+5 if self.y<=650-124-5 else 650-124
                elif event.key==K_SPACE:
                    oneBiu=HeroBullet(self.x+39,self.y-22,windows)
                    self.biuList.append(oneBiu)
    def pzjc(self,bList):#爆炸检测条件
        eRect=Rect(self.x,self.y,100,124)
        for biu in bList:
            bRect=Rect(biu.x,biu.y,9,21)
            if bRect.colliderect(eRect):
                self.isbomb=True
windows=pygame.display.set_mode((480,650),0,32)
pygame.display.set_caption('灰机大战')#写游戏的名称
icon=pygame.image.load(getPic('icon72x72.png'))
pygame.key.set_repeat(20,30)#设置按键的延迟,增加按键的灵活度用key.set_repeat(delay,interval)就是按住某个键每隔interval毫秒产生一个KEYDOWN事件,delay就是多少毫秒后才开始触发这个事件。
pygame.display.set_icon(icon)#更新游戏图标
bg=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(bg,(0,0))#贴背景图
    enemyPlane.pzjc(heroPlane.biuList)
    heroPlane.pzjc(enemyPlane.biuList)
    heroPlane.draw()#调用函数
    enemyPlane.draw()
    heroPlane.dealEvent(pygame.event.get())
    pygame.display.update()#游戏更新


父类调用子类方法,父类不能被实例化
class Base():
    def __init__(self,name):
  
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值