上一篇(Python游戏之Pygame——太空飞机大战(一))文章简单对游戏进行了说明,给出了部分配置文件以及子弹类。下面给出敌机类和方法。
大家直到,飞机必须能非,最好能非直线飞行。因此必须有X_speed和Y_speed。飞机还要能发射子弹或导弹等,飞机有多少个发射位,多久发射一次,还有飞机被子弹击中或者敌机与英雄战机相撞等各种情况都是需要处理的。
下面先给出敌机类和方法。
class EnemyPlane(Sprite):
def __init__(self, flightType, layerGroup, ePlaneGroup):
self.groups = layerGroup, ePlaneGroup
self.type = flightType
self._layer = ENEMY_PLANES[self.type]['LAYER']
super().__init__(self.groups)
self.image = pygame.image.load(IMAGE_PATH + ENEMY_PLANES[self.type]['IMAGE']).convert_alpha()
self.rect = Rect((random.randint(60, SCREEN_SIZE[0] - 60), 0), self.image.get_size())
self.move_direction = random.choice([-1, 0, 1]) # 1表示向右, 0表示直线飞行
self.defence = ENEMY_PLANES[self.type]['DEFENCE']
self.destroyValue = ENEMY_PLANES[self.type]['COLLISION']
self.bulletType = random.choice(list(ENEMY_BULLETS_TYPE.values()))
self.missileType = random.choice(list(ENEMY_MISSILE_TYPE.values()))
self.bShootCount = ENEMY_PLANES[self.type]['B_COUNT']