Exemple #1
0
def mainloop(device):
    world = World()

    finished = False
    clock = pygame.time.Clock()
    begin_motion = Vector2D(0.0, 0.0)
    end_motion = Vector2D(0.0, 0.0)
    #font = pygame.font.Font(None, 36)
    #text = font.render("Pummel The Chimp, And Win $$$", 1, (10, 10, 10))
    bigbody = True
    move = 6
    
    while not finished:
        for e in pygame.event.get():
            if e.type == pygame.QUIT:
                finished = True
                break
            elif e.type == pygame.KEYDOWN:
                sc = world.root.scale.x
                if e.key == pygame.K_PAGEDOWN:
                    world.root.scale *= 0.9
                elif e.key == pygame.K_PAGEUP:
                    world.root.scale *= 1.1
                elif e.key == pygame.K_DOWN:
                    world.root.move.y -= move*sc
                elif e.key == pygame.K_UP:
                    world.root.move.y += move*sc
                elif e.key == pygame.K_LEFT:
                    world.root.move.x += move*sc
                elif e.key == pygame.K_RIGHT:
                    world.root.move.x -= move*sc
                elif e.key == pygame.K_SPACE:
                    bigbody = not bigbody
                elif e.key == pygame.K_TAB:
                    move += 2/sc
            elif e.type == pygame.KEYUP:
                if e.key == pygame.K_DOWN:
                    world.root.move.y = 0
                elif e.key == pygame.K_UP:
                    world.root.move.y = 0
                elif e.key == pygame.K_LEFT:
                    world.root.move.x = 0
                elif e.key == pygame.K_RIGHT:
                    world.root.move.x = 0
            elif e.type == pygame.MOUSEBUTTONDOWN:
                sc = world.root.scale.x
                begin_motion = (Vector2D(e.pos[0], e.pos[1]) - world.root.position)/sc
            elif e.type == pygame.MOUSEBUTTONUP:
                sc = world.root.scale.x
                end_motion = (Vector2D(e.pos[0], e.pos[1]) - world.root.position)/sc

                # arbitrary constant to give it a little more oomph :)
                motion = (end_motion - begin_motion) * 50

                mass = 1
                if not bigbody:
                    mass = 0.1
                node = MyNode(mass, world)
                node.position = (Vector2D(e.pos[0], e.pos[1]) - world.root.position)/sc
                node.last_position = node.position
                node.scale = Vector2D(40.0, 40.0)
                node.push = motion

                world.root.add(node)

        """for n in world.root._children:
            n.mass = 0
            for c in n.world.root._children:
                if c == n:
                    continue
                dist = (c.position - n.position).magnitude() / 100.0
                n.mass += n.trumass/math.e**dist
            n.mass /= len(world.root._children) + 0.01"""

        device.clear()
        world.update(clock.tick(60) / 1000.0)
        world.render()
        device.flip()
        pygame.time.wait(0)