Skip to content

Commit

Permalink
added easing functions + demo
Browse files Browse the repository at this point in the history
  • Loading branch information
saraaldosari committed Nov 10, 2022
1 parent 5b68f3d commit 71d4535
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
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)

18 changes: 17 additions & 1 deletion 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,22 @@ 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

0 comments on commit 71d4535

Please sign in to comment.