飞机大战没有封装的前的代码
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):
self.name=name
def say(self):#如果调用子类函数,父类不能被实例化,一定会报错
self.change()
class Bu(Base):
def change(self):
print('刘德华')
a=Bu('张三')
a.say()
b=Base('李四')
b.say()
最终封装
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 Base():
def __init__(self,x,y,windows):
self.x=x
self.y=y
self.windows=windows
class BaseBullet(Base):
def draw(self):
self.windows.blit(self.pic,(self.x,self.y))
self.move()
class HeroBullet(BaseBullet):
def __init__(self,x,y,windows):
super().__init__(x,y,windows)
self.pic = pygame.image.load(getPic("bullet.png"))
def move(self):
self.y-=5
class EnemyBullet(BaseBullet):
def __init__(self,x,y,windows):
super().__init__(x, y, windows)
self.pic = pygame.image.load(getPic("bullet1.png"))
def move(self):
self.y+=5
class BasePlane(Base):
def __init__(self,x,y,windows,normalImageList,bombImageList):
super().__init__(x,y,windows)
self.normalImageList=normalImageList
self.normalIndex=0
self.bombImageList = bombImageList
self.bombImageIndex = 0
self.isBomb = False
self.biuList = []
def draw(self):
if self.isBomb == False:
pic=pygame.image.load(getPic(self.normalImageList[self.normalIndex]))
self.windows.blit(pic, (self.x, self.y))
self.normalIndex = (self.normalIndex + 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)
class EnemyPlane(BasePlane):
def __init__(self,x,y,windows):
super().__init__(x,y,windows,['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.biuList:
biu.draw()
self.biuList.remove(biu) if biu.y>650 else ""
def move(self):
if self.direct == "左":
self.x -= 3
if self.x <= 0:
self.direct = "右"
elif self.direct == "右":
self.x += 3
if self.x >= 480 - 69:
self.direct = "左"
def fire(self):
x=random.randint(0,100)
if x == 9 or x==78:
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(BasePlane):
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'])
def draw(self):
super().draw()
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-100-5 else 480-100
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)
bg=pygame.image.load(getPic("background.png"))
pygame.display.set_caption("灰机大战")
icon=pygame.image.load(getPic("icon72x72.png"))
pygame.display.set_icon(icon)
pygame.key.set_repeat(20,30)#让按键有延迟,更灵活
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()