def update(self, dt): for event in self.input.events(): if event.keyboard_key and event.keyboard_key.is_key_down: if event.keyboard_key.key == Keycode.n: self.spawn_object() elif event.keyboard_key.key == Keycode.r: self.random_collisions = not self.random_collisions print("Random collisions: {}".format(self.random_collisions)) elif event.keyboard_key.key == Keycode.c: self.collision_spawning = not self.collision_spawning if self.collision_spawning: print("Collision spawning enabled (for one collision)") elif event.keyboard_key.key == Keycode.s: self.observed_ball.velocity *= 1.5 elif event.keyboard_key.key == Keycode.t: print(self.observed_ball.transformation) print(self.observed_ball.transformation.decompose()) self.observed_ball.transformation = \ ( Transformation(scale=Vector(2., 2.)) | Transformation(translate=Vector(100., 100.)) ) elif event.keyboard_key.key == Keycode.l: all_balls = [ n for n in self.space.children if isinstance(n, FlyingBall) ] if all_balls: target_ball = random.choice(all_balls) target_ball.lifetime = 1500 if ( event.mouse_button and event.mouse_button.is_button_down and event.mouse_button.button == MouseButton.left ): self.spawn_object( position=self.camera.unproject_position( event.mouse_button.position, ), velocity=Vector(0., 0.), ) if self.input.keyboard.is_pressed(Keycode.q): print("q Pressed - Exiting") self.engine.quit() self.camera.position = self.observed_ball.position
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 _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, ))
points_invalid = [ Vector(0., 0.), Vector(1., 1.), Vector(1., -1.), Vector(-1., -1.), Vector(-1., 1.) ] fmt_print("classification of polygon {}\n --> {!r}", points, classify_polygon(points)) fmt_print("classification of polygon {}\n --> {!r}", rev_points, classify_polygon(rev_points)) fmt_print("classification of polygon {}\n --> {!r}", points_invalid, classify_polygon(points_invalid)) fmt_print("Transformation()\n --> {}", Transformation()) fmt_print("Transformation.translate(Vector(20, -10))\n --> {}", Transformation.translate(Vector(20, -10))) fmt_print("Transformation.rotate_degrees(90)\n --> {}", Transformation.rotate_degrees(90)) fmt_print("Transformation.scale(Vector(2, 2))\n --> {}", Transformation.translate(Vector(2, 2))) fmt_print("Transformation.translate(Vector(20, -10).inverse())\n --> {}", Transformation.translate(Vector(20, -10)).inverse()) fmt_print("Transformation.scale(Vector(2, 2).inverse())\n --> {}", Transformation.translate(Vector(2, 2)).inverse()) fmt_print( "Transformation.scale(Vector(2, 2)) | Transformation.translate(Vector(20, -10))\n --> {}", Transformation.scale(Vector(2, 2)) | Transformation.translate(Vector(20, -10))) fmt_print(