def load_static_data(): # numbers sprites for score display IMAGES['numbers'] = ( pygame.image.load('assets/sprites/0.png').convert_alpha(), pygame.image.load('assets/sprites/1.png').convert_alpha(), pygame.image.load('assets/sprites/2.png').convert_alpha(), pygame.image.load('assets/sprites/3.png').convert_alpha(), pygame.image.load('assets/sprites/4.png').convert_alpha(), pygame.image.load('assets/sprites/5.png').convert_alpha(), pygame.image.load('assets/sprites/6.png').convert_alpha(), pygame.image.load('assets/sprites/7.png').convert_alpha(), pygame.image.load('assets/sprites/8.png').convert_alpha(), pygame.image.load('assets/sprites/9.png').convert_alpha()) # game over sprite IMAGES['gameover'] = pygame.image.load( 'assets/sprites/gameover.png').convert_alpha() # message sprite for welcome screen IMAGES['message'] = pygame.image.load( 'assets/sprites/message.png').convert_alpha() # base (ground) sprite IMAGES['base'] = pygame.image.load( 'assets/sprites/base.png').convert_alpha() # sounds if 'win' in sys.platform: soundExt = '.wav' else: soundExt = '.ogg' SOUNDS['die'] = pygame.mixer.Sound('assets/audio/die' + soundExt) SOUNDS['hit'] = pygame.mixer.Sound('assets/audio/hit' + soundExt) SOUNDS['point'] = pygame.mixer.Sound('assets/audio/point' + soundExt) SOUNDS['swoosh'] = pygame.mixer.Sound('assets/audio/swoosh' + soundExt) SOUNDS['wing'] = pygame.mixer.Sound('assets/audio/wing' + soundExt) # hismask for pipes HITMASKS['pipe'] = ( getHitmask( pygame.transform.rotate( pygame.image.load(PIPES_LIST[0]).convert_alpha(), 180)), getHitmask(pygame.image.load(PIPES_LIST[0]).convert_alpha()), ) # hitmask for player HITMASKS['player'] = ( getHitmask(pygame.image.load(PLAYERS_LIST[0][0]).convert_alpha()), getHitmask(pygame.image.load(PLAYERS_LIST[1][0]).convert_alpha()), getHitmask(pygame.image.load(PLAYERS_LIST[2][0]).convert_alpha()), )
def __init__(self, bg_size, landy, color, ai=False): pygame.sprite.Sprite.__init__(self) if 0 <= color <= 2: self.images = list(map(lambda x: x.convert_alpha(), image[color])) elif color == 3: self.images = list( map(lambda x: x.convert_alpha(), image[random.choice([0, 1, 2])])) elif color == 4: try: self.images = [ pygame.image.load( "assets/images/birds/custom_0.png").convert_alpha(), pygame.image.load( "assets/images/birds/custom_1.png").convert_alpha(), pygame.image.load( "assets/images/birds/custom_2.png").convert_alpha() ] except Exception: self.images = list( map(lambda x: x.convert_alpha(), image[random.choice([0, 1, 2])])) self.image_index_cycle = cycle([0, 1, 2, 1]) self.index = next(self.image_index_cycle) self.image = self.images[self.index] self.mask = getHitmask(self.image) self.width, self.height = bg_size self.landy = landy self.rect = self.images[0].get_rect() self.rect.left = int(self.width * 0.2) self.rect.top = (self.height - self.rect.height) // 2 self.center = self.rect.centerx, self.rect.centery self.alive = True self.ai = ai # 当前速度(负值向上) if self.ai: self.speedy = 0 else: self.speedy = -9 # 最大向下速度 self.max_speedy = 10 # 向下加速度 self.down_speedy_a = 1 # 角速度 self.angular_speed = 3 # 旋转角度 self.rotate = 45 if not self.ai: self.image = pygame.transform.rotate(self.images[self.index], self.rotate) else: self.image = self.images[self.index]
def __init__(self, bg_size): pygame.sprite.Sprite.__init__(self) self.image = pygame.image.load("assets/images/land.png").convert() self.mask = getHitmask(self.image) self.width, self.height = bg_size self.rect = self.image.get_rect() self.rect.top = int(self.height * 0.79) self.LANDSHIFT = self.rect.width - self.width self.rect.left = 0
def move(self, delay): if self.ai: self.image = self.images[self.image_index(delay)] self.mask = getHitmask(self.image) self.center = self.rect.centerx, self.rect.centery self.rect = self.image.get_rect() self.rect.centerx, self.rect.centery = self.center else: if self.rotate >= -90: self.rotate -= self.angular_speed self.image = pygame.transform.rotate( self.images[self.image_index(delay)], self.rotate) self.mask = getHitmask(self.image) self.center = self.rect.centerx, self.rect.centery self.rect = self.image.get_rect() self.rect.centerx, self.rect.centery = self.center if self.landy - self.rect.top - self.rect.height + 20 >= 0: self.rect.top += min( self.speedy, self.landy - self.rect.top - self.rect.height + 20) if self.speedy <= self.max_speedy: self.speedy += self.down_speedy_a
def __init__(self, bg_size, location): global RANDOM_RECT pygame.sprite.Sprite.__init__(self) self.image = DPIPE_IMAGE[PIPE_INDEX].convert_alpha() self.mask = getHitmask(self.image) self.width, self.height = bg_size self.rect = self.image.get_rect() self.rect.top = RANDOM_RECT + PIPEGAPSIZE if location: self.rect.left = location else: self.rect.left = self.width + 10
def main(): pygame.init() v.FPSCLOCK = pygame.time.Clock() v.SCREEN = pygame.display.set_mode( (int(v.SCREENWIDTH), int(v.SCREENHEIGHT))) pygame.display.set_caption('Flappy Bird') # numbers sprites for score display v.IMAGES['numbers'] = ( pygame.image.load('assets/sprites/0.png').convert_alpha(), pygame.image.load('assets/sprites/1.png').convert_alpha(), pygame.image.load('assets/sprites/2.png').convert_alpha(), pygame.image.load('assets/sprites/3.png').convert_alpha(), pygame.image.load('assets/sprites/4.png').convert_alpha(), pygame.image.load('assets/sprites/5.png').convert_alpha(), pygame.image.load('assets/sprites/6.png').convert_alpha(), pygame.image.load('assets/sprites/7.png').convert_alpha(), pygame.image.load('assets/sprites/8.png').convert_alpha(), pygame.image.load('assets/sprites/9.png').convert_alpha()) # game over sprite v.IMAGES['gameover'] = pygame.image.load( 'assets/sprites/gameover.png').convert_alpha() # message sprite for welcome screen v.IMAGES['message'] = pygame.image.load( 'assets/sprites/message.png').convert_alpha() # base (ground) sprite v.IMAGES['base'] = pygame.image.load( 'assets/sprites/base.png').convert_alpha() while True: # select random background sprites randBg = random.randint(0, len(v.BACKGROUNDS_LIST) - 1) v.IMAGES['background'] = pygame.image.load( v.BACKGROUNDS_LIST[randBg]).convert() # select random player sprites randPlayer = random.randint(0, len(v.PLAYERS_LIST) - 1) v.IMAGES['player'] = ( pygame.image.load(v.PLAYERS_LIST[randPlayer][0]).convert_alpha(), pygame.image.load(v.PLAYERS_LIST[randPlayer][1]).convert_alpha(), pygame.image.load(v.PLAYERS_LIST[randPlayer][2]).convert_alpha(), ) # select random pipe sprites pipeindex = random.randint(0, len(v.PIPES_LIST) - 1) v.IMAGES['pipe'] = ( pygame.transform.rotate( pygame.image.load(v.PIPES_LIST[pipeindex]).convert_alpha(), 180), pygame.image.load(v.PIPES_LIST[pipeindex]).convert_alpha(), ) # hismask for pipes v.HITMASKS['pipe'] = ( utils.getHitmask(v.IMAGES['pipe'][0]), utils.getHitmask(v.IMAGES['pipe'][1]), ) # hitmask for player v.HITMASKS['player'] = ( utils.getHitmask(v.IMAGES['player'][0]), utils.getHitmask(v.IMAGES['player'][1]), utils.getHitmask(v.IMAGES['player'][2]), ) movementInfo = utils.showWelcomeAnimation() # global fitness for idx in range(v.total_models): v.fitness[idx] = 0 crashInfo = Engine.mainGame(movementInfo) utils.showGameOverScreen(crashInfo)