コード例 #1
0
ファイル: contra.py プロジェクト: GerardoGR/gamedev
    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()
コード例 #2
0
ファイル: contra.py プロジェクト: GerardoGR/gamedev
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)