python飞机大战3关BOSS

这里是引用

import time ,random,pygame,os
from pygame.locals import *
def getPic(path):
return os.path.join(‘E:\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 HeroBullet(Base):
def draw(self):
self.pic = pygame.image.load(getPic(‘bullet.png’))
self.windows.blit(self.pic,(self.x,self.y))
self.y-=15

class enemyBullet(Base):
def draw(self):
self.pic=pygame.image.load(getPic(‘bullet2.png’))
self.windows.blit(self.pic,(self.x,self.y))
self.y+=3
class BasePlane(Base):
def init(self,x,y,windows,normalImageList,bombImageList):#自己属性
super().init(x,y,windows)#父类属性
self.normalImageList=normalImageList#未爆炸
self.normalImageIndex=0#nolmal正常的
self.bombImageList=bombImageList
self.bombImageIndex=0
self.isbomb=False
self.isbomb2 = False
self.BulletList = []
self.count=0
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):
if self.isbombFalse:
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):
self.count+=1
self.bombImageIndex=0
self.isbomb=False
if self.count
6:
fail = pygame.image.load(getPic(‘fail.jpg’))
self.windows.blit(fail, (0, 0))
pygame.display.update()
time.sleep(1)
exit(0)
pic = pygame.image.load(getPic(self.bombImageList[self.bombImageIndex]))
self.windows.blit(pic, (self.x, self.y))
self.bombImageIndex += 1
time.sleep(0.1)
for hero2 in self.BulletList:
hero2.draw()
self.BulletList.remove(hero2) if self.y<=0 else ‘’
def dealEvent(self,eventList):
for event in eventList:
if event.typeQUIT:
exit(0)
key_pressed=pygame.key.get_pressed()
#if event.type
KEYDOWN:
#if event.keyK_LEFT:
if key_pressed[K_LEFT]:
self.x=self.x-5 if self.x>5 else 0
if key_pressed[K_RIGHT]:
#if event.key
K_RIGHT:
self.x=self.x+5 if self.x<480-100-5 else 380
if key_pressed[K_UP]:
#if event.keyK_UP:
self.y=self.y-5 if self.y>5 else 0
if key_pressed[K_DOWN]:
#if event.key
K_DOWN:
self.y=self.y+5 if self.y<650-124-5 else 526
if key_pressed[K_SPACE]:
#if event.keyK_SPACE:
hero=HeroBullet(self.x+50-11,self.y-22,windows)
self.BulletList.append(hero)
def pzjc(self,bList):
heroRect=Rect(self.x,self.y,100,124)
for enemy in bList:
enemyRect=Rect(enemy.x,enemy.y,9,21)
if enemyRect.colliderect(heroRect):
self.isbomb = True
bList.remove(enemy)
for enemyPlane in enemyList:
enemyRect = Rect(enemyPlane.x, enemyPlane.y, 69, 89)
if enemyRect.colliderect(heroRect):
self.isbomb = True
enemyPlane.isbomb=True
class EnemyPlane(BasePlane):
def init(self,x,y,windows):
super().init(x,y,windows,[‘enemy1.png’,‘enemy-2.gif’],[‘enemy1_down1.png’,‘enemy1_down2.png’,‘enemy1_down3.png’,‘enemy1_down4.png’])
self.direct=‘左’
self.count=0
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):
self.bombImageIndex=0
self.isbomb=False
enemyList.remove(enemyPlane)#可变变量
if len(enemyCount1)%1615 and len(enemyList)0 :
enemyCount1.append(1)
guoguan=pygame.image.load(getPic(‘guoguan.jpg’))
self.windows.blit(guoguan,(0,0))
pygame.display.update()
time.sleep(1)
heroPlane.x=480/2-50
heroPlane.y=650-124
heroPlane.BulletList=[]
for i in range (len(enemyBulletList)):
enemyBulletList.pop(0)
for i in range (len(enemyBulletList)):
enemyBulletList.pop(0)
print(len(enemyBulletList))
pygame.display.update()
time.sleep(1)
guan.append(1)
if len(guan)3 :
success = pygame.image.load(getPic(‘success.jpg’))
self.windows.blit(success, (0, 0))
pygame.display.update()
time.sleep(3)
exit(0)
pic = pygame.image.load(getPic(self.bombImageList[self.bombImageIndex]))
self.windows.blit(pic, (self.x, self.y))
self.bombImageIndex += 1
time.sleep(0.1)
self.fire()
self.dealEvent()
def dealEvent(self):
y=random.randint(0,240)
if self.direct
’左’:
#类里对象函数和类函数调用全局变量得传入
self.x-=3#函数里和类里的静态函数以及类直接调用全局变量不用传入
self.y+=1
if self.x<3 or y
1:#静态函数和类函数不能调用self
self.direct = ‘右’
elif self.direct == ‘右’:
self.x+=3
self.y+=1
if self.x>480-69-3 or y
2:
self.direct = ‘左’
if self.y650:
enemyList.remove(enemyPlane)
def fire(self):
x=random.randint(0,150)
if x
6 or x60:
enemy=enemyBullet(self.x+146/2-9/2,self.y+246,windows)
enemyBulletList.append(enemy)
def pzjc(self,bList):
enemyRect=Rect(self.x,self.y,69,89)
for hero in bList:#bList
实例化的HeroPlane的BulletList属性
heroRect=Rect(hero.x,hero.y,22,22)
if heroRect.colliderect(enemyRect):
self.isbomb = True
bList.remove(hero)
class bossPlane(EnemyPlane):
def init(self,x,y,windows):
super().init(x, y, windows) # 父类属性
self.normalImageList = [‘enemy2.png’,‘enemy2_.gif’] # 未爆炸
self.normalImageIndex = 0 # nolmal正常的
self.bombImageList = [‘enemy2_down1.png’,‘enemy2_down2.png’,‘enemy2_down3.png’,‘enemy2_down4.png’,‘enemy2_down5.png’,‘enemy2_down6.png’]
self.bombImageIndex = 0
self.isbomb = False
self.isbomb2 = False
self.BulletList = []
self.direct=‘左’
def draw(self):
if self.isbombFalse:
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):
self.bombImageIndex=0
BossCount.append(1)
self.isbomb=False
enemyList.remove(BossPlane)#可变变量
print(len(enemyList))
print(self.count)
print(len(enemyCount1)%16)
if len(enemyCount1)%16
14 and len(enemyList)0 and len(BossCount)6 :
BossCount.clear()
enemyCount1.append(1)
enemyCount1.append(1)
guoguan=pygame.image.load(getPic(‘guoguan.jpg’))
self.windows.blit(guoguan,(0,0))
pygame.display.update()
time.sleep(1)
heroPlane.x=480/2-50
heroPlane.y=650-124
heroPlane.BulletList=[]
for i in range (len(enemyBulletList)):
enemyBulletList.pop(0)
for i in range (len(enemyBulletList)):
enemyBulletList.pop(0)
print(len(enemyBulletList))
pygame.display.update()
time.sleep(1)
guan.append(1)
if len(guan)3 :
success = pygame.image.load(getPic(‘success.jpg’))
self.windows.blit(success, (0, 0))
pygame.display.update()
time.sleep(3)
exit(0)
pic = pygame.image.load(getPic(self.bombImageList[self.bombImageIndex]))
self.windows.blit(pic, (self.x, self.y))
self.bombImageIndex += 1
time.sleep(0.1)
self.fire()
self.dealEvent()
def dealEvent(self):
y = random.randint(0, 240)
if self.direct == ‘左’:
# 类里对象函数和类函数调用全局变量得传入
self.x -= 3 # 函数里和类里的静态函数以及类直接调用全局变量不用传入
if self.x < 3 or y == 1: # 静态函数和类函数不能调用self
self.direct = ‘右’
elif self.direct == ‘右’:
self.x += 3
if self.x > 480 - 165 - 3 or y == 2:
self.direct = ‘左’
def fire(self):
x=random.randint(0,300)
if x%30
0:
enemy=enemyBullet(self.x+165/2-9/2,self.y+246,windows)
enemyBulletList.append(enemy)
def pzjc(self,bList):
BossRect=Rect(self.x,self.y,165,246)
for hero in bList:#bList
实例化的HeroPlane的BulletList属性
heroRect=Rect(hero.x,hero.y,22,22)
if heroRect.colliderect(BossRect):
self.isbomb = True
bList.remove(hero)
heroRect = Rect(heroPlane.x, heroPlane.y, 100, 124)
if BossRect.colliderect(heroRect):
self.isbomb = True
heroPlane.isbomb = True
heroPlane.count=6
windows=pygame.display.set_mode((480,650),0,32)#mode风格
pygame.display.set_caption(‘飞机大战’)#设置头部
icon=pygame.image.load(getPic(‘icon72x72.png’))
pygame.display.set_icon(icon)#设置图标
bg=pygame.image.load(getPic(‘background1.jpg’))
heroPlane=HeroPlane(190,526,windows)#实例化对必须放在循环外
enemyPlane=EnemyPlane((480-69)/2,0,windows)
BossCount=[]
#pygame.key.set_repeat(15,15)
time1=0
enemyList=[]
enemyCount=0
enemyCount1=[]
enemyBulletList=[]
guan=[]
while True:
time1+=1
windows.blit(bg, (0, 0))#blit位块传输#加载图片
heroPlane.draw()
if len(enemyCount1)%16
14 :
if len(enemyList)0:
print(1)
BossPlane = bossPlane((480 - 165) / 2, 0, windows)
enemyList.append(BossPlane)
# BossPlane.draw()
# BossPlane.pzjc(heroPlane.BulletList) # heroPlane是对象名要非常注意
# heroPlane.pzjc(enemyBulletList)
print(2)
else:
pass
elif len(enemyList)3 or len(enemyCount1)%1615:
pass
elif time1 % 5
0 :
enemyCount+=1
enemyCount1.append(1)
x=random.randint(0,480-69)
enemyPlane = EnemyPlane(x, 0, windows)
enemyList.append(enemyPlane)
for enemyPlane in enemyList:
enemyPlane.draw()
enemyPlane.pzjc(heroPlane.BulletList) # heroPlane是对象名要非常注意
heroPlane.pzjc(enemyBulletList)
for enemy in enemyBulletList:
enemy.draw()
enemyBulletList.remove(enemy) if enemy.y >= 650 else 0
heroPlane.dealEvent(pygame.event.get())
for heroBullet in heroPlane.BulletList:
HeroBulletRect = Rect(heroBullet.x, heroBullet.y, 22, 22)
for enemy in enemyBulletList:
enemyRect = Rect(enemy.x, enemy.y, 9, 21)
if enemyRect.colliderect(HeroBulletRect):
enemyBulletList.remove(enemy)
heroPlane.BulletList.remove(heroBullet)
break
pygame.display.update()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值