Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added easing functions + demo #307

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions examples/basic/easing_functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from pgzero.builtins import *
from pgzero import animation
import itertools

WIDTH = 800
HEIGHT = 800
#print(animation.TWEEN_FUNCTIONS
MARGIN = 20

def position_for_block(i):
BLOCK_POSITIONS = [[(150, (i * 50 + i * MARGIN)), (WIDTH - 150, (i * 50 + i * MARGIN))] for i in range(len(animation.TWEEN_FUNCTIONS))]
return BLOCK_POSITIONS[i]



positions = [itertools.cycle(position_for_block(i)) for i in range(len(animation.TWEEN_FUNCTIONS))]
blocks = [Actor('block', center=next(positions[i])) for i in range(len(animation.TWEEN_FUNCTIONS))]

line_centers = [(WIDTH/2, i * (50 + MARGIN)) for i in range(len(animation.TWEEN_FUNCTIONS))]

def draw():
screen.clear()
#[block.draw() for block in blocks]
for i, easing_f_name in enumerate(animation.TWEEN_FUNCTIONS):
block = blocks[i]
screen.draw.text(easing_f_name, line_centers[i])
block.draw()


def move_blocks():
for i, easing_f_name in enumerate(animation.TWEEN_FUNCTIONS):
animate(
blocks[i],
easing_f_name,
duration=5,
pos = next(positions[i])

)

"""
def move_block():
animate(
block,
'bounce_end',
duration=1,
pos=next(block_positions)
)
""";

move_blocks() # start one move now
clock.schedule_interval(move_blocks, 6)

22 changes: 21 additions & 1 deletion src/pgzero/animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# http://www.clutter-project.org/docs/clutter/stable/ClutterAlpha.html


from math import sin, pow, pi
from math import sin, cos, pow, pi

from .clock import each_tick, unschedule
from .spellcheck import suggest
Expand Down Expand Up @@ -75,6 +75,26 @@ def in_out_elastic(n):
return pow(2, -10 * q) * sin((q - s) * (2.0 * pi) / p) * .5 + 1.0


@tweener
def out_sine(n):
return sin(n * pi/2)


@tweener
def in_sine(n):
return 1 - cos(n * pi/2)


@tweener
def in_out_sine(n):
return -(cos(pi * n) - 1)/2


@tweener
def out_expo(n):
return 1 if n == 1 else 1 - pow(2, -10 * n)


def _out_bounce_internal(t, d):
p = t / d
if p < (1.0 / 2.75):
Expand Down
Loading