A small tweening module
Project description
tween
A tweening library inspired by flux by rxi. Using the PyTweening module by AlSweigart.
Installing
pip install tween
Example using a pygame setup
import sys
import pygame
from pygame.locals import QUIT
import tween
pygame.init()
screen = pygame.display.set_mode(400,400)
clock = pygame.time.Clock()
dt = 0.0
class Character:
def __init__(self, surface, x, y):
self.sprite = surface
self.x = x
self.y = y
def draw(surface):
surface.blit(self.sprite, (self.x, self.y))
hero_image = pygame.image.load("path/to/image.png")
hero = Character(hero_image, 0, 200)
hero_tween = tween.to(hero, "x", 400, 5.0, "easeInOutQuad") #Starting a tween.
def say_message():
print("Started moving!")
hero_tween.on_start(say_message) #Adding function that runs at the start of the tween-
#.on_start() will only have an effect if you call it before the first time the tween is updated
def update(dt):
tween.update(dt) #Updating all active tweens within the default group
def draw(surface):
surface.fill(0,0,0)
hero.draw(surface)
pygame.display.flip()
while 1:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
update(dt)
draw(screen)
dt = clock.tick(60) / 1000.0 #Divide by 1000.0 to get dt (time_passed) in seconds
Unless there is a typo in my code, the hero object should move from (y = 200, x = 0) to (y = 200, x = 400) in the span of exactly 5 seconds. Using "easeInOutQuad" the sprite will slowly accelerate and decelerate.
Functions and classes
tween.to(container, key, final_value, time, easing_type = "linear", _group = tween.tweens) --> tween.Tween
Creates and adds a Tween object to the default tween module group.
The container argument can be a list, dictionary or object.
- If the container is a list, the key must be an integer.
- If the container is a dictionary, the key must be a string.
- If the container is an object, the key must be a string.
final_value is the target value the tween will stop at.
time is how long the tween should take to finish in seconds.
easing_type is a string describing the easing function you want to use. There is a list of all types at the bottom of this readme.
If there already exists a tween for that container and key, the existing tween will delete itself os the new one can start
The _group argument should not be passed.
Instead you should create an instance of the tween.Group class, and call its .to method
tweening_group = tween.Group()
tweening_group.to(container, key, final_value, time, easing_type = "linear") --> tween.Tween
The tween module and all instances of tween.Group has an update function/method.
tween.update(passed_time, _group = tween.tweens)
tween.Group.update(passed_time)
The _group argument should not be passed here either. The tween module will pick the rigth group for you.
passed_time is the time since the last update was called in seconds.
If you want to pause all tweens within a certain group, it is as simple as not calling the .update method for that group.
The tween.Tween object returned from the .to function/method has a couple methods of its own.
tween.Tween.stop() #stops and deletes the tween from its group.
tween.Tween.on_start(func) #appends a function that runs at the start of the tween.
tween.Tween.on_update(func) #appends a function that runs every update.
tween.Tween.on_complete(func) #appends a function that runs at the end of the tween.
You can append as many functions as you like.
Easing types
- easeInBack
- easeInBounce
- easeInCirc
- easeInCubic
- easeInElastic
- easeInExpo
- easeInOutBack
- easeInOutBounce
- easeInOutCirc
- easeInOutCubic
- easeInOutElastic
- easeInOutExpo
- easeInOutQuad
- easeInOutQuart
- easeInOutQuint
- easeInOutSine
- easeInQuad
- easeInQuart
- easeInQuint
- easeInSine
- easeOutBack
- easeOutBounce
- easeOutCirc
- easeOutCubic
- easeOutElastic
- easeOutExpo
- easeOutQuad
- easeOutQuart
- easeOutQuint
- easeOutSine
- linear
If you want to get a list of all the easing types without reading this readme, you can do:
tween.print_ease_types()
or simply run the module directly in the terminal: python3 -m tween
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file tween-0.0.3.tar.gz
.
File metadata
- Download URL: tween-0.0.3.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 080491b80ce6aa06f5611d14362e049a506e5199bdf1d5d68e7628cb4a0c333d |
|
MD5 | 311e1537a3ab716f233acc1015465ad1 |
|
BLAKE2b-256 | b1feb742fd08c033814881b095ba969858b1b49cbf796463b2d7a024e9f5d77d |