| 
 | 
 
 
 楼主 |
发表于 2021-3-19 16:20:01
|
显示全部楼层
 
 
 
子弹模块里的 
 
class Bullet1(pygame.sprite.Sprite): 
    def __init__(self, position): 
        pygame.sprite.Sprite.__init__(self) 
 
        self.image = pygame.image.load('飞机大战素材/images/bullet1.png').convert_alpha() 
        self.rect = self.image.get_rect() 
        self.left, self.top = position 
        self.active = True 
        self.speed = 12 
        self.mask = pygame.mask.from_surface(self.image) 
 
    def move(self): 
        self.rect.bottom -= self.speed 
        if self.rect.bottom < 0: 
            self.active = False 
 
    def reset(self, position): 
        self.rect.left, self.rect.top = position 
        self.active = True 
 
 
main函数里的 
 
# 发射子弹,每10帧反射一次 
            if not (delay % 10): 
                bullet_sound.play() 
                if is_double_bullet: 
                    bullets = bullet2 
                    bullets[bullet2_index].reset((me.rect.centerx - 33, me.rect.centery)) 
                    bullets[bullet2_index + 1].reset((me.rect.centerx + 30, me.rect.centery)) 
                    bullet2_index = (bullet2_index + 2) % BULLET2_NUM 
                else: 
                    bullets = bullet1 
                    bullets[bullet1_index].reset(me.rect.midtop) 
                    bullet1_index = (bullet1_index + 1) % BULLET1_NUM 
                # 检测子弹是否击中敌机 
                for b in bullets: 
                    if b.active: 
                        b.move() 
                        screen.blit(b.image, b.rect) 
                        enemy_hit = pygame.sprite.spritecollide( 
                            b, enemies, False, pygame.sprite.collide_mask) 
                        if enemy_hit: 
                            b.active = False 
                            for e in enemy_hit: 
                                if e in mid_enemies or e in big_enemies: 
                                    e.hit = True 
                                    e.energy -= 1 
                                    if e.energy == 0: 
                                        e.active = False 
                                else: 
                                    e.active = False 
 |   
 
 
 
 |