Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def main():
pygame.init()
screen = pygame.display.set_mode((600,600))
clock = pygame.time.Clock()
space = pymunk.Space()
space.gravity = 0,980
static= [
pymunk.Segment(space.static_body, (0, -50), (-50, 650), 5),
pymunk.Segment(space.static_body, (0, 650), (650, 650), 5),
pymunk.Segment(space.static_body, (650, 650), (650, -50), 5),
pymunk.Segment(space.static_body, (-50, -50), (650, -50), 5),
]
for s in static:
s.collision_type = 1
space.add(static)
def pre_solve(arb, space, data):
s = arb.shapes[0]
space.remove(s.body, s)
return False
space.add_collision_handler(0, 1).pre_solve = pre_solve
terrain_surface = pygame.Surface((600,600))
terrain_surface.fill(pygame.color.THECOLORS["white"])
pygame.init()
screen = pygame.display.set_mode((600, 600))
clock = pygame.time.Clock()
running = True
### Physics stuff
space = pymunk.Space()
space.gravity = (0.0, -900.0)
draw_options = pymunk.pygame_util.DrawOptions(screen)
# disable the build in debug draw of collision point since we use our own code.
draw_options.flags = draw_options.flags ^ pymunk.pygame_util.DrawOptions.DRAW_COLLISION_POINTS
## Balls
balls = []
### walls
static_lines = [pymunk.Segment(space.static_body, (11.0, 280.0), (407.0, 246.0), 0.0)
,pymunk.Segment(space.static_body, (407.0, 246.0), (407.0, 343.0), 0.0)
]
for l in static_lines:
l.friction = 0.5
space.add(static_lines)
ticks_to_next_ball = 10
ch = space.add_collision_handler(0, 0)
ch.data["surface"] = screen
ch.post_solve = draw_collision
while running:
for event in pygame.event.get():
if event.type == QUIT:
running = False
b = pymunk.Body(body_type=pymunk.Body.KINEMATIC)
b.position = (230,430)
t = pymunk.Transform(ty=-100)
s = pymunk.Poly(b, [(19.0, -50.0), (30.0, -5.0), (26.0, 15.0), (10.0, 38.0), (-10.0, 38.0), (-26.0, 15.0), (-30.0, -5.0), (-19.0, -50.0)], t)
space.add(s)
### Dynamic
captions.append(((390,680), "Dynamic Shapes"))
#Dynamic Segments
b = pymunk.Body(1,1)
segments = [pymunk.Segment(b, (350, 400), (350, 600), 0),
pymunk.Segment(b, (360, 400), (360, 600), 1),
pymunk.Segment(b, (370, 400), (370, 600), 3),
pymunk.Segment(b, (390, 400), (390, 600), 5),
]
space.add(segments)
b = pymunk.Body(1,1)
b.position = (380,630)
b.angle = 3.14/7
s = pymunk.Segment(b, (-30,0), (30,0), 7)
space.add(s)
# Dynamic Circles
b = pymunk.Body(1,1)
b.position = (460,630)
s = pymunk.Circle(b, 10)
space.add(s)
b = pymunk.Body(1,1)
clock = pygame.time.Clock()
running = True
### Physics stuff
space = pymunk.Space()
space.gravity = Vec2d(0.0, -900.0)
## logo
logo_img = pygame.image.load("pymunk_logo_googlecode.png")
logos = []
### Static line
static_body = pymunk.Body()
static_lines = [pymunk.Segment(static_body, (11.0, 280.0), (407.0, 246.0), 0.0)
,pymunk.Segment(static_body, (407.0, 246.0), (407.0, 343.0), 0.0)
]
for l in static_lines:
l.friction = 0.5
space.add(static_lines)
ticks_to_next_spawn = 10
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
elif event.type == KEYDOWN and event.key == K_p:
pygame.image.save(screen, "using_sprites.png")
a = b = None
clr = self.get_color()
shapes = []
for p in pointlist:
if a == None:
# Starting Point
a = p
else:
# Ending Point
b = p
# add shape beween a and b
# print a,b
shape = pm.Segment(body, Vec2d(a), Vec2d(b), 2.0)
shape.group = 1
shape.friction = friction
shape.color = clr
shapes.append(shape)
# Last Ending Point gets next starting Point
a = b
# Append to Space
self.space.add(body, shapes)
self.element_count += 1
#Create the pymunk space that will simulate the physics.
self.space = pymunk.Space()
'''Used to reduce oscillating contacts and keep the collision
cache warm. Defaults to 0.1. If you have poor simulation quality,
increase this number as much as possible without allowing visible
amounts of overlap.'''
self.space.collision_slop = 0.3
self.space.gravity = (0,-700)
#The map boundaries.
self.boundary_left = pymunk.Segment(self.space.static_body, (0, 0), (0, 500), 1)
self.boundary_left.friction = 10
self.boundary_left.collision_type = 4
self.boundary_right = pymunk.Segment(self.space.static_body, (3200, 0), (3200, 500), 1)
self.boundary_right.friction = 10
#The platform object.
self.platform = pymunk.Segment(self.space.static_body, (0, 64), (3200, 64), 1)
self.platform.friction = 5
self.platform.collision_type = 4
self.floor = cocos.draw.Line((0,64), (3200,64), (255,255,255,255), 5)
self.floor.visible = False
#Create actors and other physics objects.
self.player = player.Player()
self.block = blocks.Block(pos = (400,300))
self.zombie = npc.Zombie()
self.particle = Explosion()
self.particle.gravity = Point2(700, 0)
self.particle.size = 5
self._pymunk_body.position = self.sprite.x + (self.sprite.x1 - self.sprite.x)/2, self.sprite.y + (self.sprite.y1 - self.sprite.y)/2
else:
self._pymunk_body.position = self.sprite.x, self.sprite.y
self._pymunk_body.angle = _math.radians(self.sprite.angle)
if self.can_move:
self._pymunk_body.velocity = (self._x_speed, self._y_speed)
if not self.obeys_gravity:
self._pymunk_body.velocity_func = lambda body, gravity, damping, dt: None
if isinstance(self.sprite, Circle):
self._pymunk_shape = _pymunk.Circle(self._pymunk_body, self.sprite.radius, (0,0))
elif isinstance(self.sprite, line):
self._pymunk_shape = _pymunk.Segment(self._pymunk_body, (self.sprite.x, self.sprite.y), (self.sprite.x1, self.sprite.y1), self.sprite.thickness)
else:
self._pymunk_shape = _pymunk.Poly.create_box(self._pymunk_body, (self.sprite.width, self.sprite.height))
self._pymunk_shape.elasticity = _clamp(self.bounciness, 0, .99)
self._pymunk_shape.friction = self._friction
_physics_space.add(self._pymunk_body, self._pymunk_shape)
# we skip the line which has less than 35 height, since its the "hole" in
# the p in pymunk, and we dont need it.
if h < 35:
continue
center = Vec2d(min_x + w/2., min_y + h/2.)
t = pymunk.Transform(a=1.0, d=1.0, tx=-center.x, ty=-center.y)
r += 30
if r > 255:
r = 0
if True:
for i in range(len(line)-1):
shape = pymunk.Segment(space.static_body, line[i], line[i+1], 1)
shape.friction = 0.5
shape.color = (255,255,255)
space.add(shape)
floor = pymunk.Segment(space.static_body, (-100,300),(1000,220),5)
floor.friction = 1.0
space.add(floor)
### events
def big_ball(space):
mass = 1000
radius = 50
moment = pymunk.moment_for_circle(mass, 0, radius)
b = pymunk.Body(mass, moment)
c = pymunk.Circle(b, radius)
### Dynamic
captions.append(((390,680), "Dynamic Shapes"))
#Dynamic Segments
b = pymunk.Body(1,1)
segments = [pymunk.Segment(b, (350, 400), (350, 600), 0),
pymunk.Segment(b, (360, 400), (360, 600), 1),
pymunk.Segment(b, (370, 400), (370, 600), 3),
pymunk.Segment(b, (390, 400), (390, 600), 5),
]
space.add(segments)
b = pymunk.Body(1,1)
b.position = (380,630)
b.angle = 3.14/7
s = pymunk.Segment(b, (-30,0), (30,0), 7)
space.add(s)
# Dynamic Circles
b = pymunk.Body(1,1)
b.position = (460,630)
s = pymunk.Circle(b, 10)
space.add(s)
b = pymunk.Body(1,1)
b.position = (460,630)
s = pymunk.Circle(b, 10, (-30,0))
space.add(s)
b = pymunk.Body(1,1)
b.position = (460,560)
b.angle = 3.14/4
b = pymunk.Body()
b.position = (150,300)
b.angle = 3.14/2
s = pymunk.Poly(b, [(0, -50),(30, 50),(-30, 50)], (-100,0), radius=3)
space.add(s)
b = pymunk.Body()
b.position = (150,200)
s = pymunk.Poly(b, [(0, -50), (50, 0), (30, 50),(-30, 50),(-50, 0)], (0,-100))
space.add(s)
### Dynamic
#Dynamic Segments
b = pymunk.Body(1,1)
segments = [ pymunk.Segment(b, (210, 50), (210, 500), 1)
,pymunk.Segment(b, (230, 50), (230, 500), 2)
,pymunk.Segment(b, (250, 50), (250, 500), 3)
,pymunk.Segment(b, (270, 50), (270, 500), 5)
]
space.add(segments)
b = pymunk.Body(1,1)
b.position = (240,530)
b.angle = 3.14/7
s = pymunk.Segment(b, (-30,0), (30,0), 2)
space.add(s)
# Dynamic Circles
b = pymunk.Body(1,1)
b.position = (350,500)
s = pymunk.Circle(b, 10)