Created
July 17, 2016 11:20
-
-
Save viblo/0b947b50323b5568af1541c5b59b87ba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import pyglet | |
import pymunk | |
import pymunk.pyglet_util | |
import sys | |
window = pyglet.window.Window(width = 800, height = 600) | |
space = pymunk.Space() | |
space.gravity = 0, -980 | |
ball_body = pymunk.Body(1, 100) | |
ball_body.position = 400, 300 | |
ball_body.angular_velocity = 10 | |
ball_shape = pymunk.Circle(ball_body, 15) | |
ball_shape.friction = 0.5 | |
ball_shape.elasticity = 0.9 | |
ball_shape.color = (0,255,0) | |
space.add(ball_body, ball_shape) | |
static_lines = [pymunk.Segment(space.static_body, (20,20), (780,20), 12), | |
pymunk.Segment(space.static_body, (20,1160), (780,1160), 12), | |
pymunk.Segment(space.static_body, (20,20), (20,1160), 12), | |
pymunk.Segment(space.static_body, (780,20), (780,1160), 12)] | |
for static_line in static_lines: | |
static_line.color = (255,255,255) | |
static_line.elasticity = 0.9 | |
static_line.friction = 1 | |
space.add(static_lines) | |
i = 0 | |
prev_body = pymunk.Body(10, 10000) | |
prev_body.position = (400, 570) | |
chain_fix_point = pymunk.Body(body_type = pymunk.Body.KINEMATIC) | |
chain_fix_point.position = (400, prev_body.position[1]) | |
while i < 12: | |
# rotation_center_body = pymunk.Body() | |
# rotation_center_body.position = (400, prev_body.position[1] - 20) | |
body = pymunk.Body(1, 1) | |
body.position = (400, prev_body.position[1] - 10) | |
line = pymunk.Circle(body, 5) | |
line.elasticity = 0 | |
line.friction = 1 | |
if i == 0: | |
rotation_center_joint = pymunk.PinJoint(body, chain_fix_point) | |
else: | |
rotation_center_joint = pymunk.PinJoint(body, prev_body) | |
space.add(line, body, rotation_center_joint) | |
prev_body = body | |
i += 1 | |
blob_body = pymunk.Body(5, 10000) | |
blob_body.position = prev_body.position[0], prev_body.position[1] + 30 | |
blob_shape = pymunk.Circle(blob_body, 20) | |
rotation_center_joint = pymunk.PinJoint(blob_body, prev_body) | |
space.add(blob_body, blob_shape, rotation_center_joint) | |
box_body = pymunk.Body(1,10000) | |
box_body.position = 600, 200 | |
box_vertices = [(570, 170),(570, 230),(630,230),(630,170)] | |
box_shape = pymunk.Poly(box_body, box_vertices, radius=1) #.create_box(box_body, size = (60,60)) | |
box_shape.friction = 0.5 | |
box_shape.elasticity = 0.9 | |
box_shape.color = (255,0,0) | |
space.add(box_body, box_shape) | |
draw_options = pymunk.pyglet_util.DrawOptions() | |
def update(dt): | |
#if ball_body.angle != 0: | |
# angle += ball_body.angular_velocity | |
#for x in range(10): | |
space.step(1/60.0) | |
# img = pygame.transform.rotate(appleimg, angle) | |
@window.event | |
def on_key_press(symbol, modifiers): | |
# Symbolic names: | |
if symbol == pyglet.window.key.SPACE: | |
ball_body.apply_impulse_at_local_point((1000, 1000)) | |
@window.event | |
def on_draw(): | |
window.clear() | |
space.debug_draw(draw_options) | |
pyglet.clock.schedule(update) | |
pyglet.app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment