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: key_pressde = pygame.key.get_pressed() if key_pressde[K_LEFT]: self.x = self.x - 5 if self.x >= 5 else 0 if key_pressde[K_RIGHT]: self.x = self.x + 5 if self.x < 375 else 375 if key_pressde[K_UP]: self.y = self.y - 5 if self.y > 0 else 0 if key_pressde[K_DOWN]: self.y = self.y + 5 if self.y < 650 - 124 else 650 - 124 if key_pressde[K_SPACE]: oneBiu = HeroBullet(self.x + 39, self.y - 22, windows) self.biuList.append(oneBiu)
#检测我方飞机是否被击中 def pzjc(self,bList): #检测敌方飞机何时被击中 hRect=Rect(self.x,self.y,100,124) for biu in bList: bRect=Rect(biu.x,biu.y,9,21) if bRect.colliderect(hRect): self.isBomb=True
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_down3.png"] ) self.direct = "左" #类调用函数需要传入参数才能使用,静态属性不用传参,跟在类外写函数一样,设置飞机的初始飞行方向 self.enemyBiuList=[] #设置敌机的弹夹列表 def draw(self): #画出飞机图片 super().draw() self.move() #调用飞机移动方法 self.fire() #调用开火函数 for biu in self.enemyBiuList: biu.draw() self.enemyBiuList.remove(biu) if biu.y < 0 else ""
def move(self): #飞机移动的方法 k=random.randint(0,480) #设置随机数,当飞机为随机数时让飞机往相反方向飞行 if self.direct == "左": self.x = self.x - 3 if self.x <= 0 or self.x==k: self.direct = "右" elif self.direct == "右": self.x = self.x + 3 if self.x >= 480 - 69 or self.x==k: self.direct = "左"
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
#定义开火装填子弹,定义随机数,当x为随机数时生成子弹,然后把子弹装进enemyBiuList列表里 def fire(self): x = random.randint(0, 50) if x == 5 or x == 7: oneBiu = EnemyBullt(self.x + 69 // 2 - 9 // 2, self.y + 89, windows) self.enemyBiuList.append(oneBiu)