Ejemplo n.º 1
0
def simulate(world, balls, edges, pockets, screen, clock, do_draw):
    background_color = (20, 130, 57) # Green felty color.
    world.Step(TIME_STEP, 10, 10)    # Kick things off.

    # Run simulation until balls stop moving.
    while is_moving(world):
        # Check for quit event
        for event in pygame.event.get():
            if event.type == QUIT:
                exit()

        # Apply friction forces to balls
        for body in world.bodies:
            apply_friction(body)

        # Simulate the next step of the Box2D world and destroy balls in the destroy queue (i.e. balls that touched pockets)
        world.Step(TIME_STEP, 10, 10)
        world.contactListener.DestroyBalls(world)
        world.ClearForces()

        # Draw the world
        if do_draw:
            # Fill in the table background, draw the bodies, and update the display and clock.
            draw(world, screen, background_color)
            pygame.display.flip()
            clock.tick(FPS)

    # See if the cue ball went into a pocket.
    scratch = is_made(balls[0])
    if scratch:
        balls[0].body.position = (TABLE_WIDTH / 2.0 + TABLE_WIDTH / 4.0 + 0.4, TABLE_HEIGHT / 2.0)

    return scratch
Ejemplo n.º 2
0
def floaty_sheep(datadir, clock):
    '''Make an inert sheep unaffected by gravity'''
    sheep_frame = util.load_frame(datadir, 'sheep')
    sheep = components.entity('Sheep', clock, location=(500, 400),
                              motion=components.motion(),
                              graphics=components.graphics(None),
                              hitpoints=2)
    sheep.set_frame(sheep_frame)
    components.physics(sheep)
    sheep.physics.add(physics.apply_friction(0.5), components.physics.GROUP_VELOCITY)
    return sheep
Ejemplo n.º 3
0
def drake(datadir, clock):
    '''Make an inert drake'''
    drake_frame = util.load_frame(datadir, 'drake')
    drake = components.entity('Drake', clock, location=(500, 0),
                              motion=components.motion(),
                              graphics=components.graphics(None),
                              hitpoints=200)
    drake.set_frame(drake_frame)
    physics.regular_physics(drake)
    drake.physics.add(physics.apply_friction(5), components.physics.GROUP_VELOCITY)
    return drake
Ejemplo n.º 4
0
def viking(datadir, clock, keyboard, key_left, key_right, key_jump, key_punch):
    '''Make a viking controlled with the given keys'''

    from . import viking_parts

    player = components.entity('Player', clock, keyboard,
                               location=(100, 0),
                               motion=components.motion(),
                               graphics=components.graphics(None, None))

    physics.regular_physics(player)
    player.physics.add(physics.apply_friction(2.0, 1.0), components.physics.GROUP_VELOCITY)
    player.physics.add(physics.speed_limiter((10, 10000)), components.physics.GROUP_VELOCITY)

    player.controller = controls.Controller(player, viking_parts.IdleRight, viking_parts.IdleRightAnimation,
                                            {(key_left, KEYUP): 'left_release',
                                             (key_left, KEYDOWN): 'left_press',
                                             (key_right, KEYUP): 'right_release',
                                             (key_right, KEYDOWN): 'right_press',
                                             (key_jump, KEYUP): 'jump_release',
                                             (key_jump, KEYDOWN): 'jump_press',
                                             (key_punch, KEYUP): 'punch_release',
                                             (key_punch, KEYDOWN): 'punch_press',})
    return player