PyGameUI Documentation: Animations

←Back: Buttons Next: Functions table→

Any UI element (text, inputs and general elements) can be moved around using a sweet of diffrent functions that are listet on the bottom of this page.

For now lets start with the fundamentals:

Animations: Flow and Jump

Animations are done by calling the functions on the UI element you want to animate. The functions are called on the UI element object.

In this case we use the flow function,

            
my_element = pygameui.Element((250, 250), content="Im movi'n")
my_element.flow((250, 250), (250, 400), 100)
            
        

Now the element knows that it should flow between two points, but as you've maybe noticed, it isn't doing much moving right now...

That's because we need to call the update() function on the element to make it move, put the update() function in your main loop:

            
my_element.update()
            
        

Now the element should be moving between the two points.

Final code:

            
import pygame, sys
import pygameui

pygame.init()

win = pygame.display.set_mode((500, 500))

my_element = pygameui.Element((250, 250), content="Im movi'n")
my_element.flow((250, 250), (250, 400), 100)

clock = pygame.time.Clock()
while True:
    win.fill((0, 0, 0))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    # Update element
    my_element.update()

    # Draw element
    my_element.draw(win)

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

Result:

By switching the flow() function with the jump() function, you can make the button jump between the two positions instead:

And lets decreate the iterations from 100 to 30 so it jumps a little faster.

Final code:

            
import pygame, sys
import pygameui

pygame.init()

win = pygame.display.set_mode((500, 500))

my_element = pygameui.Element((250, 250), content="Im movi'n")
my_element.jump((250, 250), (250, 400), 30)

clock = pygame.time.Clock()
while True:
    win.fill((0, 0, 0))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    # Update element
    my_element.update()

    # Draw element
    my_element.draw(win)

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

Result:

(The fast jumping once in a while is just the gif resetting)


Functions

Function Explanation Arguments
move_to()Moves the element to provided x,y cordinates (instantly, no animation)x, y
move()Moves the element by x, y steps (instantly, no animation)x, y
flow()Moves the element between two points. The more iterations the slower the animation. (Requires update())position1(x,y), position2(x,y), iterations
jump()Jumpes the element between two points. The more iterations the longer between the jumps. (Requires update())position1(x,y), position2(x,y), iterations
flow_toggle()Toggles flowing on or off.None
jump_toggle()Toggles jumping on or off.None
hide_toggle()Hides/shows the element.None
update()Every time update is called, 1 iteration for jump() and flow() is done.None