def __init__(self): self.gravity_strength = 5.e6 self.camera.position = Vector(0, 0) self.space = self.root.add_child(SpaceNode(position=Vector(0, 0))) image = Sprite(PYTHON_IMAGE_PATH) self.attractor = self.space.add_child( BodyNode( shape=Circle(50), body_type=BodyNodeType.static, color=Color.from_int(196, 196, 196, 255) ) ) self.attractor.add_child(HitboxNode(shape=self.attractor.shape)) self.nodes = [] for _ in range(200): offset = Vector(random.randrange(1, 100), random.randrange(1, 100)) node = BodyNode( sprite=image, angular_velocity=5, velocity=Vector(30, 15), position=Vector(-512, -384) + offset, shape=Polygon.from_box(Vector(10, 10)), ) node.add_child(HitboxNode(shape=node.shape)) self.space.add_child(node) self.nodes.append(node)
def __init__(self): logger.info("Initializing scene: demo/basic.") self.camera.position = Vector(0., 0.) self.seg_node = Node() self.seg_node.shape = Segment(Vector(-2., -2.), Vector(2., 2.,)) self.circle_node = Node() self.circle_node.shape = Circle(2., Vector(2., 2.)) self.box_node = Node() self.box_node.shape = Polygon.from_box(Vector(1.5, 1.5)) logger.info("Node scene before adding to tree: %s", self.seg_node.scene) self.root.add_child(self.seg_node) self.root.add_child(self.circle_node) self.root.add_child(self.box_node) logger.info("Node scene after adding to tree: %s", self.seg_node.scene) self.virtual_resolutions_cycle = itertools.cycle( [Vector(10, 10), Vector(20, 20), Vector(5, 15), Vector(15, 5)] ) self.virtual_resolution_modes_cycle = itertools.cycle( [VirtualResolutionMode.aggresive_stretch, VirtualResolutionMode.no_stretch, VirtualResolutionMode.adaptive_stretch] ) logger.info("Displays info:") for display in self.engine.get_displays(): logger.info("* %r", display)
def __init__(self, *, speed_mod, faded=False, **kwargs): assert self.SPRITE_FRAMES assert self.TRIGGER_ID super().__init__( body_type=BodyNodeType.kinematic, position=random.choice(LANE_ENEMY_SLOTS), velocity=Vector(0, random.uniform(300, 300 + speed_mod)), angular_velocity_degrees=random.uniform(-20, 20), **kwargs, ) self._is_destroying = False self.hitbox = self.add_child( HitboxNode( trigger_id=self.TRIGGER_ID, shape=Circle(48.), # color=Color(1., 0., 0., 0.5), z_index=100, )) self.transitions_manager.set( 'animation', NodeSpriteTransition(self.SPRITE_FRAMES, loops=0, duration=len(self.SPRITE_FRAMES) * self.FRAME_DURATION), ) if faded: self.color = Color(0.5, 0.5, 0.5, 1.)
def _perform_ray_query(self, point_a, point_b): results = self.space.query_ray( point_a, point_b, ) for r in results: self.space.add_child(Node( position=r.point, shape=Circle(0.5), color=Color(1., 0., 1., 1.), lifetime=0.2, z_index=3, ))
def _perform_point_query(self): results = self.space.query_point_neighbors( self.pointer.position, max_distance=50., ) print("Point query results count: {}".format(len(results))) for r in results: self.space.add_child(Node( position=r.point, shape=Circle(0.5), color=Color(1., 1., 0., 1.), lifetime=0.2, z_index=3, ))
def __init__(self, *args, **kwargs): super().__init__(sprite=registry.global_controllers.assets_controller.grenade_launcher_bullet_img, z_index=30, body_type=BodyNodeType.kinematic, # as we want to handle collision effects on our own lifetime=5000, # will be removed from the scene automatically after 5 secs rotation_degrees=random.uniform(0, 360), # a random rotation between 0 and 360 degs *args, **kwargs) self.add_child(HitboxNode(shape=Circle(radius=6), # circular hitbox mask=HitboxMask.bullet, # we are bullet collision_mask=HitboxMask.enemy, # want to collide with objects whose mask is enemy trigger_id=settings.COLLISION_TRIGGER_GRENADE_LAUNCHER_BULLET # used when registering collision handler function ))
def __init__(self, *args, **kwargs): super().__init__( sprite=registry.global_controllers.assets_controller. force_gun_bullet_img, z_index=30, body_type=BodyNodeType.dynamic, mass=random.uniform(0.5, 8), # a random mass, lifetime= 10000, # will be removed from the scene automatically after 10 secs *args, **kwargs) self.add_child( HitboxNode(shape=Circle(radius=10), mask=HitboxMask.bullet, collision_mask=HitboxMask.all, trigger_id=settings.COLLISION_TRIGGER_FORCE_GUN_BULLET))
def __init__(self, sound_path, music_path): logger.info("Initializing scene: demo/basic.") self.camera.position = Vector(0., 0.) self.seg_node = Node() self.seg_node.shape = Segment(Vector(-2., -2.), Vector( 2., 2., )) self.circle_node = Node() self.circle_node.shape = Circle(2., Vector(2., 2.)) self.box_node = Node() self.box_node.shape = Polygon.from_box(Vector(1.5, 1.5)) print("Node scene before adding to tree: {}".format( self.seg_node.scene)) self.root.add_child(self.seg_node) self.root.add_child(self.circle_node) self.root.add_child(self.box_node) print("Node scene after adding to tree: {}".format( self.seg_node.scene)) if sound_path: self.sound = Sound(sound_path) else: self.sound = None if music_path: self.music = Music(music_path) else: self.music = None self.virtual_resolutions_cycle = itertools.cycle( [Vector(10, 10), Vector(20, 20), Vector(5, 15), Vector(15, 5)]) self.virtual_resolution_modes_cycle = itertools.cycle([ VirtualResolutionMode.aggresive_stretch, VirtualResolutionMode.no_stretch, VirtualResolutionMode.adaptive_stretch ]) print("Displays info:") for display in self.engine.get_displays(): print("* {!r}".format(display))
def _perform_shape_query(self): results = self.space.query_shape_overlaps( self.pointer.shape | Transformation(translate=self.pointer.position), collision_mask=QueryMask.clickable, ) print("Shape query results count: {}".format(len(results))) for r in results: r.hitbox.color = Color(0., 1., 1., 1.) r.body.velocity = Vector(0, 0) for cp in r.contact_points: self.space.add_child(Node( position=cp.point_b, shape=Circle(0.5), color=Color(1., 0., 0., 1.), lifetime=0.2, z_index=3, ))
def __init__(self): self.camera.position = Vector(0., 0.) self.python_img = Sprite(PYTHON_IMAGE_PATH) self.obj = self.root.add_child(Node( position=Vector(-50, 0), shape=Circle(10.), color=random_color(), )) self.obj.transitions_manager.set( 'movement', NodeTransition( Node.position, Vector(100, 30), 3., advance_method=AttributeTransitionMethod.add, loops=0, back_and_forth=True, ) )
def spawn_object(self, position=None, velocity=None): if position is None: position = Vector(random.randint(-100, 100), random.randint(-100, 100)) if velocity is None: velocity = Vector(random.gauss(0, 0.1), random.gauss(0, 0.1) * 100) * 10. obj = self.space.add_child(FlyingBall( sprite=self.python_img, body_type=BodyNodeType.dynamic, position=position, velocity=velocity, angular_velocity_degrees=random.gauss(0., 50.), )) obj.add_child(HitboxNode( shape=Circle(10.), trigger_id=COLLISION_TRIGGER, )) self.objects.append(obj)
def __init__(self): self.camera.position = Vector(0., 0.) self.python_img = Sprite(PYTHON_IMAGE_PATH) self.collisions_enabled = True self.space = self.root.add_child(SpaceNode(position=Vector(0, 0), )) self.box = self.space.add_child( BodyNode( body_type=BodyNodeType.kinematic, angular_velocity=0.3, )) # create bounding collision box with segments self.box_seg1 = self.box.add_child( HitboxNode( shape=Segment(Vector(-4, -4), Vector(-4, 4)), mask=HitboxMask.side, collision_mask=HitboxMask.all, color=Color(0.5, 0.5, 0.5), )) self.box_seg2 = self.box.add_child( HitboxNode( shape=Segment(Vector(-4, 4), Vector(4, 4)), mask=HitboxMask.side, collision_mask=HitboxMask.all, color=Color(0.5, 0.5, 0.5), )) self.box_seg3 = self.box.add_child( HitboxNode( shape=Segment(Vector(4, 4), Vector(4, -4)), mask=HitboxMask.side, collision_mask=HitboxMask.all, color=Color(0.5, 0.5, 0.5), )) self.box_seg4 = self.box.add_child( HitboxNode( shape=Segment(Vector(4, -4), Vector(-4, -4)), mask=HitboxMask.side, collision_mask=HitboxMask.all, color=Color(0.5, 0.5, 0.5), )) self.obj1 = self.space.add_child( BodyNode( mass=1e10, position=Vector(0, -2), velocity=Vector(-3.0, 0.5), # * 10, shape=Circle(0.2), sprite=self.python_img, color=Color(1., 0., 0., 1.), )) self.obj1_hitbox = self.obj1.add_child( HitboxNode( shape=Circle(0.2), mask=HitboxMask.snake, collision_mask=HitboxMask.all, trigger_id=CollisionTrigger.obj, visible=False, )) self.obj2 = self.space.add_child( BodyNode( mass=1e10, position=Vector(0, 2), velocity=Vector(4.0, -0.1), # * 10, shape=Circle(0.2), sprite=self.python_img, )) self.obj2_hitbox = self.obj2.add_child( HitboxNode( shape=Circle(0.2), mask=HitboxMask.snake, collision_mask=HitboxMask.all, trigger_id=CollisionTrigger.obj, visible=False, )) self.obj3 = self.space.add_child( BodyNode( mass=1e15, moment=1e20, position=Vector(-1.5, 0.1), velocity=Vector(-1.0, -2.0), angular_velocity_degrees=35., shape=Polygon([ Vector(-1, -1), Vector(1, -1), Vector(1, 1), Vector(-1, 1) ]), )) self.obj3_hitbox = self.obj3.add_child( HitboxNode( mask=HitboxMask.box1, collision_mask=HitboxMask.all, shape=self.obj3.shape, visible=False, elasticity=0.3, )) self.obj4 = self.space.add_child( BodyNode( position=Vector(0, -2), velocity=Vector(1.0, 5.0), angular_velocity_degrees=-5., shape=Polygon.from_box(Vector(0.5, 1.)), )) self.obj4_hitbox = self.obj4.add_child( HitboxNode( mask=HitboxMask.box2, collision_mask=HitboxMask.all, shape=self.obj4.shape, visible=False, elasticity=0.6, )) self.space.set_collision_handler(CollisionTrigger.obj, CollisionTrigger.obj, self.handle_collision)
Vector, Circle, Polygon, BoundingBox, Transformation, ) from kaa.nodes import Node from kaa.physics import SpaceNode, BodyNode, HitboxNode from kaa.timers import Timer from kaa.input import Keycode, MouseButton from kaa.colors import Color POINTER_COLOR_NORMAL = Color(1., 1., 1., 0.3) POINTER_COLOR_ACTIVE = Color(1., 1., 1., 0.7) PIECE_SHAPES = [ Polygon.from_box(Vector(10, 10)), Polygon.from_box(Vector(15, 5)), Circle(8.), Polygon([Vector(0, 0), Vector(10, 0), Vector(0, 10)]), ] POINTER_SHAPES = [ Polygon.from_box(Vector(40, 30)), Circle(10.), Circle(25.), Polygon([Vector(-35, -35), Vector(0, -40), Vector(35, -35), Vector(40, 0), Vector(35, 35)]), ] class QueryMask(enum.IntFlag): clickable = enum.auto() not_clickable = enum.auto()