Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def add_base(space):
body = pymunk.Body()
body.position = (0, 300)
shape = pymunk.Poly.create_box(body, (SCREEN_WIDTH, 20), ((SCREEN_WIDTH/2), -10), 0)
shape.friction = 1.0
shape.collision_type = 0x6 # base
space.add(shape)
return shape
static_lines = [pymunk.Segment(self.space.static_body, Vec2d(20,55), Vec2d(600,55), 1),
pymunk.Segment(self.space.static_body, Vec2d(550,55), Vec2d(550,400), 1)
]
for l in static_lines:
l.friction = 0.3
self.space.add(static_lines)
for x in range(5):
for y in range(10):
size = 20
mass = 10.0
moment = pymunk.moment_for_box(mass, (size, size))
body = pymunk.Body(mass, moment)
body.position = Vec2d(300 + x*50, 105 + y * (size +.1))
shape = pymunk.Poly.create_box(body, (size, size))
shape.friction = 0.3
self.space.add(body,shape)
# Remove balls and bricks
for s in space.shapes[:]:
if s.body.body_type == pymunk.Body.DYNAMIC and s.body not in [player_body]:
space.remove(s.body, s)
# Spawn a ball for the player to have something to play with
spawn_ball(space, player_body.position + (0,40), random.choice([(1,10),(-1,10)]))
# Spawn bricks
for x in range(0,21):
x = x * 20 + 100
for y in range(0,5):
y = y * 10 + 400
brick_body = pymunk.Body(body_type=pymunk.Body.KINEMATIC)
brick_body.position = x, y
brick_shape = pymunk.Poly.create_box(brick_body, (20,10))
brick_shape.elasticity = 1.0
brick_shape.color = THECOLORS['blue']
brick_shape.group = 1
brick_shape.collision_type = collision_types["brick"]
space.add(brick_body, brick_shape)
# Make bricks be removed when hit by ball
def remove_brick(arbiter, space, data):
brick_shape = arbiter.shapes[0]
space.remove(brick_shape, brick_shape.body)
h = space.add_collision_handler(
collision_types["brick"],
collision_types["ball"])
h.separate = remove_brick
def init_man(self, pos=None, vel=None):
if pos is None:
pos = Vec2d(self.size.x / 2, self.man_size.y / 2)
if vel is None:
vel = Vec2d(0, 0)
# physics for man
shape = pymunk.Poly.create_box(pymunk.Body(20, 10000000), self.man_size)
shape.body.position = pos
shape.body.velocity = vel
shape.body.angular_velocity_limit = 0
self.man_angle = shape.body.angle
shape.elasticity = 0
shape.friction = 3.0
shape.collision_type = Collision.Default
self.space.add(shape.body, shape)
self.man = shape
space.add(wheel1_b, wheel1_s)
mass = 100
radius = 25
moment = pymunk.moment_for_circle(mass, 20, radius)
wheel2_b = pymunk.Body(mass, moment)
wheel2_s = pymunk.Circle(wheel2_b, radius)
wheel2_s.friction = 1.5
wheel2_s.color = wheel_color
space.add(wheel2_b, wheel2_s)
mass = 100
size = (50,30)
moment = pymunk.moment_for_box(mass, size)
chassi_b = pymunk.Body(mass, moment)
chassi_s = pymunk.Poly.create_box(chassi_b, size)
space.add(chassi_b, chassi_s)
vs = [(0,0),(25,45),(0,45)]
shovel_s = pymunk.Poly(chassi_b, vs, transform = pymunk.Transform(tx=85))
shovel_s.friction = 0.5
shovel_s.color = shovel_color
space.add(shovel_s)
wheel1_b.position = pos - (55,0)
wheel2_b.position = pos + (55,0)
chassi_b.position = pos + (0,-25)
space.add(
pymunk.PinJoint(wheel1_b, chassi_b, (0,0), (-25,-15)),
pymunk.PinJoint(wheel1_b, chassi_b, (0,0), (-25, 15)),
pymunk.PinJoint(wheel2_b, chassi_b, (0,0), (25,-15)),
shape = pymunk.Segment(body, [0, floor_height], [SCREEN_WIDTH, floor_height], 0.0)
shape.friction = 10
self.space.add(shape)
self.static_lines.append(shape)
# Create the stacks of boxes
for row in range(10):
for column in range(10):
size = 32
mass = 1.0
x = 500 + column * 32
y = (floor_height + size / 2) + row * size
moment = pymunk.moment_for_box(mass, (size, size))
body = pymunk.Body(mass, moment)
body.position = pymunk.Vec2d(x, y)
shape = pymunk.Poly.create_box(body, (size, size))
shape.elasticity = 0.2
shape.friction = 0.9
self.space.add(body, shape)
# body.sleep()
sprite = BoxSprite(shape, ":resources:images/tiles/boxCrate_double.png", width=size, height=size)
self.sprite_list.append(sprite)
floor_height = 80
body = pymunk.Body(body_type=pymunk.Body.STATIC)
shape = pymunk.Segment(body, [0, floor_height], [SCREEN_WIDTH, floor_height], 0.0)
shape.friction = 10
self.space.add(shape)
self.static_lines.append(shape)
# Create the stacks of boxes
for x in range(6):
for y in range(12):
size = 45
mass = 12.0
moment = pymunk.moment_for_box(mass, (size, size))
body = pymunk.Body(mass, moment)
body.position = pymunk.Vec2d(300 + x*50, (floor_height + size / 2) + y * (size + .01))
shape = pymunk.Poly.create_box(body, (size, size))
shape.friction = 0.3
self.space.add(body, shape)
sprite = BoxSprite(shape, "images/boxCrate_double.png", width=size, height=size)
self.sprite_list.append(sprite)
shape = pymunk.Segment(self.space.static_body, (5, 100), (595, 100), 1.0)
shape.friction = 1.0
self.space.add(shape)
self.bodies = []
self.balls = []
for x in range(5):
for y in range(10):
size = 20
mass = 10.0
moment = pymunk.moment_for_box(mass, (size, size))
body = pymunk.Body(mass, moment)
body.position = Vec2d(300 + x * 50, 105 + y * (size + 0.1))
shape = pymunk.Poly.create_box(body, (size, size))
shape.friction = 0.3
self.space.add(body, shape)
self.bodies.append(body)