예제 #1
0
class Item(arcade.Sprite):
    '''
    Item Class
    '''
    def __init__(self, filename, scale,
                 name, description,
                 set_position,
                 CAN_BE_PICKED_UP,
                 level, **kwargs):

        super().__init__(filename, scale)
        self.name = name
        self.description = description
        self.center_x, self.center_y = set_position
        self.CAN_BE_PICKED_UP = CAN_BE_PICKED_UP
        self.IN_INVENTORY = kwargs.get('IN_INVENTORY', None)
        self.Z_INDEX = 1
        self.Z_INDEX = kwargs.get('Z_INDEX', None)
        self.level = level

    def examine(self):
        return self.description

    def use(self):
        pass

    def draw(self, **kwargs):
        """ Draw the sprite. """

        if self._sprite_list is None:
            from arcade import SpriteList
            self._sprite_list = SpriteList()
            self._sprite_list.append(self)

        self._sprite_list.draw(**kwargs)
예제 #2
0
class Simulation(arcade.Window):
    def __init__(self):
        super().__init__()

        self.objects: List[Moon] = []
        self.obj_sprite: Dict[Moon, arcade.Sprite] = {}
        self.sprites = SpriteList()

        self.SCALE = 10

        self.UPS = 60
        self._last_update = 0

    def on_update(self, delta_time: float):
        self._last_update += delta_time
        if self._last_update > 1 / self.UPS:
            self._last_update = 0

            # Update sprites and viewport
            min_x, max_x = 0, 0
            min_y, max_y = 0, 0
            for obj in self.objects:
                sprite = self.obj_sprite.get(obj)

                if sprite is None:
                    sprite = self.new_sprite(obj)
                    self.obj_sprite[obj] = sprite
                    self.sprites.append(sprite)

                sprite.center_x = obj.x * self.SCALE
                sprite.center_y = obj.y * self.SCALE

                min_x = min(sprite.center_x, min_x)
                max_x = max(sprite.center_x, max_x)
                min_y = min(sprite.center_y, min_y)
                max_y = max(sprite.center_y, max_y)

            margine = 100
            self.set_viewport(min_x - margine, max_x + margine,
                              min_y - margine, max_y + margine)

    def new_sprite(self, _: Moon):
        sprite = arcade.Sprite('meteorGrey_big1.png', scale=0.3)
        sprite.angle = randint(0, 360)
        return sprite

    def on_draw(self):
        arcade.start_render()
        self.sprites.draw()
예제 #3
0
class Game(ge.Platformer):
    """
	Gaguita's Game
	"""

    title = 'Gaguita: Platformer Game'
    width = 1850
    height = 1000
    world_theme = 'brown'
    #background_color = (120, 213, 219)
    #background_near = SpriteList(use_spatial_hash=False)
    #background_fixed = SpriteList(use_spatial_hash=False)

    player_color = 'red'
    player_initial_tile = -2, 3
    player_class = Player
    viewport_margin_horizontal = 900
    viewport_margin_vertical = 0

    player_score = 0
    player_life = 3
    count_frames = 0
    jumping = False
    spike_collision = False
    enemy_collision = False

    start_sound = pyglet.media.load('gaguita/src/sounds/MattOglseby-3.wav')
    coin_sound = arcade.load_sound('gaguita/src/sounds/SFX1.wav')
    jump_sound = arcade.load_sound('gaguita/src/sounds/Sword-Swing.wav')
    life_sound = arcade.load_sound('gaguita/src/sounds/UI1.wav')
    life_lost_sound = arcade.load_sound('gaguita/src/sounds/SFX5.wav')
    enemy_killed_sound = arcade.load_sound(
        'gaguita/src/sounds/Lazer-Ricochet.wav')

    level_sound = pyglet.media.Player()
    level_sound.loop = True
    level_sound.queue(start_sound)

    def init_world(self):
        self.level_sound.play()
        """
		Gui's fase
		Part 1
		"""
        self.world_theme = 'green'

        # primeiro bloco
        self.create_arrow('right', (1, 2))
        self.create_foreground('other/plant/green-6', (3, 2))
        self.create_tower(2, 8, coords=(-3, 0))

        # torre / segundo bloco
        self.create_tower(4, 3, coords=(5, 0))
        self.create_foreground('other/plant/green-4', (6, 4))
        self.create_platform(1, coords=(7, 5))
        self.create_enemy(2, direction='y', coords=(9, 6), walk_size=4)  #enemy

        self.create_object('other/spikes/spikes-high', (8, 0),
                           sprite_list=self.spikes)  #spike
        self.create_object('other/spikes/spikes-high', (9, 0),
                           sprite_list=self.spikes)  #spike
        self.create_object('other/spikes/spikes-high', (10, 0),
                           sprite_list=self.spikes)  #spike
        self.create_object('other/spikes/spikes-high', (11, 0),
                           sprite_list=self.spikes)  #spike
        self.create_object('other/spikes/spikes-high', (12, 0),
                           sprite_list=self.spikes)  #spike
        self.create_object('other/spikes/spikes-high', (13, 0),
                           sprite_list=self.spikes)  #spike
        self.create_object('other/spikes/spikes-high', (14, 0),
                           sprite_list=self.spikes)  #spike
        self.create_object('other/spikes/spikes-high', (15, 0),
                           sprite_list=self.spikes)  #spike
        self.create_object('other/spikes/spikes-high', (16, 0),
                           sprite_list=self.spikes)  #spike

        # bloco cinzento / terceiro bloco
        self.create_block('grey', (11.5, 5.5))  #block #coin

        # bloco cinzento / quarto bloco
        self.create_block('grey', (15.5, 6.5))

        # CAMINHO DE CIMA
        # ground / quinto bloco
        self.create_ground(5, coords=(18, 7), end='round')
        self.create_platform(3, coords=(20, 9))
        self.create_foreground('other/plant/dark-4', (21, 9.89))  #plant

        # ground / sexto bloco
        self.create_foreground('other/plant/dark-6', (27, 7))  #plant
        self.create_ground(1, coords=(27, 6))

        # ground / setimo bloco
        self.create_ground(2, coords=(31, 6), end='sharp')

        self.create_object('other/items/yellowCrystal', (34, 10),
                           sprite_list=self.coins)  #coin

        # CAMINHO DE BAIXO
        # torre / quinto bloco
        self.create_tower(5, 4, coords=(17, 0))

        # ground / sexto bloco
        self.create_enemy(coords=(22, 3), walk_size=4)  #enemy
        self.create_ground(4, coords=(22, 2), end='round')

        # groun / setimo bloco
        self.create_ground(6, coords=(27, 0))
        self.create_object('other/spikes/spikes-high', (28, 1),
                           sprite_list=self.spikes)  #spike
        self.create_background('other/plant/top-read', (30, 4))
        self.create_background('other/plant/bottom-2', (30, 3))
        self.create_background('other/plant/stem-vertical', (30, 2))
        self.create_background('other/plant/leaf-1', (30, 1))

        # ground / oitavo bloco
        self.create_foreground('other/plant/dark-2', (35, 3))  #plant
        self.create_ground(1, coords=(35, 2))

        # CAMINHO COMUM
        # tower / ultimo bloco
        self.create_object('other/items/discGreen', (38, 6),
                           sprite_list=self.lifes)  #life
        self.create_tower(5, 3, coords=(37, 0))

        # Tiles
        self.create_foreground('tile/green/e2', (2, 0))
        self.create_foreground('tile/green/e3', (6, 1))
        self.create_foreground('tile/green/e3', (6, 2))

        self.create_foreground('tile/green/e2', (18, 1))
        self.create_foreground('tile/green/e2', (18, 2))
        self.create_foreground('tile/green/e3', (19, 2))
        self.create_foreground('tile/green/e2', (18, 3))

        self.create_foreground('tile/green/e2', (38, 1))
        self.create_foreground('tile/green/e2', (38, 2))
        self.create_foreground('tile/green/e2', (39, 0))
        self.create_foreground('tile/green/e2', (39, 3))

        # Fim da parte super legal, agora comeca a super chata
        """
		Italo's fase
		Part 2
		"""
        self.world_theme = 'brown'

        self.create_tower(3, 17, coords=(42, 0))
        self.create_foreground('other/plant/red-4', (43, 3))
        self.create_arrow('right', (45, 3))
        self.create_object('other/items/yellowCrystal', (45.5, 6),
                           sprite_list=self.coins)  #coin
        self.create_block('grey', (46, 5))
        self.create_foreground('other/plant/red-5', (49, 3))
        self.create_background('other/plant/top-read', (50, 6))
        self.create_background('other/plant/bottom-2', (50, 5))
        self.create_background('other/plant/stem-vertical', (50, 4))
        self.create_background('other/plant/leaf-1', (50, 3))
        self.create_background('other/plant/top-blue', (52, 7))
        self.create_background('other/plant/leaf-2', (52, 6))
        self.create_background('other/plant/leaf-1', (52, 5))
        self.create_background('other/plant/leaf-2', (52, 4))
        self.create_background('other/plant/stem-vertical', (52, 3))
        self.create_background('other/plant/top-yellow', (53, 7))
        self.create_background('other/plant/bottom-2', (53, 6))
        self.create_background('other/plant/leaf-1', (53, 5))
        self.create_background('other/plant/stem-vertical', (53, 4))
        self.create_background('other/plant/leaf-1', (53, 3))
        self.create_enemy(coords=(52, 3), walk_size=4)  #enemy
        self.create_object('other/spikes/spikes-high', (54, 3),
                           sprite_list=self.spikes)  #spike
        self.create_object('other/items/yellowCrystal', (56.5, 6),
                           sprite_list=self.coins)  #coin
        self.create_block('grey', (57, 5))
        self.create_foreground('other/plant/red-2', (57, 3))
        self.create_tower(4, 2, coords=(59, 0))
        self.create_tower(5, 3, coords=(61, 0))
        self.create_foreground('other/plant/red-6', (62, 5))

        self.create_ground(6, coords=(66, 2), end='sharp')
        self.create_enemy(coords=(66, 3), walk_size=6)  #enemy
        self.create_foreground('other/plant/red-3', (67, 3))
        self.create_object('other/spikes/spikes-high', (68, 3),
                           sprite_list=self.spikes)  #spike
        self.create_background('other/plant/top-read', (70, 6))
        self.create_background('other/plant/bottom-2', (70, 5))
        self.create_background('other/plant/leaf-1', (70, 4))
        self.create_background('other/plant/leaf-2', (70, 3))

        self.create_platform(5, coords=(74, 4))
        self.create_enemy(coords=(74, 5), walk_size=4)  #enemy
        self.create_foreground('other/plant/red-2', (76, 5))
        self.create_arrow('right', (81, 7))
        self.create_platform(1, coords=(81, 6))
        self.create_ground(10, coords=(75, 1), end='sharp')
        self.create_enemy(coords=(76, 2), walk_size=7)  #enemy
        self.create_foreground('other/plant/red-1', (77, 2))
        self.create_foreground('other/plant/red-1', (80, 2))
        self.create_background('other/plant/top-read', (81, 4))
        self.create_background('other/plant/leaf-1', (81, 3))
        self.create_background('other/plant/bottom-2', (81, 2))
        self.create_object('other/spikes/spikes-high', (82, 2),
                           sprite_list=self.spikes)  #spike
        self.create_foreground('other/plant/red-6', (83, 2))

        self.create_foreground('other/plant/red-2', (86, 7))
        self.create_platform(1, coords=(86, 6))
        self.create_enemy(2, direction='y', coords=(89, 3),
                          walk_size=6)  #enemy
        self.create_arrow('top-right', (91, 4))
        self.create_platform(1, coords=(91, 3))
        self.create_object('other/items/yellowCrystal', (96, 6),
                           sprite_list=self.coins)  #coin
        self.create_platform(3, coords=(95, 5))

        #self.create_arrow('right', (103, 9))
        #self.create_tower(9, 4, coords=(102, 0))
        self.create_foreground('other/plant/red-6', (101, 8))
        self.create_object('other/items/discGreen', (102, 8),
                           sprite_list=self.lifes)  #life
        self.create_ground(3, coords=(100, 7), end='round')
        self.create_block('grey', (100, 5))

        self.create_object('other/spikes/spikes-high', (99, 2),
                           sprite_list=self.spikes)  #spike
        self.create_foreground('other/plant/red-6', (100, 2))
        self.create_ground(5, coords=(98, 1))

        # Tiles
        self.create_foreground('tile/brown/e2', (42, 1))
        self.create_foreground('tile/brown/e2', (44, 1))
        self.create_foreground('tile/brown/e2', (46, 0))
        self.create_foreground('tile/brown/e3', (49, 1))
        self.create_foreground('tile/brown/e3', (51, 1))
        self.create_foreground('tile/brown/e3', (54, 0))
        self.create_foreground('tile/brown/e2', (58, 1))
        self.create_foreground('tile/brown/e2', (60, 1))
        self.create_foreground('tile/brown/e3', (61, 2))
        self.create_foreground('tile/brown/e2', (62, 3))
        self.create_foreground('tile/brown/e3', (63, 2))
        self.create_foreground('tile/brown/e3', (63, 1))
        """
		Gabrie's fase
		Part 3
		"""
        self.world_theme = 'blue'

        self.create_arrow('sign', (105, 7))
        self.create_foreground('other/plant/dark-2', (107, 7))
        self.create_tower(7, 4, coords=(105, 0))

        self.create_fence('middle', (109, 5))
        self.create_fence('right', (110, 5))
        self.create_foreground('other/plant/dark-6', (111, 5))
        self.create_foreground('other/plant/top-yellow', (112, 6))
        self.create_foreground('other/plant/bottom-2', (112, 5))
        self.create_tower(5, 5, coords=(108, 0))

        self.create_object('other/spikes/spikes-high', (113, 0),
                           sprite_list=self.spikes)  #spike
        self.create_object('other/spikes/spikes-high', (114, 0),
                           sprite_list=self.spikes)  #spike
        self.create_object('other/spikes/spikes-high', (115, 0),
                           sprite_list=self.spikes)  #spike
        self.create_object('other/spikes/spikes-high', (116, 0),
                           sprite_list=self.spikes)  #spike
        self.create_object('other/spikes/spikes-high', (117, 0),
                           sprite_list=self.spikes)  #spike
        self.create_object('other/spikes/spikes-high', (118, 0),
                           sprite_list=self.spikes)  #spike
        self.create_object('other/spikes/spikes-high', (119, 0),
                           sprite_list=self.spikes)  #spike
        self.create_object('other/spikes/spikes-high', (120, 0),
                           sprite_list=self.spikes)  #spike
        self.create_object('other/spikes/spikes-high', (121, 0),
                           sprite_list=self.spikes)  #spike
        self.create_object('other/spikes/spikes-high', (122, 0),
                           sprite_list=self.spikes)  #spike

        self.create_enemy(coords=(114, 4), walk_size=4)  #enemy
        self.create_foreground('other/plant/blue-6', (115, 4))
        self.create_ground(4, coords=(114, 3), end='round')

        self.create_block('grey', (116, 9))
        self.create_enemy(2, direction='y', coords=(118, 8),
                          walk_size=4)  #enemy
        self.create_block('grey', (120, 11))

        self.create_foreground('other/plant/blue-1', (120, 4))
        self.create_ground(2, coords=(120, 3), end='sharp')
        self.create_foreground('other/plant/blue-4', (124, 6))
        self.create_tower(6, 3, coords=(123, 0))

        self.create_foreground('other/plant/blue-5', (128, 7))
        self.create_tower(7, 3, coords=(127, 0))

        self.create_foreground('other/plant/blue-2', (132, 8))
        self.create_tower(8, 3, coords=(131, 0))

        self.create_arrow('top-right', (136, 10))
        self.create_object('other/items/yellowCrystal', (137, 10),
                           sprite_list=self.coins)  #coin
        self.create_platform(2, coords=(136, 9))

        self.create_enemy(coords=(140, 11), walk_size=2)  #enemy
        self.create_platform(2, coords=(140, 10))
        self.create_object('other/items/yellowCrystal', (145, 11),
                           sprite_list=self.coins)  #coin
        self.create_platform(2, coords=(144, 10))
        self.create_enemy(2, direction='y', coords=(147, 8),
                          walk_size=5)  #enemy

        self.create_ground(2, coords=(137, 6), end='sharp')
        self.create_foreground('other/plant/blue-6', (141, 5))
        self.create_ground(3, coords=(140, 4), end='round')
        self.create_ground(2, coords=(144, 2), end='sharp')

        self.create_block('grey', (150, 8))
        self.create_block('grey', (154, 6))
        self.create_background('other/plant/top-read', (150, 4))
        self.create_background('other/plant/bottom-2', (150, 3))
        self.create_background('other/plant/stem-vertical', (150, 2))
        self.create_background('other/plant/leaf-1', (150, 1))
        self.create_foreground('other/plant/blue-5', (151, 1))
        self.create_object('other/spikes/spikes-low', (153, 1),
                           sprite_list=self.spikes)  #spike
        self.create_ground(7, coords=(149, 0))

        # Tiles
        self.create_foreground('tile/blue/e2', (106, 5))
        self.create_foreground('tile/blue/e2', (106, 1))
        self.create_foreground('tile/blue/e2', (106, 2))
        self.create_foreground('tile/blue/e2', (107, 1))
        self.create_foreground('tile/blue/e3', (110, 3))
        self.create_foreground('tile/blue/e3', (111, 3))
        self.create_foreground('tile/blue/e3', (111, 2))

        self.create_foreground('tile/blue/e2', (124, 4))
        self.create_foreground('tile/blue/e2', (124, 2))
        self.create_foreground('tile/blue/e2', (128, 5))
        self.create_foreground('tile/blue/e2', (128, 2))
        self.create_foreground('tile/blue/e2', (128, 3))
        self.create_foreground('tile/blue/e2', (132, 6))
        self.create_foreground('tile/blue/e2', (132, 2))
        self.create_foreground('tile/blue/e2', (132, 3))
        self.create_foreground('tile/blue/e2', (132, 4))

    def create_enemy(self,
                     enemy_type=1,
                     direction='x',
                     coords=(0, 0),
                     walk_size=4):
        """
		Creates enemies in the world.

		Enemy type 1: Walking.
		Enemy type 2: Flying.
		"""
        enemy = arcade.AnimatedWalkingSprite()
        enemy_name = 'enemyFlying_' if enemy_type == 2 else 'enemyWalking_'
        enemy_path = 'fgarcade/data/themes/abstract/enemy/'

        x, y = coords
        bound1_x, bound1_y = x - 1, y
        bound2_x, bound2_y = x + walk_size, y

        border_path = '../../../../../fgarcade/gaguita/src/img/'
        texture_range = (2, 3, 2, 1) if enemy_type == 2 else (2, 1, 2, 1)
        speed_x = 1.3
        speed_y = 0

        if direction == 'y':
            bound1_x, bound1_y = x, y - 1
            bound2_x, bound2_y = x, y + walk_size
            speed_x = 0
            speed_y = 1.0

        # First enemy boundary
        self.create_object(border_path + 'transparent',
                           coords=(bound1_x, bound1_y),
                           role=Role.BACKGROUND,
                           sprite_list=self.enemy_limits)

        # Second enemy boundary
        self.create_object(border_path + 'transparent',
                           coords=(bound2_x, bound2_y),
                           role=Role.BACKGROUND,
                           sprite_list=self.enemy_limits)

        enemy.stand_left_textures = []
        enemy.stand_right_textures = []
        enemy.walk_left_textures = []
        enemy.walk_right_textures = []

        enemy.stand_left_textures.append(
            arcade.load_texture(enemy_path + enemy_name + "1.png",
                                scale=self.scaling,
                                mirrored=True))
        enemy.stand_right_textures.append(
            arcade.load_texture(enemy_path + enemy_name + "1.png",
                                scale=self.scaling))

        for i in texture_range:
            enemy.walk_left_textures.append(
                arcade.load_texture(enemy_path + enemy_name + str(i) + ".png",
                                    scale=self.scaling,
                                    mirrored=True))

        for i in texture_range:
            enemy.walk_right_textures.append(
                arcade.load_texture(enemy_path + enemy_name + str(i) + ".png",
                                    scale=self.scaling))

        x = int(64 * x + 32)
        y = int((64 * y) + (44 / 2))

        enemy.change_x = speed_x
        enemy.change_y = speed_y
        enemy.texture_change_distance = 20
        enemy.left = x
        enemy.bottom = y

        self.enemies.append(enemy)
        return enemy

    def init_enemies(self):
        self.enemies = SpriteList()
        self.enemy_limits = SpriteList()

    def init_items(self):
        self.coins = SpriteList()
        self.lifes = SpriteList()
        self.spikes = SpriteList()

    def init(self):
        self.init_items()
        self.init_enemies()
        self.init_world()

    def collision_with_coins(self):
        self.coins.update()
        coins_hit_list = arcade.check_for_collision_with_list(
            self.player, self.coins)

        for coin in coins_hit_list:
            coin.remove_from_sprite_lists()
            self.player_score += 1
            arcade.play_sound(self.coin_sound)

    def collision_with_lifes(self):
        self.lifes.update()
        lifes_hit_list = arcade.check_for_collision_with_list(
            self.player, self.lifes)

        for life in lifes_hit_list:
            life.remove_from_sprite_lists()
            self.player_life += 1
            arcade.play_sound(self.life_sound)

    def collision_with_spikes(self):
        self.spikes.update()
        spikes_hit_list = arcade.check_for_collision_with_list(
            self.player, self.spikes)

        if len(spikes_hit_list) > 0:
            if not self.spike_collision:
                self.player_life -= 1
                arcade.play_sound(self.life_lost_sound)
            self.spike_collision = True
        else:
            self.spike_collision = False

    def collision_with_enemies(self):
        self.enemies.update()
        enemies_hit_list = arcade.check_for_collision_with_list(
            self.player, self.enemies)

        for enemy in self.enemies:
            for border in self.enemy_limits:
                if arcade.check_for_collision(enemy, border):
                    enemy.change_x *= -1
                    enemy.change_y *= -1

        if len(enemies_hit_list) > 0:
            for enemy in enemies_hit_list:
                if self.player.change_y < -0.5 and not self.enemy_collision:
                    enemy.remove_from_sprite_lists()
                    self.player_score += 3
                    arcade.play_sound(self.enemy_killed_sound)
                else:
                    if not self.enemy_collision:
                        self.player_life -= 1
                        arcade.play_sound(self.life_lost_sound)
                    self.enemy_collision = True
        else:
            self.enemy_collision = False

    def player_info(self):
        text_x = (self.viewport_horizontal_start + self.width) - (self.width *
                                                                  0.15)
        text_y = (self.viewport_vertical_start + self.height) - (self.height *
                                                                 0.1)

        score_text = f"SCORE:  {self.player_score}"
        arcade.draw_text(
            score_text,
            text_x,
            text_y,
            arcade.csscolor.GRAY,
            45,
            font_name=('fgarcade/data/fonts/monogram_extended.ttf'))

        life_text = f"LIFE:  {self.player_life}"
        arcade.draw_text(
            life_text,
            text_x,
            text_y - 35,
            arcade.csscolor.GREY,
            45,
            font_name=('fgarcade/data/fonts/monogram_extended.ttf'))

    def on_update(self, dt):
        super().on_update(dt)

        self.enemies.update()
        self.enemies.update_animation()

        self.collision_with_coins()
        self.collision_with_lifes()
        self.collision_with_spikes()
        self.collision_with_enemies()

        if self.player.change_y > 0:
            if not self.jumping:
                arcade.play_sound(self.jump_sound)
            self.jumping = True
        else:
            self.jumping = False

        if self.player_life <= 0:
            self.game_over()

        if self.player.center_x > self.width:
            #self.next_level()
            pass

        if self.player.center_y < 10:
            self.game_over()

        if self.player.change_x == 0:
            self.count_frames += 1
            if self.count_frames >= 50:
                self.count_frames = 0
                self.game_over()

    def draw_elements(self):
        super().draw_elements()

        self.coins.draw()
        self.lifes.draw()
        self.spikes.draw()
        self.enemies.draw()
        self.enemy_limits.draw()
        self.player_info()

    def game_over(self):
        arcade.pause(1)
        arcade.close_window()
예제 #4
0
class UIManager(EventDispatcher):
    """
    Central component of :py:mod:`arcade.gui` .
    Holds :py:class:`arcade.gui.UIElement` and connects them with :py:class:`arcade.Window` callbacks.

    Basics:

    * Add :py:class:`arcade.gui.UIElement` with :py:meth:`arcade.gui.UIManager.add_ui_element()`
    * Remove all :py:class:`arcade.gui.UIElement` with :py:meth:`arcade.gui.UIManager.purge_ui_elements()`

    """
    def __init__(self, window=None, attach_callbacks=True, **kwargs):
        """
        Creates a new :py:class:`arcade.gui.UIManager` and
        registers the corresponding handlers to the current window.

        The UIManager has to be created, before
        :py:meth:`arcade.Window.show_view()`
        has been called.

        To support multiple views a singleton UIManager should be passed to all views.
        As an alternative you can remove all registered handlers of a UIManager by calling
        :py:meth:`arcade.gui.UIManager.unregister_handlers()` within :py:meth:`arcade.View.on_hide_view()`.

        :param arcade.Window window: Window to register handlers to, defaults to :py:meth:`arcade.get_window()`
        :param kwargs: catches unsupported named parameters
        """
        super().__init__()
        # TODO really needed?
        self.window: Window = window if window else arcade.get_window()

        self._focused_element: Optional[UIElement] = None
        self._hovered_element: Optional[UIElement] = None

        self._ui_elements: SpriteList = SpriteList(use_spatial_hash=True)
        self._id_cache: Dict[str, UIElement] = {}

        self.register_event_type('on_ui_event')

        if attach_callbacks:
            self.register_handlers()

    def register_handlers(self):
        """
        Registers handler functions (`on_...`) to :py:attr:`arcade.gui.UIElement`
        """
        # self.window.push_handlers(self) # Not as explicit as following
        self.window.push_handlers(
            self.on_resize,
            self.on_update,
            self.on_draw,
            self.on_mouse_press,
            self.on_mouse_release,
            self.on_mouse_scroll,
            self.on_mouse_motion,
            self.on_key_press,
            self.on_key_release,
            self.on_text,
            self.on_text_motion,
            self.on_text_motion_select,
        )

    def unregister_handlers(self):
        """
        Remove handler functions (`on_...`) from :py:attr:`arcade.Window`

        If every :py:class:`arcade.View` uses its own :py:class:`arcade.gui.UIManager`,
        this method should be called in :py:meth:`arcade.View.on_hide_view()`.
        """
        self.window.remove_handlers(
            self.on_resize,
            self.on_update,
            self.on_draw,
            self.on_mouse_press,
            self.on_mouse_release,
            self.on_mouse_scroll,
            self.on_mouse_motion,
            self.on_key_press,
            self.on_key_release,
            self.on_text,
            self.on_text_motion,
            self.on_text_motion_select,
        )

    @property
    def focused_element(self) -> Optional[UIElement]:
        """
        :return: focused UIElement, only one UIElement can be focused at a time
        """
        return self._focused_element

    @focused_element.setter
    def focused_element(self, new_focus: UIElement):
        if self._focused_element is not None:
            self._focused_element.on_unfocus()
            self._focused_element = None

        if new_focus is not None:
            new_focus.on_focus()

        self._focused_element = new_focus

    @property
    def hovered_element(self) -> Optional[UIElement]:
        """
        :return: hovered UIElement, only one UIElement can be focused at a time
        """
        return self._hovered_element

    @hovered_element.setter
    def hovered_element(self, new_hover: UIElement):
        if self._hovered_element is not None:
            self._hovered_element.on_unhover()
            self._hovered_element = None

        if new_hover is not None:
            new_hover.on_hover()

        self._hovered_element = new_hover

    def purge_ui_elements(self):
        """
        Removes all UIElements which where added to the :py:class:`arcade.gui.UIManager`.
        """
        self._ui_elements = SpriteList()
        self._id_cache = {}

    def add_ui_element(self, ui_element: UIElement):
        """
        Adds a :py:class:`arcade.gui.UIElement` to the :py:class:`arcade.gui.UIManager`.
        :py:attr:`arcade.gui.UIElement.id` has to be unique.

        The :py:class:`arcade.gui.UIElement` will be drawn by the :py:class:`arcade.gui.UIManager`.
        :param UIElement ui_element: element to add.
        """
        if not hasattr(ui_element, 'id'):
            raise UIException(
                'UIElement seems not to be properly setup, please check if you'
                ' overwrite the constructor and forgot "super().__init__(**kwargs)"'
            )

        ui_element.render()
        self._ui_elements.append(ui_element)
        ui_element.mng = self

        # Add elements with id to lookup
        if ui_element.id is not None:
            if ui_element.id in self._id_cache:
                raise UIException(f'duplicate id "{ui_element.id}"')

            self._id_cache[ui_element.id] = ui_element

    def find_by_id(self, ui_element_id: str) -> Optional[UIElement]:
        """
        Finds an :py:class:`arcade.gui.UIElement` by its ID.

        :param str ui_element_id: id of the :py:class:`arcade.gui.UIElement`
        :return: :py:class:`arcade.gui.UIElement` if available else None
        """
        return self._id_cache.get(ui_element_id)

    def on_resize(self, width, height):
        """
        Callback triggered on window resize
        """
        pass

    def on_draw(self):
        """
        Draws all added :py:class:`arcade.gui.UIElement`.
        """
        self._ui_elements.draw()

    def on_update(self, dt):
        """
        Callback triggered on update
        """
        pass

    def dispatch_ui_event(self, event: UIEvent):
        """
        Dispatches a :py:class:`arcade.gui.UIEvent` to all added :py:class:`arcade.gui.UIElement`\ s.

        :param UIEvent event: event to dispatch
        :return:
        """
        self.dispatch_event('on_ui_event', event)

    def on_ui_event(self, event: UIEvent):
        """
        Processes UIEvents, forward events to added elements and manages focused and hovered elements
        """
        for ui_element in list(self._ui_elements):
            ui_element = cast(UIElement, ui_element)

            if event.type == MOUSE_PRESS:
                if ui_element.collides_with_point(
                    (event.get('x'), event.get('y'))):
                    self.focused_element = ui_element

                elif ui_element is self.focused_element:
                    # TODO does this work like expected?
                    self.focused_element = None

            if event.type == MOUSE_MOTION:
                if ui_element.collides_with_point(
                    (event.get('x'), event.get('y'))):
                    self.hovered_element = ui_element

                elif ui_element is self.hovered_element:
                    self.hovered_element = None

            ui_element.on_ui_event(event)

    def on_mouse_press(self, x: float, y: float, button: int, modifiers: int):
        """
        Dispatches :py:meth:`arcade.View.on_mouse_press()` as :py:class:`arcade.gui.UIElement`
        with type :py:attr:`arcade.gui.MOUSE_PRESS`
        """
        self.dispatch_ui_event(
            UIEvent(MOUSE_PRESS, x=x, y=y, button=button, modifiers=modifiers))

    def on_mouse_release(self, x: float, y: float, button: int,
                         modifiers: int):
        """
        Dispatches :py:meth:`arcade.View.on_mouse_release()` as :py:class:`arcade.gui.UIElement`
        with type :py:attr:`arcade.gui.MOUSE_RELEASE`
        """
        self.dispatch_ui_event(
            UIEvent(MOUSE_RELEASE,
                    x=x,
                    y=y,
                    button=button,
                    modifiers=modifiers))

    def on_mouse_scroll(self, x: int, y: int, scroll_x: int, scroll_y: int):
        """
        Dispatches :py:meth:`arcade.View.on_mouse_scroll()` as :py:class:`arcade.gui.UIElement`
        with type :py:attr:`arcade.gui.MOUSE_SCROLL`
        """
        self.dispatch_ui_event(
            UIEvent(
                MOUSE_SCROLL,
                x=x,
                y=y,
                scroll_x=scroll_x,
                scroll_y=scroll_y,
            ))

    def on_mouse_motion(self, x: float, y: float, dx: float, dy: float):
        """
        Dispatches :py:meth:`arcade.View.on_mouse_motion()` as :py:class:`arcade.gui.UIElement`
        with type :py:attr:`arcade.gui.MOUSE_MOTION`
        """
        self.dispatch_ui_event(UIEvent(
            MOUSE_MOTION,
            x=x,
            y=y,
            dx=dx,
            dy=dy,
        ))

    def on_key_press(self, symbol: int, modifiers: int):
        """
        Dispatches :py:meth:`arcade.View.on_key_press()` as :py:class:`arcade.gui.UIElement`
        with type :py:attr:`arcade.gui.KEY_PRESS`
        """
        self.dispatch_ui_event(
            UIEvent(KEY_PRESS, symbol=symbol, modifiers=modifiers))

    def on_key_release(self, symbol: int, modifiers: int):
        """
        Dispatches :py:meth:`arcade.View.on_key_release()` as :py:class:`arcade.gui.UIElement`
        with type :py:attr:`arcade.gui.KEY_RELEASE`
        """
        self.dispatch_ui_event(
            UIEvent(KEY_RELEASE, symbol=symbol, modifiers=modifiers))

    def on_text(self, text):
        """
        Dispatches :py:meth:`arcade.View.on_text()` as :py:class:`arcade.gui.UIElement`
        with type :py:attr:`arcade.gui.TEXT_INPUT`
        """
        self.dispatch_ui_event(UIEvent(
            TEXT_INPUT,
            text=text,
        ))

    def on_text_motion(self, motion):
        """
        Dispatches :py:meth:`arcade.View.on_text_motion()` as :py:class:`arcade.gui.UIElement`
        with type :py:attr:`arcade.gui.TEXT_MOTION`
        """
        self.dispatch_ui_event(UIEvent(
            TEXT_MOTION,
            motion=motion,
        ))

    def on_text_motion_select(self, selection):
        """
        Dispatches :py:meth:`arcade.View.on_text_motion_select()` as :py:class:`arcade.gui.UIElement`
        with type :py:attr:`arcade.gui.TEXT_MOTION_SELECT`
        """
        self.dispatch_ui_event(
            UIEvent(
                TEXT_MOTION_SELECTION,
                selection=selection,
            ))
예제 #5
0
class Game(ge.Platformer):
    """
    Simple game my_level
    """

    final = 90
    SCORE = 2
    viewport_margin_horizontal = 400
    viewport_margin_vertical = 300
    LIMIT = 3
    cal = False

    # SOUNDS
    coin_sound = arcade.load_sound('examples/sounds/coin.wav')
    jump_sound = arcade.load_sound('examples/sounds/jump.wav')
    disc_sound = arcade.load_sound('examples/sounds/disc.wav')

    def init_world(self):

        # Inicio e chão
        self.create_tower(10, 2, coords=(0, 1))
        self.create_ground(self.final, coords=(0, 0), smooth_ends=True)

        # Plataforma para fazer a camera acompanhar o jogador em qualquer altura
        self.create_platform(1, coords=(20, 90))

        # Cenário
        self.moving_platform_list = SpriteList()
        platform = self.create_object('tile/blue/gl', (34, 3),
                                      at=self.moving_platform_list,
                                      role=Role.OBJECT)
        platform = self.create_object('tile/blue/g', (35, 3),
                                      at=self.moving_platform_list,
                                      role=Role.OBJECT)
        platform = self.create_object('tile/blue/gr', (36, 3),
                                      at=self.moving_platform_list,
                                      role=Role.OBJECT)

        def create_spike(tam, x, y):
            for i in range(tam):
                spike = self.create_object('other/spikes/spikes-high', (x, y),
                                           at=self.spikes)
                x += 1

        self.discs = SpriteList()

        def create_disc(x, y):
            disc = self.create_object('other/items/discGreen', (x, y),
                                      at=self.discs)

        def create_door_key(x, y):
            doorkey = self.create_object('other/door/doorRed_lock', (x, y),
                                         at=self.doorkey)

        def create_door_top(x, y):
            doortop = self.create_object('other/door/doorRed_top', (x, y),
                                         at=self.doortop)

        for pt in [(3, (6, 3)), (3, (9, 6)), (3, (12, 9)), (3, (20, 9)),
                   (1, (20, 3)), (3, (62, 5)), (3, (67, 3)), (3, (30, 6))]:
            s, l = pt
            self.create_platform(s, l)

        for pt in [(2, 3, (12, 1)), (4, 3, (14, 1)), (2, 3, (24, 1)),
                   (4, 3, (26, 1)), (2, 3, (52, 1)), (2, 3, (56, 1)),
                   (5, 3, (54, 1)), (2, 3, (75, 1)), (5, 3, (73, 1)),
                   (7, 3, (87, 1))]:
            x, y, w = pt
            self.create_tower(x, y, w)

        # Final tower
        self.create_tower(10, coords=(self.final - 1, 1))

        # Spikes
        self.spikes = SpriteList()

        create_spike(7, 17, 1)
        create_spike(23, 29, 1)
        create_spike(14, 59, 1)

        # Discs
        create_disc(63, 11)
        create_disc(21, 10)
        create_disc(20, 4)
        create_disc(40, 4)
        create_disc(40, 8)

        # Door key
        self.doorkey = SpriteList()
        create_door_key(88, 8)

        # Door top
        self.doortop = SpriteList()
        create_door_top(88, 9)

        # Foreground
        self.create_arrow('right', (3, 1))
        self.create_fence('left', (52, 3))
        self.create_fence('right', (53, 3))
        self.create_fence('left', (57, 3))
        self.create_fence('left', (58, 3))
        self.create_fence('left', (58, 3))
        self.create_fence('left', (76, 3))
        self.create_fence('right', (77, 3))

        # Background
        self.background_near = SpriteList(use_spatial_hash=False)
        self.background_fixed = SpriteList(use_spatial_hash=False)
        self.background_fixed.append(Sprite(get_sprite_path('background/bg1')))

    # Limite da Plataforma que anda
    def limit_of_platforms(self):
        self.limit_of_platform_moving = SpriteList(is_static=True)
        limit = self.create_object('other/block/brown', (30, 3),
                                   at=self.limit_of_platform_moving)
        limit = self.create_object('other/block/brown', (50, 3),
                                   at=self.limit_of_platform_moving)

    # Limite do inimigo que anda
    def limit_of_enemies(self):
        self.limit_of_enemies_moving_in_x = SpriteList(is_static=True)
        limit = self.create_object('other/block/brown', (30, 7),
                                   at=self.limit_of_enemies_moving_in_x)
        limit = self.create_object('other/block/brown', (50, 7),
                                   at=self.limit_of_enemies_moving_in_x)

    def limit_enemies(self):
        self.limit_of_enemies_moving_in_y = SpriteList(is_static=True)
        limit = self.create_object('other/block/green', (72, 10),
                                   at=self.limit_of_enemies_moving_in_y)
        limit = self.create_object('other/block/green', (72, 2),
                                   at=self.limit_of_enemies_moving_in_y)

    def init_enemies(self):
        self.enemies = SpriteList(is_static=True)
        self.moving_enemies_list_in_x = SpriteList(is_static=False)
        self.moving_enemies_list_in_y = SpriteList(is_static=False)

        def create_enemy(x, y, condition, direction_x):
            if condition:
                if direction_x:
                    enemy = self.create_object(
                        'enemy/enemySwimming_1', (x, y),
                        at=self.moving_enemies_list_in_x)
                else:
                    enemy = self.create_object(
                        'enemy/enemyFlyingAlt_1', (x, y),
                        at=self.moving_enemies_list_in_y)
            else:
                enemy = self.create_object('enemy/enemyFloating_1', (x, y),
                                           at=self.enemies)

        # Parâmetro True caso seja um Inimigo que se move
        create_enemy(35, 7, True, True)
        create_enemy(35, 10, True, True)
        create_enemy(35, 8, True, True)
        create_enemy(35, 9, True, True)
        #create_enemy(21, 10, False, False)
        create_enemy(34, 6, True, False)
        #create_enemy(40, 4, False, False)
        create_enemy(46, 6, True, False)
        create_enemy(72, 6, True, False)
        create_enemy(66, 6, True, False)
        create_enemy(18, 6, True, False)
        create_enemy(23, 6, True, False)
        create_enemy(60, 6, True, False)

    def init_items(self):
        self.coins = SpriteList()

        def create_coin(x, y, *args):
            if args:
                for i in range(args[0]):
                    coin = self.create_object('other/items/yellowGem', (x, y),
                                              at=self.coins)
                    self.coins.append(coin)
                    x += 1
            else:
                coin = self.create_object('other/items/yellowGem', (x, y),
                                          at=self.coins)
                self.coins.append(coin)

        #Coins
        create_coin(6, 4, 3)
        create_coin(9, 7, 3)
        create_coin(14, 5, 3)
        create_coin(12, 10, 3)
        create_coin(20, 10, 3)
        create_coin(24, 3, 2)
        create_coin(26, 5, 3)
        create_coin(33, 4, 1)
        create_coin(35, 4, 1)
        create_coin(39, 4, 1)
        create_coin(41, 4, 1)
        create_coin(45, 4, 1)
        create_coin(47, 4, 1)
        create_coin(54, 6, 3)
        create_coin(62, 6, 3)
        create_coin(67, 4, 3)
        create_coin(78, 1, 3)
        create_coin(81, 1, 3)
        create_coin(84, 1, 3)

        self.items = SpriteList()

    def init(self):
        self.init_world()
        self.init_items()
        self.init_enemies()
        self.limit_of_platforms()
        self.limit_of_enemies()
        self.limit_enemies()
        self.score_coins = 0
        self.cont = 0
        self.player_life = 4
        self.move_platform = 3
        self.move_enemie = 5
        self.move_enemy = 5

    def collide_coins(self, dt):
        self.coins.update()
        coins_hit_list = arcade.check_for_collision_with_list(
            self.player, self.coins)
        i = 0
        for coin in coins_hit_list:
            coin.remove_from_sprite_lists()
            i += 1
            if i == self.SCORE:
                self.score_coins += 10
                i = 0
            arcade.play_sound(self.coin_sound)

    def collide_spikes(self, dt):
        self.spikes.update()
        spikes_hit_list = arcade.check_for_collision_with_list(
            self.player, self.spikes)

        for spike in spikes_hit_list:
            self.cont += 1
            self.player_die()

    def collide_enemies(self, dt):
        self.enemies.update()
        enemies_hit_list = arcade.check_for_collision_with_list(
            self.player, self.enemies)
        moving_enemies_in_x_hit_list = arcade.check_for_collision_with_list(
            self.player, self.moving_enemies_list_in_x)
        moving_enemies_in_y_hit_list = arcade.check_for_collision_with_list(
            self.player, self.moving_enemies_list_in_y)

        for enemie in enemies_hit_list:
            self.cont += 1
            self.player_die()

        for enemie in moving_enemies_in_x_hit_list:
            self.cont += 1
            self.player_die()

        for enemie in moving_enemies_in_y_hit_list:
            self.cont += 1
            self.player_die()

    def collide_discs(self, dt):
        self.discs.update()
        discs_hit_list = arcade.check_for_collision_with_list(
            self.player, self.discs)

        for disc in discs_hit_list:
            disc.remove_from_sprite_lists()
            self.player.jump += 50
            arcade.play_sound(self.disc_sound)

    def player_die(self):
        if self.cont == self.SCORE:
            sleep(0.5)
            super().player.player_initial_tile = 4, 1
            del self.physics_engine
            self.init_items()
            self.init_enemies()
            self.init_world()
            self.score_coins = 0
            self.player_life -= 1
            self.cont = 0

    def game_over(self, dt):
        game.exit()

    def object_can_move(self, name, collision):
        check_hit = []

        for limit in name:
            hit = arcade.check_for_collision_with_list(limit, collision)
            check_hit.append(hit)

        for hit in check_hit:
            if hit:
                return True
        return False

    def move_platforms(self, dt):
        check = self.object_can_move(self.limit_of_platform_moving,
                                     self.moving_platform_list)
        cont = 3
        check_player = arcade.check_for_collision_with_list(
            self.player, self.moving_platform_list)

        if check_player or self.cal:
            self.cal = True
            for platform in self.moving_platform_list:
                if check and cont == self.LIMIT:
                    self.move_platform *= (-1)
                    cont = 0
                platform.center_x += self.move_platform
            cont = 3

    def move_enemies_in_x(self, dt):
        for enemie in self.moving_enemies_list_in_x:
            check_in_x = arcade.check_for_collision_with_list(
                enemie, self.limit_of_enemies_moving_in_x)

            if check_in_x:
                self.move_enemie *= (-1)
            enemie.center_x += self.move_enemie

    def move_enemies_in_y(self, dt):
        for enemy in self.moving_enemies_list_in_y:
            check_in_y = arcade.check_for_collision_with_list(
                enemy, self.limit_of_enemies_moving_in_y)

            if check_in_y:
                self.move_enemy *= (-1)
            enemy.center_y += self.move_enemy

    def on_update(self, dt):
        if self.player_life >= 1:
            super().on_update(dt)
            self.collide_coins(dt)
            self.collide_spikes(dt)
            self.collide_enemies(dt)
            self.move_enemies_in_x(dt)
            self.move_enemies_in_y(dt)
            self.collide_discs(dt)
            self.move_platforms(dt)
        else:
            self.game_over(dt)

        # JUMP SOUND
        if self.player.change_y > 0:
            if not self.jumping:
                arcade.play_sound(self.jump_sound)
            self.jumping = True
        else:
            self.jumping = False

    def draw_elements(self):
        super().draw_elements()
        self.enemies.draw()
        self.coins.draw()
        self.spikes.draw()
        self.moving_enemies_list_in_x.draw()
        self.moving_enemies_list_in_y.draw()
        self.moving_platform_list.draw()
        self.discs.draw()
        self.doorkey.draw()
        self.doortop.draw()

        #Placar de Contagem das moedas e vidas
        output_score = f"Score: {self.score_coins} ||"
        output_life = f"Life: {self.player_life}"
        arcade.draw_text(output_score, self.viewport_horizontal_start,
                         self.viewport_vertical_start + 10, arcade.color.BLACK,
                         25)
        arcade.draw_text(output_life, self.viewport_horizontal_start + 250,
                         self.viewport_vertical_start + 10, arcade.color.BLACK,
                         25)
예제 #6
0
class MyGame(Window):
    """ Main application class. """

    def __init__(self, width, height, title):
        """
        Initializer
        """
        super().__init__(width, height, title)

        """ Set up the game and initialize the variables. """

        # Sprite lists
        self.char_list = None
        self.shoot_list = None
        self.particule_list = None
        self.background = None
        self.sky_list = None
        self.score_list = None
        self.life_list = None
        self.ship = None

        # Set up other settings
        self.score = 0
        self.time = 0
        self.frame = 0
        self.fps = 0
        self.combo = 0
        self.life = 3
        self.music = None
        self.music_timer = 0

    def setup(self):

        self.background = SpriteList()
        self.sky_list = SpriteList()
        self.particule_list = SpriteList()
        self.ship_list = SpriteList()
        self.score_list = SpriteList()
        self.life_list = SpriteList()

        # Set up lane 1
        ship_texture = []
        for i in range(6):
            ship_texture.append(
                load_texture(f"qs../ressources/E_Run_{i+1}.png")
            )

        ship = Sprite("../ressources/E_Run_1.png",
                      center_x=SCREEN_WIDTH//10,
                      center_y=SCREEN_HEIGHT//2,
                      scale= 2)
        self.ship = ship
        self.ship_list.append(ship)

        for tier in range(3):

            background = Background("../ressources/E_Sky_1.png", SCREEN_WIDTH)
            background.center_y = ((SCREEN_HEIGHT // 3) * tier + 1) + 100
            background.center_x = SCREEN_WIDTH // 2
            background.change_x = - 2
            self.background.append(background)

            background = Background("../ressources/E_Sky_1.png", SCREEN_WIDTH)
            background.center_y = ((SCREEN_HEIGHT // 3) * tier + 1) + 100
            background.center_x = SCREEN_WIDTH + SCREEN_WIDTH // 2
            background.change_x = - 2
            self.background.append(background)

        for tier in range(3):
            sky = Background("../ressources/E_Sky_2.png", SCREEN_WIDTH)
            sky.center_y = ((SCREEN_HEIGHT // 3) * tier + 1) + 100
            sky.center_x = SCREEN_WIDTH // 2
            sky.change_x = - 6
            self.sky_list.append(sky)

            sky = Background("./ressources/E_Sky_2.png", SCREEN_WIDTH)
            sky.center_y = ((SCREEN_HEIGHT // 3) * tier + 1) + 100
            sky.center_x = SCREEN_WIDTH + SCREEN_WIDTH // 2
            sky.change_x = - 6
            self.sky_list.append(sky)

        # Set up life system
        self.life = 3
        life_pos = [SCREEN_WIDTH // 2 + 40, SCREEN_HEIGHT - 30]
        for life_sprite in range(self.life):
            self.life_list.append(Sprite("../ressources/Life_Orb.png",
                                                center_x=life_pos[0],
                                                center_y=life_pos[1]))
            life_pos[0] += 40

        # Set up Combo and Score and difficulty
        self.score_list.append(Sprite("../ressources/Score_Box.png",
                                             center_x=700,
                                             center_y=560,
                                             scale=1.2))
        self.score = 0
        self.combo = 0
        self.time = 0
        self.frame = 0
        self.fps = 0

    def on_draw(self):
        """
        Function to draw the game (on_draw)
        """
        start_render()

        # Draw all the sprites (order determine Z axis)
        self.sky_list.draw(filter=GL_NEAREST)
        self.background.draw(filter=GL_NEAREST)
        self.particule_list.draw(filter=GL_NEAREST)
        self.ship_list.draw(filter=GL_NEAREST)
        self.score_list.draw(filter=GL_NEAREST)
        self.life_list.draw(filter=GL_NEAREST)

        # Put the text on the screen.
        output = f"{self.score}"
        draw_text(output, 693, 560, color.DARK_RED, 15)
        combo = f"{self.combo}"
        draw_text(combo, 693, 542, color.DARK_RED, 15)

        # Put the fps on the bottom left
        fps = f"FPS: {self.fps}"
        draw_text(fps, 730, 10, color.YELLOW, 14)

    def on_key_press(self, key, modifiers):
        """
        Called whenever a key is pressed.
        """

        if key == arcade.key.UP:
            self.ship.change_y += 5
        elif key == arcade.key.DOWN:
            self.ship.change_y -= 5
        elif key == arcade.key.LEFT:
            self.ship.change_x -= 5
        elif key == arcade.key.RIGHT:
            self.ship.change_x += 5
        elif key == arcade.key.SPACE:
            particule = Sprite("../ressources/Life_Orb.png",
                               center_x=self.ship.right,
                               center_y=self.ship.center_y)
            particule.change_x = 3
            self.particule_list.append(particule)

    def on_key_release(self, key, modifiers):
        """
        Called whenever a key is pressed.
        """
        if key == arcade.key.UP:
            self.ship.change_y = 0
        elif key == arcade.key.DOWN:
            self.ship.change_y = 0
        elif key == arcade.key.LEFT:
            self.ship.change_x = 0
        elif key == arcade.key.RIGHT:
            self.ship.change_x = 0

    def on_update(self, delta_time):
        """ Movement and game logic """
        self.time += delta_time
        self.frame += 1
        if self.time >= 1:
            self.fps = self.frame
            self.frame = 0
            self.time = 0

        # Update Sprite_Lists
        self.sky_list.update()
        self.background.update()
        self.score_list.update()
        self.ship_list.update()
        self.particule_list.update()
        self.life_list.update()
예제 #7
0
class Player(arcade.Sprite):
    '''
    Player Class
    '''

    def __init__(self):
        # Set up parent class
        super().__init__()

        self.name = 'Player'

        self.goto_x = 0
        self.goto_y = 0

        # Default to face-right
        self.character_face_direction = RIGHT_FACING

        # Used for flipping between image sequences
        self.cur_texture = 0
        self.scale = SPRITE_SCALING

        self.Z_INDEX = 0

        # --- Load Textures ---
        # Images from Kenney.nl's Asset Pack 3
        # kenney_path = ":resources:images/animated_characters/"
        # main_path = f"{kenney_path}female_adventurer/femaleAdventurer"
        # main_path = f"{kenney_path}female_person/femalePerson"
        # main_path = f"{kenney_path}male_person/malePerson"
        # main_path = f"{kenney_path}male_adventurer/maleAdventurer"
        # main_path = f"{kenney_path}zombie/zombie"
        # main_path = f"{kenney_path}robot/robot"

        # Load textures for idle standing
        self.run_texture_pair = load_texture_pair(path['img'] / "6.png")

        # Load textures for walking
        self.run_textures = []
        for i in range(1, 15):
            texture = load_texture_pair(path['img'] / f"{i}.png")
            self.run_textures.append(texture)

        # Set the initial texture
        self.texture = self.run_texture_pair[0]

        # Hit box will be set based on the first image used.
        # If you want to specify
        # a different hit box, you can do it like the code below.
        # self.set_hit_box([[-22, -64], [22, -64], [22, 28], [-22, 28]])
        # self.set_hit_box(self.texture.hit_box_points)

    def update_animation(self, delta_time):
        # Figure out if we need to flip face left or right
        if self.change_x < 0 and self.character_face_direction == RIGHT_FACING:
            self.character_face_direction = LEFT_FACING

        elif (
                self.change_x > 0 and
                self.character_face_direction == LEFT_FACING
             ):
            self.character_face_direction = RIGHT_FACING

        # Idle animation
        if self.change_x == 0 and self.change_y == 0:
            self.texture = self.run_texture_pair[self.character_face_direction]
            return

        # Walking animation
        self.cur_texture += 1
        if self.cur_texture > 13 * UPDATES_PER_FRAME:
            self.cur_texture = 0

        frames = self.cur_texture // UPDATES_PER_FRAME

        self.texture = self.run_textures[frames][self.character_face_direction]

    def on_update(self, delta_time):
        self.center_x += self.change_x*delta_time
        self.center_y += self.change_y*delta_time

        # THIS CODE PREVENTS MOVING OFF SCREEN
        # if self.left < 50:
        #     self.left = 50
        # elif self.right > SCREEN_WIDTH - 50:
        #     self.right = SCREEN_WIDTH - 50

        if self.bottom < 150:
            self.bottom = 150
        # elif self.top > SCREEN_HEIGHT - 50:
        #     self.top = SCREEN_HEIGHT - 50

        pad = 5  # Padding to detect player close to the goto position

        if (
            self.goto_x in range(
                int(self.center_x-pad), int(self.center_x+pad)) and
            self.goto_y in range(
                int(self.bottom-pad), int(self.bottom)+pad)
        ):
            # If character near the goto position, stop the character
            self.change_x = 0
            self.change_y = 0

        if DEBUG:
            print(
                 f"goto:{self.goto_x, self.goto_y} "
                 f"center:{int(self.center_x),int(self.center_y)}\n"
                 f"x:{self.change_x} y:{self.change_y}"
                 )

    def draw(self, **kwargs):
        """ Draw the sprite. """

        if self._sprite_list is None:
            from arcade import SpriteList
            self._sprite_list = SpriteList()
            self._sprite_list.append(self)

        self._sprite_list.draw(**kwargs)
예제 #8
0
class SpriteManager(object):
    def __init__(self):
        self.player_ships = None
        self.enemy_ships = None
        self.player_base = None
        self.enemy_base = None
        self.explosions_list = None
        self.player_benefit = 0
        self.enemy_benefit = 0
        self.player_idle = 0
        self.explosion_texture = load_texture(EXPLOSION_PIC)

    def setup(self):
        self.player_ships = SpriteList()
        self.enemy_ships = SpriteList()
        self.explosions_list = SpriteList()
        self.player_base = Base(PLAYER_BASE_POSITION_X, PLAYER_BASE_POSITION_Y,
                                PLAYER_BAR_POSITION_X, PLAYER_BAR_POSITION_Y)
        self.enemy_base = Base(ENEMY_BASE_POSITION_X, ENEMY_BASE_POSITION_Y,
                               ENEMY_BAR_POSITION_X, ENEMY_BAR_POSITION_Y)

    def draw_sprites(self):
        self.explosions_list.draw()
        self.player_ships.draw()
        self.enemy_ships.draw()
        self.player_base.draw()
        self.enemy_base.draw()

    def add_ship(self, fighter):
        self.player_idle = 0
        self.player_ships.append(fighter)

    def update(self, delta_time):
        self.player_idle += delta_time
        self.explosions_list.update()
        self.player_ships.update()
        self.enemy_ships.update()
        self.player_base.update()
        self.enemy_base.update()
        self.check_ships(self.player_ships)
        self.check_ships(self.enemy_ships)
        self.check_collisions(self.player_ships, self.enemy_base)
        self.check_collisions(self.enemy_ships, self.player_base)
        self.make_fights()

    def player_fight_benefit(self) -> int:
        player_benefit = self.player_benefit
        self.player_benefit = 0
        return player_benefit

    def enemy_fight_benefit(self) -> int:
        enemy_benefit = self.enemy_benefit
        self.enemy_benefit = 0
        return enemy_benefit

    def make_fights(self):
        for player_ship in self.player_ships:
            enemy_ships = check_for_collision_with_list(
                player_ship, self.enemy_ships)
            for enemy_ship in enemy_ships:
                while player_ship.get_hp() > 0 and enemy_ship.get_hp() > 0:
                    player_ship.get_damage(player_ship.blow_damage +
                                           enemy_ship.make_damage())
                    enemy_ship.get_damage(enemy_ship.blow_damage +
                                          player_ship.make_damage())
                explosion = Explosion(self.explosion_texture)
                explosion.center_x = (player_ship.center_x +
                                      enemy_ship.center_x) / 2
                explosion.center_y = player_ship.center_y
                explosion.update()
                self.explosions_list.append(explosion)
                if player_ship.get_hp() <= 0:
                    player_ship.remove_from_sprite_lists()
                if enemy_ship.get_hp() <= 0:
                    enemy_ship.remove_from_sprite_lists()
                if enemy_ship.get_hp() < player_ship.get_hp():
                    self.player_benefit += (1.5 * MONEY_COEFFICIENT - player_ship.cost) /\
                                           MONEY_COEFFICIENT * enemy_ship.cost
                else:
                    self.enemy_benefit += (1.5 * MONEY_COEFFICIENT - enemy_ship.cost) /\
                                          MONEY_COEFFICIENT * player_ship.cost

    def check_collisions(self, ships, base: Sprite):
        for ship in ships:
            if check_for_collision(ship, base):
                base.damage(ship.make_damage())
                explosion = Explosion(self.explosion_texture)
                explosion.center_x = base.center_x
                explosion.center_y = base.center_y
                explosion.update()
                self.explosions_list.append(explosion)
                ship.remove_from_sprite_lists()

    def check_ships(self, ships):
        for ship in ships:
            if ship.get_hp() <= 0:
                explosion = Explosion(ship.dying_sprite)
                explosion.center_x = ship.center_x
                explosion.center_y = ship.center_y
                explosion.update()
                self.explosions_list.append(explosion)
                ship.remove_from_sprite_lists()

    def add_enemy_ship(self, ship):
        ship.side = 'enemy'
        ship.center_x = ENEMY_LOCATION_X
        ship.center_y = ENEMY_LOCATION_Y
        ship.change_x = -FIGHTERS_SPEED
        self.enemy_ships.append(ship)

    def game_state(self):
        if self.player_idle > PLAYER_IDLE:
            return PLAYER_IDLE_NUMBER
        diff = self.enemy_base.hp - self.player_base.hp
        return diff + self.estimate_force(
            self.enemy_ships) - self.estimate_force(self.player_ships)

    def estimate_force(self, ships):
        result = 0
        for ship in ships:
            result += ship.cost / COST_COEFFICIENT + ship.get_hp(
            ) / HP_COEFFICIENT
        return result

    def check_end_game(self):
        if self.player_base.hp <= 0:
            return 1
        elif self.enemy_base.hp <= 0:
            return 2
        return 0
예제 #9
0
class MyGame(arcade.View):
    """
    Main application class.
    """
    def __init__(self, level: int = 1):
        super().__init__()
        self.score: int = 0
        self.level = level
        # These are 'lists' that keep track of our sprites. Each sprite should
        # go into a list.
        self.wall_list = None
        self.wall_counter = 0

        self.bg_list = None
        self.answer_sprites = None
        self.sky_scraper_sprites = None

        # Separate variable that holds the player sprite
        self.player_sprite = None

        self.ui_manager = UIManager()

        # Our physics engine
        self.physics_engine = None

        # Used to keep track of our scrolling
        self.view_bottom = 0
        self.view_left = 0

        self.is_dead = False
        self.won = False
        self.sound = arcade.Sound("sound_files/dying2.mp3", True)
        # keeps track of the player sprite's location from previous frame
        self.player_last_x = 0

        # Initialize equations
        self.equations: List[Equation] = None  # First is current

        # Load sounds
        self.collect_coin_sound = arcade.load_sound(
            ":resources:sounds/coin1.wav")
        self.jump_sound = arcade.load_sound(":resources:sounds/jump1.wav")
        self.courage_sound = arcade.load_sound(
            "sound_files/courage screech.mp3")
        self.dying_sound_1 = arcade.load_sound("sound_files/dying1.mp3")
        self.dying_sound_2 = arcade.load_sound("sound_files/dying2.mp3")
        self.music1 = arcade.load_sound("sound_files/music1.mp3")

        arcade.set_background_color(arcade.csscolor.CORNFLOWER_BLUE)
        self.setup()

    def setup(self):
        self.is_dead = False
        """ Set up the game here. Call this function to restart the game. """
        self.sky_scraper_sprites = SpriteList()
        self.answer_sprites = SpriteList()

        self.equations = []
        for i in range(3):
            self.equations.append(Equation(self.level))

        # Used to keep track of our scrolling
        self.view_bottom = 0
        self.view_left = 0

        # Create the Sprite lists
        self.wall_list = arcade.SpriteList()
        self.bg_list = Background(PLAYER_MOVEMENT_SPEED, self.level)

        # Set up the player, specifically placing it at these coordinates.
        image_source = "assets-target/pixelbird2/"
        self.player_sprite = Bird(image_source, CHARACTER_SCALING)
        self.player_sprite.center_x = 250
        self.player_sprite.center_y = SCREEN_HEIGHT // 2
        self.player_sprite.change_x = PLAYER_MOVEMENT_SPEED

        # Load in walls (invisible)
        self.wall_list = SpriteList()
        wall_offset = STARTING_X_OFFSET + 830
        create_wall_list = lambda x_offset=0: [
            Sprite("stoneMid.png",
                   scale=TILE_SCALING,
                   center_x=wall_offset + x_offset,
                   center_y=SCREEN_HEIGHT // 2 - 100),
            Sprite("stoneMid.png",
                   scale=TILE_SCALING,
                   center_x=wall_offset + x_offset,
                   center_y=SCREEN_HEIGHT // 2 + 100),
        ]
        self.wall_list.extend(create_wall_list())
        self.wall_list.extend(create_wall_list(1250))

        ys = [116, 316, 516]
        values = self.equations[0].answers
        for i in range(3):
            answer = Answer(COIN_SCALING)
            answer.center_x = 920 + STARTING_X_OFFSET
            answer.center_y = ys[i]
            answer.set_number(values[i])
            self.answer_sprites.append(answer)

        # create the sky scrapers
        center_x = STARTING_X_OFFSET + 920 - 1250 * 2
        center_y = SCREEN_HEIGHT // 2
        print([e.answers for e in self.equations])
        for i in range(3):
            values = self.equations[(i - 1) % len(self.equations)].answers
            sky_scraper = SkyScraper(values, id=self.wall_counter)
            self.wall_counter += 1
            sky_scraper.center_x = center_x
            sky_scraper.center_y = center_y
            center_x = sky_scraper.move_forward()
            print("hello!")
            self.sky_scraper_sprites.append(sky_scraper)
            print("sky_scraper_sprites", len(self.sky_scraper_sprites))
            if i == 0:
                # make the first invisible temporarily so it doesn't get in the way
                sky_scraper.scale = 0

        # Create the 'physics engine'
        self.physics_engine = arcade.PhysicsEnginePlatformer(
            self.player_sprite, self.wall_list, 0)

    def on_draw(self):
        """ Render the screen. """

        # Clear the screen to the background color
        arcade.start_render()

        # Draw our sprites
        # self.wall_list.draw()  # invisible
        # self.answer_sprites.draw()  # invisible
        self.bg_list.draw()
        self.sky_scraper_sprites.draw()
        self.player_sprite.draw()
        self.draw_stats()
        w = 200
        h = 60
        arcade.draw_xywh_rectangle_filled(SCREEN_WIDTH // 2 - w // 2 +
                                          self.view_left,
                                          SCREEN_HEIGHT - h,
                                          width=w,
                                          height=h,
                                          color=arcade.color.BLACK)
        arcade.draw_text(self.equations[0].equationUnsolved(),
                         self.view_left + SCREEN_WIDTH // 2,
                         600 + self.view_bottom,
                         arcade.csscolor.WHITE,
                         18,
                         anchor_x="center")

    def on_key_press(self, key, modifiers):
        """Called whenever a key is pressed. """

        if key == arcade.key.UP or key == arcade.key.W:
            self.player_sprite.change_y = PLAYER_MOVEMENT_SPEED
        elif key == arcade.key.DOWN or key == arcade.key.S:
            self.player_sprite.change_y = -PLAYER_MOVEMENT_SPEED
        # elif key == arcade.key.LEFT:
        #     self.player_sprite.change_x = -PLAYER_MOVEMENT_SPEED
        # elif key == arcade.key.RIGHT:
        #     self.player_sprite.change_x = PLAYER_MOVEMENT_SPEED

    def on_key_release(self, key, modifiers):
        """Called when the user releases a key. """

        if key == arcade.key.UP or key == arcade.key.W:
            self.player_sprite.change_y = 0
        elif key == arcade.key.DOWN or key == arcade.key.S:
            self.player_sprite.change_y = 0
        # elif key == arcade.key.LEFT:
        #     self.player_sprite.change_x = 0
        # elif key == arcade.key.RIGHT:
        #     self.player_sprite.change_x = 0

    def on_update(self, delta_time: float):

        # --- Manage Animations ---
        self.player_sprite.on_update(delta_time)

    def update(self, delta_time):
        if self.is_dead:
            print("CANCEELED")
            return
        """ Movement and game logic """
        # Stop the player from leaving the screen
        if self.player_sprite.center_y > 600:
            self.player_sprite.change_y = 0
            self.player_sprite.center_y = 599
        elif self.player_sprite.center_y < 25:
            self.player_sprite.change_y = 0
            self.player_sprite.center_y = 26

        # record the player's last location to get their true speed
        self.player_last_x = self.player_sprite.center_x

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

        # get player's speed and update backgrounds
        player_speed = self.player_sprite.center_x - self.player_last_x
        self.bg_list.update(player_speed, self.player_sprite.center_x)

        # --- Manage Scrolling ---

        changed = False

        # 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 = 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

        if changed:
            # Only scroll to integers. Otherwise we end up with pixels that
            # don't line up on the screen
            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)

        for wall in self.wall_list:
            if wall.center_x < self.player_sprite.center_x - 500:
                wall.center_x += 1250

        # if self.won:
        #     arcade.play_sound(self.courage_sound)
        #     time.sleep(1)
        #     new_view = game_won.GameWon()
        #     self.window.show_view(new_view)
        #     if wall.center_x < self.player_sprite.center_x - 500:
        #         wall.center_x += 2500

        # manage threads
        for i in range(len(self.sky_scraper_sprites)):
            ss: SkyScraper = self.sky_scraper_sprites[i]
            if not ss.thread.is_alive() and not ss.loaded:
                ss.load_image()
        current_equation = self.equations[0]
        closest_sprite: Sprite = arcade.get_closest_sprite(
            self.player_sprite, self.answer_sprites)[0]
        if type(closest_sprite
                ) == Answer and self.player_sprite.left > closest_sprite.left:
            answer: Answer = closest_sprite

            # player hit the correct answer
            if answer.get_number() == current_equation.answer:
                self.score += 1
                # Reset the equation and answers
                self.get_new_equation()
                current_equation = self.equations[0]
            else:
                self.kill_bird()

            # move answers
            values = current_equation.answers
            for i in range(len(self.answer_sprites)):
                a: Answer = self.answer_sprites[i]
                a.center_x += 1250
                value = values[i]
                a.set_number(value)
                a.is_correct = current_equation.answer == value
            if len(self.sky_scraper_sprites) == 3:
                sprite: SkyScraper = self.sky_scraper_sprites.pop(0)
                center = (sprite.center_x, sprite.center_y)
                print("reloading", [e.answers for e in self.equations])
                new_sprite = SkyScraper(self.equations[1].answers,
                                        id=self.wall_counter)
                self.wall_counter += 1
                new_sprite.center_x = center[0]
                new_sprite.center_y = center[1]
                new_sprite.move_forward(how_many=3)
                self.sky_scraper_sprites.append(new_sprite)

        # bird death detection
        if player_speed == 0:
            print("Bird hit the wall")
            self.kill_bird()

    def kill_bird(self):
        self.is_dead = True
        print("Bird died")
        arcade.play_sound(self.dying_sound_2)
        self.tear_down(self.sky_scraper_sprites)
        self.tear_down(self.answer_sprites)
        self.tear_down(self.wall_list)
        new_view = game_over.GameOver(self, self.score)
        self.window.show_view(new_view)

    def tear_down(self, sprite_list: SpriteList):
        for i in range(len(sprite_list)):
            sprite = sprite_list.pop()
            sprite.scale = 0

    def draw_stats(self):
        start_x = SCREEN_WIDTH + self.view_left
        start_y = SCREEN_HEIGHT
        font_size = 20
        if self.score > 0:
            number_width = math.floor(math.log10(self.score) + 1) * font_size
        else:
            number_width = 1 * font_size
        arcade.draw_xywh_rectangle_filled(start_x - number_width,
                                          start_y - 100,
                                          width=100,
                                          height=100,
                                          color=arcade.color.BLACK)
        arcade.draw_text(str(self.score),
                         start_x,
                         start_y,
                         arcade.color.WHITE,
                         font_size,
                         anchor_x="right",
                         anchor_y="top")

    def get_new_equation(self):
        self.equations.pop(0)
        self.equations.append(Equation(self.level))
        print("get_new_equation", [e.answers for e in self.equations])
예제 #10
0
파일: manager.py 프로젝트: Hoyin7123/2048
class UIManager(EventDispatcher):
    def __init__(self, window: Window, *args, **kwargs):
        super().__init__()
        self.window: Window = window

        self._focused_element: Optional[UIElement] = None
        self._hovered_element: Optional[UIElement] = None

        self._ui_elements: SpriteList = SpriteList(use_spatial_hash=True)
        self._id_cache: Dict[str, UIElement] = {}

        self.register_event_type('on_ui_event')

        # self.window.push_handlers(self) # Not as explicit as following
        self.window.push_handlers(
            self.on_draw,
            self.on_mouse_press,
            self.on_mouse_release,
            self.on_mouse_scroll,
            self.on_mouse_motion,
            self.on_key_press,
            self.on_key_release,
            self.on_text,
            self.on_text_motion,
            self.on_text_motion_select,
        )

    @property
    def focused_element(self):
        return self._focused_element

    @focused_element.setter
    def focused_element(self, new_focus: UIElement):

        if self._focused_element is not None:
            self._focused_element.on_unfocus()
            self._focused_element = None

        if new_focus is not None:
            new_focus.on_focus()

        self._focused_element = new_focus

    @property
    def hovered_element(self):
        return self._hovered_element

    @hovered_element.setter
    def hovered_element(self, new_hover: UIElement):
        if self._hovered_element is not None:
            self._hovered_element.on_unhover()
            self._hovered_element = None

        if new_hover is not None:
            new_hover.on_hover()

        self._hovered_element = new_hover

    def purge_ui_elements(self):
        self._ui_elements = SpriteList()
        self._id_cache = {}

    def add_ui_element(self, ui_element: UIElement):
        if not hasattr(ui_element, 'id'):
            raise UIException(
                'UIElement seems not to be properly setup, please check if you'
                ' overwrite the constructor and forgot "super().__init__(**kwargs)"'
            )

        self._ui_elements.append(ui_element)
        ui_element.mng = self

        # Add elements with id to lookup
        if ui_element.id is not None:
            if ui_element.id in self._id_cache:
                raise UIException(f'duplicate id "{ui_element.id}"')

            self._id_cache[ui_element.id] = ui_element

    def find_by_id(self, ui_element_id: str) -> Optional[UIElement]:
        return self._id_cache.get(ui_element_id)

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

    def disptach_ui_event(self, event: UIEvent):
        self.dispatch_event('on_ui_event', event)

    def on_ui_event(self, event: UIEvent):
        """
        Processes UIEvents, forward events to registered elements and manages focused element
        """
        for ui_element in self._ui_elements:
            ui_element = cast(UIElement, ui_element)

            if event.type == MOUSE_PRESS:
                if ui_element.collides_with_point(
                    (event.get('x'), event.get('y'))):
                    self.focused_element = ui_element

                elif ui_element is self.focused_element:
                    # TODO does this work like expected?
                    self.focused_element = None

            if event.type == MOUSE_MOTION:
                if ui_element.collides_with_point(
                    (event.get('x'), event.get('y'))):
                    self.hovered_element = ui_element

                elif ui_element is self.hovered_element:
                    self.hovered_element = None

            ui_element.on_ui_event(event)

    def on_mouse_press(self, x: float, y: float, button: int, modifiers: int):
        self.disptach_ui_event(
            UIEvent(MOUSE_PRESS, x=x, y=y, button=button, modifiers=modifiers))

    def on_mouse_release(self, x: float, y: float, button: int,
                         modifiers: int):
        self.disptach_ui_event(
            UIEvent(MOUSE_RELEASE,
                    x=x,
                    y=y,
                    button=button,
                    modifiers=modifiers))

    def on_mouse_scroll(self, x: int, y: int, scroll_x: int, scroll_y: int):
        self.disptach_ui_event(
            UIEvent(
                MOUSE_SCROLL,
                x=x,
                y=y,
                scroll_x=scroll_x,
                scroll_y=scroll_y,
            ))

    def on_mouse_motion(self, x: float, y: float, dx: float, dy: float):
        self.disptach_ui_event(UIEvent(
            MOUSE_MOTION,
            x=x,
            y=y,
            dx=dx,
            dy=dy,
        ))

    def on_key_press(self, symbol: int, modifiers: int):
        self.disptach_ui_event(
            UIEvent(KEY_PRESS, symbol=symbol, modifiers=modifiers))

    def on_key_release(self, symbol: int, modifiers: int):
        self.disptach_ui_event(
            UIEvent(KEY_RELEASE, symbol=symbol, modifiers=modifiers))

    def on_text(self, text):
        self.disptach_ui_event(UIEvent(
            TEXT_INPUT,
            text=text,
        ))

    def on_text_motion(self, motion):
        self.disptach_ui_event(UIEvent(
            TEXT_MOTION,
            motion=motion,
        ))

    def on_text_motion_select(self, selection):
        self.disptach_ui_event(
            UIEvent(
                TEXT_MOTION_SELECTION,
                selection=selection,
            ))
예제 #11
0
class Game(ge.Platformer):
    """
    Simple platformer example
    """
    title = 'KiJogão'
    player_initial_tile = 4 , 1
    player_class = Player
    game_over = False
    score = 0
    life = 0
    output_timer = 0.0
    timer = 0.0
    def init_world(self):
        self.world_color = 'yellow'
        self.player_initial_position = (5, 1.5)
        self.create_tower(12, 2, coords=(0, 1))
        self.create_ground(10, coords=(0, 0), smooth_ends=False)
        self.create_platform(3, coords=(12,3))
        self.create_platform(2, coords=(17,6))
        self.create_platform(5, coords=(20,2))
        self.create_platform(2, coords=(25,5))
        self.create_platform(3, coords=(28,2))
        self.create_platform(2, coords=(32,4))
        self.create_ground(33, coords=(38, 0), smooth_ends=False)
        self.create_ground(2, coords=(42, 1))
        #inimigo
        self.create_ground(2, coords=(52, 1))
        self.create_ramp('up', 6, coords=(65, 1))
        #self.create_platform(2, coords=(69,9))
        self.create_platform(1, coords=(72,5))
        self.create_platform(10, coords=(75,2))
        self.create_ground(10, coords=(90, 0))
        #inimigo
        self.create_tower(12, coords=(100, 0))

    def init_enemies(self):
        
        self.enemies_list = SpriteList(is_static=True)
        def create_enemy(x,y):
            enemy = self.create_object('other/spikes/spikes-high',(x,y))
            self.enemies_list.append(enemy)
        create_enemy(7,1)
        create_enemy(45,1)
        create_enemy(48,1)
        create_enemy(22,2.9)
        create_enemy(54,1)
        create_enemy(57,1)
        create_enemy(60,1)
        create_enemy(63,1)
        create_enemy(77,2.9)
        create_enemy(80,2.9)
        create_enemy(83,2.9)
        for i in range(100):
            create_enemy(i,-3)
        
        
    def init_flag(self):
        self.flags_list = SpriteList(is_static=True)
        def create_flag(x,y):
            flag = self.create_object('other/flag/flagRed_up',(x,y))
            self.flags_list.append(flag)
        create_flag(94,1)
    
    def init_coins(self):
        self.coins_list = SpriteList()
        def create_coin(x,y):
            coin = self.create_object('other/items/yellowJewel',(x,y))
            self.coins_list.append(coin)
        create_coin(7,4)
        create_coin(20,8)
        create_coin(12,7)
        create_coin(23,8)
        create_coin(35,7)            
        create_coin(46,4)
        create_coin(49,5)
        create_coin(42,5)
        create_coin(50,5)
        create_coin(55,2)
        create_coin(57,4)
        create_coin(60,3)
        create_coin(63,5)   
        create_coin(75,5)
        create_coin(77,4)
        create_coin(80,7)
        create_coin(81,3)       

    def draw_elements(self):
        super().draw_elements()
        self.enemies_list.draw()
        
        posicaox = self.viewport_horizontal_start + 615
        posicaoy = self.viewport_vertical_start +540

        output = f"Pontos: {self.score}"
        arcade.draw_text(output,posicaox ,posicaoy - 20,arcade.color.BLACK,20)
        
        minutes = int(self.timer) // 60
        seconds = int(self.timer) % 60 
        self.output_timer = f"Tempo: {minutes:02d}:{seconds:02d}"
        arcade.draw_text(self.output_timer,posicaox,posicaoy,arcade.color.BLACK,20)
        
        output_life = f"Vidas: {self.life}"
        arcade.draw_text(output_life,posicaox,posicaoy + 20,arcade.color.BLACK,20)

        if self.game_over:
            output_game_over = "Game Over"
            arcade.draw_text(output_game_over,posicaox - 240,posicaoy -300,arcade.color.BLACK,20)
        
            
    def update_collision(self,dt):
        if not self.game_over:
            self.enemies_list.update()
            
            if len(check_for_collision_with_list(self.player, self.enemies_list)) > 0:
                if self.life > 0:
                    self.score = 0
                    self.life -= 1
                    super().player.player_initial_tile = 4, 1
                    super().physics_engine
                else:
                    self.game_over = True
                    self.timerend = self.output_timer
                    arcade.draw_text(self.timerend, self.viewport_horizontal_start - 240,self.viewport_vertical_start -200 ,arcade.color.BLACK,20)
                    print(self.timerend)
            if len(check_for_collision_with_list(self.player, self.flags_list)) > 0:

                self.timerend = self.output_timer 
                output_win = "You Win"
                arcade.draw_text(output_win, self.viewport_horizontal_start - 240,self.viewport_vertical_start -200 ,arcade.color.BLACK,20)
                #aparecer timer, com o tempo e as vidas o timer tambem
                arcade.draw_text(self.timerend, self.viewport_horizontal_start - 240,self.viewport_vertical_start - 200,arcade.color.BLACK,20)
                print(self.timerend)
                
                if self.life == 3 :
                    arcade.draw_text("SS", self.viewport_horizontal_start - 240,self.viewport_vertical_start - 200,arcade.color.BLACK,20)
                elif self.life == 2: 
                    arcade.draw_text("Ms", self.viewport_horizontal_start - 240,self.viewport_vertical_start - 200,arcade.color.BLACK,20)
                elif self.life <= 1:
                    arcade.draw_text("MM", self.viewport_horizontal_start - 240,self.viewport_vertical_start - 200,arcade.color.BLACK,20)
    
            hit_coin = check_for_collision_with_list(self.player, self.coins_list)
            
            for coin in hit_coin:
                coin.remove_from_sprite_lists()
                self.score += 1
                if self.score % 5 == 0:
                    self.life += 1
       
        if self.game_over:
            self.draw_elements()


    def update(self, dt):
        super().update(dt)
        self.update_collision(dt)   
        self.timer += dt

    def init(self):
        self.init_world()
        self.init_flag()
        self.init_enemies()
        self.init_coins()
예제 #12
0
class Canvas(arcade.Window):
    def __init__(self, world_state: Dict[Vector, int], queue: Queue):
        super().__init__()

        self.world_state = world_state
        self.key_queue = queue

        self._sprites = SpriteList()
        self._obj_sprite = dict()

        self.score = 0
        self.SCALE = 64

        # shoul_update
        self.UPS = 60
        self._last_update = 0

    def create_new_sprite(self, obj):
        sprite = Sprite('wall.png', scale=0.5)
        sprite.append_texture(load_texture('box.png', scale=0.5))
        sprite.append_texture(load_texture('paddle.png', scale=2))
        sprite.append_texture(load_texture('ball.png', scale=0.5))
        return sprite

    def on_key_press(self, symbol: int, modifiers: int):
        if symbol == arcade.key.LEFT:
            self.key_queue.put(-1)
        elif symbol == arcade.key.RIGHT:
            self.key_queue.put(1)
        elif symbol == arcade.key.DOWN:
            self.key_queue.put(0)
        elif symbol == arcade.key.SPACE:
            # auto play
            self.auto_turn()

    def auto_turn(self):
        ball = None
        paddle = None
        for vec, kind in self.world_state.items():
            if kind == 4:
                ball = vec
            if kind == 3:
                paddle = vec

            if None not in (ball, paddle):
                break
        else:
            self.key_queue.put(0)
            return

        if paddle.x < ball.x:
            self.key_queue.put(1)
        elif paddle.x > ball.x:
            self.key_queue.put(-1)
        else:
            self.key_queue.put(0)

    def on_update(self, delta_time: float):
        if self.should_update(delta_time):
            self.auto_turn()

        # Update sprites and viewport
        for vec, kind in list(self.world_state.items()):
            sprite = self._obj_sprite.get(vec)

            if sprite is None:
                sprite = self.create_new_sprite(vec)
                self._obj_sprite[vec] = sprite
                self._sprites.append(sprite)

            if kind == 0:
                sprite.alpha = 0
            else:
                sprite.alpha = 255
                if kind == 1:
                    sprite.set_texture(0)
                elif kind == 2:
                    sprite.set_texture(1)
                elif kind == 3:
                    sprite.set_texture(2)
                elif kind == 4:
                    sprite.set_texture(3)

            sprite.center_x = vec.x * self.SCALE
            sprite.center_y = vec.y * self.SCALE

        self.apply_margine()

    def should_update(self, dt):
        self._last_update += dt
        if self._last_update > 1 / self.UPS:
            self._last_update = 0
            return True
        else:
            return False

    def apply_margine(self):
        min_x, max_x = 0, 0
        min_y, max_y = 0, 0
        for sprite in self._sprites:
            min_x = min(sprite.center_x, min_x)
            max_x = max(sprite.center_x, max_x)
            min_y = min(sprite.center_y, min_y)
            max_y = max(sprite.center_y, max_y)
        margine = 100
        self.set_viewport(min_x - margine, max_x + margine, max_y + margine, min_y - margine)

    def on_draw(self):
        arcade.start_render()
        self._sprites.draw()