コード例 #1
0
    def initialize(self):
        self.size = self.width, self.height = constants.width, constants.height
        self.screen = pygame.display.set_mode(self.size)
        self.clock = pygame.time.Clock()

        # Initialize Background
        self.background = sprites.Background('stars.png')

        # Initialize Players
        self.player1 = sprites.Player(1)
        self.player2 = sprites.Player(2)

        # Initialize Scoreboard
        self.scoreboard1 = sprites.Scoreboard(1, constants.width / 4,
                                              self.player1)
        self.scoreboard2 = sprites.Scoreboard(2, 3 * constants.width / 4,
                                              self.player2)

        # Initialize Platforms
        self.platforms = pygame.sprite.Group()
        self.platform1 = sprites.Platform(constants.width / 2, 50,
                                          self.player1)
        self.platform2 = sprites.Platform(constants.width / 2,
                                          constants.height - 50, self.player2)
        self.platforms.add(self.platform1)
        self.platforms.add(self.platform2)

        # Initialize Balls
        self.balls = pygame.sprite.Group()
        self.ball1 = sprites.Ball(self.platform1)
        self.ball2 = sprites.Ball(self.platform2)
        self.ball1.other = self.ball2
        self.ball2.other = self.ball1
        self.balls.add(self.ball1)
        self.balls.add(self.ball2)
コード例 #2
0
ファイル: main.py プロジェクト: clementchx/ToTheAbyss
    def new(self):
        #Create a new game
        self.score = 0
        self.boost = 0
        self.ground_tempo = 0
        self.ground_count = 0
        self.collide = False

            #Creation of player
        self.characters = pg.sprite.Group()
        self.man = sprites.Player()
        self.characters.add(self.man)
            #Creation of ceil
        self.platforms = pg.sprite.Group()
        ceil = sprites.Platform(0, SPREAD, WIDTH)
        self.platforms.add(ceil)
            #Customize creation of first floor (to avoid hole below player at the beginning)
        p1left = sprites.Platform(0, self.platforms.sprites()[-1].rect.y + SPREAD, WIDTH / 2)
        p1right = sprites.Platform(WIDTH / 2 + HOLE_SIZE, self.platforms.sprites()[-1].rect.y + SPREAD, WIDTH / 2 - HOLE_SIZE)
        self.platforms.add(p1left, p1right)
            #Creation of 5 next floors
        for i in range(1, 6):
            self.create_floor()

        self.render()
コード例 #3
0
ファイル: main.py プロジェクト: HT1en/Major2020
    def update(self):
        # Game Loop - Update
        self.all_sprites.update()
        # check if player hits a platform - only if falling
        if self.player.vel.y > 0:
            hits = pg.sprite.spritecollide(self.player, self.platforms, False)
            if hits:
                self.player.pos.y = hits[0].rect.top
                self.player.vel.y = 0

        # if player reaches top 1/4 of screen
        if self.player.rect.top <= HEIGHT / 4:
            self.player.pos.y += abs(self.player.vel.y)
            for plat in self.platforms:
                plat.rect.y += abs(self.player.vel.y)
                if plat.rect.top >= HEIGHT:
                    plat.kill()
                    self.score += 10

        # spawn new platforms to keep same average number
        while len(self.platforms) < 6:
            width = random.randrange(50, 100)
            p = sprites.Platform(random.randrange(0, WIDTH - width),
                                 random.randrange(-75, -30), width, 20)
            self.platforms.add(p)
            self.all_sprites.add(p)

        #DIE!
        if self.player.rect.bottom > HEIGHT:
            for sprite in self.all_sprites:
                sprite.rect.y -= max(self.player.vel.y, 10)
                if sprite.rect.bottom < 0:
                    sprite.kill()
        if len(self.platforms) == 0:
            self.playing = False
コード例 #4
0
    def new(self):
        # start new game, reset game
        self.player = sprites.Player()
        self.all_sprites = pygame.sprite.Group()
        self.platforms = pygame.sprite.Group()
        self.all_sprites.add(self.player)

        platform1 = sprites.Platform(0, settings.window_height - 50,
                                     settings.window_width, 40)
        self.platforms.add(platform1)
        self.all_sprites.add(platform1)

        platform2 = sprites.Platform(settings.window_width / 2 - 50,
                                     settings.window_height * 3 / 4, 100, 20)
        self.all_sprites.add(platform2)
        self.platforms.add(platform2)

        game.run()
コード例 #5
0
ファイル: main.py プロジェクト: NyanBlak/School
    def new(self):
        self.all_sprites = pygame.sprite.Group()
        self.platforms = pygame.sprite.Group()

        self.player = sprites.Player()
        for i in platform_list:
            p = sprites.Platform(i[0], i[1], i[2], i[3])
            self.platforms.add(p)
            self.all_sprites.add(p)
        self.all_sprites.add(self.player)
        self.run()
コード例 #6
0
ファイル: main.py プロジェクト: HT1en/Major2020
 def new(self):
     # start a new game
     pg.init()
     self.score = 0
     self.all_sprites = pg.sprite.Group()
     self.platforms = pg.sprite.Group()
     self.enemy = pg.sprite.Group()
     self.player = sprites.Player(self)
     self.all_sprites.add(self.player)
     for plat in PLATFORM_LIST:
         p = sprites.Platform(*plat)
         self.all_sprites.add(p)
         self.platforms.add(p)
     pg.mixer.music.load(os.path.join(self.snd_dir, "ud.ogg"))
     self.run()
コード例 #7
0
    def update(self):
        #game loop - updates
        self.all_sprites.update()

        #player collision when falling
        if self.player.vel.y > 0:
            hits = pygame.sprite.spritecollide(self.player,
                                               self.platform_sprites, False)
            if hits and self.player.pos.y < hits[0].rect.bottom:
                self.player.pos.y = hits[0].rect.top + 1
                self.player.vel.y = 0

        #prevent players from forever accelerating && deleting platforms that are out of sight
        if self.player.rect.top < HEIGHT / 3:
            self.player.pos.y += max(2, abs(self.player.vel.y))
            for p in self.platform_sprites:
                p.rect.y += abs(self.player.vel.y)
                if p.rect.top >= HEIGHT:
                    p.kill()
                    self.Score += 1

        #spawn new platforms

        while len(self.platform_sprites) < MAX_PLATFORM:
            w = random.randint(70, 100)
            x = random.randint(40, WIDTH - w - 40)
            y = random.randint(-160, -80)
            p = sprites.Platform(x, y, w, 15, self.pTile)
            self.platform_sprites.add(p)
            self.all_sprites.add(p)

        #tape headBand and maintain sprite integrity
        if self.player.facingRight:
            self.player.headBand.rect.y = self.player.rect.y - 10
            self.player.headBand.rect.x = self.player.rect.x - 9
        else:
            self.player.headBand.rect.y = self.player.rect.y - 10
            self.player.headBand.rect.x = self.player.rect.x - 1

        #kill any sprite that falls beneath sight
        if self.player.rect.bottom > HEIGHT:
            for s in self.all_sprites:
                s.rect.y -= max(self.player.vel.y, 8)
                if s.rect.bottom < 0:
                    s.kill()

        if len(self.platform_sprites) == 0: self.playing = False
コード例 #8
0
    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)
コード例 #9
0
    def __init__(self, player, over_level):
        super().__init__(player, over_level)
        self.background = pygame.image.load(constants.PATH + "background_l1.png").convert()
        self.background.set_colorkey(constants.WHITE)
        self.max_frames = 0
        self.number = 0

        platforms = sprites.Platform(pygame.image.load(constants.PATH + "level_1-2.png"), 0, 0)
        platforms.player = self.player
        self.platform_list.add(platforms)

        level_enemies = [sprites.Koopa(612, 373),
                         sprites.Koopa(729, 303),
                         sprites.Koopa(852, 223)]

        utils.add_all(level_enemies, self.enemy_list, None, self.player)
        self.add_flag(25)
コード例 #10
0
    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)
コード例 #11
0
ファイル: map.py プロジェクト: emeline-random/Myrio
    def __init__(self, player):
        super().__init__(player)
        self.background = pygame.image.load(constants.PATH +
                                            "background_menu.png").convert()
        self.max_frames = 1

        menu_levels = [
            sprites.LevelRound(141, 440, Level1(self.player)),
            sprites.LevelRound(847, 440, Level2(self.player), 300)
        ]

        platforms = sprites.Platform(MENU_PLATFORMS, 0, 0)
        platforms.player = self.player
        self.platform_list.add(platforms)

        wall = sprites.ClimbingWall(1435, 200, images.PYRANA_PLANT)
        self.wall_list.add(wall)

        for level in menu_levels:
            self.reward_list.add(level)
コード例 #12
0
    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
コード例 #13
0
ファイル: functions.py プロジェクト: juniperlynx/Contrast
def generate_level(show_title):
    for sprite in public.all_sprites:
        if sprite.type != 'Cloud':
            sprite.kill()

    level_data = dictionaries.LEVELS[public.level]
    public.bg_type = level_data[1]
    public.wrapping = level_data[2]

    platform = sprites.Platform()

    if show_title:
        title = sprites.Title(level_data[0])

    for i, row in enumerate(level_data[3]):
        for _i, col in enumerate(row):
            if col in 'ABC':
                color = color_return('ABC', col)
                block = sprites.Block((_i * 20, i * 20), color)

            elif col in 'DEF':
                color = color_return('DEF', col)
                exit = sprites.Exit((_i * 20, i * 20), color)

            elif col in 'GHI':
                color = color_return('GHI', col)
                pit = sprites.Pit((_i * 20, i * 20), color, 'U')

            elif col in 'JKL':
                color = color_return('JKL', col)
                pit = sprites.Pit((_i * 20, i * 20), color, 'D')

            elif col in 'MNO':
                color = color_return('MNO', col)
                jumpad = sprites.Jumpad((_i * 20, i * 20), color, 'U')

            elif col in 'PQR':
                color = color_return('PQR', col)
                jumpad = sprites.Jumpad((_i * 20, i * 20), color, 'D')

            elif col in 'STU':
                color = color_return('STU', col)
                breakable = sprites.Breakable((_i * 20, i * 20), color, 'U')

            elif col in 'VWX':
                color = color_return('VWX', col)
                breakable = sprites.Breakable((_i * 20, i * 20), color, 'D')

            elif col in '123':
                color = color_return('123', col)
                flipad = sprites.Flipad((_i * 20, i * 20), color, 'U')

            elif col in '456':
                color = color_return('456', col)
                flipad = sprites.Flipad((_i * 20, i * 20), color, 'D')

            elif col in '><':
                public.spawn = (_i * 20, i * 20)

                if col == '>':
                    public.player = sprites.Ox(public.spawn, 'R')

                elif col == '<':
                    public.player = sprites.Ox(public.spawn, 'L')

            elif col == '.':
                sphere = sprites.RGBSphere((_i * 20, i * 20), 192)

    for sprite in public.blocks:
        if sprite.type == 'Block':
            sprite.image = block_return(sprite, sprite.color)

        elif sprite.type == 'Pit':
            pit_return(sprite, sprite.color)

        elif sprite.type == 'Breakable':
            sprite.image = breakable_return(sprite, sprite.color)

            if sprite.direction == 'D':
                sprite.image = pygame.transform.flip(sprite.image, 0, 1)
コード例 #14
0
def level1():  #Our level 1
    '''main game'''
    player = sprites.Player()  #Entities / Players
    tank = sprites.Tank()
    playerGrp = pygame.sprite.Group(tank, player)
    current_player = player
    clean_bkgd = pygame.image.load('images\\bkgd.png').convert()  #Background
    bkgd = sprites.Background(player)
    wall = sprites.Platform(((1438, 380), (1, 100)))  #Map Objects
    platforms = pygame.sprite.Group([
        sprites.Platform(dimension)
        for dimension in (((0, 366), (1400, 1)), ((1438, 450), (2507, 1)),
                          ((1845, 342), (110, 1)), ((2032, 260), (348, 1)),
                          ((2380, 342), (130, 1)), ((2510, 260), (290, 1)),
                          ((2915, 260), (345, 1)), ((3260, 342), (150, 1)))
    ])
    pBulletsGrp = pygame.sprite.Group()  #Projectiles (bullets and grenades)
    eBulletsGrp = pygame.sprite.Group()
    grenadeGrp = pygame.sprite.Group()
    scoreboard = sprites.ScoreBoard(player, tank)  #Scoreboard
    enemiesGrp = pygame.sprite.Group([
        sprites.Enemy(midbottom) for midbottom in (  #Enemies
            (500, 366), (800, 366), (1000, 366), (1100, 366), (1200, 366),
            (1300, 366), (1700, 450), (1800, 450), (1900, 450), (2300, 450),
            (2400, 450), (2500, 450), (2600, 450), (2700, 450), (2800, 450),
            (2900, 450), (3000, 450), (3100, 450), (3200, 450), (3400, 450),
            (3500, 450), (3600, 450), (3800, 450), (1880, 342), (2040, 260),
            (2200, 260), (2400, 342), (2550, 260), (2700, 260), (2950, 260),
            (3100, 260), (3280, 342))
    ])
    pygame.mixer.music.load('sounds\\music.mp3')  #Music
    pygame.mixer.music.play(-1)

    allSprites = pygame.sprite.OrderedUpdates(enemiesGrp, playerGrp,
                                              eBulletsGrp, pBulletsGrp,
                                              grenadeGrp)
    clock = pygame.time.Clock()
    keepGoing = True
    pygame.mouse.set_visible(False)
    while keepGoing:  #Another While Loop
        clock.tick(30)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                keepGoing = False
                exitstatus = 2
            if not current_player.get_dying():
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_e:
                        if pygame.sprite.collide_rect(
                                player,  #Enter the TANK
                                tank
                        ) and current_player == player and not tank.get_dying(
                        ):
                            current_player = tank
                            player.kill()
                        elif current_player == tank:  #Exit the TANK
                            player.respawn(tank)
                            playerGrp.add(player)
                            allSprites.add(playerGrp)
                            current_player = player
                            tank.die()
                    elif event.key == pygame.K_l:
                        if current_player == tank:  #Fire Cannon from the TANK
                            if tank.shoot_cannon():
                                grenadeGrp.add(sprites.TankShell(tank))
                                allSprites.add(grenadeGrp)
                        elif player.get_grenades():  #Player throw grenade
                            player.throw_grenade()
                            grenadeGrp.add(sprites.Grenade(player))
                            allSprites.add(grenadeGrp)

        if not current_player.get_dying():
            keys_pressed = pygame.key.get_pressed()
            if keys_pressed[pygame.K_d] and keys_pressed[
                    pygame.K_a]:  #In game movement, left and right
                pass
            elif keys_pressed[pygame.K_a]:
                current_player.move(-1)
            elif keys_pressed[pygame.K_d]:
                current_player.move(1)
            if keys_pressed[pygame.K_j]:  #In game action, Jump
                current_player.jump()

            if current_player == tank:  #Tank Controls
                if keys_pressed[pygame.K_k]:  #Tank shooting Machine Gun
                    tank.shoot_mg()
                    pBulletsGrp.add(sprites.TankBullet(bkgd, tank))
                    allSprites.add(pBulletsGrp)
                if keys_pressed[pygame.K_w] and keys_pressed[
                        pygame.K_s]:  #The machine gun rotation
                    pass
                elif keys_pressed[pygame.K_w]:
                    tank.rotate(5)
                elif keys_pressed[pygame.K_s]:
                    tank.rotate(-5)
            else:  #Player Control
                if keys_pressed[pygame.K_k]:
                    if player.get_weapon():  #Player shoot Machine Gun
                        pBulletsGrp.add(
                            sprites.MGBullet(bkgd, player, player.shoot()))
                        allSprites.add(pBulletsGrp)
        for item in (player, tank):  #Collision Detection
            if pygame.sprite.collide_rect(item, wall):  #Collision with wall
                item.collide_wall(wall)

            collision = pygame.sprite.spritecollide(
                item, platforms, False)  #Collision with platform
            if collision:
                item.land(max(platform.rect.top for platform in
                              collision))  #Find lowest platform to step on
            else:
                item.fall()

        for bullet in pygame.sprite.spritecollide(
                current_player, eBulletsGrp,
                False):  #Bullet collision with player
            if not current_player.get_dying():
                bullet.kill()
                current_player.hurt(50)

        for enemy in pygame.sprite.spritecollide(
                tank, enemiesGrp, False):  #The tank collision with enemies
            enemy.die()

        for bullet, enemy in pygame.sprite.groupcollide(
                pBulletsGrp, enemiesGrp, False,
                False).items():  #Bullet colision with enemies
            if enemy and not enemy[0].get_dying():
                bullet.kill()
                enemy[0].die()

        for grenade, enemy in pygame.sprite.groupcollide(
                grenadeGrp, enemiesGrp, False,
                True).items():  #Grenade collision with enemies
            if enemy:
                grenade.explode()
                for i in enemy:
                    i.die()

        for grenade, platform in pygame.sprite.groupcollide(
                grenadeGrp, platforms, False,
                False).items():  #Grenade collision with the platform
            if platform:
                grenade.explode()

        for enemy in enemiesGrp:  #Enemy shooting
            if enemy.get_shooting():
                eBulletsGrp.add(sprites.EnemyBullet(enemy, current_player))
                allSprites.add(eBulletsGrp)

        if tank.get_dying(
        ):  #If the tank is destroyed, respawn the player to the current status
            player.respawn(tank)
            playerGrp.add(player)
            allSprites.add(playerGrp)
            current_player = player
        if player.get_dying() == 2:  #Exit game loop when player died
            keepGoing = False
            exitstatus = 1
        if current_player.rect.right >= bkgd.image.get_width(
        ):  #Checking if the player has defeated the boss
            keepGoing = False
            exitstatus = 0

        # REFRESH SCREEN
        # We draws all of the sprites in the background
        bkgd.image.blit(clean_bkgd, (0, 0))
        allSprites.update(current_player)
        allSprites.draw(bkgd.image)

        # Update the background based on the position of the player
        bkgd.update(current_player)
        screen.blit(bkgd.image, bkgd.rect)

        # Update the scoreboard
        scoreboard.update(current_player)
        screen.blit(scoreboard.image, scoreboard.rect)

        pygame.display.flip()

    pygame.mixer.music.stop()
    if tank.get_dying():
        return exitstatus, player
    return exitstatus, player, tank
コード例 #15
0
def boss(prev_player, prev_tank=None):
    # Use the previous player or previous tank if we still in one
    player = sprites.Player(prev_player)
    if prev_tank:
        tank = sprites.Tank(prev_tank)
        current_player = tank
        playerGrp = pygame.sprite.Group(tank)
    else:
        tank = None
        current_player = player
        playerGrp = pygame.sprite.Group(player)

    # Clean the background then load the background image for the boss
    clean_bkgd = pygame.image.load('images\\bossbkgd2.png').convert()
    bkgd = sprites.Background(player, 1)

    # Map objects such as platform
    wall = sprites.Platform(((865, 0), (1, 480)))
    platform = sprites.Platform(((0, 432), (1280, 1)))

    # The boss laser projectile
    laser = sprites.Laser()
    pBulletsGrp = pygame.sprite.Group()
    shellGrp = pygame.sprite.Group()
    pGrenadeGrp = pygame.sprite.Group()

    mgicon = sprites.MGIcon()

    # Current scoreboard
    scoreboard = sprites.ScoreBoard(player, tank)

    # The boss
    boss = sprites.Boss()

    # The boss stage sound and also the mission complete sound
    pygame.mixer.music.load('sounds\\boss.mp3')
    pygame.mixer.music.play(-1)
    missioncomplete = pygame.mixer.Sound('sounds\\mission complete.wav')

    allSprites = pygame.sprite.OrderedUpdates(playerGrp, boss, mgicon,
                                              pBulletsGrp, shellGrp,
                                              pGrenadeGrp, laser)

    clock = pygame.time.Clock()
    keepGoing = True
    pygame.mouse.set_visible(False)
    cutscene = True

    # Loop
    while keepGoing:

        clock.tick(30)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                keepGoing = False
                exitstatus = 2
            if not cutscene and not current_player.get_dying():
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_e:
                        # Let the player to exit the tank
                        if current_player == tank:
                            player.respawn(tank)
                            playerGrp.add(player)
                            allSprites.add(playerGrp)
                            current_player = player
                            tank.die()
                    elif event.key == pygame.K_l:
                        # If the player inside of the tank it can shoot cannon
                        if current_player == tank:
                            if tank.shoot_cannon():
                                pGrenadeGrp.add(sprites.TankShell(tank))
                                allSprites.add(pGrenadeGrp)
                        # Player can throw grenade
                        elif player.get_grenades():
                            player.throw_grenade()
                            pGrenadeGrp.add(sprites.Grenade(player))
                            allSprites.add(pGrenadeGrp)

        # Cutscene (not included)
        if cutscene:
            current_player.move(1)
            if current_player.rect.left + 700 >= boss.rect.right:
                cutscene = False
                boss.start()

        elif not current_player.get_dying():
            keys_pressed = pygame.key.get_pressed()
            # left and right movement for the boss stage
            if keys_pressed[pygame.K_d] and keys_pressed[pygame.K_a]:
                pass
            elif keys_pressed[pygame.K_a]:
                current_player.move(-1)
            elif keys_pressed[pygame.K_d]:
                current_player.move(1)
            # JUMP for the boss stage
            if keys_pressed[pygame.K_j]:
                current_player.jump()

            # The controls for the tank in boss stage
            if current_player == tank:
                # Player can shoot machine gun in the boss stage
                if keys_pressed[pygame.K_k]:
                    tank.shoot_mg()
                    pBulletsGrp.add(sprites.TankBullet(bkgd, tank))
                    allSprites.add(pBulletsGrp)
                    # Rotating machine gun in tank
                if keys_pressed[pygame.K_w] and keys_pressed[pygame.K_s]:
                    pass
                elif keys_pressed[pygame.K_w]:
                    tank.rotate(5)
                elif keys_pressed[pygame.K_s]:
                    tank.rotate(-5)
            # Players controls
            else:
                if keys_pressed[pygame.K_k]:
                    # Shooting bullets (projectile)
                    if player.get_weapon():
                        pBulletsGrp.add(
                            sprites.MGBullet(bkgd, player, player.shoot()))
                        allSprites.add(pBulletsGrp)
                    # Shoot using pistol (erased)
                    elif player.shoot():
                        pBulletsGrp.add(sprites.PistolBullet(bkgd, player))
                        allSprites.add(pBulletsGrp)

        # Detect any collision with wall or the tank
        for item in filter(bool, (player, tank)):
            # Collision with wall
            if pygame.sprite.collide_rect(item, wall):
                item.collide_wall(wall, 1)

                # Collision with platforms
            if pygame.sprite.collide_rect(item, platform):
                # Lowest platform to land on
                item.land(platform.rect.top)
            else:
                item.fall()

                # The boss laser collision with the player
        if pygame.sprite.collide_rect(laser, current_player):
            current_player.hurt(50)

            # The icon of machine gun collision with the player
        if pygame.sprite.collide_rect(mgicon, current_player):
            player.pickup()
            mgicon.hide()

        # The shell collision
        for shell in pygame.sprite.spritecollide(current_player, shellGrp,
                                                 False):
            if not current_player.get_dying():
                shell.explode()
                current_player.hurt(50)

        # Shell collision with the ground
        for shell in pygame.sprite.spritecollide(platform, shellGrp, False):
            shell.explode()

            # Collision between grenade and the boss
        for grenade in pygame.sprite.spritecollide(boss, pGrenadeGrp, False):
            grenade.explode()
            boss.hurt(5)

            # Collision of bullets and boss
        for bullet in pygame.sprite.spritecollide(boss, pBulletsGrp, False):
            bullet.kill()
            boss.hurt(1)

            # Collision grenade with the platform
        for grenade in pygame.sprite.spritecollide(platform, pGrenadeGrp,
                                                   False):
            grenade.explode()

            # The boss shooting the tank shell
        if boss.get_attack() == 1:
            shellGrp.add(sprites.TankShell())
            allSprites.add(shellGrp)
        # Boss allowed to attack using laser
        elif boss.get_attack() == 2:
            laser.reset()

        # If the tank breaks the player respawn as the last state or status
        if tank and tank.get_dying():
            player.respawn(tank)
            playerGrp.add(player)
            allSprites.add(playerGrp)
            current_player = player

        # Exits game loop if the animation is over
        if player.get_dying() == 2:
            keepGoing = False
            exitstatus = 1

        # Checks if the player have completed the level.
        if boss.get_dead():
            pygame.mixer.music.stop()
            missioncomplete.play()
            screen.blit(
                pygame.image.load(
                    'images\\mission complete.png').convert_alpha(),
                (109, 167))
            pygame.display.flip()
            pygame.time.wait(8000)
            keepGoing = False
            exitstatus = 0

        # REFRESH SCREEN and load sprites at the background
        bkgd.image.blit(clean_bkgd, (0, 0))
        allSprites.update(current_player)
        allSprites.draw(bkgd.image)

        # Updates background position
        bkgd.update(current_player)
        screen.blit(bkgd.image, bkgd.rect)

        # Update the scoreboard to the screen
        scoreboard.update(current_player)
        screen.blit(scoreboard.image, scoreboard.rect)

        pygame.display.flip()

    return exitstatus
コード例 #16
0
ファイル: main.py プロジェクト: clementchx/ToTheAbyss
 def create_floor(self):
     p = sprites.Platform(0, self.platforms.sprites()[-1].rect.y + SPREAD, WIDTH)
     pleft, pright = p.divide()
     p.kill()
     self.platforms.add(pleft, pright)
コード例 #17
0
    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