Example #1
0
def main():
    pygame.init()
    screen = pygame.display.set_mode((800, 600))
    pygame.display.set_caption("blocks.")
    clock = pygame.time.Clock()
    world = World()
    world.load_level(sys.argv[1])

    #def handle_collision(space, arbiter):
    #    if target.contour not in arbiter.shapes:
    #        return
    #    print "OUCH"
    #space.set_default_collision_handler(None, None, None, handle_collision)

    running = True
    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 == MOUSEBUTTONDOWN:
                if event.button == 1:
                    mpos = pygame.mouse.get_pos()
                    mpos = mpos[0], screen.get_height() - mpos[1]
                    pygame.mouse.get_rel()
            elif event.type == MOUSEBUTTONUP:
                if event.button == 1:
                    ball = Ball(world.space, *mpos, radius=20)
                    rel = pygame.mouse.get_rel()
                    rel = rel[0] * 2, -rel[1] * 2
                    world.append(ball)
                    ball.apply_impulse(*rel)

        screen.fill(THECOLORS["white"])

        world.space.step(1/200.0)

        world.draw(screen)

        pygame.display.flip()
        clock.tick(200)
Example #2
0
def main():
    pygame.init()
    screen = pygame.display.set_mode((800, 600))
    pygame.display.set_caption("blocks.")
    clock = pygame.time.Clock()
    world = World()
    world.load_level(sys.argv[1])

    running = True
    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
                world.save_level(sys.argv[2])
            elif event.type == MOUSEBUTTONDOWN:
                if event.button == 1:
                    mpos = pygame.mouse.get_pos()
                    mpos = mpos[0], screen.get_height() - mpos[1]
                    pygame.mouse.get_rel()
            elif event.type == MOUSEBUTTONUP:
                if event.button == 1:
                    ball = Ball(world.space, *mpos, radius=20)
                    rel = pygame.mouse.get_rel()
                    rel = rel[0] * 2, -rel[1] * 2
                    world.append(ball)
                    ball.apply_impulse(*rel)
            elif event.type == KEYDOWN and event.key == 115:
                world.space.step(1/200.0)
                print list(world.get_state())

        screen.fill(THECOLORS["white"])

        world.draw(screen)

        pygame.display.flip()
        clock.tick(200)