import pygame,random,time,os
from pygame.locals import *
def getPic(path):#创建一个函数
#path只是文件名称,在这里是图片名称,加到绝对路径上易于修改
return os.path.join('E:\\python使用软件\\IT研究院-Python\\New_Stydy\\img',path)#获取文件名
class HeroBullt():
def __init__(self,x,y,windows):
self.x=x
self.y=y
self.windoes=windows
self.pic=pygame.image.load(getPic('plane.png'))
def draw(self):
self.windoes.blit(self.pic,(self.x,self.y))
self.move()
def move(self):
self.y-=4
class enemyBullt():
def __init__(self,x,y,windows):
self.x=x
self.y=y
self.windoes=windows
self.pic=pygame.image.load(getPic('enemy0.png'))
def draw(self):
self.windoes.blit(self.pic,(self.x,self.y))
self.move()
def move(self):
self.y+=5
class HeroPlane():
def __init__(self,x,y,windows):
self.x = x
self.y = y
self.windoes = windows
self.normalImageList=['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.bombImageIndex=0#爆炸图像索引
self.isBomb=False
self.biuList=[]
def draw(self):
if self.isBomb == False:
pic = pygame.image.load(getPic(self.normalImageList[self.normalIndex]))
self.windoes.blit(pic, (self.x, self.y))
self.normalIndex = (self.normalIndex + 1) % len(self.normalImageList) # if else 循环
else:
if self.bombImageIndex==len(self.bombImageList):
time.sleep(0.8)
exit(0)
pic = pygame.image.load(getPic(self.bombImageList[self.bombImageIndex]))
self.windoes.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-10 if self.x>=10 else 0
elif event.key==K_RIGHT:
self.x=self.x+10 if self.x<=370 else 380
elif event.key == K_UP:
self.y=self.y-10 if self.y>=10 else 0
elif event.key == K_DOWN:
self.y=self.y+10 if self.y<=516 else 526
elif event.key ==K_SPACE:
oneBiu=HeroBullt(self.x+5 ,self.y-10,windows)
twoBiu = HeroBullt(self.x + 61 , self.y - 10, windows)
self.biuList.append(oneBiu)
self.biuList.append(twoBiu)
def pzjc(self,bList):
eRect=Rect(self.x,self.y,100,124)
for biu in bList:
bRect=Rect(biu.x,biu.y,51,39)
if bRect.colliderect(eRect):
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.biuList=[]
self.direct='左'
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.8)
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.6)
for biu in self.biuList:
biu.draw()
self.biuList.remove(biu) if biu.y>650 else''
self.move()
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 - 60:
self.direct = '左'
x=random.randint(0,100)
if x==32 or x==78:
oneBiu = enemyBullt(self.x+25 , 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,38,38)
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(10,10)#移动连续,括号里面第一个是按下10毫秒反应,后面是10毫秒后没抬起继续
pygame.display.set_icon(icon)#游戏显示图标
bg=pygame.image.load(getPic('background.png'))#背景图片
heroPlane=HeroPlane(480//2-100//2,650-124,windows)
enemyPlan=EnemyPlane(480//2-69//2,0,windows)
while True:
windows.blit(bg,(0,0))
heroPlane.draw()
enemyPlan.draw()
enemyPlan.pzjc(heroPlane.biuList)
heroPlane.pzjc(enemyPlan.biuList)
heroPlane.dealEvent(pygame.event.get())
pygame.display.update()
飞机大战源代码
最新推荐文章于 2024-07-20 22:34:54 发布