Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
body = pymunk.Body(body_type=pymunk.Body.STATIC)
shape = pymunk.Segment(body, [50, 10], [0, 30], 0.0)
shape.friction = 10
self.space.add(shape)
self.static_lines.append(shape)
radius = 20
separation = 150
for row in range(6):
for column in range(6):
x = column * separation + (separation // 2 * (row % 2))
y = row * separation + separation // 2
body = pymunk.Body(body_type=pymunk.Body.STATIC)
body.position = x, y
shape = pymunk.Circle(body, radius, pymunk.Vec2d(0, 0))
shape.friction = 0.3
self.space.add(body, shape)
sprite = CircleSprite("images/bumper.png", shape)
self.peg_list.append(sprite)
def testShapesCollide(self):
b1 = p.Body(1,1)
s1 = p.Circle(b1, 10)
b2 = p.Body(1,1)
b2.position = 30,30
s2 = p.Circle(b2, 10)
c = s1.shapes_collide(s2)
self.assertEqual(c.normal, (1, 0))
self.assertEqual(len(c.points), 1)
point = c.points[0]
self.assertEqual(point.point_a, (10,0))
self.assertEqual(point.point_b, (-10,0))
self.assertEqual(point.distance, -20)
def create_ball(self, radius=3):
inertia = pymunk.moment_for_circle(1, 0, radius, (0, 0))
body = pymunk.Body(1, inertia)
position = np.array(self.initial_position) + self.initial_std * np.random.normal(size=(2,))
position = np.clip(position, self.dd + radius + 1, self.res[0] - self.dd - radius - 1)
body.position = position
shape = pymunk.Circle(body, radius, (0, 0))
shape.elasticity = .9
shape.color = color["white"]
return shape
def small_ball(self, space):
for x in range(10):
mass = 3
radius = 8
moment = pymunk.moment_for_circle(mass, 0, radius)
b = pymunk.Body(mass, moment)
c = pymunk.Circle(b, radius)
c.friction = 1
x = random.randint(100, 350)
y = random.randint(300, 340)
b.position = x,y
space.add(b,c)
with self.canvas:
Color(.2,.6,.86)
c.ky = self.ellipse_from_circle(c)
def __init__(self):
rob_mass = 1 # 1 kg
# 0.1 meter radius, converted to pixels for display
rob_radius = 0.1 * M_TO_PIXELS
# moment of inertia for disk
rob_I = moment_for_circle(rob_mass, 0, rob_radius)
self.body = Body(rob_mass, rob_I)
self.body.position = 0, 0
self.body.angle = 0
self.body.velocity = 0, 0
self.body.angular_velocity = 0
self.shape = Circle(self.body, rob_radius)
self.shape.color = 255, 0, 0 # red
self._command = Twist()
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)
# bottom - a sensor that removes anything touching it
bottom = pymunk.Segment(space.static_body, (50, 50), (550, 50), 5)
bottom.sensor = True
bottom.collision_type = 1
bottom.color = THECOLORS['red']
def remove_first(space, arbiter):
first_shape = arbiter.shapes[0]
space.add_post_step_callback(space.remove, first_shape, first_shape.body)
return True
space.add_collision_handler(0, 1, begin = remove_first)
space.add(bottom)
### Player ship
player_body = pymunk.Body(500, pymunk.inf)
player_shape = pymunk.Circle(player_body, 35)
player_shape.color = THECOLORS["red"]
player_shape.elasticity = 1.0
player_body.position = 300,100
# restrict movement of player to a straigt line
move_joint = pymunk.GrooveJoint(space.static_body, player_body, (100,100), (500,100), (0,0))
space.add(player_body, player_shape, move_joint)
global state
# Start game
setup_level(space, player_body)
while running:
for event in pygame.event.get():
if event.type == QUIT:
running = False
elif event.type == KEYDOWN and (event.key in [K_ESCAPE, K_q]):
running = False
def make_circle(self, x, y):
size = 20
mass = 12.0
moment = pymunk.moment_for_circle(mass, 0, size, (0, 0))
body = pymunk.Body(mass, moment)
body.position = pymunk.Vec2d(x, y)
shape = pymunk.Circle(body, size, pymunk.Vec2d(0, 0))
shape.friction = 0.3
self.space.add(body, shape)
sprite = CircleSprite(shape, "images/coin_01.png")
self.sprite_list.append(sprite)
running = False
elif event.type == KEYDOWN and event.key == K_ESCAPE:
running = False
elif event.type == KEYDOWN and event.key == K_p:
pygame.image.save(screen, "contact_with_friction.png")
ticks_to_next_ball -= 1
if ticks_to_next_ball <= 0:
ticks_to_next_ball = 100
mass = 0.1
radius = 25
inertia = pymunk.moment_for_circle(mass, 0, radius, (0,0))
body = pymunk.Body(mass, inertia)
x = random.randint(115,350)
body.position = x, 400
shape = pymunk.Circle(body, radius, (0,0))
shape.friction = 0.5
space.add(body, shape)
balls.append(shape)
### Clear screen
screen.fill(THECOLORS["white"])
### Draw stuff
space.debug_draw(draw_options)
balls_to_remove = []
for ball in balls:
if ball.body.position.y < 200: balls_to_remove.append(ball)
for ball in balls_to_remove:
space.remove(ball, ball.body)
balls.remove(ball)
def __init__(self, x, y, r, *args, **kwargs):
mass = 10
# Create a Body with mass and moment
self.body = pymunk.Body(
mass, pymunk.moment_for_circle(mass, 0, r, (0, 0)))
self.body.position = x, y
# Create a box shape and attach to body
self.shape = pymunk.Circle(self.body, r, offset=(0, 0))
self.shape.elasticity = 0.99999
self.shape.friction = 0.8
self.gui_circle_figure = None