import pygame,time,random,os
from pygame.locals import *
def GetPic(path):
return os.path.join("E:\\IT研究院-Python\\New_Stydy\\img",path)
class Base():
def __init__(self,x,y,windows):
self.x=x
self.y=y
self.windows=windows
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
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(2)
# exit(0)
self.isbomb=False
return
pic = pygame.image.load(GetPic(self.bombImageList[self.bombImageIndex]))
self.windows.blit(pic, (self.x, self.y))
self.bombImageIndex +=1
time.sleep(0.5)
class BaseBullet(Base):
def __init__(self,x,y,windows,bulImage):
super().__init__(x,y,windows)
self.pic=pygame.image.load(GetPic(bulImage))
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,"bullet.png")
def move(self):
self.y-=5
class EnemyBullet(BaseBullet):
def __init__(self,x,y,windows):
super().__init__(x,y,windows,"bullet2.png")
def move(self):
self.y+=5
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"])
self.BiuList = []
def draw(self):
super().draw()
for biu in self.BiuList:
biu.draw()
self.BiuList.remove(biu) if biu.y<10 else ""
def pzjc(self,blist):
hRect=Rect(self.x,self.y,100,124)
for biu in blist:
biuRect=Rect(biu.x,biu.y,9,21)
if biuRect.colliderect(hRect):
blist.remove(biu)
self.isbomb=True
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+100//2-22/2,self.y+22,windows)
self.BiuList.append(onebiu)
class EnemyPlane(BasePlane):
def __init__(self,x,y,windows):
super().__init__(x,y,windows,["enemy2.png","enemy21.png"],["enemy2_down1.png","enemy2_down2.png","enemy2_down3.png","enemy2_down4.png","enemy2_down5.png","enemy2_down6.png"])
self.BiuList = []
self.direct="左"
def draw(self):
super().draw()
for biu in self.BiuList:
biu.draw()
self.BiuList.remove(biu) if biu.x>600 else ""
self.move()
self.biu()
def move(self):
n=random.randint(0,15)
if self.direct == "左":
self.x -= 5
if self.x <= 0 or n==5:
self.direct = "右"
elif self.direct == "右":
self.x += 5
if self.x >= 480 - 165 or n==10:
self.direct = "左"
def biu(self):
x=random.randint(0,30)
if x==10:
twobiu=EnemyBullet(self.x+165//2-9//2,self.y+246,windows)
self.BiuList.append(twobiu)
def pzjc(self,blist):
eRect=Rect(self.x,self.y,165,246)
for biu in blist:
biuRect=Rect(biu.x,biu.y,22,22)
if biuRect.colliderect(eRect):
blist.remove(biu)
self.isbomb=True
windows=pygame.display.set_mode((480,650),0,32)
pygame.display.set_caption("飞机大战")
icon=pygame.image.load(GetPic("icon72x72.png"))
pygame.display.set_icon(icon)
bg=pygame.image.load(GetPic("background.png"))
heroPlane=HeroPlane(480//2-100//2,650-124,windows)
pygame.key.set_repeat(1,2)
enemyPlane=EnemyPlane(480//2-165//2,0,windows)
while True:
windows.blit(bg,(0,0))
heroPlane.draw()
heroPlane.dealevent(pygame.event.get())
enemyPlane.draw()
enemyPlane.pzjc(heroPlane.BiuList)
heroPlane.pzjc(enemyPlane.BiuList)
pygame.display.update()