Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def create_poly(space, points, mass = -5.0, pos = (0,0)):
moment = pm.moment_for_poly(mass, points, pm.Vec2d(0,0))
#moment = 1000
body = pm.Body(mass, moment)
body.position = pm.Vec2d(pos)
shape = pm.Poly(body, points, pm.Vec2d(0,0))
shape.friction = 0.5
#shape.collision_type = 0
#space.add(body, shape)
#space.add_static(shape)
return body, shape
shape.friction = 1.0
self.space.add(shape)
### pyramid
x=Vec2d(-270, 7.5) + (300,100)
y=Vec2d(0,0)
deltaX=Vec2d(0.5625, 1.1)*20
deltaY=Vec2d(1.125, 0.0)*20
for i in range(25):
y = Vec2d(x)
for j in range(i, 25):
size = 10
points = [(-size, -size), (-size, size), (size,size), (size, -size)]
mass = 1.0
moment = pymunk.moment_for_poly(mass, points, (0,0))
body = pymunk.Body(mass, moment)
body.position = y
shape = pymunk.Poly(body, points)
shape.friction = 1
self.space.add(body,shape)
y += deltaY
x += deltaX
pm_obj = pm.Circle(pm_body, radius, offset)
elif isinstance(shape, Segment):
# WARNING: the segment is assumed to be centered around its position
# points a and b are equally distant from the segments center
position = Vec2d(shape.position)
length = Vec2d(shape.length, 0)
a = position - 0.5 * length
b = position + 0.5 * length
radius = shape.radius
pm_obj = pm.Segment(pm_body, a, b, radius)
elif isinstance(shape, Polygon):
vertices = shape.vertices
# WARNING: for now, polygon shapes are completely aligned to their
# body's center of gravity
offset = (0, 0)
moment = pm.moment_for_poly(mass, vertices, offset)
pm_body = pm.Body(mass, moment)
pm_obj = pm.Poly(pm_body, vertices, offset)
return pm_obj
def create_arrow():
vs = [(-30,0), (0,3), (10,0), (0,-3)]
mass = 1
moment = pymunk.moment_for_poly(mass, vs)
arrow_body = pymunk.Body(mass, moment)
arrow_shape = pymunk.Poly(arrow_body, vs)
arrow_shape.friction = .5
arrow_shape.collision_type = 1
return arrow_body, arrow_shape
### walls
static_lines = [pymunk.Segment(space.static_body, (150, 100.0), (50.0, 550.0), 1.0)
,pymunk.Segment(space.static_body, (450.0, 100.0), (550.0, 550.0), 1.0)
,pymunk.Segment(space.static_body, (50.0, 550.0), (300.0, 600.0), 1.0)
,pymunk.Segment(space.static_body, (300.0, 600.0), (550.0, 550.0), 1.0)
,pymunk.Segment(space.static_body, (300.0, 420.0), (400.0, 400.0), 1.0)
]
for line in static_lines:
line.elasticity = 0.7
line.group = 1
space.add(static_lines)
fp = [(20,-20), (-120, 0), (20,20)]
mass = 100
moment = pymunk.moment_for_poly(mass, fp)
# right flipper
r_flipper_body = pymunk.Body(mass, moment)
r_flipper_body.position = 450, 100
r_flipper_shape = pymunk.Poly(r_flipper_body, fp)
space.add(r_flipper_body, r_flipper_shape)
r_flipper_joint_body = pymunk.Body(body_type=pymunk.Body.KINEMATIC)
r_flipper_joint_body.position = r_flipper_body.position
j = pymunk.PinJoint(r_flipper_body, r_flipper_joint_body, (0,0), (0,0))
#todo: tweak values of spring better
s = pymunk.DampedRotarySpring(r_flipper_body, r_flipper_joint_body, 0.15, 20000000,900000)
space.add(j, s)
# left flipper
l_flipper_body = pymunk.Body(mass, moment)
def boxfloor(self, space):
mass = 10
vs = [(-50,30),(60,22),(-50, 22)]
moment = pymunk.moment_for_poly(mass, vs)
b = pymunk.Body(mass, moment)
s = pymunk.Poly(b, vs)
s.friction = 1
s.color = 0,0,0
b.position = 600,250
space.add(b,s)
with self.canvas:
Color(0.2,0.2,0.2)
s.ky = Triangle(points=self.points_from_poly(s))
def spawn_logo(dt):
x = random.randint(20,400)
y = 500
angle = random.random() * math.pi
vs = [(-23,26), (23,26), (0,-26)]
mass = 10
moment = pymunk.moment_for_poly(mass, vs)
body = pymunk.Body(mass, moment)
shape = pymunk.Poly(body, vs)
shape.friction = 0.5
body.position = x, y
body.angle = angle
space.add(body, shape)
sprite = pyglet.sprite.Sprite(logo_img, batch=batch)
sprite.shape = shape
sprite.body = body
logos.append(sprite)