Python 游戏编程之实现飞机大战(含源代码)( 三 )

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.rect.left, self.rect.top = position self.speed = 12 self.active = False self.mask = pygame.mask.from_surface(self.image) def move(self): self.rect.top -= self.speed if self.rect.top < 0: self.active = False def reset(self, position): self.rect.left, self.rect.top = position self.active = Trueclass Bullet2(pygame.sprite.Sprite): def __init__(self, position): pygame.sprite.Sprite.__init__(self) self.image = pygame.image.load("images/bullet2.png").convert_alpha self.rect = self.image.get_rect self.rect.left, self.rect.top = position self.speed = 14 self.active = False self.mask = pygame.mask.from_surface(self.image) def move(self): self.rect.top -= self.speed if self.rect.top < 0: self.active = False def reset(self, position): self.rect.left, self.rect.top = position self.active = True五.发放补给包
游戏设计每30秒随机发放一个补给包,可 能是超级子弹,也可能是全屏炸弹 。 补给包有自己的图像和运动轨迹 , 定义一个模块supply.py:
import pygamefrom random import *class Bullet_Supply(pygame.sprite.Sprite): def __init__(self, bg_size): pygame.sprite.Sprite.__init__(self) self.image = pygame.image.load("images/bullet_supply.png").convert_alpha self.rect = self.image.get_rect self.width, self.height = bg_size[0], bg_size[1] self.rect.left, self.rect.bottom = \ randint(0, self.width - self.rect.width), -100 self.speed = 5 self.active = False self.mask = pygame.mask.from_surface(self.image) def move(self): if self.rect.top < self.height: self.rect.top += self.speed else: self.active = False def reset(self): self.active = True self.rect.left, self.rect.bottom = \ randint(0, self.width - self.rect.width), -100class Bomb_Supply(pygame.sprite.Sprite): def __init__(self, bg_size): pygame.sprite.Sprite.__init__(self) self.image = pygame.image.load("images/bomb_supply.png").convert_alpha self.rect = self.image.get_rect self.width, self.height = bg_size[0], bg_size[1] self.rect.left, self.rect.bottom = \ randint(0, self.width - self.rect.width), -100 self.speed = 5 self.active = False self.mask = pygame.mask.from_surface(self.image) def move(self): if self.rect.top < self.height: self.rect.top += self.speed else: self.active = False def reset(self): self.active = True self.rect.left, self.rect.bottom = \ randint(0, self.width - self.rect.width), -100六.主模块所有的模块都到齐了 , 接下来就该实现我们的主模块:
# main.pyimport pygameimport sysimport tracebackimport myplaneimport enemyimport bulletimport supplyfrom pygame.locals import *from random import *pygame.initpygame.mixer.initbg_size = width, height = 480, 700screen = pygame.display.set_mode(bg_size)pygame.display.set_caption("飞机大战 -- FishC Demo")background = pygame.image.load("images/background.png").convertBLACK = (0, 0, 0)WHITE = (255, 255, 255)GREEN = (0, 255, 0)RED = (255, 0, 0)# 载入游戏音乐pygame.mixer.music.load("sound/game_music.ogg")pygame.mixer.music.set_volume(0.2)bullet_sound = pygame.mixer.Sound("sound/bullet.wav")bullet_sound.set_volume(0.2)bomb_sound = pygame.mixer.Sound("sound/use_bomb.wav")bomb_sound.set_volume(0.2)supply_sound = pygame.mixer.Sound("sound/supply.wav")supply_sound.set_volume(0.2)get_bomb_sound = pygame.mixer.Sound("sound/get_bomb.wav")get_bomb_sound.set_volume(0.2)get_bullet_sound = pygame.mixer.Sound("sound/get_bullet.wav")get_bullet_sound.set_volume(0.2)upgrade_sound = pygame.mixer.Sound("sound/upgrade.wav")upgrade_sound.set_volume(0.2)enemy3_fly_sound = pygame.mixer.Sound("sound/enemy3_flying.wav")