Exemplo n.º 1
0
    def think(self):
        if self.player_input.is_pressed(
                self.ACT_MOVE_RIGHT) and self.velocity.len() <= self.SPEED_CAP:
            self.force.x += 6000

        if self.player_input.is_pressed(
                self.ACT_MOVE_LEFT) and self.velocity.len() <= self.SPEED_CAP:
            self.force.x -= 6000

        if self.player_input.is_pressed(
                self.ACT_MOVE_UP) and self.velocity.len() <= self.SPEED_CAP:
            self.force.y += 6000

        if self.player_input.is_pressed(
                self.ACT_MOVE_DOWN) and self.velocity.len() <= self.SPEED_CAP:
            self.force.y -= 6000

        if self.player_input.is_pressed(self.ACT_EXIT):
            logger.info('Exiting')
            entity_registry.get_by_class(World)[0].stop()

        if self.player_input.is_pressed(self.ACT_FIRE):
            self.shoot()

        self._update_rotation()
Exemplo n.º 2
0
 def render(self):
     viewports = entity_registry.get_by_class(Viewport)
     entities = entity_registry.get_by_class(DrawableEntity)
     self.clear_screen()
     for entity in sorted(filter(is_in_visible_rendering_group, entities),
                          key=lambda x: -x.z):
         for viewport in viewports:
             entity.draw(viewport)
Exemplo n.º 3
0
 def _get_collision_candidates(self, entity, entity_offset):
     entity_projection = copy(entity)
     entity_projection.position = entity.position + entity_offset
     static_entities = entity_registry.get_by_class(StaticPhysicEntity)
     other_dynamic_entities = filter(
         lambda x: x is not entity,
         entity_registry.get_by_class(DynamicPhysicEntity))
     return [
         ent for ent in static_entities + other_dynamic_entities
         if self.are_entities_collidable(entity, ent)
         and entity_projection.collides(ent)
     ]
Exemplo n.º 4
0
 def initialize(self):
     key_mapping = KeyMapping({
         FmPlayer.ACT_MOVE_RIGHT: [(Keyboard, KEYS.d)],
         FmPlayer.ACT_MOVE_LEFT: [(Keyboard, KEYS.a)],
         FmPlayer.ACT_MOVE_UP: [(Keyboard, KEYS.w)],
         FmPlayer.ACT_MOVE_DOWN: [(Keyboard, KEYS.s)],
         FmPlayer.ACT_EXIT: [(Keyboard, KEYS.esc)],
         FmPlayer.ACT_FIRE: [(Keyboard, KEYS.space), (Mouse, MBUTTONS.lmb)],
     })
     keyboard = entity_registry.get_by_class(Keyboard)[0]
     mouse = entity_registry.get_by_class(Mouse)[0]
     self._player = FmPlayer(
         PlayerInput([keyboard, mouse], key_mapping),
         get_vec_fact().vector2(1, 200),
         z=1,
         collision_category=self.COLLSION_CAT_PLAYER,
         projectile_collision_category=self.COLLSION_CAT_PLAYER_PROJECTILE)
     entity_registry.add(self._player)
     entity_registry.add(
         Wall(get_vec_fact().vector2(-1, -1),
              get_vec_fact().vector2(1000, 1000), (200, 200, 255),
              z=2,
              collision_category=self.COLLSION_CAT_BACKGROUND))
     entity_registry.add(
         DummyEnemy(get_vec_fact().vector2(300, 300),
                    1000,
                    2,
                    z=1,
                    collision_category=self.COLLSION_CAT_ENEMY,
                    color=(255, 0, 0)))
     physics = SpacecraftPhysics()
     physics.set_collidable_categories(self.COLLSION_CAT_BACKGROUND, [])
     physics.set_collidable_categories(self.COLLSION_CAT_PLAYER, [])
     physics.set_collidable_categories(self.COLLSION_CAT_PLAYER_PROJECTILE,
                                       [self.COLLSION_CAT_ENEMY])
     entity_registry.add(physics)
     entity_registry.add(ActorDriver(20))
     entity_registry.add(FpsCounter())
     screen = entity_registry.get_by_class(Screen)[0]
     screen.set_resolution(400, 500)
     renderer = entity_registry.get_by_class(Renderer2d)[0]
     viewport = renderer.create_viewport((0, 0, 400, 500), (0, 0, 400, 500))
     entity_registry.add(viewport)
     mouse.set_viewport(viewport)
     entity_registry.add(
         Hud(get_vec_fact().vector2(0, 0),
             get_vec_fact().vector2(400, 100), self._player))
     self._game_time = entity_registry.get_by_class(GameTime)[0]
Exemplo n.º 5
0
    def __init__(self,
                 frames_count,
                 animation_time,
                 speed=1.0,
                 playing=True,
                 cycle=False,
                 destroy_on_completion=False,
                 **kwargs):
        super(Animation, self).__init__(**kwargs)
        if frames_count <= 0:
            raise Exception('Invalid number of animation frames: %s' %
                            self._frames_count)

        self._speed = None
        self._frame_time = None
        self._previous_frame_time = 0
        self._current_frame = 0
        self._game_time = entity_registry.get_by_class(GameTime)[0]
        self._playing = None
        self._frames_count = frames_count
        self._animation_time = animation_time

        self.cycle = cycle
        self.destroy_on_completion = destroy_on_completion
        self.speed = speed
        self.playing = playing
Exemplo n.º 6
0
 def __init__(self, position, movement_force=1, hp=2, z=0, **kwargs):
     size = get_vec_fact().vector2(20, 10)
     super(DummyEnemy, self).__init__(position=position,
                                      z=z,
                                      size=size,
                                      placeholder_size=size,
                                      mass=10,
                                      **kwargs)
     self.movement_force = movement_force
     self.hp = hp
     self._player = entity_registry.get_by_class(FmPlayer)[0]
Exemplo n.º 7
0
    def run(self, skip_frame):
        self._reset_static_entities_collisions()
        time_delta = self._game_time.now - self.last_run
        dynamic_physic_entities = entity_registry.get_by_class(
            DynamicPhysicEntity)
        for entity in dynamic_physic_entities:
            self._update_forces(entity)
            self._update_velocity(entity, time_delta)
            collision = self._get_collision(entity, time_delta)
            self._process_collision(entity, collision, time_delta)
            entity.force.zero()

        return True
Exemplo n.º 8
0
 def __init__(self, rect, screen_rect):
     super(PygameViewport, self).__init__(rect, screen_rect)
     screen = entity_registry.get_by_class(Screen)[0].screen
     logger.debug('PygameViewport surface: %s, %s, %s, %s' % (
         screen_rect[0],
         screen.get_height() - screen_rect[1] - screen_rect[3],
         screen_rect[2],
         screen_rect[3],
     ))
     self.surface = screen.subsurface((
         screen_rect[0],
         screen.get_height() - screen_rect[1] - screen_rect[3],
         screen_rect[2],
         screen_rect[3],
     ))
Exemplo n.º 9
0
 def __init__(self,
              player_input,
              position,
              z=0,
              collision_category=None,
              projectile_collision_category=None):
     size = get_vec_fact().vector2(20, 10)
     super(FmPlayer, self).__init__(player_input=player_input,
                                    placeholder_size=size,
                                    position=position,
                                    mass=10.0,
                                    size=size,
                                    z=z,
                                    collision_category=collision_category)
     self.projectile_collision_category = projectile_collision_category
     self.fire_cooldown = 0.2
     self._last_shot_time = 0
     self._game_time = entity_registry.get_by_class(GameTime)[0]
Exemplo n.º 10
0
 def register_mouse(self):
     already_registered = len(entity_registry.get_by_class(PygameMouse)) > 0
     if not already_registered:
         entity_registry.add(PygameMouse())
Exemplo n.º 11
0
 def register_keyboard(self):
     already_registered = len(
         entity_registry.get_by_class(PygameKeyboard)) > 0
     if not already_registered:
         entity_registry.add(PygameKeyboard())
Exemplo n.º 12
0
 def _reset_static_entities_collisions(self):
     for entity in entity_registry.get_by_class(StaticPhysicEntity):
         entity.collision = None
Exemplo n.º 13
0
 def __init__(self, tolerance=0.001):
     super(PlatformerPhysics, self).__init__()
     self._game_time = entity_registry.get_by_class(GameTime)[0]
     self._tolerance = tolerance
Exemplo n.º 14
0
def initialize_subsystems():
    subsystems = entity_registry.get_by_class(Subsystem)
    for subsystem in subsystems:
        subsystem.initialize()
Exemplo n.º 15
0
    def _get_screen(self):
        if not self._screen:
            self._screen = entity_registry.get_by_class(Screen)[0]

        return self._screen
Exemplo n.º 16
0
 def clear_screen(self):
     screen = entity_registry.get_by_class(Screen)[0].screen
     screen.fill((0, 0, 0, 0))
Exemplo n.º 17
0
 def renderer(self):
     return entity_registry.get_by_class(self.RENDERER_CLASS)[0]
Exemplo n.º 18
0
 def run(self, skip_frame):
     actors = entity_registry.get_by_class(Actor)
     for actor in actors:
         actor.think()