Skip to content

Instantly share code, notes, and snippets.

@kkestell
Created November 10, 2011 15:57
Show Gist options
  • Save kkestell/1355200 to your computer and use it in GitHub Desktop.
Save kkestell/1355200 to your computer and use it in GitHub Desktop.
Rabbyt, Pymunk, and Pyglet
import rabbyt
import pygame
import pymunk
from pygame.locals import QUIT
from pyglet.gl import *
pygame.init()
pygame.display.set_mode((640,480), pygame.OPENGL | pygame.DOUBLEBUF)
rabbyt.set_viewport((640,480))
rabbyt.set_default_attribs()
pymunk.init_pymunk()
space = pymunk.Space()
space.gravity = (0.0, -900.0)
space.resize_static_hash()
space.resize_active_hash()
mass = 100
points = [pymunk.Vec2d(-32.0, 32.0), pymunk.Vec2d(32.0, 32.0), pymunk.Vec2d(32.0, -32.0), pymunk.Vec2d(-32.0, -32.0)]
moment = pymunk.moment_for_poly(mass, points, pymunk.Vec2d(0.0, 0.0))
body = pymunk.Body(mass, moment)
body.angular_velocity = -2.8
shape = pymunk.Poly(body, points, pymunk.Vec2d(0.0, 0.0))
shape.elasticity = 0.8
shape.friction = 0.8
space.add(body, shape)
body.position = (200.0, 100.0)
static_body = pymunk.Body(pymunk.inf, pymunk.inf)
static_line = pymunk.Segment(static_body, pymunk.Vec2d(-100.0, -150.0), pymunk.Vec2d(300.0, -100.0), 0.0)
static_line.elasticity = 0.5
static_line.friction = 1
space.add_static(static_line)
clock = pygame.time.Clock()
sprite = rabbyt.Sprite('test.png')
keep_going = True
while keep_going:
clock.tick(60)
for event in pygame.event.get():
if event.type == QUIT:
keep_going = False
rabbyt.clear()
space.step(1.0 / 60.0)
glBegin(GL_LINES)
glVertex2f(-100, -150)
glVertex2f(300, -100)
glEnd()
sprite.x = body.position.x
sprite.y = body.position.y
sprite.rot = body.angle * 57.2957795
sprite.render()
pygame.display.flip()
Copy link

ghost commented Oct 8, 2015

The title is incorrect: this uses Pygame, not Pyglet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment