def __init__(self, shape, coordinate_system, rotation=Angle.from_degrees(7.5)): self.shape = shape self.rotation = rotation self.coordinate_system = coordinate_system
def create_static_obstacle(self, position: (int, int), size: (int, int), rotation: Angle = Angle.from_degrees(0), elasticity: float = 1.0): width, height = size node, shape = self.nodes_factory.create_rect( width=width, height=height, body_type=pymunk.Body.STATIC, position=position, rotation=rotation, elasticity=elasticity) self.nodes.append(node)
def create_rect(self, width: int, height: int, body_type=pymunk.Body.KINEMATIC, position: (int, int) = (0, 0), rotation: Angle = Angle.from_degrees(0), elasticity: float = 1.0) -> (Polygon, pymunk.Poly): body = pymunk.Body(1, 16000, body_type) body.position = position body.angle = rotation half_width = width / 2 half_height = height / 2 vertices = [(-half_width, -half_height), (half_width, -half_height), (half_width, half_height), (-half_width, half_height)] pymunk_poly = pymunk.Poly(body, vertices) pymunk_poly.elasticity = elasticity self.space.add(body, pymunk_poly) return Polygon(VerticesGetter(pymunk_poly, self.coordinate_system)), pymunk_poly
def create_draggable_obstacle(self, position: (int, int), size: (int, int), rotation: Angle = Angle.from_degrees(0), elasticity: float = 1.0): width, height = size node, shape = self.nodes_factory.create_rect( width=width, height=height, body_type=pymunk.Body.KINEMATIC, position=position, elasticity=elasticity) node.color = (0, 255, 0, 255) shape.body.angle = rotation self.event_handlers.append( DragHandler(shape=shape, coordinate_system=self.coordinate_system)) self.event_handlers.append( RotationHandler(shape=shape, coordinate_system=self.coordinate_system)) self.nodes.append(node)
from angle import Angle from level_generator import LevelDefinition level2 = LevelDefinition() level2.draggable_objects = [ { "position": (1000, 500), "size": (50, 20), "rotation": Angle.from_degrees(90) }, { "position": (1040, 500), "size": (50, 20), "rotation": Angle.from_degrees(90) } ] level2.static_objects = [ { "position": (600, 200), "size": (500, 20), "rotation": Angle.from_degrees(90) } ] level2.ball_position = (400, 200) level2.target_position = (700, 250)