Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# Create the stacks of boxes
for column in range(6):
for row in range(column):
x = 600 + column * size
y = (floor_height + size / 2) + row * size
sprite = PymunkSprite("images/boxCrate_double.png", x, y, scale=0.5, friction=0.4)
self.dynamic_sprite_list.append(sprite)
self.space.add(sprite.body, sprite.shape)
# Create player
x = 50
y = (floor_height + size / 2)
self.player = PymunkSprite("images/character.png", x, y, scale=0.5, moment=pymunk.inf, mass=1)
self.dynamic_sprite_list.append(self.player)
self.space.add(self.player.body, self.player.shape)
def testNoStaticShape(self):
space = p.Space()
b1 = p.Body(1, p.inf)
c1 = p.Circle(b1, 10)
c1.name = "c1"
c1.collision_type = 2
b2 = p.Body(1, p.inf)
c2 = p.Circle(b2, 10)
c2.name = "c2"
b3 = p.Body(1, p.inf)
c3 = p.Circle(b3, 10)
c3.name = "c3"
b1.position = 0,0
b2.position = 9,0
b3.position = -9,0
def __init__(self, pos, image, space,anim_name = "",num_frames = 1,sequence = [0,1],frequency = 8):
self.is_movable = True
GameObject.__init__(self, pos, util.to_sprite(util.load_image("data/info_sign0.png")), space, OBJECT_TYPE_INFO, pm.inf)
self.body, self.shape = create_box(space, (pos.x, pos.y), frequency, 12.0)
self.shape.collision_type = OBJECT_TYPE_INFO
self.info_bubble = util.load_image(image)
space.add_static(self.shape)
self._show_info = False
self.cool_down = 0.0
if not anim_name == "":
self.animation = animation.new_animation(anim_name,"png",num_frames,frequency,sequence)
self.animation.play()
def spawn_ball(space, position, direction):
ball_body = pymunk.Body(1, pymunk.inf)
ball_body.position = position
ball_shape = pymunk.Circle(ball_body, 5)
ball_shape.color = THECOLORS["green"]
ball_shape.elasticity = 1.0
ball_shape.collision_type = collision_types["ball"]
ball_body.apply_impulse_at_local_point(Vec2d(direction))
# Keep ball velocity at a static value
def constant_velocity(body, gravity, damping, dt):
body.velocity = body.velocity.normalized() * 400
ball_body.velocity_func = constant_velocity
space.add(ball_body, ball_shape)
def create_obstacle(self, x, y, r):
c_body = pymunk.Body(pymunk.inf, pymunk.inf)
c_shape = pymunk.Circle(c_body, r)
c_shape.elasticity = 1.0
c_body.position = x, y
c_shape.color = THECOLORS["blue"]
self.space.add(c_body, c_shape)
return c_body
clock = pygame.time.Clock()
running = True
### Physics stuff
pm.init_pymunk()
space = pm.Space()
space.gravity = Vec2d(0.0, -900.0)
space.resize_static_hash()
space.resize_active_hash()
## Balls
balls = []
### walls
static_body = pm.Body(pm.inf, pm.inf)
static_lines = [pm.Segment(static_body, (111.0, 280.0), (407.0, 246.0), 0.0)
,pm.Segment(static_body, (407.0, 246.0), (407.0, 343.0), 0.0)
]
space.add_static(static_lines)
ticks_to_next_ball = 10
### Here we set the collision callback function
space.set_default_collisionpair_func(coll_func, screen)
while running:
for event in pygame.event.get():
if event.type == QUIT:
running = False
elif event.type == KEYDOWN and event.key == K_ESCAPE:
running = False
def static_rounded_box(p, width, height, radius):
"""Creates a box with rounded corners that remains fixed in place.
:param p: The upper left corner of the box
:type p: (int, int)
:param width: The width of the box
:type width: int
:param height: The height of the box
:type height: int
:param radius: The radius of the rounded corners
:type radius: int
:rtype: shape
"""
return rounded_box(p, width, height, radius, pymunk.inf, True)
def _make_pymunk(self):
mass = self.mass if self.can_move else 0
# non-moving line shapes are platforms and it's easier to take care of them less-generically
if not self.can_move and isinstance(self.sprite, line):
self._pymunk_body = _physics_space.static_body.copy()
self._pymunk_shape = _pymunk.Segment(self._pymunk_body, (self.sprite.x, self.sprite.y), (self.sprite.x1, self.sprite.y1), self.sprite.thickness)
else:
if self.stable:
moment = _pymunk.inf
elif isinstance(self.sprite, Circle):
moment = _pymunk.moment_for_circle(mass, 0, self.sprite.radius, (0, 0))
elif isinstance(self.sprite, line):
moment = _pymunk.moment_for_box(mass, (self.sprite.length, self.sprite.thickness))
else:
moment = _pymunk.moment_for_box(mass, (self.sprite.width, self.sprite.height))
if self.can_move and not self.stable:
body_type = _pymunk.Body.DYNAMIC
elif self.can_move and self.stable:
if self.obeys_gravity or _physics_space.gravity == 0:
body_type = _pymunk.Body.DYNAMIC
else:
body_type = _pymunk.Body.KINEMATIC
else:
body_type = _pymunk.Body.STATIC
def setup_level(space, player_body):
# Remove balls and bricks
for s in space.shapes[:]:
if not s.body.is_static 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,1),(-1,1)]))
# 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(pymunk.inf, pymunk.inf)
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 = 2
space.add(brick_body, brick_shape)
# Make bricks be removed when hit by ball
def remove_first(space, arbiter):
first_shape = arbiter.shapes[0]
space.add_post_step_callback(space.remove, first_shape, first_shape.body)
space.add_collision_handler(2, 0, separate = remove_first)