How to use the pymunk.pygame_util function in pymunk

To help you get started, we’ve selected a few pymunk examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github viblo / pymunk / examples / slide_and_pinjoint.py View on Github external
def main():
    pygame.init()
    screen = pygame.display.set_mode((600, 600))
    pygame.display.set_caption("Joints. Just wait and the L will tip over")
    clock = pygame.time.Clock()

    space = pymunk.Space()
    space.gravity = (0.0, -900.0)

    lines = add_L(space)
    balls = []
    draw_options = pymunk.pygame_util.DrawOptions(screen)

    ticks_to_next_ball = 10
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                sys.exit(0)
            elif event.type == KEYDOWN and event.key == K_ESCAPE:
                sys.exit(0)
            elif event.type == KEYDOWN and event.key == K_p:
                pygame.image.save(screen, "slide_and_pinjoint.png")

        ticks_to_next_ball -= 1
        if ticks_to_next_ball <= 0:
            ticks_to_next_ball = 25
            ball_shape = add_ball(space)
            balls.append(ball_shape)
github viblo / pymunk / examples / platformer.py View on Github external
def main():

    ### PyGame init
    pygame.init()
    screen = pygame.display.set_mode((width,height)) 

    clock = pygame.time.Clock()
    running = True
    font = pygame.font.SysFont("Arial", 16)
    sound = pygame.mixer.Sound("sfx.wav")
    img = pygame.image.load("xmasgirl1.png")
    
    ### Physics stuff
    space = pymunk.Space()   
    space.gravity = 0,-1000
    draw_options = pymunk.pygame_util.DrawOptions(screen)

    # box walls 
    static = [pymunk.Segment(space.static_body, (10, 50), (300, 50), 3)
                , pymunk.Segment(space.static_body, (300, 50), (325, 50), 3)
                , pymunk.Segment(space.static_body, (325, 50), (350, 50), 3)
                , pymunk.Segment(space.static_body, (350, 50), (375, 50), 3)
                , pymunk.Segment(space.static_body, (375, 50), (680, 50), 3)
                , pymunk.Segment(space.static_body, (680, 50), (680, 370), 3)
                , pymunk.Segment(space.static_body, (680, 370), (10, 370), 3)
                , pymunk.Segment(space.static_body, (10, 370), (10, 50), 3)
                ]  
    static[1].color = pygame.color.THECOLORS['red']
    static[2].color = pygame.color.THECOLORS['green']
    static[3].color = pygame.color.THECOLORS['red']
    
    # rounded shape
github viblo / pymunk / examples / deformable.py View on Github external
color = pygame.color.THECOLORS["pink"] 
    pygame.draw.circle(terrain_surface, color, (450,120), 100)
    generate_geometry(terrain_surface, space)
    for x in range(25):
        mass = 1
        moment = pymunk.moment_for_circle(mass, 0, 10)
        body = pymunk.Body(mass, moment)
        body.position = 450, 120
        shape = pymunk.Circle(body, 10)
        shape.friction = .5
        space.add(body, shape)


    draw_options = pymunk.pygame_util.DrawOptions(screen)
    pymunk.pygame_util.positive_y_is_up = False

    fps = 60
    while True:
        for event in pygame.event.get():
            if event.type == QUIT or \
                event.type == KEYDOWN and (event.key in [K_ESCAPE, K_q]):  
                sys.exit(0)
            elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 3:
                pass
            elif event.type == KEYDOWN and event.key == K_r:
                terrain_surface.fill(pygame.color.THECOLORS["white"])
                for s in space.shapes:
                    if hasattr(s, "generated") and s.generated:
                        space.remove(s)
github viblo / pymunk / examples / flipper.py View on Github external
from pygame.locals import *
from pygame.color import *

import pymunk
from pymunk import Vec2d
import pymunk.pygame_util

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)

## Balls
balls = []

### walls
static_lines = [pymunk.Segment(space.static_body, (150, 100.0), (50.0, 550.0), 1.0)
                ,pymunk.Segment(space.static_body, (450.0, 100.0), (550.0, 550.0), 1.0)
                ,pymunk.Segment(space.static_body, (50.0, 550.0), (300.0, 600.0), 1.0)
                ,pymunk.Segment(space.static_body, (300.0, 600.0), (550.0, 550.0), 1.0)
                ,pymunk.Segment(space.static_body, (300.0, 420.0), (400.0, 400.0), 1.0)
                ]  
for line in static_lines:
    line.elasticity = 0.7
    line.group = 1
space.add(static_lines)
github rszeto / moving-symbols / moving_symbols / moving_symbols.py View on Github external
))

        # Convert translation/rotation/scale period/speed limits to lists
        if isinstance(self.params['scale_period_limits'], tuple):
            self.params['scale_period_limits'] = [self.params['scale_period_limits']]
        if isinstance(self.params['rotation_speed_limits'], tuple):
            self.params['rotation_speed_limits'] = [self.params['rotation_speed_limits']]
        if isinstance(self.params['position_speed_limits'], tuple):
            self.params['position_speed_limits'] = [self.params['position_speed_limits']]

        self.cur_rng_seed = seed
        np.random.seed(self.cur_rng_seed)

        if self.debug_options is not None:
            self._pg_screen = pg.display.set_mode(self.video_size)
            self._pg_draw_options = pmu.DrawOptions(self._pg_screen)
            pg.font.init()
            font_size = self.debug_options['frame_number_font_size']
            self._pg_font = pg.font.SysFont(pg.font.get_default_font(), font_size)
            self._pg_clock = pg.time.Clock()
            self._add_init_message(dict(
                step=-1,
                type='debug_options',
                meta=dict(debug_options)
            ))

        self._space = pm.Space()
        self.symbols = []
        image_loader = ImageLoader(os.path.join(self.params['data_dir'], self.params['split']),
                                   'tight_crop')
        if self.params['background_data_dir'] is None or self.params['background_labels'] is None:
            bg_image_loader = None
github simonkamronn / kvae / kvae / datasets / pong.py View on Github external
pygame.init()

        self.dt = dt
        self.res = res
        if os.environ.get('SDL_VIDEODRIVER', '') == 'dummy':
            pygame.display.set_mode(res, 0, 24)
            self.screen = pygame.Surface(res, pygame.SRCCOLORKEY, 24)
            pygame.draw.rect(self.screen, (0, 0, 0), (0, 0, res[0], res[1]), 0)
        else:
            self.screen = pygame.display.set_mode(res, 0, 24)
        self.gravity = (0.0, 0.0)
        self.initial_position = init_pos
        self.initial_std = init_std
        self.space = pymunk.Space()
        self.space.gravity = self.gravity
        self.draw_options = pymunk.pygame_util.DrawOptions(self.screen)
        self.clock = pygame.time.Clock()
        self.wall = wall
        self.static_lines = None

        self.dd = 2
github viblo / pymunk / examples / damped_rotary_spring_pointer.py View on Github external
def main():
    pygame.init()
    screen = pygame.display.set_mode((600, 600))
    clock = pygame.time.Clock()
    running = True
    
    ### Physics stuff
    space = pymunk.Space()
    draw_options = pymunk.pygame_util.DrawOptions(screen)
                
    pointer_body = pymunk.Body(body_type=pymunk.Body.KINEMATIC)
    
    ps = [(80,0),(0,20),(0,-20)]
    moment = pymunk.moment_for_poly(1, ps)
    gun_body = pymunk.Body(1, moment)
    gun_body.position = 300,300
    gun_shape = pymunk.Poly(gun_body, ps)
    
    rest_angle = 0
    stiffness = 125000.
    damping = 6000.
    
    rotary_spring = pymunk.constraint.DampedRotarySpring(pointer_body, gun_body, rest_angle, stiffness, damping)
    
    space.add(gun_body, gun_shape, rotary_spring)
github viblo / pymunk / examples / contact_with_friction.py View on Github external
def main():
    
    global contact
    global shape_to_remove
        
    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
github estevaofon / angry-birds-python / examples / damped_rotary_spring_pointer.py View on Github external
damping = 6000.

    rotary_spring = pymunk.constraint.DampedRotarySpring(pointer_body, gun_body, rest_angle, stiffness, damping)

    space.add(gun_body, gun_shape, rotary_spring)

    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, "aiming.png")
            elif event.type == pygame.MOUSEMOTION:
                mouse_pos = pymunk.pygame_util.get_mouse_pos(screen)
                pointer_body.position = mouse_pos
                pointer_body.angle = (pointer_body.position - gun_body.position).angle

            # to easily find good values for the damped rortary spring
            # as with most simulations done with pymunk, the imporant thing
            # is that it looks good, not the exact parameters
            elif event.type == KEYDOWN and event.key == K_q:
                rotary_spring.stiffness *= .5
                print rotary_spring.stiffness, rotary_spring.damping
            elif event.type == KEYDOWN and event.key == K_w:
                rotary_spring.stiffness *= 2
                print rotary_spring.stiffness, rotary_spring.damping
            elif event.type == KEYDOWN and event.key == K_a:
                rotary_spring.damping *= .5
                print rotary_spring.stiffness, rotary_spring.damping
            elif event.type == KEYDOWN and event.key == K_s:
github CapAI / ship-sim-gym / ship_gym / game.py View on Github external
def render(self):
        """
        The main render loop clears the screen and draws primitives if requested
        """
        self.screen.fill((0, 0, 200))
        if self.debug_mode:
            options = pm.pygame_util.DrawOptions(self.screen)
            options.flags = pymunk.SpaceDebugDrawOptions.DRAW_SHAPES
            self.space.debug_draw(options)

            res = self.player.lidar.query_results
            for r in res:
                if r is not None and r.shape is None:
                    p = r.point
                    p = self.invert_p(p)
                    p = (round(p.x), round(p.y))

                    # Green circle indicating the rays did not hot anything
                    pygame.draw.circle(self.screen, (0, 255, 0), p, 10)
                else:
                    p = r.point
                    p = self.invert_p(p)
                    p = (round(p.x), round(p.y))