Beispiel #1
0
class Window(pyglet.window.Window):
    def __init__(self, *args, **kwargs):
        super(Window, self).__init__(*args, **kwargs)
        self.menu_id = 0
        self.menu_cursor = 0
        self.pause = False
        self.menu1 = [178,146,114,80]
        self.menu2 = [258,162]
        self.keys = key.KeyStateHandler()
        self.push_handlers(self.keys)

        self.enemies = []
        self.enemies_bullets = []
        self.enemy_spawner_timer = 0
        self.enemy_spawner = 10

        # self.stage1 = Stage(
        #     [Platform(0, 50, self.background.width,True),
        #         Platform(1410,170,118,False), Platform(1624,256,319,False),
        #         Platform(1484,325,219,False), Platform(3968,170,120,False),
        #         Platform(4210,170,118,False), Platform(4050,256,218,False),
        #         Platform(4289,325,397,False), Platform(4430,256,316,False),
        #         Platform(4688,170,118,False), Platform(6557,256,217,False),
        #         Platform(6717,170,117,False), Platform(6797,325,299,False),
        #         Platform(6937,256,220,False), Platform(7098,170,118,False),
        #         Platform(12138,170,92,False), Platform(11637,172,161,False),
        #         Platform(12239,378,72,False), Platform(13308,165,78,False),
        #         Platform(13468,165,79,False), Platform(13756,165,160,False),
        #         Platform(13993,165,160,False), Platform(15454,245,66,False),
        #         Platform(15695,169,78,False), Platform(16576,239,160,False),
        #         Platform(16576,153,180,False)
        #     ],
        #     self.background
        # )

        # self.enemies = [enemy.Sniper(1553,325), enemy.Sniper(3288,256), enemy.Sniper(4123,51), enemy.Sniper(5041,281), enemy.Sniper(6510,255), enemy.Sniper(7461,282),
        #     enemy.EnemyDog(2166, 70), enemy.EnemyDog(2806,70), enemy.EnemyDog(3769,70),
        #     enemy.Floor_Turret(1287, 38), enemy.Floor_Turret(1928, 38), enemy.Floor_Turret(6439, 38), enemy.Floor_Turret(7781,38),
        #     enemy.Defense_Cannon_Left(4986, 205), enemy.Defense_Cannon_Right(5047, 205), enemy.Defense_Cannon_Left(7396, 205), enemy.Defense_Cannon_Right(7456, 205)]

        # self.stages = [self.stage1]
        # self.stage_transition = 0

        self.game = Game()

    def generate_enemies(self):
        if self.stages[self.stage_transition].x < self.stages[self.stage_transition].NO_ENEMY_ZONE[0] and self.stages[self.stage_transition].x > self.stages[self.stage_transition].NO_ENEMY_ZONE[1]:
            #enemy_side: 0 = Rigth side, 1 = Left side
            side = 0
            for i in range(len(self.stages[self.stage_transition].LEFT_SIDE_ENEMY_SPAWN)):
                if self.stages[self.stage_transition].x < self.stages[self.stage_transition].LEFT_SIDE_ENEMY_SPAWN[i][0] and self.stages[self.stage_transition].x > self.stages[self.stage_transition].LEFT_SIDE_ENEMY_SPAWN[i][1]:
                    side = 1
            #enemies = 1 #enemy_id: 0 = Blue soldier, 1 = Red soldier
            self.enemy_spawner_timer += 1
            if self.enemy_spawner_timer == self.enemy_spawner:
                enemy_side = random.randint(side, 1)
                enemy_id = random.randint(0, 1)
                if enemy_id == 0:
                    self.enemies.append(enemy.Runner(bool(enemy_side)))
                elif enemy_id == 1:
                    self.enemies.append(enemy.RunnerShooter(bool(enemy_side)))
                else:
                    self.enemies.append(EnemyDog(bool(enemy_side)))
                self.enemy_spawner = random.randint(10,20)
                self.enemy_spawner_timer = 0

    def reset(self):
        self.stage_transition = 0
        for i in range(len(self.stages)):
            self.stages[i].x = 0
        self.y = 0
        self.enemies = [enemy.Sniper(1553,325), enemy.Sniper(3288,256), enemy.Sniper(4123,51), enemy.Sniper(5041,281), enemy.Sniper(6510,255), enemy.Sniper(7461,282),
            enemy.EnemyDog(2166, 70), enemy.EnemyDog(2806,70), enemy.EnemyDog(3769,70),
            enemy.Floor_Turret(1287, 38), enemy.Floor_Turret(1928, 38), enemy.Floor_Turret(6439, 38), enemy.Floor_Turret(7781,38),
            enemy.Defense_Cannon_Left(4986, 205), enemy.Defense_Cannon_Right(5047, 205), enemy.Defense_Cannon_Left(7396, 205), enemy.Defense_Cannon_Right(7456, 205)]
        self.enemies_bullets = []
        self.menu_id = 0

    def on_draw(self):
        self.game.draw()

    def update(self, dt):
        #Main Menu
        if self.menu_id == 0:
            if self.keys[key.W]:
                if self.menu_cursor == 0:
                    self.menu_cursor = len(self.menu1) - 1
                else:
                    self.menu_cursor -= 1
            elif self.keys[key.S]:
                if self.menu_cursor == len(self.menu1) - 1:
                    self.menu_cursor = 0
                else:
                    self.menu_cursor += 1
            elif self.keys[key.SPACE] or self.keys[key.ENTER]:
                if self.menu_cursor == 0:
                    self.loading_level_flag = 1
                    self.player1 = Player(
                        Controls(key.W, key.S, key.A, key.D, key.SPACE, key.F), (3 + self.player_lives * 2)
                    )
                    self.player1.stage = self.stages[self.stage_transition]
                    self.menu_id = -1
                #elif self.menu_cursor == 1: #Two players A
                #elif self.menu_cursor == 2: #Two players B
                elif self.menu_cursor == 3:
                    self.menu_id = 1
                    self.menu_cursor = 0
        #Options Menu
        elif self.menu_id == 1:
            if self.keys[key.ENTER]:
                self.menu_id = 0
                self.menu_cursor = 0
            if self.keys[key.W]:
                if self.menu_cursor == 0:
                    self.menu_cursor = len(self.menu2) - 1
                else:
                    self.menu_cursor -= 1
            elif self.keys[key.S]:
                if self.menu_cursor == len(self.menu2) - 1:
                    self.menu_cursor = 0
                else:
                    self.menu_cursor += 1
            elif self.keys[key.A]:
                if self.menu_cursor == 0:
                    if self.player_lives == 0:
                        self.player_lives = 2
                    else:
                        self.player_lives -= 1
            elif self.keys[key.D]:
                if self.menu_cursor == 0:
                    if self.player_lives == 2:
                        self.player_lives = 0
                    else:
                        self.player_lives += 1
            elif self.keys[key.SPACE]:
                if self.menu_cursor == 1:
                    self.menu_id = 0
                    self.menu_cursor = 0
        #Game Over, must implement
        elif self.menu_id == 2:
            print 'honk honk'
        #Game Paused
        elif self.pause:
            if self.keys[key.SPACE] or self.keys[key.ENTER]:
                self.pause = False
        #Game
        else:
            self.generate_enemies()
            for e in self.enemies:
                if not e.done:
                    if e.alive:
                        e.bullet_collision(self.player1.bullets)
                    if e.sprite.x < -41 or e.sprite.x > 681:
                        if e.__class__.__name__ in STATIONARY_ENEMIES:
                            e.activated = False
                        elif e.__class__.__name__ == "EnemyDog":
                            if e.state != 2:
                                e.activated = False
                            else:
                                e.done = True
                        else:
                            e.done = True
                    if e.__class__.__name__ == "RunnerShooter":
                        e.update(self.enemies_bullets)
                    elif e.__class__.__name__ == "Sniper":
                        e.update(self.stages[self.stage_transition].get_stage_position(), self.enemies_bullets, self.player1.get_position())
                    elif e.__class__.__name__ == "EnemyDog":
                        e.update(self.stages[self.stage_transition].get_stage_position())
                    elif e.__class__.__name__ == "Floor_Turret":
                        e.update(self.stages[self.stage_transition].get_stage_position(), self.enemies_bullets)
                    elif e.__class__.__name__ == "Defense_Cannon_Left" or e.__class__.__name__ == "Defense_Cannon_Right":
                        e.update(self.stages[self.stage_transition].get_stage_position(), self.enemies_bullets)
                    else:
                        e.update()

            for e in self.enemies:
                if e.done:
                    self.enemies.pop(self.enemies.index(e))

            for b in self.player1.bullets:
                b.update()
                if b.sprite.x < -10 or b.sprite.x > 650 or b.sprite.y < -10 or b.sprite.y > 490:
                    b.done = True

            for b in self.player1.bullets:
                if b.done:
                    self.player1.bullets.pop(self.player1.bullets.index(b))

            for b in self.enemies_bullets:
                b.update()
                if b.sprite.x < -10 or b.sprite.x > 650 or b.sprite.y < -10 or b.sprite.y > 490:
                    b.done = True

            for b in self.enemies_bullets:
                if b.done:
                    self.enemies_bullets.pop(self.enemies_bullets.index(b))

            self.stages[self.stage_transition].update()
            if self.keys[key.ENTER]:
                self.pause = True

            #player1 alive
            if self.player1.is_alive():
                self.player1.clear_speed()

                self.player1.hazard_collision(self.enemies)
                self.player1.hazard_collision(self.enemies_bullets)
                self.player1.shield += 1

                if self.keys[key.F]:
                    player1_action = self.player1.react(self.keys)
                    if player1_action != None:
                        player1_action()

                if self.keys[key.SPACE]:
                    if self.keys[key.S]:
                        self.player1.drop()
                    else:
                        self.player1.jump()

                if self.keys[key.D]:
                    self.player1.move_forward(
                        self.stages[self.stage_transition]
                    )
                    if self.keys[key.W]:
                        self.player1.aim_up()
                    elif self.keys[key.S]:
                        self.player1.aim_down()
                    else:
                        self.player1.cancel_y_aim()

                elif self.keys[key.A]:
                    self.player1.move_backward(
                        self.stages[self.stage_transition]
                    )
                    if self.keys[key.W]:
                        self.player1.aim_up()
                    elif self.keys[key.S]:
                        self.player1.aim_down()
                    else:
                        self.player1.cancel_y_aim()

                else:
                    if self.keys[key.W]:
                        self.player1.cancel_x_aim()
                        self.player1.aim_up()

                    elif self.keys[key.S]:
                        if self.player1.looking_forward:
                            self.player1.aim_forward()
                        else:
                            self.player1.aim_backward()

                        if not self.player1.in_mid_air:
                            self.player1.cancel_y_aim()
                            self.player1.duck()
                        else:
                            self.player1.cancel_x_aim()
                            self.player1.aim_down()

                    else:
                        if self.player1.looking_forward:
                            self.player1.aim_forward()
                        else:
                            self.player1.aim_backward()
                        self.player1.cancel_y_aim()

                if self.player1.in_mid_air:
                    self.player1.fall()
                else:
                    self.player1.platform_walk()

                self.stages[self.stage_transition].move(self.player1)
            #player1 dead
            else:
                if self.player1.lives == 0: #Temporal Game Over, must implement a real Game Over
                    self.reset()
            self.player1.draw()

    def press_key(self, dt):
        self.game.react(self.keys)
Beispiel #2
0
def main():
    """ Main Program """
    pygame.init()

    # Set the height and width of the screen
    size = [constants.SCREEN_WIDTH, constants.SCREEN_HEIGHT]
    screen = pygame.display.set_mode(size)

    pygame.display.set_caption("Homeward")

    # Create the player
    player = Player()

    # Create all the levels
    level_list = []
    level_list.append(levels.MainMenu(player))
    level_list.append(levels.LevelTutorial(player))
    level_list.append(levels.Level_01(player))
    level_list.append(levels.Level_02(player))
    level_list.append(levels.Level_03(player))
    level_list.append(levels.Level_04(player))
    level_list.append(levels.YouWin(player))
    level_list.append(levels.GameOver(player))

    # Set the current level
    current_level_no = 0
    current_level = level_list[current_level_no]

    #HUD
    game_HUD = health.HUD(player)
    score_HUD = score.ScoreHUD(player)


    active_sprite_list = pygame.sprite.Group()
    player.level = current_level

    player.rect.x = 340
    player.rect.y = constants.SCREEN_HEIGHT - player.rect.height - 25
    active_sprite_list.add(player)

    #Loop until the user clicks the close button.
    done = False

    # Used to manage how fast the screen updates
    clock = pygame.time.Clock()

    # -------- Main Program Loop -----------
    while not done:
        for event in pygame.event.get(): # User did something
            if event.type == pygame.QUIT: # If user clicked close
                done = True # Flag that we are done so we exit this loop

            left_control_keys = [pygame.K_LEFT, pygame.K_a]
            right_control_keys = [pygame.K_d, pygame.K_RIGHT]
            up_control_keys = [pygame.K_w, pygame.K_UP, pygame.K_SPACE]
            down_control_keys = [pygame.K_DOWN, pygame.K_s]

            if event.type == pygame.KEYDOWN:
                if event.key in left_control_keys:
                    player.go_left()
                if event.key in right_control_keys:
                    player.go_right()
                if event.key in up_control_keys:
                    player.jump()
                if event.key in down_control_keys:
                    player.duck()

                if event.key == pygame.K_LEFT and pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    if current_level_no > 0:
                        current_level_no -= 1
                        current_level = level_list[current_level_no]
                        player.level = current_level
                        player.rect.y = constants.SCREEN_HEIGHT - player.rect.height - 25

                if event.key == pygame.K_RIGHT and pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    if current_level_no < len(level_list)-1:
                        current_level_no += 1
                        current_level = level_list[current_level_no]
                        player.level = current_level
                        player.rect.y = constants.SCREEN_HEIGHT - player.rect.height - 25

            if event.type == pygame.KEYUP:
                if event.key in left_control_keys and player.change_x < 0:
                    player.stop()
                if event.key in right_control_keys and player.change_x > 0:
                    player.stop()
                if event.key in down_control_keys and player.change_y > 0:
                    player.stand_up()

        # Update the player.
        active_sprite_list.update()

        # Update HUD
        game_HUD.update()
        score_HUD.update()

        # Update items in the level
        current_level.update()

        current_position = player.rect.x + current_level.world_shift

        # If the player gets near the right side, shift the world left (-x)
        if player.rect.x >= 500:
            diff = player.rect.x - 500
            player.rect.x = 500
            current_level.shift_world(-diff)

        # If the player gets near the left side, shift the world right (+x)
        if player.rect.x <= 120 and current_level.world_shift < -1:  # -1 to prevent edge of wall being seen
            diff = 120 - player.rect.x
            player.rect.x = 120
            current_level.shift_world(diff)

        # If the player gets to the end of the level, go to the next level
        if current_position < current_level.level_limit:
            player.rect.x = 120
            if current_level_no < len(level_list)-1:
                current_level_no += 1
                print(current_level_no)
                current_level = level_list[current_level_no]
                player.level = current_level
                player.rect.y = constants.SCREEN_HEIGHT - player.rect.height - 25

        if player.health <= 0:
            current_level_no = len(level_list)-1
            current_level = level_list[current_level_no]
            player.level = current_level
            player.rect.y = constants.SCREEN_HEIGHT - player.rect.height - 40
            player.rect.x = (constants.SCREEN_WIDTH/2) - (player.rect.width/2)
            time_since_death = pygame.time.get_ticks() - player.death_time
            if time_since_death > 2000:
                player.health = 100
                current_level_no = 0
                current_level = level_list[current_level_no]
                player.level = current_level
                player.rect.y = constants.SCREEN_HEIGHT - player.rect.height - 40

        # ALL CODE TO DRAW SHOULD GO BELOW THIS COMMENT
        current_level.draw(screen)
        active_sprite_list.draw(screen)
        game_HUD.draw(screen)
        score_HUD.draw(screen)

        # ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT

        # Limit to 60 frames per second
        clock.tick(60)

        # Go ahead and update the screen with what we've drawn.
        pygame.display.flip()

    # Be IDLE friendly. If you forget this line, the program will 'hang'
    # on exit.
    pygame.quit()
Beispiel #3
0
class Gameplay:
    STATIONARY_ENEMIES = [
        'Sniper',
        'EnemyGranadier',
        'Floor_Turret',
        'Defense_Cannon_Right',
        'Defense_Cannon_Left',
        'DefenseRedExplosive'
    ]

    def __init__(self, player_lives):
        stage_1_background = pyglet.image.ImageGrid(
            pyglet.image.load('Contra_stage_images/Level_1.png'),
            1, 27, 640, 480
        )
        self.stage_1 = Stage(
            [
                Platform(0, 50, stage_1_background.width,True),
                Platform(1410,170,118,False),
                Platform(1624,256,319,False),
                Platform(1484,325,219,False),
                Platform(3968,170,120,False),
                Platform(4210,170,118,False),
                Platform(4050,256,218,False),
                Platform(4289,325,397,False),
                Platform(4430,256,316,False),
                Platform(4688,170,118,False),
                Platform(6557,256,217,False),
                Platform(6717,170,117,False),
                Platform(6797,325,299,False),
                Platform(6937,256,220,False),
                Platform(7098,170,118,False),
                Platform(12138,170,92,False),
                Platform(11637,172,161,False),
                Platform(12239,378,72,False),
                Platform(13308,165,78,False),
                Platform(13468,165,79,False),
                Platform(13756,165,160,False),
                Platform(13993,165,160,False),
                Platform(15454,245,66,False),
                Platform(15695,169,78,False),
                Platform(16576,239,160,False),
                Platform(16576,153,180,False)
            ],
            stage_1_background
        )
        self.enemies = [
            enemy.Sniper(1553,325),
            enemy.Sniper(3288,256),
            enemy.Sniper(4123,51),
            enemy.Sniper(5041,281),
            enemy.Sniper(6510,255),
            enemy.Sniper(7461,282),
            enemy.EnemyDog(2166, 70),
            enemy.EnemyDog(2806,70),
            enemy.EnemyDog(3769,70),
            enemy.Floor_Turret(1287, 38),
            enemy.Floor_Turret(1928, 38),
            enemy.Floor_Turret(6439, 38),
            enemy.Floor_Turret(7781,38),
            enemy.Defense_Cannon_Left(4986, 205),
            enemy.Defense_Cannon_Right(5047, 205),
            enemy.Defense_Cannon_Left(7396, 205),
            enemy.Defense_Cannon_Right(7456, 205)
        ]
        self.enemies_bullets = []
        player1_lives = (3 + player_lives * 2)
        player1_controls = Controls(
            key.W,
            key.S,
            key.A,
            key.D,
            key.SPACE,
            key.F
        )
        self.player1 = Player(player1_controls, player1_lives)
        self.player1.stage = self.stage_1

    def draw(self):
        self.stage_1.draw()
        for e in self.enemies:
            if e.__class__.__name__ in Gameplay.STATIONARY_ENEMIES:
                if e.activated:
                    e.sprite.draw()
            else:
                e.sprite.draw()
        for b in self.player1.bullets:
            b.sprite.draw()
        for b in self.enemies_bullets:
            b.sprite.draw()
        self.player1.draw()

    def react(self, keys):
        self.player1.clear_speed()

        self.player1.hazard_collision(self.enemies)
        self.player1.hazard_collision(self.enemies_bullets)
        self.player1.shield += 1

        if keys[key.F]:
            player1_action = self.player1.react(keys)
            if player1_action != None:
                player1_action()

        if keys[key.SPACE]:
            if keys[key.S]:
                self.player1.drop()
            else:
                self.player1.jump()

        if keys[key.D]:
            self.player1.move_forward(
                self.stage_1
            )
            if keys[key.W]:
                self.player1.aim_up()
            elif keys[key.S]:
                self.player1.aim_down()
            else:
                self.player1.cancel_y_aim()

        elif keys[key.A]:
            self.player1.move_backward(
                self.stage_1
            )
            if keys[key.W]:
                self.player1.aim_up()
            elif keys[key.S]:
                self.player1.aim_down()
            else:
                self.player1.cancel_y_aim()

        else:
            if keys[key.W]:
                self.player1.cancel_x_aim()
                self.player1.aim_up()

            elif keys[key.S]:
                if self.player1.looking_forward:
                    self.player1.aim_forward()
                else:
                    self.player1.aim_backward()

                if not self.player1.in_mid_air:
                    self.player1.cancel_y_aim()
                    self.player1.duck()
                else:
                    self.player1.cancel_x_aim()
                    self.player1.aim_down()

            else:
                if self.player1.looking_forward:
                    self.player1.aim_forward()
                else:
                    self.player1.aim_backward()
                self.player1.cancel_y_aim()

        if self.player1.in_mid_air:
            self.player1.fall()
        else:
            self.player1.platform_walk()

        self.stage_1.move(self.player1)