コード例 #1
0
ファイル: main2.py プロジェクト: 0Mysterio0/ArcadeProject1.1
 def level_generator(fruits,
                     tiers,
                     suckers,
                     birbs,
                     birb_level=False,
                     change_music=False,
                     background=arcade.csscolor.CORNFLOWER_BLUE):
     """As a sort of after thought, condensed a significant portion of level making operations,
     the first argument is the fruits for that level, in the order of how they will be stacked.
     The second argument is the tier list that corresponds to each fruit, third argument is for music,
      fourth argument is used to change background."""
     if change_music:
         if self.background_playing:
             arcade.stop_sound(self.background_music_player)
             self.background_playing = False
     arcade.set_background_color(background)
     #Setup Fruits, putting them into their fruit lists and applying fruit movement
     for fruit in range(len(fruits)):
         Basic_Fruit_Movement(fruits[fruit])
         self.revamped_fruit_list.append(fruits[fruit])
         tiers[fruit].append(fruits[fruit])
         self.complete_sprite_list.append(fruits[fruit])
     #Setup Suckers, putting them into their list and applying sucker movement
     for sucker in suckers:
         Sucker_Movement(sucker)
         self.Sucker_list.append(sucker)
         self.complete_sprite_list.append(sucker)
     if birb_level:
         for birb in birbs:
             Birb_Movement(birb)
             self.birds_list.append(birb)
             self.complete_sprite_list.append(birb)
コード例 #2
0
 def _stop_music_track(self):
     try:
         stop_sound(self.current_music)
     except AttributeError:
         pass
     finally:
         self.current_music = None
コード例 #3
0
    def update(self, delta, fireflies, shaked_fireflies: set):
        firefly_shaked = lambda mouse: arcade.NamedPoint(
            random.uniform(mouse.center_x - mouse.radius, mouse.center_x +
                           mouse.radius),
            random.uniform(mouse.center_y - mouse.radius, mouse.center_y +
                           mouse.radius),
        )

        if self.display:
            # si l'utilisateur clique, les lucioles sont attirées
            for f in fireflies:
                if f.collides_with_sprite(self):  ## collision avec la souris
                    f.is_shaked = True
                    f.target = firefly_shaked(self)
                    f.speed = 100
                    shaked_fireflies.add(f)
                elif (f in shaked_fireflies
                      ):  ## elles ne sont plus dans la range de la souris
                    f.is_shaked = False
                    f.speed = f.original_speed
                    shaked_fireflies.remove(f)
        else:
            # les lucioles retournent à leur business
            for f in shaked_fireflies:
                f.is_shaked = False
                f.target = Firefly.find_new_target()
                f.speed = (
                    f.original_speed
                )  ## on rétablit la vitesse par défaut de la luciole
                f.timer = random.uniform(0.0, f.countdown)

            shaked_fireflies.clear()

        # gestion du son
        volume = lambda: self.fade / Firefly.FADE_SOUND
        if len(shaked_fireflies):
            ## création du son / fade in
            self.fade = min(1, self.fade + delta)
            if not self.player_sound:
                self.player_sound = arcade.play_sound(self.firefly_sound,
                                                      volume=volume(),
                                                      looping=True)
            else:
                self.firefly_sound.set_volume(volume(), self.player_sound)
        elif not len(shaked_fireflies) and self.player_sound:
            ## supression du son / fade out
            self.fade = max(0, self.fade - delta)
            if volume() > 0:
                self.firefly_sound.set_volume(volume(), self.player_sound)
            else:
                arcade.stop_sound(self.player_sound)
                self.player_sound = None
コード例 #4
0
def move(delta_time):
    global times
    global first
    global track
    times += 1
    for each in movers:
        thick = 3
        x = each[0]
        y = each[1]
        radius = each[2]
        if radius <= 20:
            thick = 2
        if times > 25 and times < 150:
            if first:
                time.sleep(15)
                first = False
            arcade.draw_circle_filled(x, y, radius, arcade.color.WHEAT)  # big circle body
            arcade.draw_circle_outline(x, y, radius, arcade.color.WHEAT, thick)  # outline
            arcade.draw_circle_filled(x, y, radius * (2 / 3), arcade.color.WHEAT)  # smaller circle in body
            arcade.draw_circle_outline(x, y, radius * (2 / 3), arcade.color.WHEAT, thick)  # outline
            arcade.draw_circle_filled(x, y, (radius * (2 / 3)) / 2, arcade.color.WHEAT)  # smallest circle in body
            arcade.draw_circle_outline(x, y, (radius * (2 / 3)) / 2, arcade.color.WHEAT, thick)  # outline
            arcade.draw_arc_filled(x, y + radius - (radius * .16666667), radius * 1.5, radius * 1.5, arcade.color.WHEAT,
                                   0, 180)
            arcade.draw_arc_outline(x, y + radius - (radius * .16666667), radius * 1.5, radius * 1.5,
                                    arcade.color.WHEAT, 0, 180, thick + 2)
            arcade.draw_line(x - radius * 1.5 / 2, y + radius * (5 / 6), x - radius * 1.5 * -1 / 2,
                             y + radius * (5 / 6), arcade.color.WHEAT, thick)
            arcade.draw_circle_filled(x, y + radius + (radius * .1966667), radius / 4.5, arcade.color.WHEAT)
            arcade.draw_circle_outline(x, y + radius + (radius * .1966667), radius / 4.5, arcade.color.WHEAT, thick)
            each[0] = random.randint(50, 550)
            each[1] = random.randint(50, 550)
            each[2] = radius
            draw_BB8(each[0], each[1], radius)
    print("times:", times)
    if times >= 150 and times <= 200:
        # arcade.play_sound(arcade.load_sound("alarm.mp3"))
        draw_BB8(random.randint(50, 550), random.randint(50, 550), random.randint(10, 60), True)
    elif times > 200 and times <= 320:
        track += 1
        if track == 20:
            print("track")
            arcade.stop_sound(arcade.load_sound("alarm.mp3"))
            arcade.play_sound(arcade.load_sound("alarm.mp3"))
            track = 0
        for i in range(25):
            draw_BB8(random.randint(50, 550), random.randint(50, 550), random.randint(10, 60), True)
    elif times > 320:
        arcade.stop_sound(arcade.load_sound("alarm.mp3"))
        for i in range(60):
            draw_BB8(random.randint(50, 550), random.randint(50, 550), random.randint(times-200, times-100), True)
            print("big")
コード例 #5
0
ファイル: test.py プロジェクト: ianneyens/Ch.13_User_Control
 def on_key_release(self, key, modifiers):
     if key == arcade.key.LEFT:
         self.ball.dx = 0
         arcade.stop_sound(self.snd)
     elif key == arcade.key.RIGHT:
         self.ball.dx = 0
         arcade.stop_sound(self.snd)
     elif key == arcade.key.UP:
         self.ball.dy = 0
         arcade.stop_sound(self.snd)
     elif key == arcade.key.DOWN:
         self.ball.dy = 0
         arcade.stop_sound(self.snd)
コード例 #6
0
    def setup(self, nb: int = 100):
        # préchargement du son (sinon freeze)
        arcade.stop_sound(arcade.play_sound(self.firefly_sound))

        # lancement de la musique
        arcade.play_sound(self.music, volume=0.5, looping=True)

        # objet conteneur de la souris
        self.mouse = Mouse()
        self.mouse.firefly_sound = self.firefly_sound

        # message
        FadingMessage.COLOR_START = self.BACKGROUND
        self.message = FadingMessage(self.width / 2, self.height,
                                     "clique avec la souris")

        # initialisation des lucioles aléatoirement (position, taille, cible, état de l'éclairage, vitesse d'éclairage)
        self.shaked_fireflies = set()
        self.fireflies = arcade.SpriteList()
        for _ in range(nb):
            x, y, r = (
                random.randint(0, self.width),
                random.randint(0, self.height),
                random.randint(4, 7),
            )
            countdown = random.uniform(1, 5)
            timer = random.uniform(0, countdown)

            f = Firefly(
                x,
                y,
                r,
                speed=random.uniform(*Firefly.SPEED),
                timer=timer,
                target=Firefly.find_new_target(),
                countdown=countdown,
            )
            self.fireflies.append(f)
コード例 #7
0
ファイル: Game.py プロジェクト: LMGerard/Pong
    def on_update(self, delta_time: float):
        if self.mode == "game":
            self.players.update()
            self.ball.update()

            if self.ball.right >= self.window.width:
                self.player_1.score += 1
                if self.player_1.score == 3:
                    self.mode = "end"
                    self.winner = "Player 1"
                    self.end_timer = time()
                self.start()
            elif self.ball.left <= 0:
                self.player_2.score += 1
                if self.player_2.score == 3:
                    self.mode = "end"
                    self.winner = "Player 2"
                    self.end_timer = time()
                self.start()
        elif self.mode == "end":
            if time() - self.end_timer >= 5:
                ac.stop_sound(self.music_player)
                self.window.show_menu()
コード例 #8
0
ファイル: .py プロジェクト: Alperendmr/2d-game-python
    def on_update(self, delta_time):
        # We're calling physics engine
        # Enemy
        if not self.health == 0:
            self.enemy_list.update()

        self.physics_engine.update()

        self.total_time -= delta_time

        # Update animations
        if self.physics_engine.can_jump():
            self.player_sprite.can_jump = False
        else:
            self.player_sprite.can_jump = True

        if self.physics_engine.is_on_ladder(
        ) and not self.physics_engine.can_jump():
            self.player_sprite.is_on_ladder = True
            self.process_keychange()
        else:
            self.player_sprite.is_on_ladder = False
            self.process_keychange()

        self.coin_list.update_animation(delta_time)
        self.background_list.update_animation(delta_time)
        self.player_list.update_animation(delta_time)

        self.wall_list.update()

        # See if the moving wall hit a boundary and needs to reverse direction.
        for wall in self.wall_list:

            if wall.boundary_right and wall.right > wall.boundary_right and wall.change_x > 0:
                wall.change_x *= -1
            if wall.boundary_left and wall.left < wall.boundary_left and wall.change_x < 0:
                wall.change_x *= -1
            if wall.boundary_top and wall.top > wall.boundary_top and wall.change_y > 0:
                wall.change_y *= -1
            if wall.boundary_bottom and wall.bottom < wall.boundary_bottom and wall.change_y < 0:
                wall.change_y *= -1

        # if you hit any coins
        coin_hit_list = arcade.check_for_collision_with_list(
            self.player_sprite, self.coin_list)

        for coin in coin_hit_list:
            self.game_score += 1
            # Remove the coin
            coin.remove_from_sprite_lists()
            # Play sound
            arcade.play_sound(self.collect_coin_sound)

        # enemy
        for enemy in self.enemy_list:
            if len(arcade.check_for_collision_with_list(enemy,
                                                        self.wall_list)) > 0:
                enemy.change_x *= -1
            elif enemy.boundary_left is not None and enemy.left < enemy.boundary_left:
                enemy.change_x *= -1
            elif enemy.boundary_right is not None and enemy.right > enemy.boundary_right:
                enemy.change_x *= -1

        changed_viewport = False

        if self.player_sprite.center_x >= self.end_of_map:
            self.level += 1
            self.setup(self.level)
            self.view_left = 0
            self.view_bottom = 0
            arcade.stop_sound(self.game_sound)
            arcade.play_sound(self.level_update_sound)
            # arcade.play_sound(self.game_sound)
            changed_viewport = True

        # Manage Scrolling

        if len(
                arcade.check_for_collision_with_list(self.player_sprite,
                                                     self.enemy_list)) > 0:
            self.player_sprite.center_x = PLAYER_START_X
            self.player_sprite.center_y = PLAYER_START_Y

            self.view_left = 0
            self.view_bottom = 0
            changed_viewport = True
            arcade.play_sound(self.game_over)
            self.health -= 1

        # if player falls
        if self.player_sprite.center_y < -100:
            self.player_sprite.center_x = PLAYER_START_X
            self.player_sprite.center_y = PLAYER_START_Y

            # Set the camera
            self.view_left = 0
            self.view_bottom = 0
            changed_viewport = True
            arcade.play_sound(self.game_over)
            self.health -= 1

        if self.health == 0:
            arcade.stop_sound(self.game_sound)
            arcade.pause(3)
            arcade.play_sound(self.game_sound)
            self.total_time = 303.0
            self.view_left = 0
            self.view_bottom = 0
            changed_viewport = True
            self.health = 3
            self.game_score = self.game_score - 20

        # end of the game
        if self.level > 3:
            game_over_view = GameOverView()
            self.window.show_view(game_over_view)

        # if character hits don't touch
        if arcade.check_for_collision_with_list(self.player_sprite,
                                                self.dont_touch_list):
            self.player_sprite.change_x = 0
            self.player_sprite.change_y = 0
            self.player_sprite.center_x = PLAYER_START_X
            self.player_sprite.center_y = PLAYER_START_Y

            # Set the camera to the start
            self.view_left = 0
            self.view_bottom = 0
            changed_viewport = True
            arcade.play_sound(self.game_over)
            self.health -= 1

        # Scroll left
        left_boundary = self.view_left + LEFT_VIEWPORT_MARGIN
        if self.player_sprite.left < left_boundary:
            self.view_left -= left_boundary - self.player_sprite.left
            changed_viewport = True

        # Scroll right
        right_boundary = self.view_left + SCREEN_WIDTH - RIGHT_VIEWPORT_MARGIN
        if self.player_sprite.right > right_boundary:
            self.view_left += self.player_sprite.right - right_boundary
            changed_viewport = True

        # Scroll up
        top_boundary = self.view_bottom + SCREEN_HEIGHT - TOP_VIEWPORT_MARGIN
        if self.player_sprite.top > top_boundary:
            self.view_bottom += self.player_sprite.top - top_boundary
            changed_viewport = True

        # Scroll down
        bottom_boundary = self.view_bottom + BOTTOM_VIEWPORT_MARGIN
        if self.player_sprite.bottom < bottom_boundary:
            self.view_bottom -= bottom_boundary - self.player_sprite.bottom
            changed_viewport = True

        if changed_viewport:
            self.view_bottom = int(self.view_bottom)
            self.view_left = int(self.view_left)

            # Done the Scrolling

            arcade.set_viewport(self.view_left, SCREEN_WIDTH + self.view_left,
                                self.view_bottom,
                                SCREEN_HEIGHT + self.view_bottom)
コード例 #9
0
    def update(self, delta_time):
        """ Movement and game logic """

        # Move the player with the physics engine
        self.physics_engine.update()

        # Run main BGM loop if intro is finished
        position = self.intro.get_stream_position(self.introduction)
        if position == 0 and self.bgm == None:
            self.bgm = arcade.play_sound(self.music, .5, 0, True)

        # Make goblin patrol
        self.enemy_sprite.enemy_walk()

        # See if we hit any hazards or enemies
        danger_hit_list = arcade.check_for_collision_with_list(
            self.player_sprite, self.danger_list)
        danger_hit_list += arcade.check_for_collision_with_list(
            self.player_sprite, self.foe_list)

        if danger_hit_list:
            # "Kill" player, play death sound, and move to the game over screen
            self.player_sprite.kill()
            arcade.play_sound(self.death)

            # End music
            if self.intro.is_playing(self.introduction):
                arcade.stop_sound(self.introduction)
            else:
                arcade.stop_sound(self.bgm)

            game_view = EndingView(1)
            self.window.show_view(game_view)

        # See if we have collected any gems
        gem_hit_list = arcade.check_for_collision_with_list(
            self.player_sprite, self.gem_list)

        for gem in gem_hit_list:
            gem.remove_from_sprite_lists()
            self.player_sprite.change_score(1)
            arcade.play_sound(self.gem_collect)

        # --- Manage Scrolling ---

        # Track if we need to change the viewport
        changed = False

        # Scroll left
        left_boundary = self.view_left + LEFT_VIEWPOINT_MARGIN
        if self.player_sprite.left < left_boundary:
            self.view_left -= left_boundary - self.player_sprite.left
            changed = True

        # Scroll right
        right_boundary = self.view_left + SCREEN_WIDTH - RIGHT_VIEWPORT_MARGIN
        if self.player_sprite.right > right_boundary:
            self.view_left += self.player_sprite.right - right_boundary
            changed = True

        # Scroll up
        top_boundary = self.view_bottom + SCREEN_HEIGHT - TOP_VIEWPORT_MARGIN
        if self.player_sprite.top > top_boundary:
            self.view_bottom += self.player_sprite.top - top_boundary
            changed = True

        # Scroll down
        bottom_boundary = self.view_bottom + BOTTOM_VIEWPORT_MARGIN
        if self.player_sprite.bottom < bottom_boundary:
            self.view_bottom -= bottom_boundary - self.player_sprite.bottom
            changed = True

        if changed:
            # Only scroll full integers to avoid mismanagement of pixels
            self.view_bottom = int(self.view_bottom)
            self.view_left = int(self.view_left)

            # Do the scrolling
            arcade.set_viewport(self.view_left, SCREEN_WIDTH + self.view_left,
                                self.view_bottom,
                                SCREEN_HEIGHT + self.view_bottom)

        # Game finish condition
        self.exit_access = arcade.check_for_collision(self.player_sprite,
                                                      self.escape)
        if self.player_sprite.score == 5 and self.exit_access:
            # End music
            if self.intro.is_playing(self.introduction):
                arcade.stop_sound(self.introduction)
            else:
                arcade.stop_sound(self.bgm)

            # Move to victory screen
            game_view = EndingView(2)
            self.window.show_view(game_view)
コード例 #10
0
 def reset_level(self):
     arcade.stop_sound(self.music)
     # Exhaust generator since we don't need load view for resetting
     for _ in self.setup(self.current_level):
         pass
コード例 #11
0
 def on_show(self):
     for sound in self.sound_list:
         arcade.stop_sound(sound)
コード例 #12
0
ファイル: main2.py プロジェクト: 0Mysterio0/ArcadeProject1.1
    def on_update(self, delta_time):
        """ Movement and game logic """
        if not self.game_over:
            # Move the player with the physics engine
            self.physics_engine.update()

            # Updating suckers
            self.Sucker_list.update()

            #Update birbs
            self.birds_list.update()

            # Condensing our fruit update operations
            self.revamped_fruit_list.update()

            # New hitting ground function to optimize some things
            def fruit_hitting_ground(fruits):
                """When fruit hits ground, this function will reset it"""
                #fruits.bottom = FRUIT_SIZE * 9.75
                #fruits.left = FRUIT_SIZE * rdm.randint(1, 14)
                fruits.change_y = rdm.randint(-4, -2)
                fruits.center_x = rdm.randrange(100, SCREEN_WIDTH - 100)
                fruits.center_y = SCREEN_HEIGHT + 100

            def sucker_hitting_ground(sucker):
                """When sucker hits ground, this function will reset it"""
                sucker.bottom = FRUIT_SIZE * 9.75
                sucker.left = FRUIT_SIZE * rdm.randint(2, 13)
                sucker.boundary_right = FRUIT_SIZE
                sucker.boundary_left = FRUIT_SIZE
                sucker.change_y = rdm.choice([-5, -4, -3, -2])
                sucker.change_x = rdm.choice([-1, 0, 1])

            # Update suckers when they hit the ground
            for sucker in self.Sucker_list:
                if arcade.check_for_collision_with_list(
                        sucker, self.wall_list):
                    sucker_hitting_ground(sucker)
                    sucker.update()

            # Update each tier of fruit separately to manage shake and stacked conditions, which is still condensed
            # compared to updating every fruit on their own. All tiers can now be setup this way, with no need to
            # reference particular levels.
            for fruit in self.tier_1_fruit_list:
                if arcade.check_for_collision_with_list(fruit, self.wall_list):
                    fruit_hitting_ground(fruit)
                    if self.Shake_1:
                        arcade.play_sound(self.fruit_hit_ground,
                                          volume=sound_effect_volume)
                    self.Shake_1 = False
                    self.Stacked_1 = False
                    self.Catch_1 = False
                    fruit.update()
            for fruit in self.tier_2_fruit_list:
                if arcade.check_for_collision_with_list(fruit, self.wall_list):
                    fruit_hitting_ground(fruit)
                    if self.Shake_2:
                        arcade.play_sound(self.fruit_hit_ground,
                                          volume=sound_effect_volume)
                    self.Shake_2 = False
                    self.Stacked_2 = False
                    self.Catch_2 = False
                    fruit.update()
            for fruit in self.tier_3_fruit_list:
                if arcade.check_for_collision_with_list(fruit, self.wall_list):
                    fruit_hitting_ground(fruit)
                    if self.Shake_3:
                        arcade.play_sound(self.fruit_hit_ground,
                                          volume=sound_effect_volume)
                    self.Shake_3 = False
                    self.Stacked_3 = False
                    self.Catch_3 = False
                    fruit.update()
            for fruit in self.tier_4_fruit_list:
                if arcade.check_for_collision_with_list(fruit, self.wall_list):
                    fruit_hitting_ground(fruit)
                    if self.Shake_4:
                        arcade.play_sound(self.fruit_hit_ground,
                                          volume=sound_effect_volume)
                    self.Shake_4 = False
                    self.Stacked_4 = False
                    self.Catch_4 = False
                    fruit.update()
            for fruit in self.tier_5_fruit_list:
                if arcade.check_for_collision_with_list(fruit, self.wall_list):
                    fruit_hitting_ground(fruit)
                    if self.Shake_5:
                        arcade.play_sound(self.fruit_hit_ground,
                                          volume=sound_effect_volume)
                    self.Shake_5 = False
                    self.Stacked_5 = False
                    self.Catch_5 = False
                    fruit.update()

            # Stacking operations, each successive fruit will follow the previous fruit.
            # Occurs only when the previous fruit is stacked.
            for tier_1_fruit in self.tier_1_fruit_list:
                if arcade.check_for_collision(tier_1_fruit, self.player_sprite) and not self.Shake_1 \
                        and not self.Catch_1:
                    # Play good sound here when this occurs
                    tier_1_fruit.follow_sprite(self.player_sprite)
                    if not self.Stacked_1:
                        arcade.play_sound(self.picking_up_sound,
                                          volume=sound_effect_volume)
                    self.Stacked_1 = True
                if self.Catch_1:
                    tier_1_fruit.follow_below(self.crow)

                for tier_2_fruit in self.tier_2_fruit_list:
                    if arcade.check_for_collision_with_list(tier_2_fruit, self.tier_1_fruit_list) and not self.Shake_2 \
                            and self.Stacked_1\
                            and not self.Catch_2:
                        # Play good sound here when this occurs
                        tier_2_fruit.follow_sprite(tier_1_fruit)
                        if not self.Stacked_2:
                            arcade.play_sound(self.picking_up_sound,
                                              volume=sound_effect_volume)
                        self.Stacked_2 = True
                    if self.Catch_2:
                        tier_2_fruit.follow_below(self.crow)
                    for tier_3_fruit in self.tier_3_fruit_list:
                        if arcade.check_for_collision_with_list(tier_3_fruit, self.tier_2_fruit_list) \
                                and not self.Shake_3 \
                                and self.Stacked_2\
                                and not self.Catch_3:
                            # Play good sound here when this occurs
                            tier_3_fruit.follow_sprite(tier_2_fruit)
                            if not self.Stacked_3:
                                arcade.play_sound(self.picking_up_sound,
                                                  volume=sound_effect_volume)
                            self.Stacked_3 = True
                        if self.Catch_3:
                            tier_3_fruit.follow_below(self.crow)
                        for tier_4_fruit in self.tier_4_fruit_list:
                            if arcade.check_for_collision_with_list(tier_4_fruit, self.tier_3_fruit_list) \
                                    and not self.Shake_4 \
                                    and self.Stacked_3\
                                    and not self.Catch_4:
                                # Play good sound here when this occurs
                                tier_4_fruit.follow_sprite(tier_3_fruit)
                                if not self.Stacked_4:
                                    arcade.play_sound(
                                        self.picking_up_sound,
                                        volume=sound_effect_volume)
                                self.Stacked_4 = True
                            if self.Catch_4:
                                tier_1_fruit.follow_below(self.crow)
                            for tier_5_fruit in self.tier_5_fruit_list:
                                if arcade.check_for_collision_with_list(tier_5_fruit, self.tier_4_fruit_list) \
                                        and not self.Shake_5 \
                                        and self.Stacked_4\
                                        and not self.Catch_5:
                                    # Play good sound here when this occurs
                                    tier_5_fruit.follow_sprite(tier_4_fruit)
                                    if not self.Stacked_5:
                                        arcade.play_sound(
                                            self.picking_up_sound,
                                            volume=sound_effect_volume)
                                    self.Stacked_5 = True
                                if self.Catch_5:
                                    tier_5_fruit.follow_below(self.crow)

            # Revamped collision system, if a sucker collides with fruit that is stacked, it will knock ONLY the top one off.
            for sucker in self.Sucker_list:
                if (((arcade.check_for_collision_with_list(sucker, self.tier_1_fruit_list)
                      and self.Stacked_1)) or
                    (arcade.check_for_collision_with_list(sucker, self.tier_2_fruit_list)
                     and self.Stacked_2) or
                    (arcade.check_for_collision_with_list(sucker, self.tier_3_fruit_list)
                     and self.Stacked_3) or
                    (arcade.check_for_collision_with_list(sucker, self.tier_4_fruit_list)
                     and self.Stacked_4)) \
                        and (not self.Shake_1 or not self.Shake_2 or not self.Shake_3 or not self.Shake_4
                             or not self.Shake_5):
                    if self.Stacked_4:
                        # Play bad sound here when this occurs.
                        if not self.Shake_4:
                            arcade.play_sound(self.losing_fruit_sound,
                                              volume=sound_effect_volume)
                        self.Shake_4 = True
                    elif self.Stacked_3:
                        # Play bad sound here when this occurs.
                        if not self.Shake_3:
                            arcade.play_sound(self.losing_fruit_sound,
                                              volume=sound_effect_volume)
                        self.Shake_3 = True
                    elif self.Stacked_2:
                        if not self.Shake_2:
                            # Play bad sound here when this occurs.
                            arcade.play_sound(self.losing_fruit_sound,
                                              volume=sound_effect_volume)
                        self.Shake_2 = True
                    elif self.Stacked_1:
                        if not self.Shake_1:
                            # Play bad sound here when this occurs.
                            arcade.play_sound(self.losing_fruit_sound,
                                              volume=sound_effect_volume)
                        self.Shake_1 = True

            for birb in self.birds_list:
                if self.Stacked_1 and (not self.Catch_1 or not self.Catch_2
                                       or not self.Catch_3 or not self.Catch_4
                                       or not self.Catch_5):
                    birb.follow_sprite(self.player_sprite)
                if (((arcade.check_for_collision_with_list(birb, self.tier_1_fruit_list)
                          and self.Stacked_1)) or
                        (arcade.check_for_collision_with_list(birb, self.tier_2_fruit_list)
                         and self.Stacked_2) or
                        (arcade.check_for_collision_with_list(birb, self.tier_3_fruit_list)
                        and self.Stacked_3) or
                        (arcade.check_for_collision_with_list(birb, self.tier_4_fruit_list)
                        and self.Stacked_4)) \
                            and (not self.Catch_1 or not self.Catch_2 or not self.Catch_3 or not self.Catch_4
                                 or not self.Catch_5):
                    if self.Stacked_4:
                        # Play bad sound here when this occurs.
                        if not self.Catch_4:
                            arcade.play_sound(self.losing_fruit_sound,
                                              volume=sound_effect_volume)
                        self.Catch_4 = True
                        self.Stacked_3 = False
                        birb.follow_sprite(self.birb_spawn_right)
                    elif self.Stacked_3:
                        # Play bad sound here when this occurs.
                        if not self.Catch_3:
                            arcade.play_sound(self.losing_fruit_sound,
                                              volume=sound_effect_volume)

                        self.Catch_3 = True
                        self.Stacked_2 = False
                        birb.follow_sprite(self.birb_spawn_right)
                    elif self.Stacked_2:
                        if not self.Catch_2:
                            # Play bad sound here when this occurs.
                            arcade.play_sound(self.losing_fruit_sound,
                                              volume=sound_effect_volume)

                        self.Catch_2 = True
                        self.Stacked_1 = False
                        birb.follow_sprite(self.birb_spawn_right)
                    elif self.Stacked_1:
                        if not self.Catch_1:
                            # Play bad sound here when this occurs.
                            arcade.play_sound(self.losing_fruit_sound,
                                              volume=sound_effect_volume)
                        self.Catch_1 = True
                        self.Stacked_1 = False
                        birb.follow_sprite(self.birb_spawn_right)

                if not self.Stacked_1:
                    birb.follow_sprite(self.birb_spawn_left)

            if len(
                    arcade.check_for_collision_with_list(
                        self.player_sprite, self.cherry_list)) > 0:
                self.instr_objective = True
            if len(
                    arcade.check_for_collision_with_list(
                        self.player_sprite, self.cherry_list_2)) > 0:
                self.restart_objective = True
            for item in self.outro_list:
                if arcade.check_for_collision_with_list(item, self.wall_list):
                    arcade.play_sound(self.burst_sound,
                                      volume=sound_effect_volume)
                    item.kill()
            self.outro_list.update()

            # See if the user got to the end of the level
            if self.level == 0:
                if self.instr_objective or self.skip_level:
                    # once we hit the cherry, go to next level
                    self.skip_level = False
                    self.instr_objective = False
                    self.level = 1
                    # Load the next level
                    self.setup(self.level)
            if self.level == 1:
                if self.reverse_level:
                    self.reverse_level = False
                    self.level = 0
                    if self.background_playing:
                        arcade.stop_sound(self.background_music_player)
                        self.background_playing = False
                    self.setup(self.level)
                if self.Stacked_3 or self.skip_level:
                    self.skip_level = False
                    # once we hit 3 fruits, go to next level
                    self.level = 2
                    # Load the next level
                    self.setup(self.level)
                    self.Stacked_1 = False
                    self.Stacked_2 = False
                    self.Stacked_3 = False
                    self.Stacked_4 = False
                    self.Stacked_5 = False
            if self.level == 2:
                if self.reverse_level:
                    self.level = 1
                    self.reverse_level = False
                    self.setup(self.level)
                if self.Stacked_4 or self.skip_level:
                    self.skip_level = False
                    # once we hit 4 fruits, go to next level
                    self.level = 3
                    # Load the next level
                    self.setup(self.level)
                    self.Stacked_1 = False
                    self.Stacked_2 = False
                    self.Stacked_3 = False
                    self.Stacked_4 = False
                    self.Stacked_5 = False
            if self.level == 3:
                if self.reverse_level:
                    self.reverse_level = False
                    self.level = 2
                    self.setup(self.level)
                if self.Stacked_5 or self.skip_level:
                    self.skip_level = False
                    # once we hit 5 fruits, go to next level
                    self.level = 4
                    # Load the next level
                    self.setup(self.level)
                    self.Stacked_1 = False
                    self.Stacked_2 = False
                    self.Stacked_3 = False
                    self.Stacked_4 = False
                    self.Stacked_5 = False
            if self.level == 4:
                if self.restart_objective:
                    self.restart_objective = False
                    #once we hit the cherry, restart at level 1
                    self.level = 0
                    self.Stacked_1 = False
                    self.Stacked_2 = False
                    self.Stacked_3 = False
                    self.Stacked_4 = False
                    self.Stacked_5 = False
                    # Load the next level
                    arcade.stop_sound(self.intro_player)
                    self.setup(self.level)
            # See if the player walks to the door. If so, game over.
            if len(
                    arcade.check_for_collision_with_list(
                        self.player_sprite, self.door_list)) > 0:
                self.game_over = True
        else:
            MyGame.close(self)
コード例 #13
0
ファイル: main2.py プロジェクト: 0Mysterio0/ArcadeProject1.1
    def setup(self, level):
        """ Set up the game here. Call this function to restart the game. """
        # Create the Sprite lists
        self.player_list = arcade.SpriteList()
        self.wall_list = arcade.SpriteList(use_spatial_hash=True)
        self.orders_list = arcade.SpriteList(use_spatial_hash=True)
        self.instructions_list = arcade.SpriteList(use_spatial_hash=True)
        self.intro_list = arcade.SpriteList(use_spatial_hash=True)

        self.cherry_list = arcade.SpriteList()
        self.cherry_list_2 = arcade.SpriteList()
        self.door_list = arcade.SpriteList()
        self.outro_list = arcade.SpriteList()
        #We use this list for each level of fruits
        self.revamped_fruit_list = arcade.SpriteList()

        #Complete list for easier drawing
        self.complete_sprite_list = arcade.SpriteList()
        # Consider a list for each tier of fruit in the tower, but don't actually draw
        # from this list
        # Think of these as categories for operations
        self.tier_1_fruit_list = arcade.SpriteList()
        self.tier_2_fruit_list = arcade.SpriteList()
        self.tier_3_fruit_list = arcade.SpriteList()
        self.tier_4_fruit_list = arcade.SpriteList()
        self.tier_5_fruit_list = arcade.SpriteList()

        # This sucker list works for every level
        self.Sucker_list = arcade.SpriteList()

        self.birds_list = arcade.SpriteList()
        self.crow = Birb("Our Images/Enemies/birb.png", FRUIT_SCALING * 0.4)

        self.birb_spawn_right = arcade.Sprite(
            ":resources:images/tiles/boxCrate_double.png", TILE_SCALING)
        self.birb_spawn_left = arcade.Sprite(
            ":resources:images/tiles/boxCrate_double.png", TILE_SCALING)

        self.Sucker1 = arcade.Sprite("Our Images/Suckers/sucker1.png",
                                     FRUIT_SCALING * 1.8)
        self.Sucker2 = arcade.Sprite("Our Images/Suckers/sucker2.png",
                                     FRUIT_SCALING * 1.8)
        self.Sucker3 = arcade.Sprite("Our Images/Suckers/sucker3.png",
                                     FRUIT_SCALING * 1.8)
        self.Sucker4 = TurningSprite("Our Images/Suckers/sucker3.png",
                                     FRUIT_SCALING * 1.8)
        self.Sucker5 = arcade.Sprite("Our Images/Suckers/sucker1.png",
                                     FRUIT_SCALING * 1.8)
        self.Sucker6 = arcade.Sprite("Our Images/Suckers/sucker2.png",
                                     FRUIT_SCALING * 1.8)
        #We only need individual fruit coins instead!
        self.Grape_coin = Coin("Our Images/Fruits/grapes.png",
                               FRUIT_SCALING * 1.8)
        self.Cherry_coin = Coin("Our Images/Fruits/Cherry.png",
                                FRUIT_SCALING * 1.8)
        self.Watermelon_coin = Coin("Our Images/Fruits/Watermelon.png",
                                    FRUIT_SCALING * 1.8)
        self.Orange_coin = Coin("Our Images/Fruits/orange.png",
                                FRUIT_SCALING * 1.8)
        self.Pear_coin = Coin("Our Images/Fruits/pear.png",
                              FRUIT_SCALING * 1.8)
        self.Pineapple_coin = Coin("Our Images/Fruits/Pineapple.png",
                                   FRUIT_SCALING * 1.8)
        self.Plum_coin = Coin("Our Images/Fruits/plum.png",
                              FRUIT_SCALING * 1.8)
        self.Strawberry_coin = Coin("Our Images/Fruits/Strawberry.png",
                                    FRUIT_SCALING * 1.8)
        self.Kiwi_coin = Coin("Our Images/Fruits/kiwi.png",
                              FRUIT_SCALING * 1.8)
        self.Lemon_coin = Coin("Our Images/Fruits/lemon.png",
                               FRUIT_SCALING * 1.8)
        self.Apple_coin = Coin("Our Images/Fruits/Apple.png",
                               FRUIT_SCALING * 1.8)
        self.Bannana_coin = Coin("Our Images/Fruits/Bannana.png",
                                 FRUIT_SCALING * 1.8)

        # Set up the player, specifically placing it at these coordinates.
        image_source = "Our Images/Gal_with_basket.PNG"
        self.player_sprite = arcade.Sprite(image_source, CHARACTER_SCALING)
        self.player_sprite.center_x = PLAYER_START_X
        self.player_sprite.center_y = PLAYER_START_Y
        self.player_list.append(self.player_sprite)
        self.complete_sprite_list.append(self.player_sprite)
        # Create the ground
        # This shows using a loop to place multiple sprites horizontally
        for x in range(0, 1750, 64):
            wall = arcade.Sprite(":resources:images/tiles/grassMid.png",
                                 TILE_SCALING)
            wall.center_x = x
            wall.center_y = 32
            self.wall_list.append(wall)
            self.complete_sprite_list.append(wall)
        # We use crates to define boundaries of our game, and put them in the wall_list.
        # This shows using a coordinate list to place sprites
        coordinate_list = [[-230, 96], [1245, 96]]
        for coordinate in coordinate_list:
            # Add a crate on the ground
            wall = arcade.Sprite(":resources:images/tiles/boxCrate_double.png",
                                 TILE_SCALING * 0.7)
            wall.position = coordinate
            self.wall_list.append(wall)
            self.complete_sprite_list.append(wall)

        def Basic_Fruit_Movement(fruit):
            """Defining a function that condenses the fruit movement operations, input what fruit you want
            to move down the screen"""
            fruit.change_y = rdm.choice([-4, -3, -2])
            fruit.center_y = SCREEN_HEIGHT + 100
            fruit.center_x = rdm.randrange(SCREEN_WIDTH)

        # Still use this sucker movement.
        def Sucker_Movement(sucker):
            """Defining a function that has the suckers moving different than the fruits"""
            #sucker.bottom = FRUIT_SIZE * 9.75
            # fruit.left will have to by FRUIT_SIZE * an random integer --> use random here
            #sucker.left = FRUIT_SIZE * rdm.randint(0, 14)

            #sucker.boundary_right = FRUIT_SIZE
            #sucker.boundary_left = FRUIT_SIZE
            sucker.center_y = SCREEN_HEIGHT + 100
            sucker.center_x = rdm.randrange(SCREEN_WIDTH)
            sucker.change_y = rdm.choice([-5, -4, -3, -2])
            sucker.change_x = rdm.choice([-1, 0, 1])

        def Birb_Movement(birb):
            """Defining a function that has the suckers moving different than the fruits"""
            birb.center_x = -100
            birb.center_y = 400

            #birb.boundary_right = FRUIT_SIZE
            #birb.boundary_left = FRUIT_SIZE
            #birb.change_y = rdm.choice([-5, -4, -3, -2])
            #birb.change_x = rdm.choice([-1, 0, 1])

        # Instruction screen
        if self.level == 0:
            arcade.set_background_color(arcade.csscolor.CORNFLOWER_BLUE)
            self.intro_player = arcade.play_sound(self.intro_theme,
                                                  volume=music_effect_volume,
                                                  looping=True)
            self.intro_playing = True
            lvl_0 = "Our Images/Intro/Title.PNG"
            intro_coordinate_list = [[700, 550]]
            for coordinate in intro_coordinate_list:
                intro = arcade.Sprite(lvl_0, TILE_SCALING)
                intro.position = coordinate
                self.intro_list.append(intro)
                self.complete_sprite_list.append(intro)
            lvl_0_instr = "Our Images/Intro/Instructions.png"
            instruction_coordinate_list = [[700, 360]]
            for coordinate in instruction_coordinate_list:
                instructions = arcade.Sprite(lvl_0_instr, TILE_SCALING * .3)
                instructions.position = coordinate
                self.instructions_list.append(instructions)
                self.complete_sprite_list.append(instructions)
            # Placing fruit to take us into the game when collected
            fruit = "Our Images/Fruits/Cherry.png"
            cherry_coordinate_list = [[1125, 175]]
            for coordinate in cherry_coordinate_list:
                cherry_instr = arcade.Sprite(fruit, FRUIT_SCALING * 1.8)
                cherry_instr.position = coordinate
                self.cherry_list.append(cherry_instr)
                self.complete_sprite_list.append(cherry_instr)

        def level_generator(fruits,
                            tiers,
                            suckers,
                            birbs,
                            birb_level=False,
                            change_music=False,
                            background=arcade.csscolor.CORNFLOWER_BLUE):
            """As a sort of after thought, condensed a significant portion of level making operations,
            the first argument is the fruits for that level, in the order of how they will be stacked.
            The second argument is the tier list that corresponds to each fruit, third argument is for music,
             fourth argument is used to change background."""
            if change_music:
                if self.background_playing:
                    arcade.stop_sound(self.background_music_player)
                    self.background_playing = False
            arcade.set_background_color(background)
            #Setup Fruits, putting them into their fruit lists and applying fruit movement
            for fruit in range(len(fruits)):
                Basic_Fruit_Movement(fruits[fruit])
                self.revamped_fruit_list.append(fruits[fruit])
                tiers[fruit].append(fruits[fruit])
                self.complete_sprite_list.append(fruits[fruit])
            #Setup Suckers, putting them into their list and applying sucker movement
            for sucker in suckers:
                Sucker_Movement(sucker)
                self.Sucker_list.append(sucker)
                self.complete_sprite_list.append(sucker)
            if birb_level:
                for birb in birbs:
                    Birb_Movement(birb)
                    self.birds_list.append(birb)
                    self.complete_sprite_list.append(birb)

        # Better yet, lets only have one option per level, and save us
        # a lot of extra hassle!
        if self.level == 1:
            # Originially we picked random orders for each level, but this made
            # the code needlessly longer and more of a hassle.

            # lvl_1_orders= ["Our Images/Orders/Lvl1/Order1.1.PNG", "Our Images/Orders/Lvl1/Order1.2.PNG",
            # "Our Images/Orders/Lvl1/Order1.3.PNG"]
            # rdm_lvl_1_order = rdm.choice(lvl_1_orders)
            if self.intro_playing:
                arcade.stop_sound(self.intro_player)
                self.intro_playing = False
            if not self.background_playing:
                self.background_music_player = arcade.play_sound(
                    self.background_music,
                    volume=music_effect_volume,
                    looping=True)
                self.background_playing = True
            # Now, we use only one order per level.
            lvl_1_order = "Our Images/Orders/Lvl1/Order1.2.PNG"
            # Place Order:
            order_coordinate_list = [[950, 550]]
            for coordinate in order_coordinate_list:
                orders = arcade.Sprite(lvl_1_order, TILE_SCALING)
                orders.position = coordinate
                self.orders_list.append(orders)
                self.complete_sprite_list.append(orders)
                """This is the model level setup """
            level_generator(
                [self.Kiwi_coin, self.Pineapple_coin, self.Strawberry_coin], [
                    self.tier_1_fruit_list, self.tier_2_fruit_list,
                    self.tier_3_fruit_list, self.tier_4_fruit_list,
                    self.tier_5_fruit_list
                ], [self.Sucker1, self.Sucker2, self.Sucker3, self.Sucker4],
                [self.crow])

        if self.level == 2:
            lvl_2_order = "Our Images/Orders/Lvl2/Order1.2.PNG"
            # Place Order:
            order_coordinate_list = [[950, 530]]
            for coordinate in order_coordinate_list:
                orders = arcade.Sprite(lvl_2_order, TILE_SCALING)
                orders.position = coordinate
                self.orders_list.append(orders)
                self.complete_sprite_list.append(orders)
            """This is most of the level setup """
            level_generator([
                self.Watermelon_coin, self.Bannana_coin, self.Grape_coin,
                self.Cherry_coin
            ], [
                self.tier_1_fruit_list, self.tier_2_fruit_list,
                self.tier_3_fruit_list, self.tier_4_fruit_list,
                self.tier_5_fruit_list
            ], [
                self.Sucker1, self.Sucker2, self.Sucker3, self.Sucker4,
                self.Sucker5
            ], [self.crow],
                            background=arcade.csscolor.LIGHT_YELLOW)

        if self.level == 3:
            lvl_3_order = "Our Images/Orders/Lvl3/Lvl3Order1.1.PNG"
            # Place Order:
            order_coordinate_list = [[950, 510]]
            for coordinate in order_coordinate_list:
                orders = arcade.Sprite(lvl_3_order, TILE_SCALING)
                orders.position = coordinate
                self.orders_list.append(orders)
                self.complete_sprite_list.append(orders)
            self.birb_spawn_right.position = (1500, 500)
            self.complete_sprite_list.append(self.birb_spawn_right)
            self.wall_list.append(self.birb_spawn_right)
            self.birb_spawn_left.position = (10, 500)
            self.complete_sprite_list.append(self.birb_spawn_left)
            self.wall_list.append(self.birb_spawn_left)
            """This is most of the level setup """
            level_generator([
                self.Pineapple_coin, self.Apple_coin, self.Plum_coin,
                self.Pear_coin, self.Orange_coin
            ], [
                self.tier_1_fruit_list, self.tier_2_fruit_list,
                self.tier_3_fruit_list, self.tier_4_fruit_list,
                self.tier_5_fruit_list
            ], [
                self.Sucker1, self.Sucker2, self.Sucker3, self.Sucker4,
                self.Sucker5, self.Sucker6
            ], [self.crow],
                            birb_level=True,
                            background=arcade.csscolor.ORANGE)

        #End Screen
        if self.level == 4:
            if self.background_playing:
                arcade.stop_sound(self.background_music_player)
                self.background_playing = False
            self.intro_player = arcade.play_sound(self.intro_theme,
                                                  volume=music_effect_volume)
            arcade.set_background_color(arcade.csscolor.PALE_VIOLET_RED)
            # Placing everything related to the ending
            lvl_4 = "Our Images/Outro/Outro.PNG"
            ending_coordinate_list = [[500, 450]]
            for coordinate in ending_coordinate_list:
                ending = arcade.Sprite(lvl_4, TILE_SCALING * .37)
                ending.position = coordinate
                self.instructions_list.append(ending)
                self.complete_sprite_list.append(ending)
            fruit = "Our Images/Fruits/Cherry.png"
            cherry_coordinate_list = [[150, 175]]
            for coordinate in cherry_coordinate_list:
                cherry_instr = arcade.Sprite(fruit, FRUIT_SCALING * 1.8)
                cherry_instr.position = coordinate
                self.cherry_list_2.append(cherry_instr)
                self.complete_sprite_list.append(cherry_instr)
            door = "Our Images/Outro/pixel door.png"
            door_coordinate_list = [[890, 143]]
            for coordinate in door_coordinate_list:
                door_1 = arcade.Sprite(door, FRUIT_SCALING * .75)
                door_1.position = coordinate
                self.door_list.append(door_1)
                self.complete_sprite_list.append(door_1)
            self.player_sprite.set_position(500, PLAYER_START_Y)
            self.outro_list.append(self.Orange_boi)
            self.outro_list.append(self.Yellow_boi)
            self.outro_list.append(self.Blue_boi)
            self.outro_list.append(self.Purple_boi)
            self.outro_list.append(self.Red_boi)
            for item in self.outro_list:
                Basic_Fruit_Movement(item)
                self.complete_sprite_list.append(item)
        # Create the 'physics engine'
        self.physics_engine = arcade.PhysicsEnginePlatformer(
            self.player_sprite, self.wall_list)
コード例 #14
0
    def update(self, delta_time):
        """All the logic to move, and the game logic goes here. """
        global explode, explode_x, explode_y, fps, position_y_1, position_y_2, level, prompt, prompt_time, boss_hp, boss_hp_current
        global up_pressed, down_pressed, left_pressed, right_pressed, laser_bomb, laser_effect, laser_fps, laser_counter, laser_counter_update
        global boss_create_fps, boss_sound_on, game_sound_on, game_sound_1, game_sound_2, game_sound_3, boss_sound_1, boss_sound_2, boss_sound_3, game_sound, boss_sound_4

        if self.current_state != GAME_RUNNING and self.frame_count % 3480 == 0:
            try:
                arcade.play_sound(background_sound)
            except Exception as e:
                print("Error playing sound.", e)
            pass
        if self.current_state == GAME_RUNNING:
            try:
                arcade.stop_sound(background_sound)
            except Exception as e:
                print("Error pausing sound.", e)
            pass

        if level == 4:
            self.current_state = WIN
            return
        if self.current_state == GAME_RUNNING:

            if self.boss and boss_sound_on == 0:
                boss_sound_on = 1
                try:
                    if level == 0:
                        arcade.stop_sound(game_sound)
                        arcade.play_sound(boss_sound_1)
                    if level == 1:
                        game_sound_1.pause()
                        arcade.play_sound(boss_sound_2)
                    if level == 2:
                        game_sound_2.pause()
                        arcade.play_sound(boss_sound_3)
                    if level == 3:
                        game_sound_3.pause()
                        arcade.play_sound(boss_sound_4)
                except Exception as e:
                    print("Error pausing sound.", e)
                pass

            if not self.boss:
                try:
                    if level == 0:
                        boss_sound_1.pause()
                    if level == 1:
                        boss_sound_2.pause()
                    if level == 2:
                        boss_sound_3.pause()
                    if level == 3:
                        boss_sound_4.pause()

                except Exception as e:
                    print("Error pausing sound.", e)
                pass

                boss_sound_on = 0
                # if (self.frame_count - fps) == 180 and fps != 0:
                #     game_sound_on = 0

            if game_sound_on == 0:
                try:
                    if level == 0:
                        arcade.play_sound(game_sound)
                    if level == 1:
                        arcade.play_sound(game_sound_1)
                    if level == 2:
                        arcade.play_sound(game_sound_2)
                    if level == 3:
                        arcade.play_sound(game_sound_3)

                except Exception as e:
                    print("Error playing sound.", e)
                pass
                game_sound_on = 1

            # update remaining laser based on current score
            laser_counter = Score // 1000 + 1
            if laser_counter + laser_counter_update == 1:
                arcade.play_sound(missile_sound_1)
                self.laser_player += 1
                laser_counter_update -= 1

            if self.hp <= 0:
                game_sound_on = 10
                try:
                    arcade.stop_sound(game_sound)
                    # game_sound_1.pause()
                    # game_sound_2.pause()
                    # game_sound_3.pause()
                    # boss_sound_1.pause()
                    # boss_sound_2.pause()
                    # boss_sound_3.pause()
                    # boss_sound_4.pause()

                except Exception as e:
                    print("Error pausing sound.", e)

                self.dead()

            else:
                # drop hp bonus every 60s
                if self.frame_count % 3600 == 3599:
                    bonus_hp = arcade.Sprite("images/hp_bonus.png", 0.45)
                    bonus_hp.center_x = random.randrange(0, SCREEN_WIDTH)
                    bonus_hp.center_y = random.randrange(
                        SCREEN_HEIGHT, SCREEN_HEIGHT * 1.25)
                    self.bonus.append(bonus_hp)

                if self.frame_count % 240 == 0 and not self.boss and not 1 <= explode <= 4:
                    for _ in range(2 + level):
                        # generate randomly enemy planes of different levels
                        ranNum = random.randint(0, 1000)
                        if ranNum < 500:
                            enemy = Enemy("images/plane_small.png", 0.8, 2, 10,
                                          4, False)
                        elif ranNum < 850:
                            enemy = Enemy("images/bigplane0.png", 0.7, 3, 50,
                                          3, False)
                        else:
                            enemy = Enemy("images/boss0.png", 0.35, 5, 100, 2,
                                          False)

                        enemy.center_x = random.randrange(0, SCREEN_WIDTH)
                        enemy.center_y = random.randrange(
                            SCREEN_HEIGHT, SCREEN_HEIGHT * 1.25)
                        enemy.angle = 180
                        self.enemy_list.append(enemy)

                # create a boss and ensure no small enemies appear during the boss battle
                elif self.frame_count - fps == (
                        1799 *
                    (level + 1)) and not self.boss and not 1 <= explode <= 4:
                    # 提示
                    boss_create_fps = self.frame_count
                    prompt = True
                    prompt_time = self.frame_count

                    # update boss image based on game level
                    if level == 0:
                        enemy = Enemy("images/boss_2.png", 0.8, 25, 500, 2,
                                      True)
                    elif level == 1:
                        enemy = Enemy("images/boss_4.png", 0.8, 35, 1000, 3,
                                      True)
                    elif level == 2:
                        enemy = Enemy("images/boss_1.png", 0.8, 50, 2000, 3,
                                      True)
                    elif level == 3:
                        enemy = Enemy("images/boss_5.png", 0.8, 70, 4000, 3,
                                      True)

                    enemy.center_x = random.randrange(0, SCREEN_WIDTH)
                    enemy.center_y = SCREEN_HEIGHT * 2
                    enemy.angle = 180
                    self.enemy_list.append(enemy)
                    self.boss = True
                    boss_hp = enemy.ehp

                # set time for boss prompt to be 3s
                if self.frame_count - prompt_time == 180 and prompt:
                    prompt = False

                # update player's hp based on different damage levels from boss
                for boss in self.enemy_list:
                    if 1 <= laser_effect <= 6:
                        # realize the disappearance of self bullet when it hits boss
                        for e in self.bullet_self_list:
                            if boss.center_x - 20 <= e.center_x <= boss.center_x + 20:
                                e.kill()
                        # calculate different damage levels of laser from boss
                        if level == 0:
                            if self.player.center_x - 36 < boss.center_x < self.player.center_x + 36:
                                self.hp = max(0, self.hp - 0.8)
                        if level == 1:
                            if self.player.center_x - 36 < boss.center_x < self.player.center_x + 36:
                                self.hp = max(0, self.hp - 0.9)
                        if level == 2:
                            if self.player.center_x - 36 < boss.center_x - 45 < self.player.center_x + 36 or self.player.center_x - 36 < boss.center_x + 15 < self.player.center_x + 36:
                                self.hp = max(0, self.hp - 1)
                        if level == 3:
                            if self.player.center_x - 36 < boss.center_x - 45 < self.player.center_x + 36 or self.player.center_x - 36 < boss.center_x < self.player.center_x + 36 or self.player.center_x - 36 < boss.center_x + 15 < self.player.center_x + 36:
                                self.hp = max(0, self.hp - 1.1)

                # update the background position
                position_y_1 -= 1
                position_y_2 -= 1

                if position_y_1 == -300:
                    position_y_1 = 900
                if position_y_2 == -300:
                    position_y_2 = 900

                # collision with bullet
                bullet_collide_list = arcade.check_for_collision_with_list(
                    self.player, self.bullet_list)
                for collide_bullet in bullet_collide_list:
                    collide_bullet.kill()
                    self.hp = max(0, self.hp - 5)

                # collision with enemy
                enemy_collide_list = arcade.check_for_collision_with_list(
                    self.player, self.enemy_list)
                for collide_enemy in enemy_collide_list:
                    collide_enemy.kill()
                    if self.boss:
                        self.hp = 0
                    self.hp = max(0, self.hp - 30)

                # calculate different damage of player's bullet or bomb makes on enemy or boss
                for e in self.enemy_list:
                    if e.boss:
                        boss_hp_current = e.ehp
                    bullet_hit_list = arcade.check_for_collision_with_list(
                        e, self.bullet_self_list)
                    for bullet_hit in bullet_hit_list:
                        bullet_hit.kill()

                        boss_hit = e.hitted(1)
                        if boss_hit[0] == 1:
                            self.boss = False

                            explode = 1
                            explode_x = boss_hit[1]
                            explode_y = boss_hit[2]
                            fps = self.frame_count

                for bomb in self.assist:
                    bullet_hit_list = arcade.check_for_collision_with_list(
                        bomb, self.bullet_list)

                    for b in bullet_hit_list:
                        b.kill()

                for e in self.enemy_list:
                    if e.boss:
                        boss_hp_current = e.ehp
                    bullet_hit_list = arcade.check_for_collision_with_list(
                        e, self.assist)
                    for bullet_hit in bullet_hit_list:

                        boss_hit = e.hitted(0.3)
                        if boss_hit[0] == 1:
                            self.boss = False

                            explode = 1
                            explode_x = boss_hit[1]
                            explode_y = boss_hit[2]
                            fps = self.frame_count

                # boss explode animation
                if explode == 1 and self.frame_count - fps == 20:
                    arcade.play_sound(bomb_sound)
                    explode += 1
                elif explode == 2 and self.frame_count - fps == 40:
                    explode += 1
                elif explode == 3 and self.frame_count - fps == 60:
                    explode += 1
                elif explode == 4 and self.frame_count - fps == 180:
                    explode += 1
                    level += 1
                    # bomb_sound.pause()
                    game_sound_on = 0

                # use loop to make all enemies facing to the player
                for enemy in self.enemy_list:

                    # First, calculate the angle to the player. We could do this
                    # only when the bullet fires, but in this case we will rotate
                    # the enemy to face the player each frame, so we'll do this
                    # each frame.

                    # Position the start at the enemy's current location
                    start_x = enemy.center_x
                    start_y = enemy.center_y

                    # list_1[i][2]Get the destination location for the bullet
                    dest_x = self.player.center_x
                    dest_y = self.player.center_y

                    # Do math to calculate how to get the bullet to the destination.
                    # Calculation the angle in radians between the start points
                    # and end points. This is the angle the bullet will travel.
                    x_diff = dest_x - start_x
                    y_diff = dest_y - start_y
                    angle = math.atan2(y_diff, x_diff)

                    # use if statement to exclude the boss angle
                    if enemy.boss:
                        enemy.angle = 0
                    else:
                        enemy.angle = math.degrees(angle) - 270

                    # determine the shooting characteristics of enemy / boss planes
                    if enemy.boss and self.frame_count % (
                        (120 - 20 * level) // 2) == 0:
                        bullet = arcade.Sprite("images/boss_bullet.png", 0.5)
                        bullet.center_x = start_x
                        bullet.center_y = start_y
                        bullet.angle = 0
                        bullet.change_x = 0
                        bullet.change_y = -BULLET_SPEED * (level // 3 + 1)
                        self.bullet_list.append(bullet)
                    elif self.frame_count % (120 - 20 * level) == 0:
                        bullet = arcade.Sprite("images/enemy_bullet.png", 0.5)
                        bullet.center_x = start_x
                        bullet.center_y = start_y
                        bullet.angle = math.degrees(angle)
                        bullet.change_x = math.cos(angle) * BULLET_SPEED * (
                            level // 3 + 1)
                        bullet.change_y = math.sin(angle) * BULLET_SPEED * (
                            level // 3 + 1)
                        self.bullet_list.append(bullet)

                # determine the shooting frequency of the player airplane
                if self.frame_count % (15 - 2 * level) == 0:
                    bullet = arcade.Sprite("images/Bomb2.png", 0.7)
                    bullet.center_x = self.player.center_x
                    bullet.center_y = self.player.center_y

                    # Angle the bullet sprite
                    bullet.angle = 0

                    # Taking into account the angle, calculate our change_x
                    # and change_y. Velocity is how fast the bullet travels.
                    bullet.change_x = 0
                    bullet.change_y = BULLET_SPEED * 3

                    self.bullet_self_list.append(bullet)
                    # arcade.play_sound(bullet_sound)

                # use loops to remove the bullet when it flies off-screen
                for bullet in self.bullet_self_list:
                    if bullet.bottom > 600:
                        bullet.kill()

                for bullet in self.assist:
                    if bullet.bottom > 600:
                        bullet.kill()

                for bullet in self.bullet_list:
                    if bullet.top < 0:
                        bullet.kill()

                # use loops to control the dropping of hp_bonus
                for hp_bonus in self.bonus:
                    hp_bonus.center_y -= 5
                    # update player's hp when it catches hp_bonus
                    if arcade.check_for_collision(self.player, hp_bonus):
                        self.hp = min(100, self.hp + 30)
                        arcade.play_sound(hp_bonus_sound)
                        hp_bonus.kill()
                    # remove hp_bonus when it gets out of windows
                    if hp_bonus.top < 0:
                        hp_bonus.kill()

                # keyboard control the movement of the player
                if up_pressed:
                    self.player.center_y = min(552, self.player.center_y + 5)
                if down_pressed:
                    self.player.center_y = max(48, self.player.center_y - 5)
                if left_pressed:
                    self.player.center_x = max(36, self.player.center_x - 5)
                if right_pressed:
                    self.player.center_x = min(764, self.player.center_x + 5)

                # trigger the missile
                if laser_bomb and self.laser_player > 0 and len(
                        self.assist) <= 1:
                    assist_bomb = arcade.Sprite("images/assisent1_1.png", 1)
                    assist_bomb.center_x = self.player.center_x - 25
                    assist_bomb.center_y = self.player.center_y
                    assist_bomb.angle = 0
                    assist_bomb.change_x = 0
                    assist_bomb.change_y = 10
                    self.assist.append(assist_bomb)

                    assist_bomb = arcade.Sprite("images/assisent1_1.png", 1)
                    assist_bomb.center_x = self.player.center_x + 25
                    assist_bomb.center_y = self.player.center_y
                    assist_bomb.angle = 0
                    assist_bomb.change_x = 0
                    assist_bomb.change_y = 10
                    self.assist.append(assist_bomb)

                    self.laser_player -= 1

                # use if statement to set the laser shooting period to be 8s
                if self.boss and (self.frame_count -
                                  boss_create_fps) % 480 == 0 and (
                                      self.frame_count - boss_create_fps) != 0:
                    laser_effect = 1
                    laser_fps = self.frame_count

                # use if statement to animate laser
                if laser_effect == 1 and self.frame_count - laser_fps == 20:
                    laser_effect += 1
                elif laser_effect == 2 and self.frame_count - laser_fps == 40:
                    laser_effect += 1
                elif laser_effect == 3 and self.frame_count - laser_fps == 60:
                    laser_effect += 1
                elif laser_effect == 4 and self.frame_count - laser_fps == 80:
                    laser_effect += 1
                elif laser_effect == 5 and self.frame_count - laser_fps == 100:
                    laser_effect += 1
                elif laser_effect == 6 and self.frame_count - laser_fps == 120:
                    laser_effect += 1

                # realize the dropping of boss and enemy planes
                for e in self.enemy_list:
                    e.drop()

                if level == 4:
                    self.current_state = WIN
                    self.set_mouse_visible(True)

                self.bullet_list.update()
                self.bullet_self_list.update()
                self.assist.update()
        # update the frame_count
        self.frame_count += 1
コード例 #15
0
 def on_hide_view(self) -> None:
     """Called when this view is not shown anymore."""
     self.ui_manager.unregister_handlers()
     if self.background_player and self.background_player.playing:
         arcade.stop_sound(self.background_player)