Skip to main content

Add your description here

Project description

transytion (beta)

transytion is a utility library for easily creating and managinig tweens in game engines like Pygame. It is heavily inspired by Flux and HUMP.timer from the Love2d community.

What is a tween?

A tween takes a variable and gradually changes it from one value to another as time progresses. A player may move from one point to another over a time period for instance. Thus, at the heart of it, a tween must:

  • Take a certain duration over which to change a variable.
  • Take one or more variables to change (called the targets)
  • Take values to gradually change them too.
  • A function that describes the gradual change.

Furthermore, it is often to have convenient to have a way to indicate to the program that the tween has finished. To do so, you may supply a callback function to execute once the tween has finished terminating.

How do I use it?

transytion-showcase

This library allows you to make tweens and compose them with other tweens that can be used a variety of cases. For instance, you can move objects, change their color, etc. The library operates on fields of objects. Let us see what that means with an example in Pygame-ce (although the example can be adapted to other game libraries).

import transytion as ty
from dataclasses import dataclass
from transytion.ease_funcs import quad
import pygame


pygame.init()
screen = pygame.display.set_mode((1280, 720))
clock = pygame.time.Clock()
running = True
dt = 0

@dataclass
class Ball:
    x: float
    y: float

ball = Ball(screen.get_width() / 2.0, 0.0)

# 1 second qudratic fall to center of screen.
fall = ty.Tween(1.0, # Duration of tween is 1 seconds.
                ball, # What object to mess with.
                {"y" : screen.get_height() / 2}, # Animate what to where.
                ease_func=quad) # How to animate it (defaults to linear)

ty.default_manager.add(fall) # Start the tween.

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    ty.default_manager.update(dt)
 
    screen.fill((0,0,0))
    pygame.draw.circle(screen, "red", (ball.x, ball.y), 40)

    pygame.display.flip()
    dt = clock.tick(60) / 1000

pygame.quit()

This is a modification of the first example presented here. It is recommended you become familiar with that example, and then continue with this example. If you run the program, a red circle moves down the screen. What is going on?

@dataclass
class Ball:
    x: float
    y: float

This is used to keep track of the location of the ball on the screen. Tweens operate on fields of objects, so by making a Ball object we may tween the y (or x) fields.

# 1 second qudratic fall to center of screen.
fall = ty.Tween(1.0, # Duration of tween is 1 seconds.
                ball, # What object to mess with.
                {"y" : screen.get_height() / 2}, # Animate what to where.
                ease_func=quad) # How to animate it (defaults to linear)

This constructs our tween. It should be pointed out, by itself, the Tween object does not do anything until we tell it to run.

ty.default_manager.add(fall) # Start the tween.

Once the tween is added to a TweenManager object, (such as ty.default_manager) the tween begins execution. You can make as many manager objects as you want, but you need at least one, and it is perfectly reasonable to use just one. That is why transytion includes a default_manager for ease of use.

Lastly, the TweenManager needs the time to update each tween it is managing. This is done by going to your game loop and adding the line

ty.default_manager.update(dt)

Making more complicated tweens with chain

If you perform

ty.default_manager.add(t1)
ty.default_manager.add(t2)

ty.default_manager will run both tweens simultaneously. If we want to run t1 to execute and then t2 we may chain them together:

t3 = chain([t1, t2])
ty.default_manager.add(t3)

Using chain, complicated tweens can be made from smaller tweens. See this for a complete example.

Decorators: Another Way to Tween

Often when thinking in game development terms, it can be tempting to think of game logic and then easing and tweens as an afterwards. Unfortunately, this can result in a lot of code restructuring. Transytion can help prevent major refactoring by utilizing Python decorators.

Consider the following scenario: You want a player to move and then say something. Ignoring animations, one might write:

def say_something():
    print("Hello!")

say_something()

But if we follow the example above it is a little awkward to combine this with move:

def say_something():
    print("Hello!")

move = Tween(..., callback=say_something)

...

Transytion uses decorators to minimize cognitive load:

move = Tween(...)
@tween_then_call(move)
def say_something():
    print("Hello!")

say_something()

...

The @tween_then_call(tween) decorator delays function calls to execute after the supplied tween executes. Thus, we can focus on game logic first then decorate the logic to incorporate tweens.

A full example of this (that is, with a gameloop) can be found here.

Documentation

Further documentation can be found on readthedocs.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

transytion-0.2.0.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

transytion-0.2.0-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file transytion-0.2.0.tar.gz.

File metadata

  • Download URL: transytion-0.2.0.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for transytion-0.2.0.tar.gz
Algorithm Hash digest
SHA256 ab3f773792b80e0cfb83c18b50145f4b78cd3774a129c7dcb6c82acc6f940295
MD5 20211e4f3e64bd2e149eb0da664ea40f
BLAKE2b-256 775b3453d1e99c2baceb68a821836994bc9a065ad7cf837813ab22ea59ad004a

See more details on using hashes here.

Provenance

The following attestation bundles were made for transytion-0.2.0.tar.gz:

Publisher: python-publish.yml on thyrgle/transytion

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file transytion-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: transytion-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 7.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for transytion-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f01ec1d86cf2f1cac06b64152db12f0ab278588a250c8dd90e4b81facf70245c
MD5 7f7fdaab71a1283e5bb0ea599d4a33c4
BLAKE2b-256 6e1225b3617037837df8d8056ec0c652bf0bb6517607f38f87c706ae29b78617

See more details on using hashes here.

Provenance

The following attestation bundles were made for transytion-0.2.0-py3-none-any.whl:

Publisher: python-publish.yml on thyrgle/transytion

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page