def generate_pipes(): checkpoint = sprites.Checkpoint((550, random.randint(50, 400)), public.all_sprites, public.enemies) pipe_top = sprites.Pipe((550, checkpoint.rect.top - 500), 1, public.all_sprites, public.enemies) pipe_top = sprites.Pipe((550, checkpoint.rect.bottom), 0, public.all_sprites, public.enemies)
def generate_pipes(): pipes = pygame.sprite.Group() checkpoints = pygame.sprite.Group() checkpoint = sprites.Checkpoint((530, random.randint(100, 450)), checkpoints) pipe_bottom = sprites.Pipe((530, checkpoint.rect.bottom + 200), pipes) pipe_top = sprites.Pipe((530, checkpoint.rect.top - 300), pipes) return pipes, checkpoints
def __init__(self, player): level.Level.__init__(self, player) self.background = pygame.image.load(constants.PATH + "background_l2.png").convert() self.background.set_colorkey(constants.WHITE) self.max_frames = 2 self.number = 0 platforms = sprites.Platform(pygame.image.load(constants.PATH + "level_2.png"), 0, 0) platforms.player = self.player self.platform_list.add(platforms) level_pipes = [sprites.Pipe(Level1_2(self.player, self), 0, 3880, 415, images.get_platform( images.PIPE_R, images.PIPE_R.get_rect().width, 140, images.PIPE_R_C)), sprites.Pipe(Level2_1(self.player, self), 0, 2445, 415, images.get_platform( images.PIPE_R, images.PIPE_R.get_rect().width, 140, images.PIPE_R_C))] level_blocks = [sprites.BreakableBlock(983, 256), sprites.QBlock(1029, 256, sprites.Reward(None)), sprites.QBlock(3222, 12, sprites.Reward(None), True)] level_platforms = [sprites.HorizontalMovingPlatform(images.PLATFORM_Y, 1200, 423, 1712, 1140), sprites.HorizontalMovingPlatform(images.PLATFORM_Y, 1400, 292, 1712, 1140)] level_enemies = [sprites.Cheep(1154, 214, 1647, 1147), sprites.Cheep(1177, 111, 1670, 1170), sprites.Cheep(1260, 168, 1727, 1227), sprites.Cheep(3409, 216, 3639, 3110), sprites.Cheep(3434, 138, 3664, 3135), sprites.Cheep(3482, 367, 3712, 3183), sprites.Cheep(3507, 290, 3737, 3208), sprites.Cheep(3515, 180, 3745, 3216), sprites.Cheep(3590, 333, 3820, 3291), sprites.Cheep(3654, 233, 3884, 3355), sprites.Cheep(3677, 154, 3907, 3378), sprites.Cheep(3760, 198, 3990, 3461), sprites.Hurchin(2005, 385, 45, 390), sprites.Hurchin(2180, 45, 45, 390), sprites.Hurchin(2750, 55, 45, 265, True)] star_coin = sprites.StarCoin(1820, 26, 0) self.reward_list.add(star_coin) self.star_coins.add(star_coin) # Go through the arrays above and add platforms utils.add_all(level_pipes, self.platform_list, self, self.player) utils.add_all(level_platforms, self.platform_list, None, self.player) utils.add_all(level_enemies, self.enemy_list, None, self.player) for aBlock in level_blocks: self.block_list.add(aBlock) if isinstance(aBlock, sprites.BreakableBlock): aBlock.level = self
def __init__(self, player, over_level): super().__init__(player, over_level) self.background = pygame.image.load(constants.PATH + "background_l1-1.png").convert() self.background.set_colorkey(constants.WHITE) self.max_frames = 1 self.number = 0 platforms = sprites.Platform(pygame.image.load(constants.PATH + "level_1-1.png"), 0, 0) platforms.player = self.player self.platform_list.add(platforms) pipe = sprites.Pipe(over_level, 2445, 3774, 336, images.get_platform(images.PIPE_G, images.PIPE_G.get_rect().width, 225, images.PIPE_G_C)) pipe.level = self pipe.player = self.player self.platform_list.add(pipe) star1 = sprites.StarCoin(2265, 95, 1) over_level.star_coins.add(star1) level_blocks = [sprites.BreakableBlock(925, 297), sprites.QBlock(971, 297, sprites.Reward(None)), sprites.BreakableBlock(1017, 297), sprites.BreakableBlock(3192, 201), sprites.QBlock(3238, 201, sprites.Reward(None))] level_enemies = [sprites.Goomba(969, 439), sprites.Goomba(1078, 439), sprites.Spike(2159, 76), sprites.Spike(2472, 77)] level_walls = [sprites.ClimbingWall(2121, 297, images.get_platform(images.WALL, 96, 220, images.WALL_C, images.WALL_B)), sprites.ClimbingWall(2290, 330), sprites.ClimbingWall(2451, 297, images.get_platform( images.WALL, 96, 220, images.WALL_C, images.WALL_B)), sprites.ClimbingWall(2617, 260), sprites.ClimbingWall(3109, 323), sprites.ClimbingWall(3280, 323)] level_coins = [sprites.Coin(925, 148), sprites.Coin(950, 105), sprites.Coin(990, 105), sprites.Coin(1015, 148), sprites.Coin(1707, 265), sprites.Coin(1830, 160), sprites.Coin(1975, 255), sprites.Coin(2127, 310), sprites.Coin(2178, 358), sprites.Coin(2127, 400), sprites.Coin(2178, 455), sprites.Coin(2510, 310), sprites.Coin(2458, 358), sprites.Coin(2510, 400), sprites.Coin(2458, 455), sprites.Coin(3200, 270), sprites.Coin(3245, 270), sprites.Coin(3200, 90), sprites.Coin(3245, 90), sprites.Coin(3782, 285), sprites.Coin(3830, 285), star1] for aBlock in level_blocks: self.block_list.add(aBlock) if isinstance(aBlock, sprites.BreakableBlock): aBlock.level = self utils.add_all(level_enemies, self.enemy_list, None, self.player) utils.add_all(level_walls, self.wall_list) utils.add_all(level_coins, self.reward_list)
def __init__(self, player, over_level): super().__init__(player, over_level) self.background = pygame.image.load(constants.PATH + "background_l2-1.png").convert() level_platforms = [sprites.Platform(images.get_platform(images.PIPE_R, 90, 145, images.PIPE_R_C), 210, 412), sprites.VerticalMovingPlatform(images.PLATFORM_Y, 380, 365, 175, 475), sprites.VerticalMovingPlatform(images.PLATFORM_Y, 705, 290, 175, 475), sprites.VerticalMovingPlatform(images.PLATFORM_Y, 1025, 365, 175, 475)] pipe = sprites.Pipe(over_level, 2070, 1305, 412, images.get_platform(images.PIPE_R, 90, 145, images.PIPE_R_C)) pipe.level = self pipe.player = self.player self.platform_list.add(pipe) star_coin = sprites.StarCoin(730, 10, 1) self.reward_list.add(star_coin) over_level.star_coins.add(star_coin) utils.add_all(level_platforms, self.platform_list, None, self.player)
def __init__(self, player): level.Level.__init__(self, player) self.background = pygame.image.load(constants.PATH + "background_l1.png").convert() self.background.set_colorkey(constants.WHITE) self.max_frames = 2 self.number = 0 platforms = sprites.Platform(pygame.image.load(constants.PATH + "level_1.png"), 0, 0) platforms.player = self.player self.platform_list.add(platforms) # Array with type of platform, and x, y location of the platform. level_platforms = [sprites.HorizontalMovingPlatform(images.get_platform(images.GROUND, 145, 32), 3655, 324, 4011, 3650), sprites.VerticalMovingPlatform(images.get_platform(images.GROUND, 145, 32), 4207, 400, 100, 500), sprites.VerticalMovingPlatform(images.get_platform(images.PIPE_G, 90, 440, images.PIPE_G_C), 4985, 300, 150, 427), sprites.VerticalMovingPlatform(images.get_platform(images.PIPE_R, 90, 440, images.PIPE_R_C), 5075, 350, 200, 400), sprites.VerticalMovingPlatform(images.get_platform(images.PIPE_Y, 90, 440, images.PIPE_Y_C), 5165, 250, 170, 380), sprites.VerticalMovingPlatform(images.get_platform(images.PIPE_Y, 90, 440, images.PIPE_Y_C), 5255, 400, 220, 427), sprites.VerticalMovingPlatform(images.get_platform(images.PIPE_G, 90, 440, images.PIPE_G_C), 5345, 300, 180, 380), sprites.VerticalMovingPlatform(images.get_platform(images.PIPE_G, 90, 440, images.PIPE_G_C), 5700, 270, 160, 420), sprites.VerticalMovingPlatform(images.get_platform(images.PIPE_R, 90, 440, images.PIPE_R_C), 5790, 350, 220, 400), sprites.VerticalMovingPlatform(images.get_platform(images.PIPE_Y, 90, 440, images.PIPE_Y_C), 5975, 300, 200, 390)] level_pipes = [sprites.Pipe(Level1_1(self.player, self), -350, 1776), sprites.Pipe(None, 0, 2845, 0), sprites.Pipe(Level1_2(self.player, self), 0, 5880, 339, images.get_platform(images.PIPE_B, 95, 215, images.PIPE_B_C))] level_blocks = [sprites.BreakableBlock(710, 135), sprites.BreakableBlock(802, 135), sprites.BreakableBlock(1877, 48), sprites.Block(None, 2846, 396), sprites.Block(None, 2892, 396), sprites.QBlock(756, 135, sprites.Reward(None)), sprites.QBlock(1923, 48, sprites.Coin(1923, 48)), sprites.BreakableBlock(6313, 170), sprites.QBlock(6267, 170, sprites.Reward(None))] level_enemies = [sprites.Goomba(831, 270), sprites.Koopa(3002, 358), sprites.Koopa(3400, 358)] star0 = sprites.StarCoin(1615, 418, 0) star2 = sprites.StarCoin(5220, 0, 2) self.star_coins.add(star0) self.star_coins.add(star2) level_coins = [sprites.Coin(720, 42), sprites.Coin(759, 3), sprites.Coin(796, 42), sprites.Coin(1070, 205), sprites.Coin(1210, 160), sprites.Coin(1412, 205), sprites.Coin(1950, 455), sprites.Coin(1997, 455), sprites.Coin(2042, 455), sprites.Coin(2090, 455), sprites.Coin(3104, 270), sprites.Coin(3146, 225), sprites.Coin(3200, 225), sprites.Coin(3240, 270), sprites.Coin(3545, 268), sprites.Coin(4210, 0, True, 0), sprites.Coin(4255, 0, True, 0), sprites.Coin(4300, 0, True, 0), star0, star2] # Go through the arrays above and add platforms utils.add_all(level_platforms, self.platform_list, None, self.player) utils.add_all(level_pipes, self.platform_list, self, self.player) utils.add_all(level_enemies, self.enemy_list, None, self.player) utils.add_all(level_coins, self.reward_list) for aBlock in level_blocks: self.block_list.add(aBlock) if isinstance(aBlock, sprites.BreakableBlock): aBlock.level = self
def maingame(sprites_start): ''' 游戏开始后的内容 ''' score = 0 maygetscore = False scorefigure = sprites.ScoreFigure() # 管道 pipes = {} pipecolor = random.randint(0, 1) pipepositionys = [random.randint(PP_MIN, PP_MAX) for _ in range(2)] pipeexample = pygame.image.load(PIPE_LIST[pipecolor]).convert_alpha() pipeexample_inv = pygame.transform.flip(pipeexample, 0, 1) pipe_width, pipe_height = pipeexample.get_size() pipes['top'] = ( sprites.Pipe(Vec2d(2 * SCREENWIDTH, pipepositionys[0]), pipeexample_inv.copy()), sprites.Pipe(Vec2d(2 * SCREENWIDTH + PIPESLOT, pipepositionys[1]), pipeexample_inv.copy()), ) pipes['bottom'] = ( sprites.Pipe( Vec2d(2 * SCREENWIDTH, pipepositionys[0] + pipe_height + SLOT), pipeexample.copy()), sprites.Pipe( Vec2d(2 * SCREENWIDTH + PIPESLOT, pipepositionys[1] + pipe_height + SLOT), pipeexample.copy()), ) for idx in range(2): pipes['top'][idx].mask = controllers.gethitmask( pipes['top'][idx].image) pipes['bottom'][idx].mask = controllers.gethitmask( pipes['bottom'][idx].image) # 从startMenu获取的sprites bird = sprites_start['bird'] background = sprites_start['background'] base = sprites_start['base'] # 小鸟控制器 birdcontroller = controllers.BirdController(bird) birdcontroller.flap() # 振翅 # 主循环 while True: # 事件监听 for event in pygame.event.get(): if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE): pygame.quit() sys.exit() elif event.type == KEYDOWN and event.key == K_r: return None elif (event.type == KEYDOWN and event.key in (K_SPACE, K_UP, )) \ or event.type == MOUSEBUTTONDOWN: birdcontroller.flap() # 振翅 passed_time = FPSclock.tick(FPS) # 更新小鸟、地面、管道 birdcontroller.update(passed_time) base.update(passed_time) up_y1 = pipes['top'][0].update(passed_time) up_y2 = pipes['top'][1].update(passed_time) pipes['bottom'][0].update(passed_time, up_y1) pipes['bottom'][1].update(passed_time, up_y2) scorefigure.update(score) if maygetscore and \ (bird.rect.x > pipes['top'][0].rect.x \ or bird.rect.x > pipes['top'][1].rect.x): score += 1 maygetscore = False if bird.rect.x < pipes['top'][0].rect.x \ and bird.rect.x < pipes['top'][1].rect.x: maygetscore = True # 碰撞检测 if bird.rect[1] + bird.image.get_width()/1.5 > base.position[1] or \ crashdetect(bird,( pipes['top'][0], pipes['top'][1], pipes['bottom'][0], pipes['bottom'][1],)): pygame.mixer.Sound(SOUND_HIT[0]).play() return { 'birdcontroller': birdcontroller, 'pipes': pipes, 'base': base, 'background': background, 'scorefigure': scorefigure, } # 绘制 SCREEN.blit(background, (0, 0)) SCREEN.blits(( (pipes['top'][0].image, pipes['top'][0].rect), (pipes['bottom'][0].image, pipes['bottom'][0].rect), (pipes['top'][1].image, pipes['top'][1].rect), (pipes['bottom'][1].image, pipes['bottom'][1].rect), )) SCREEN.blit(base.image, base.position) SCREEN.blit(scorefigure.image, scorefigure.rect) SCREEN.blit(bird.image, bird.rect) pygame.display.update()
def generate_pipes(pipes): checkpoint = sprites.Checkpoint((550, random.randint(100, 450)), pipes) pipe_top = sprites.Pipe((550, checkpoint.rect.top - 250), 1, pipes) pipe_bottom = sprites.Pipe((550, checkpoint.rect.bottom + 250), 0, pipes)