def __init__(self, *args, **kwargs): super().__init__( sprite=registry.global_controllers.assets_controller. machine_gun_bullet_img, z_index=30, body_type=BodyNodeType. kinematic, # MG bullets are kinematic bodies lifetime= 3000, # will be removed from the scene automatically after 3 secs *args, **kwargs) self.add_child( HitboxNode( shape=Polygon([ Vector(-13, -4), Vector(13, -4), Vector(13, 4), Vector(-13, 4), Vector(-13, -4) ]), mask=HitboxMask. bullet, # tell physics engine about object type collision_mask=HitboxMask. enemy, # tell physics engine which objects it can collide with trigger_id=settings.COLLISION_TRIGGER_MG_BULLET # ID to be used in custom collision handling function ))
def __init__(self, **kwargs): self.is_moving = False self.is_frozen = False self.current_lane = 2 self.soap_fuel = self.soap_fuel_max = 30000 super().__init__( body_type=BodyNodeType.kinematic, position=LANE_HERO_SLOTS[self.current_lane], scale=Vector.xy(0.7), **kwargs, ) self.hitbox = self.add_child( HitboxNode( trigger_id=CollisionTrigger.soap, shape=Polygon.from_box(Vector(138, 330)), # 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), )
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, position, hp=100, *args, **kwargs): # node's properties super().__init__(body_type=BodyNodeType.dynamic, mass=1, z_index=10, position=position, transition=NodeSpriteTransition(registry.global_controllers.assets_controller.enemy_frames, duration=max(200, random.gauss(400, 100)), loops=0), *args, **kwargs) # create a hitbox and add it as a child node to the Enemy self.add_child(HitboxNode( shape=Polygon([Vector(-8, -19), Vector(8, -19), Vector(8, 19), Vector(-8, 19), Vector(-8, -19)]), mask=HitboxMask.enemy, collision_mask=HitboxMask.all, trigger_id=settings.COLLISION_TRIGGER_ENEMY, )) # custom properties self.hp = hp self.stagger_time_left = 0 # 75% enemies will move towards player and 25% will move randomly if random.randint(0, 100) < 75: self.movement_mode = EnemyMovementMode.MoveToPlayer else: self.movement_mode = EnemyMovementMode.MoveToWaypoint self.current_waypoint = None # for those which move to a waypoint, we'll keep its corrdinates here self.randomize_new_waypoint() # and randomize new waypoint self.acceleration_per_second = 300 # how fast will enemy accelerate self.max_velocity = random.randint(75, 125) # we'll make enemy stop accelerating if velocity is above this value
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, *, root_node, camera): self.camera = camera self.flasher = root_node.add_child( Node( shape=Polygon.from_box(Vector(1400, 800)), color=Color(0., 0., 0., 0.), z_index=100, )) self.camera_shake_ticks = 0
def spawn(self): angles = list(range(0, 360, 10)) for angle in angles: self.space.add_child( TtlNode(mass=1e10, sprite=self.box_image, angular_velocity=1, ttl=random.randrange(3000, 6000), shape=Polygon.from_box(Vector(20, 20)), velocity=Vector.from_angle_degrees(angle) * 50))
def __init__(self): self.square_1 = Node(shape=Polygon.from_box(Vector(100, 100)), color=Color(1, 0, 0, 1)) self.square_2 = Node(shape=Polygon.from_box(Vector(100, 100)), color=Color(0, 1, 0, 1)) self.square_3 = Node(shape=Polygon.from_box(Vector(100, 100)), color=Color(0, 0, 1, 1)) common_trasformation = Transformation.rotate_degrees( 45) | Transformation.translate(Vector(300, 300)) self.square_1.transformation = Transformation.translate(Vector( -100, 0)) | common_trasformation self.square_2.transformation = Transformation.translate(Vector( 0, 0)) | common_trasformation self.square_3.transformation = Transformation.translate(Vector( 100, 0)) | common_trasformation self.root.add_child(self.square_1) self.root.add_child(self.square_2) self.root.add_child(self.square_3) MyNode(views={1}, position=Vector(200, 200))
def __init__(self, position, hp=100): # node's properties super().__init__(body_type=BodyNodeType.dynamic, mass=1, z_index=10, sprite=registry.global_controllers.assets_controller.player_img, position=position) # create a hitbox and add it as a child node to the Player self.add_child(HitboxNode( shape=Polygon([Vector(-10, -25), Vector(10, -25), Vector(10, 25), Vector(-10, 25), Vector(-10, -25)]), mask=HitboxMask.player, collision_mask=HitboxMask.enemy, trigger_id=settings.COLLISION_TRIGGER_PLAYER )) # custom properties self.hp = hp self.current_weapon = None self.change_weapon(WeaponType.MachineGun)
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 __init__(self): self.camera.position = Vector(0., 0.) self.space = self.root.add_child( SpaceNode(position=Vector(0, 0), gravity=Vector(0, 20), damping=0.9)) image = Sprite(PYTHON_IMAGE_PATH) random_angle = random.randrange(225, 315) self.node = self.space.add_child( BodyNode(mass=100, sprite=image, angular_velocity=10, shape=Polygon.from_box(Vector(20, 20)), velocity=Vector.from_angle_degrees(random_angle) * 100)) self.node.set_position_update_callback(update_position) self.node.set_velocity_update_callback(update_velocity)
def __init__(self): self.camera.position = Vector(0., 0.) snake_image = Sprite(PYTHON_IMAGE_PATH) minimap_view = self.views[1] minimap_view.origin = Vector(600, 0) minimap_view.dimensions = Vector(200, 600) minimap_view.camera.position = Vector.xy(0) minimap_view.camera.scale = Vector.xy(200 / 800) minimap_view.clear_color = Color(0.5, 0.5, 0.5, 1) for _ in range(100): x = random.randrange(-400, 400) y = random.randrange(-1200, 1200) self.root.add_child( Node(sprite=snake_image, views={0, 1}, position=Vector(x, y))) self.viewport = Node(color=Color(1, 0, 0, 0.1), views={1}, shape=Polygon.from_box(Vector(800, 600))) self.root.add_child(self.viewport)
def __init__(self): self.camera.position = Vector(0., 0.) box_image = Sprite(BOX_IMAGE_PATH) self.root.add_child( Node(sprite=box_image, shape=Polygon.from_box(Vector(10, 10))))
def __init__(self): self.camera.position = Vector(0., 0.) python_image = Sprite(PYTHON_IMAGE_PATH, ) self.root.add_child( Node(sprite=python_image, shape=Polygon.from_box(Vector(1, 1))))
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)
def __init__(self, player_state, root_node): self.player_state = player_state self.ui_root = root_node.add_child(Node(position=Vector(0, 240), )) self.soap_meter = self.ui_root.add_child( Node( position=Vector(320, 20), origin_alignment=Alignment.left, sprite=SPRITE_SOAP_METER, z_index=50, )) self.soap_meter_label = self.ui_root.add_child( TextNode( # scale=Vector(1.1, 1), position=Vector(320, -15), origin_alignment=Alignment.top_left, font=Font('hope_in_soap/assets/Pixeled_0.ttf'), font_size=42., text="Soap-o-meter", z_index=50, )) self.score = self.ui_root.add_child( TextNode( # scale=Vector(1.1, 1), position=Vector(-470, -15), origin_alignment=Alignment.top_left, font=Font('hope_in_soap/assets/Pixeled_0.ttf'), font_size=36., text="Score: 123", z_index=50, )) self.liquid_soap_powerup_status = self.ui_root.add_child( CounterStatusUINode( position=Vector(450, -130), powerup_sprite=SPRITE_LIQUID_SOAP, max_count=3, )) self.antivirus_powerup_status = self.ui_root.add_child( CounterStatusUINode( position=Vector(530, -130), powerup_sprite=SPRITE_ANTIVIRUS, max_count=3, )) self.people_label = self.ui_root.add_child( TextNode( # scale=Vector(1.1, 1), position=Vector(-620, -15), origin_alignment=Alignment.top_left, font=Font('hope_in_soap/assets/Pixeled_0.ttf'), font_size=36., text="People:", z_index=50, )) self.people_status = self.ui_root.add_child( CounterStatusUINode( position=Vector(-620, 10), powerup_sprite=SPRITE_FRAMES_PEOPLE, max_count=500, break_count=40, minor_sep=Vector(7, 0), major_sep=Vector(0, 12), )) self.game_over_background = root_node.add_child( Node( shape=Polygon.from_box(Vector(1400, 1000)), color=Color(0, 0, 0, 0), visible=False, z_index=150, )) self.game_over_text = self.game_over_background.add_child( TextNode( font_size=56., font=Font('hope_in_soap/assets/Pixeled_0.ttf'), text="GAME OVER", color=Color(0, 0, 0, 0), z_index=151, ))
from kaa.engine import Engine, Scene from kaa.geometry import ( 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()