def show_game_over(self): self.game_over_background.visible = True self.game_over_background.transition = NodeTransition( Node.color, Color(0, 0, 0, 0.8), duration=30000, ) self.game_over_text.transition = NodeTransition( Node.color, Color(1, 1, 1, 1), duration=3000, )
def flash(self): self.camera_shake_ticks = 15 self.flasher.transition = [ NodeTransition( Node.color, Color(1., 0., 0., 0.3), duration=200., ), NodeTransition( Node.color, Color(0., 0., 0., 0.0), duration=100., ) ]
def __init__(self): super().__init__() self.frag_count = 0 self.root.add_child( TextNode( font=registry.global_controllers.assets_controller.font_1, origin_alignment=Alignment.left, position=Vector(10, 20), font_size=40, z_index=1, text="WASD to move, mouse to rotate, left mouse button to shoot" )) self.root.add_child( TextNode(font=registry.global_controllers.assets_controller.font_1, origin_alignment=Alignment.left, position=Vector(10, 45), font_size=40, z_index=1, text="1, 2, 3 - change weapons. SPACE - spawn enemy")) self.root.add_child( TextNode(font=registry.global_controllers.assets_controller.font_2, origin_alignment=Alignment.right, position=Vector(1910, 20), font_size=30, z_index=1, color=Color(1, 0, 0, 1), text="Press Q to quit game")) self.frag_count_label = TextNode( font=registry.global_controllers.assets_controller.font_1, origin_alignment=Alignment.left, position=Vector(10, 70), font_size=40, z_index=1, color=Color(1, 1, 0, 1), text="") self.root.add_child(self.frag_count_label) self.space = SpaceNode(damping=0.3) self.root.add_child(self.space) self.player_controller = PlayerController(self) self.enemies_controller = EnemiesController(self) self.collisions_controller = CollisionsController(self) self.root.add_child( Node(sprite=registry.global_controllers.assets_controller. background_img, position=Vector(settings.VIEWPORT_WIDTH / 2, settings.VIEWPORT_HEIGHT / 2), z_index=0))
def random_color(): return Color.from_int( r=random.randint(0, 255), g=random.randint(0, 255), b=random.randint(0, 255), a=255 )
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, *, 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_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, *, 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 update_count(self, new_count: int): if new_count < self.current_count: for powerup in self.single_powerups[new_count:self.current_count]: powerup.transition = NodeTransitionsParallel([ NodeTransition(Node.scale, Vector(0., 0.), duration=400), NodeTransition(Node.color, Color(1., 1., 1., 0.), duration=400), ]) elif new_count > self.current_count: for powerup in self.single_powerups[self.current_count:new_count]: powerup.transition = NodeTransitionsParallel([ NodeTransition(Node.scale, Vector(1., 1.), duration=400), NodeTransition(Node.color, Color(1., 1., 1., 1.), duration=400), ]) self.current_count = new_count
def __init__(self, position): super().__init__( position=position, velocity=Vector(random.uniform(-20, 20), 0.), rotation_degrees=random.uniform(0, 360), scale=Vector(0.2, 0.2) + Vector.xy(math.fabs(random.gauss(0.0, 1.0)), ), lifetime=60000., # 60 seconds ) self.hitbox = self.add_child( HitboxNode(shape=random.choice(PIECE_SHAPES), )) if random.randint(0, 1): self.hitbox.color = Color(0.0, 1.0, 0.0, 1.) self.hitbox.mask = QueryMask.clickable else: self.hitbox.color = Color(1.0, 1.0, 0.0, 1.) self.hitbox.mask = QueryMask.not_clickable
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 _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): 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 handle_pickup_grab(self, pickup_node): if isinstance(pickup_node, OilRunner): if not self.is_frozen: self.is_frozen = True self.soap.transitions_manager.set( 'frozen', NodeTransitionsSequence([ NodeTransition(Node.color, Color(0.5, 0.5, 0.5), duration=400., back_and_forth=True), NodeTransitionCallback(self._on_end_frozen), ])) elif isinstance(pickup_node, MiniSoapRunner): self.player_state.soap_meter_counter.increase(10000) elif isinstance(pickup_node, LiquidSoapRunner): self.player_state.liquid_soap_powerup_counter.increase(1) elif isinstance(pickup_node, AntivirusRunner): self.player_state.antivirus_powerup_counter.increase(1)
def transitions_fun_stuff(self): rotate_transition = NodeRotationTransition(2*math.pi, duration=1000) # rotate 180 degrees (2*pi radians) scale_transition = NodeScaleTransition(Vector(2, 2), duration=1000) # enlarge twice color_transition = NodeColorTransition(Color(1, 0, 0, 1), duration=1000) # change color to red move_transition1 = NodePositionTransition(Vector(-200, 0), duration=1000, advance_method=AttributeTransitionMethod.add) move_transition2 = NodePositionTransition(Vector(200, 200), duration=1000, advance_method=AttributeTransitionMethod.add) move_transition3 = NodePositionTransition(Vector(200, -200), duration=1000, advance_method=AttributeTransitionMethod.add) move_transition4 = NodePositionTransition(Vector(-200, 0), duration=1000, advance_method=AttributeTransitionMethod.add) move_sequence = NodeTransitionsSequence([move_transition1, move_transition2, move_transition3, move_transition4], loops=0) paralel_sequence = NodeTransitionsParallel([rotate_transition, scale_transition, color_transition], back_and_forth=True, loops=0) # run both the movement sequence and rotate+scale+color sequence in paralel self.exit_label.transition = NodeTransitionsParallel([ move_sequence, paralel_sequence])
def __init__( self, position: Vector, powerup_sprite: typing.Union[Sprite, typing.List[Sprite]], max_count: int, break_count: int = 0, minor_sep: Vector = Vector(0, -30), major_sep: Vector = Vector(100, 0), ): super().__init__(position=position) self.single_powerups = [ self.add_child( Node( position=self._calculate_position(i, break_count, minor_sep, major_sep), sprite=(random.choice(powerup_sprite) if isinstance( powerup_sprite, list) else powerup_sprite), z_index=50 + i, # start hidden scale=Vector(0., 0.), color=Color(1., 1., 1., 0.), )) for i in range(max_count) ] self.current_count = 0
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): self.camera.position = Vector(0., 0.) self.python_img = Sprite(PYTHON_IMAGE_PATH) self.transition = NodeTransitionsSequence([ NodePositionTransition( Vector(-100., -100.), 3., advance_method=AttributeTransitionMethod.add, loops=3, easing=Easing.elastic_in_out), NodePositionTransition( Vector(100., 0.), 3., advance_method=AttributeTransitionMethod.add, easing=Easing.back_in_out), NodeTransitionDelay(1.5), NodeTransitionCallback(lambda node: setattr(node, 'sprite', None)), NodeTransitionsParallel([ NodePositionTransition( Vector(-50., 100.), 3., advance_method=AttributeTransitionMethod.add), NodeRotationTransition(1., 5.), NodeScaleTransition(Vector(2., 2.), 3.), NodeColorTransition(Color(0., 1., 0., 0.5), 4.), ], back_and_forth=True), ]) self.custom_transition = NodeCustomTransition( prepare_func=lambda node: { 'positions': [ Vector(random.uniform(-100, 100), random.uniform( -100, 100)) for _ in range(10) ] }, evaluate_func=lambda state, node, t: setattr( node, 'position', state['positions'][min(int(t * 10), 9)], ), duration=10., loops=5, ) spritesheet_frames = split_spritesheet( Sprite(EXPLOSION_IMAGE_PATH), Vector(64, 64), ) self.sprite_transition = NodeSpriteTransition( spritesheet_frames, 1., loops=0, back_and_forth=True, easing=Easing.quartic_out, ) self.space = self.root.add_child(SpaceNode()) self.obj = self.space.add_child( Node( position=Vector(50., 50.), sprite=self.python_img, transition=self.transition, )) self.custom_obj = self.space.add_child( Node( color=Color(1., 0., 0.), sprite=self.python_img, transition=self.custom_transition, )) self.phys_obj = self.space.add_child( BodyNode( color=Color(0., 1., 1.), position=Vector(-50., 50.), sprite=self.python_img, velocity=Vector(0., 30.), transition=BodyNodeVelocityTransition(Vector(0., -30.), 10.), )) self.animated_obj = self.space.add_child( Node( position=Vector(-20., 10.), transition=self.sprite_transition, ))
def __init__(self): self.camera.position = Vector(0., 0.) self.python_img = Sprite(PYTHON_IMAGE_PATH) self.transition = NodeTransitionsSequence([ NodePositionTransition( Vector(-100., -100.), 3000., advance_method=AttributeTransitionMethod.add, loops=3), NodePositionTransition( Vector(100., 0.), 3000., advance_method=AttributeTransitionMethod.add), NodeTransitionDelay(1500.), NodeTransitionCallback(lambda node: setattr(node, 'sprite', None)), NodeTransitionsParallel([ NodePositionTransition( Vector(-50., 100.), 3000., advance_method=AttributeTransitionMethod.add), NodeRotationTransition(1., 5000.), NodeScaleTransition(Vector(2., 2.), 3000.), NodeColorTransition(Color(0., 1., 0., 0.5), 4000.), ], back_and_forth=True), ]) self.custom_transition = NodeCustomTransition( lambda node: { 'positions': [ Vector(random.uniform(-100, 100), random.uniform( -100, 100)) for _ in range(10) ] }, lambda state, node, t: setattr( node, 'position', state['positions'][min(int(t * 10), 9)], ), 10000., loops=5, ) self.space = self.root.add_child(SpaceNode()) self.obj = self.space.add_child( Node( position=Vector(50., 50.), sprite=self.python_img, transition=self.transition, )) self.custom_obj = self.space.add_child( Node( color=Color(1., 0., 0.), sprite=self.python_img, transition=self.custom_transition, )) self.phys_obj = self.space.add_child( BodyNode( color=Color(0., 1., 1.), position=Vector(-50., 50.), sprite=self.python_img, velocity=Vector(0., 30.), transition=BodyNodeVelocityTransition(Vector(0., -30.), 10000.), ))
from kaa.engine import Engine, Scene, get_engine from kaa.geometry import Vector, Alignment from kaa.audio import Sound, SoundPlayback from kaa.fonts import TextNode, Font from kaa.colors import Color from kaa.input import Keycode FONT_PATH = os.path.join('kaacore', 'demos', 'assets', 'fonts', 'Roboto', 'Roboto-Regular.ttf') SampleEntry = namedtuple('SampleEntry', ['name', 'playback', 'ui_node', 'index']) ENTRY_INACTIVE_COLOR = Color(1., 1., 1.) ENTRY_ACTIVE_COLOR = Color(1., 0.5, 0.5) class SamplePlayerDemoScene(Scene): def __init__(self, samples_paths): self.font = Font(FONT_PATH) self.samples = [] for idx, path in enumerate(samples_paths): print("Loading... {}".format(path)) name = os.path.basename(path) playback = SoundPlayback(Sound(path)) ui_node = self.root.add_child( TextNode( font_size=5., font=self.font,
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, ))
def kill(self): self.soap.transition = NodeTransitionsParallel([ NodeTransition(Node.scale, Vector.xy(0.0), duration=1500), NodeTransition(Node.color, Color(1., 0., 0., 0.), duration=1500), ]) self.soap.lifetime = 1500
import itertools import math import enum 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)]), ]
def on_enter(self): print(f'{self.__class__.__name__} on_enter') self.engine.renderer.clear_color = Color(0.5, 0.25, 0.75, 1)
def random_color(): return Color(random.random(), random.random(), random.random())
def fade(self): self.transitions_manager.set( 'fade', NodeTransition(Node.color, Color(0.5, 0.5, 0.5, 1.), duration=1500), )