Giter VIP home page Giter VIP logo

tweener's Introduction

Tweener

Helper functions for easing and tweening

Installation

  pip install tweener

Reference

Tween object

Tween(begin, end, duration, easing, easing_mode, boomerang, loop, reps)
Parameter Type Description
begin float The beginning value of a property (default: 0.0)
end float The end value of a property (default: 1.0)
duration int The length of time that an animation takes to complete (default: 600 ms)
easing Easing The type of easing to apply. (default: Easing.LINEAR)
easing_mode EasingMode The mode of easing IN, OUT, IN_OUT, (default: IN)
boomerang bool Returns the animation back to its starting point and vice versa. (default: False)
loop bool Loops the animation. When used with boomerang, it does the back and forth animation. (default: False)
reps int Number of times the animation will be repeated. Zero means infinite. (default: 0)

Starts the animation

Tween().start()

Updates the animation state

Tween().update()

Pauses the animation

Tween().pause()

Resumes the animation

Tween().resume()

Easing types

LINEAR
SINE
QUAD
CUBIC
QUART
QUINT
EXPO
CIRC
BACK
ELASTIC
BOUNCE

Easing modes (except linear)

IN
OUT
IN_OUT

Example

I used Python version 3.10.5 and pygame 2.1.2

import pygame
from tweener import *

WIDTH = 600
HEIGHT = 600
TITLE = "Motion Demo"
FPS = 60

pygame.init()
canvas = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption(TITLE)

running = True
clock = pygame.time.Clock()

rect = pg.Rect(0, 100, 100, 100)
anim_y = Tween(begin=0, 
               end=WIDTH-100,
               duration=1000,
               easing=Easing.BOUNCE,
               easing_mode=EasingMode.OUT,
               boomerang=True, 
               loop=True)

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYUP:
            match event.key:
                case pygame.K_SPACE:
                    anim_y.start()
                case pygame.K_p:
                    anim_y.pause()
                case pygame.K_r:
                    anim_y.resume()

    canvas.fill(pygame.Color(255, 255, 255))

    # Update
    anim_y.update()
    rect.x = anim_y.value

    # Render
    pygame.draw.rect(canvas, (255, 0, 0), rect)
    pygame.display.flip()
    clock.tick(FPS)

pygame.quit()

Demo

demo.mp4

Contributing

Contributions are always welcome!

References

tweener's People

Contributors

jpsikstus avatar nobrelli avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.