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 apply_explosion_effects(self, explosion_center, damage_at_center=100, blast_radius=200, pushback_force_at_center=500, pushback_radius=300): # play explosion sound registry.global_controllers.assets_controller.explosion_sound.play() enemies_to_remove = [] for enemy in self.enemies: # get the distance to the explosion distance_to_explosion = enemy.position.distance(explosion_center) # if within pushback radius... if distance_to_explosion <= pushback_radius: # calculate pushback value, the further from the center, the smaller it is pushback_force_val = pushback_force_at_center * ( 1 - (distance_to_explosion / pushback_radius)) # apply the pushback force by resetting enemy velocity enemy.velocity = (enemy.position - explosion_center ).normalize() * pushback_force_val # if within blast radius... if distance_to_explosion <= blast_radius: # calculate damage, the further from the center, the smaller it is damage = damage_at_center * ( 1 - (distance_to_explosion / blast_radius)) # apply damage enemy.hp -= int(damage) # add the blood splatter animation over the enemy self.scene.root.add_child( Node(z_index=900, transition=NodeSpriteTransition( registry.global_controllers.assets_controller. blood_splatter_frames, duration=140), position=enemy.position, rotation=(enemy.position - explosion_center).to_angle() + math.pi, lifetime=140)) if enemy.hp < 0: # IZ DED! # show the death animation (pick random sprite from few animations we have loaded from one png file) self.scene.root.add_child( Node(z_index=1, transition=NodeSpriteTransition(random.choice( registry.global_controllers.assets_controller. enemy_death_frames), duration=450), position=enemy.position, rotation=enemy.rotation, origin_alignment=Alignment.right, lifetime=random.randint(10000, 20000))) # mark enemy for removal: enemies_to_remove.append(enemy) # removed killed enemies for dead_enemy in enemies_to_remove: self.remove_enemy(dead_enemy)
def on_collision_mg_bullet_enemy(self, arbiter, mg_bullet_pair, enemy_pair): print( "Detected a collision between MG bullet object {} hitbox {} and Enemy object {} hitbox {}" .format(mg_bullet_pair.body, mg_bullet_pair.hitbox, enemy_pair.body, enemy_pair.hitbox)) if arbiter.phase == CollisionPhase.begin: enemy = enemy_pair.body enemy.hp -= 10 # add the blood splatter animation to the scene self.scene.root.add_child( Node(z_index=900, transition=NodeSpriteTransition( registry.global_controllers.assets_controller. blood_splatter_frames, duration=140), position=enemy.position, rotation=mg_bullet_pair.body.rotation + math.pi, lifetime=140)) # add a random bloodstain - make smaller ones more likely since it's a small arms hit :) self.scene.root.add_child( Node(z_index=1, sprite=random.choices(registry.global_controllers. assets_controller.bloodstain_imgs, weights=[5, 3, 1, 0.5])[0], position=enemy.position, rotation=mg_bullet_pair.body.rotation + math.pi, lifetime=random.randint(20000, 40000))) if enemy.hp <= 0: # show death animation self.scene.root.add_child( Node(z_index=1, transition=NodeSpriteTransition(random.choice( registry.global_controllers.assets_controller. enemy_death_frames), duration=450), position=enemy.position, rotation=enemy.rotation, origin_alignment=Alignment.right, lifetime=random.randint(10000, 20000))) # remove enemy node from the scene self.scene.enemies_controller.remove_enemy(enemy) else: enemy.stagger() mg_bullet_pair.body.delete() # remove the bullet from the scene return 0
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, *, 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 __init__(self, *, repeat_sprite, scroll_duration, z_index, **kwargs): self.vertical_move = repeat_sprite.dimensions.y super().__init__( transition=NodeTransition(Node.position, Vector(0., self.vertical_move), loops=0, duration=scroll_duration), **kwargs, ) self.repeat_1 = self.add_child( Node( sprite=repeat_sprite, z_index=z_index, )) self.repeat_2 = self.add_child( Node( sprite=repeat_sprite, position=Vector(0., -self.vertical_move), z_index=z_index, ))
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): super().__init__() self.root.add_child(Node(sprite=registry.global_controllers.assets_controller.title_screen_background_img, z_index=0, position=Vector(0, 0), origin_alignment=Alignment.top_left)) self.root.add_child(TextNode(font=registry.global_controllers.assets_controller.font_2, font_size=30, position=Vector(settings.VIEWPORT_WIDTH / 2, 500), text="Click to start the game", z_index=1, origin_alignment=Alignment.center)) self.exit_label = TextNode(font=registry.global_controllers.assets_controller.font_2, font_size=30, position=Vector(settings.VIEWPORT_WIDTH / 2, 550), text="Press ESC to exit", z_index=1, origin_alignment=Alignment.center) self.root.add_child(self.exit_label) self.transitions_fun_stuff()
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 __init__(self): program = Program.from_files( 'demos/assets/shaders/vs_default.sc', 'demos/assets/shaders/fs_default.sc', ) uniforms = {'u_blur': Uniform(UniformType.vec4)} self.blur_quality = 20. self.material = Material(program, uniforms) self.material.set_uniform_value('u_blur', (self.blur_quality, 0, 0, 0)) self.node = self.root.add_child( Node(sprite=Sprite(PYTHON_IMAGE_PATH), position=Vector(400, 300), scale=Vector.xy(2), material=self.material))
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 __init__(self): self.space = self.root.add_child(SpaceNode( gravity=Vector(0., 15.), sleeping_threshold=1., )) self.pointer_shapes_gen = itertools.cycle(POINTER_SHAPES) self.pointer = self.root.add_child(Node( position=Vector(-500, -500), # start offscreen shape=next(self.pointer_shapes_gen), color=POINTER_COLOR_NORMAL, z_index=10, )) self.spawn_timer = Timer(self._spawn_heartbeat) self.spawn_timer.start(0.02, self)
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 on_collision_grenade_enemy(self, arbiter, grenade_pair, enemy_pair): if arbiter.phase == CollisionPhase.begin: # show explosion animation self.scene.root.add_child( Node(transition=NodeSpriteTransition( registry.global_controllers.assets_controller. explosion_frames, duration=12 * 75), position=grenade_pair.body.position, z_index=1000, lifetime=12 * 75)) # apply explosion effects to enemies (deal damage & push them back) self.scene.enemies_controller.apply_explosion_effects( grenade_pair.body.position) grenade_pair.body.delete() # remove the grenade from the scene return 0
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.root.add_child(Node(position=Vector(100, 100), sprite=Sprite(os.path.join('demos', 'assets', 'python_small.png'))))
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.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.), ))
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 __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, ))