"""Streak level 0: a 7-day streak. Eight 2-second celebrations, each showing the streak number. The worked example for the other levels: fn(i, t) -> shapes, i = frame 0..23, t = 0..1; NUM(...) is where the number goes (the app fills it in: 7 here, but lay out for up to 3 digits, since the top level is reused for 465, 565, ...). K(colour) switches colour for the shapes after it, so the number can stand out from what it sits on. Sounds: level0_sounds.py, same names as below. """ import math from anim_kit import NUM, K, C, L, P, ease_out, ring, rotate, star def rays(cx, cy, r0, r1, n, rot=0.0, w=0.1): """n short lines radiating from (cx, cy), between radius r0 and r1.""" out = [] for k in range(n): a = rot + k * 2 * math.pi / n out.append(L([(cx + r0 * math.cos(a), cy + r0 * math.sin(a)), (cx + r1 * math.cos(a), cy + r1 * math.sin(a))], w)) return out def pop_scale(t, t0, dur=0.12, over=1.25): """0 before t0, then grows to over and settles back to 1 (a springy pop).""" if t < t0: return 0.0 u = (t - t0) / dur if u < 1: return over * ease_out(u) return 1 + (over - 1) * math.exp(-(u - 1) * 1.6) * math.cos((u - 1) * 2.2) def megaphone(i, t): """A megaphone pops up out of the box and blares: sound waves pulse out and the number bursts from its mouth, bigger with every pulse.""" s = [] up = ease_out(min(1.0, t * 4)) kick = 0.22 if i % 6 in (3, 4) and i >= 3 else 0.0 # recoils on each blare ang = -0.6 # pointing up and to the right ax, ay = -0.9 - kick * math.cos(ang), -0.6 - 1.2 * up - kick * math.sin(ang) # the narrow end ca, sa = math.cos(ang), math.sin(ang) def at(x, y): return (ax + x * ca - y * sa, ay + x * sa + y * ca) s.append(P([at(-0.3, -0.3), at(0.1, -0.33), at(2.3, -1.15), at(2.3, 1.15), at(0.1, 0.33), at(-0.3, 0.3)])) s.append(P([at(0.4, 0.3), at(0.75, 0.32), at(0.7, 1.1), at(0.35, 1.1)])) # the handle s.append(K("red")) s.append(P([at(2.3, -1.2), at(2.55, -1.3), at(2.55, 1.3), at(2.3, 1.2)])) # the mouth's rim mx, my = at(2.7, 0) if i >= 3: s.append(K("yellow")) for k in range(3): # the waves, each pulse a little further age = ((i - 3) % 6) + k * 2 r = 0.6 + age * 0.4 if r < 3.2: arc = [(mx + r * math.cos(ang + a), my + r * math.sin(ang + a)) for a in (-0.75, -0.4, 0, 0.4, 0.75)] s.append(L(arc, 0.16 - age * 0.01)) g = pop_scale(t, 3 / 23, 0.15, 1.35) * (1 + 0.14 * (i % 6 in (3, 4))) d = 1.4 + 1.4 * min(1.0, (i - 3) / 6) s.append(K("red")) s.append(NUM(mx + d * ca, my + d * sa - 0.3, 1.8 * g, rot=0.14 * math.sin(i * 1.3))) return s def slotmachine(i, t): """A slot machine rises beside the box; its lever is pulled, the reel whirs, and it lands on the number as lights flash and coins spill out.""" s = [] rise = ease_out(min(1.0, t * 5)) y0 = 1.0 - rise * 1.0 # it rises a unit out of the box top, bot = y0 - 4.6, y0 - 0.6 s.append(P([(-1.9, bot), (1.9, bot), (1.9, top + 0.4), (1.5, top), (-1.5, top), (-1.9, top + 0.4)])) s.append(P([(-1.4, bot + 0.2), (1.4, bot + 0.2), (1.2, bot + 0.6), (-1.2, bot + 0.6)])) # the tray pull = 1.0 if 3 <= i <= 5 else 0.5 if i in (2, 6) else 0.0 kx, ky = 2.5, top + 0.4 + pull * 2.2 s.append(L([(1.9, top + 2.2), (2.5, top + 2.2), (kx, ky)], 0.14)) s.append(C(kx, ky, 0.28)) wy0, wy1 = top + 1.0, top + 2.8 # the window s.append(K("yellow")) s.append(P([(-1.45, wy0), (1.45, wy0), (1.45, wy1), (-1.45, wy1)], 0, 0.03)) s.append(K("blue")) if i < 13: # the reel spins: digits whizz past for k in range(3): y = wy0 + 0.2 + ((i * 0.55 + k * 0.6) % 1.4) s.append(L([(-1.1, y), (1.1, y)], 0.09)) if 4 <= i: s.append(NUM(0, wy0 + 0.3 + (i % 2) * 1.0, 0.5, rot=0.0)) else: b = [0.35, -0.12, 0.05, 0.0][min(3, i - 13)] s.append(NUM(0, (wy0 + wy1) / 2 + b, 1.15)) if i >= 13: # lights on top flash for k in range(5): s.append(K(["red", "blue", "yellow"][(k + i) % 3])) s.append(C(-1.2 + k * 0.6, top - 0.3, 0.2 if (k + i) % 2 else 0.13)) s.append(K("yellow")) for m in range(7): # coins spill from the tray u = (i - 13 - m * 0.6) / 10 if 0 < u <= 1: vx = -2.2 + m * 0.75 x, y = vx * u * 1.4, bot + 0.4 - 2.6 * u + 4.5 * u * u s.append(P(ring(x, y, 0.2, 8, 0, 0.02))) return s def bannerplane(i, t): """A little plane buzzes across above the box towing a banner with the number on it, the banner rippling in its wake.""" s = [] x = -1.0 + t * 5.6 y = -3.8 + 0.18 * math.sin(i * 0.8) z = 1.35 # the plane's size def at(px, py): return (x + px * z, y + py * z) s.append(P([at(-0.9, -0.05), at(0.6, -0.25), at(1.0, 0), at(0.6, 0.25), at(-0.9, 0.12)])) s.append(P([at(-0.75, -0.05), at(-1.05, -0.65), at(-0.65, -0.6), at(-0.45, -0.1)])) # tail fin flap = 0.2 if i % 2 else -0.1 s.append(P([at(-0.1, 0), at(0.35, 0), at(0.15, 0.6 + flap), at(-0.3, 0.6 + flap)])) # wing s.append(K("yellow")) s.append(C(*at(0.45, -0.08), 0.12)) # the window s.append(K("blue")) if i % 2: # the propeller, a blur s.append(L([at(1.1, -0.45), at(1.1, 0.45)], 0.09)) else: s.append(L([at(1.05, -0.15), at(1.15, 0.15)], 0.13)) bx1 = x - 1.25 * z - 0.5 # the banner's leading edge bw, bh = 4.0, 1.75 s.append(L([at(-0.9, 0), (bx1, y - 0.3), (bx1, y + 0.3)], 0.05)) def wave(px): return 0.2 * math.sin((bx1 - px) * 1.8 - i * 1.5) xs = [bx1 - bw * k / 8 for k in range(9)] edge = [(px, y - bh / 2 + wave(px)) for px in xs] + [(px, y + bh / 2 + wave(px) * 1.3) for px in reversed(xs)] s.append(K("yellow")) s.append(P(edge, 0, 0.03)) s.append(K("blue")) s.append(P(edge, 0.08, 0.03)) s.append(K("red")) cx = bx1 - bw / 2 s.append(NUM(cx, y + wave(cx) + 0.05, 1.3, rot=-0.15 * math.cos((bw / 2) * 1.8 - i * 1.5))) if i < 5: # whoosh lines as it sweeps in s.append(K("blue")) for k in range(3): s.append(L([(xs[-1] - 0.4 - k * 0.3, y - 0.7 + k * 0.7), (xs[-1] - 1.2 - k * 0.3, y - 0.7 + k * 0.7)], 0.07)) return s def cannon(i, t): """A little cannon rolls out by the box, its fuse fizzes, and BOOM: the number is fired high in an arc and lands with a bounce.""" s = [] rec = 0.55 if i in (6, 7) else 0.25 if i == 8 else 0.0 cx, cy = -1.0 - rec, -0.5 ang = -0.75 z = 1.35 barrel = rotate([(-0.6 * z, -0.45 * z), (1.5 * z, -0.34 * z), (1.6 * z, -0.42 * z), (1.6 * z, 0.42 * z), (1.5 * z, 0.34 * z), (-0.6 * z, 0.45 * z)], ang) s.append(P([(cx + x, cy + y) for x, y in barrel])) s.append(K("blue")) wx, wy = cx - 0.15, cy + 0.35 s.append(C(wx, wy, 0.6, 0.16)) # the wheel and its spokes for k in range(3): a = k * math.pi / 3 + rec * 3 + i * 0.05 s.append(L([(wx + 0.55 * math.cos(a), wy + 0.55 * math.sin(a)), (wx - 0.55 * math.cos(a), wy - 0.55 * math.sin(a))], 0.08)) mx, my = cx + 1.7 * z * math.cos(ang), cy + 1.7 * z * math.sin(ang) # the muzzle s.append(K("red")) if i < 6: # the fuse burns down bx, by = cx - 0.7 * z * math.cos(ang), cy - 0.7 * z * math.sin(ang) fx, fy = bx - 0.45 + i * 0.05, by - 0.3 + i * 0.03 s.append(L([(bx, by), (fx, fy)], 0.06)) s.append(K("yellow")) s.append(P(star(fx, fy, 0.35 + (i % 2) * 0.15, 0.13, 6, i))) elif i <= 8: # the blast s.append(K("yellow")) s.append(P(star(mx + 0.4, my - 0.4, 1.7 - (i - 6) * 0.4, 0.7, 10, i))) if i >= 6: s.append(K("red")) for k in range(3): # smoke puffs drifting off age = i - 6 - k if 0 <= age < 9: s.append(C(mx - 0.3 + 0.3 * k - age * 0.15, my - 0.3 - age * 0.2, 0.25 + age * 0.08, 0.08)) u = min(1.0, (i - 6) / 9) # the number's flight tx, ty = 3.6, -0.75 x = mx + (tx - mx) * u y = my + (ty - my) * u - 5.0 * u * (1 - u) h = 0.5 + 1.2 * u if i > 15: # landed: bounce and squash b = [0.0, -0.6, -0.8, -0.55, 0.0, -0.2, 0.0, 0.0][min(7, i - 16)] y = ty + b h = 1.7 s.append(K("blue")) s.append(NUM(x, y, h, rot=(1 - u) * -4.0)) if i in (15, 16, 20): s.append(K("red")) s += rays(tx, ty + 0.9, 0.4, 0.9, 5, math.pi * 0.9, 0.08) return s def drum(i, t): """A drum stands on the box and gets a rapid drumroll, then a cymbal CRASH and the number leaps out of the drum.""" s = [] top, bot = -2.0, -0.2 w = 1.3 s.append(P([(-w, top), (w, top), (w, bot), (-w, bot)])) s.append(K("yellow")) s.append(L([(-w + k * w / 4, top + 0.25 if k % 2 else bot - 0.2) for k in range(9)], 0.09)) # the lacing s.append(P([(w * math.cos(a), top + 0.36 * math.sin(a)) for a in [k * math.pi / 8 for k in range(16)]])) s.append(K("red")) hit = i < 13 for side in (-1, 1): # the sticks, left and right in turn down = hit and (i + (side > 0)) % 2 == 1 if hit: tipx, tipy = side * 0.45, top - (0.0 if down else 1.0) else: # done: held up out of the way tipx, tipy = side * (1.5 if side < 0 else 1.0), top + 0.2 hx, hy = (tipx + side * 1.6, tipy - (0.4 if down else 0.9)) if hit else (tipx + side * 0.9, tipy - 1.5) s.append(L([(hx, hy), (tipx, tipy)], 0.13)) s.append(C(tipx, tipy, 0.16)) if down: s += rays(tipx, top - 0.1, 0.25, 0.6, 3, -math.pi * 0.5 - 0.6, 0.06) s.append(K("blue")) s.append(L([(2.3, -0.2), (2.3, -2.8)], 0.09)) # the cymbal on its stand tilt = 0.25 if hit else 0.25 + 0.35 * math.sin((i - 13) * 2.0) * math.exp(-(i - 13) * 0.2) s.append(K("yellow")) s.append(P(rotate([(1.5, -2.8), (2.3, -3.02), (3.1, -2.8), (2.3, -2.7)], tilt, 2.3, -2.8))) if not hit: s += rays(2.3, -2.85, 1.0, 1.4 + 0.25 * (i % 2), 7, i * 0.2, 0.09) g = pop_scale(t, 13 / 23, 0.12, 1.3) y = top - 1.0 - 1.6 * ease_out(min(1.0, (i - 13) / 4)) s.append(K("red")) s.append(NUM(-0.3, y, 1.8 * g, rot=0.1 * math.sin(i))) if i % 2: s.append(K("blue")) s += rays(-0.3, y, 1.6, 2.0, 10, i * 0.15, 0.08) return s def pinata(i, t): """A star pinata swings over the box; a stick whacks it, whacks it again, and the third time it bursts: sweets fly and the number drops out.""" s = [] px, py = 0.4, -6.2 # where its string hangs from hits = (4, 9, 14) sw = 0.0 for h in hits: if i >= h: sw = 0.4 * math.exp(-(i - h) * 0.3) * math.sin((i - h) * 1.1 + 0.9) bx, by = px + 2.6 * math.sin(sw), py + 2.6 * math.cos(sw) for h in hits: # the stick swings in from the left if h - 2 <= i <= h: a = [-1.7, -1.0, -0.3][i - h + 2] s.append(K("red")) s.append(L([(-2.8, -0.2), (-2.8 + 3.0 * math.cos(a), -0.2 + 3.0 * math.sin(a))], 0.18)) s.append(K("yellow")) if i < hits[2]: rot = -math.pi / 2 + sw s.append(L([(px, py), (bx, by - 1.3)], 0.05)) s.append(P(star(bx, by, 1.6, 0.85, 5, rot))) s.append(K("blue")) for k in range(5): # tassels off each point a = rot + k * 2 * math.pi / 5 tx, ty = bx + 1.6 * math.cos(a), by + 1.6 * math.sin(a) s.append(L([(tx, ty), (tx + 0.15 * math.sin(i + k), ty + 0.45)], 0.08)) s.append(C(bx, by, 0.35)) s.append(K("yellow")) if i in (hits[0], hits[0] + 1, hits[1], hits[1] + 1): s += rays(bx - 1.2, by + 0.3, 0.2, 0.7, 4, math.pi * 0.75, 0.09) else: k = i - hits[2] for m in range(12): # bits of pinata and sweets a = m * 2 * math.pi / 12 + 0.4 r = 0.6 + k * 0.55 x, y = bx + r * math.cos(a), by + r * math.sin(a) + k * k * 0.07 if m % 3 == 0: s.append(K(["red", "blue"][m % 2])) s.append(C(x, y, 0.2)) else: s.append(K("yellow")) s.append(P(rotate([(x - 0.25, y - 0.15), (x + 0.25, y - 0.12), (x + 0.05, y + 0.2)], m + k, x, y))) if k <= 1: s.append(K("yellow")) s.append(P(star(bx, by, 2.1 - k * 0.4, 0.9, 10, 0.2), 0.12)) drop = min(1.0, k / 4) b = [0, 0, 0, 0, 0, -0.4, -0.12, 0, 0, 0][min(9, k)] s.append(K("red")) s.append(NUM(bx, by + drop * 2.2 + b, 0.9 + 0.9 * drop, rot=(1 - drop) * 0.7)) return s def champagne(i, t): """A bottle rises out of the box, shakes, and POP: the cork flies, a fountain of fizz sprays up, and the number rides up on the bubbles.""" s = [] rise = ease_out(min(1.0, t * 4)) shake = 0.18 * math.sin(i * 2.6) if 3 <= i < 8 else 0.0 by = 0.2 - 1.2 * rise z = 1.5 def sc(pts): return rotate([(x * z, by + (y - by) * z) for x, y in pts], shake, 0, by) s.append(P(sc([(-0.45, by), (0.45, by), (0.45, by - 1.5), (0.15, by - 2.0), (0.15, by - 2.5), (-0.15, by - 2.5), (-0.15, by - 2.0), (-0.45, by - 1.5)]))) s.append(K("yellow")) s.append(P(sc([(-0.45, by - 0.5), (0.45, by - 0.5), (0.45, by - 1.1), (-0.45, by - 1.1)]))) # label s.append(P(sc([(-0.15, by - 2.1), (0.15, by - 2.1), (0.15, by - 2.45), (-0.15, by - 2.45)]))) # foil nx, ny = sc([(0, by - 2.55)])[0] # the neck's mouth s.append(K("red")) if i < 8: s.append(P(rotate([(nx - 0.25, ny), (nx + 0.25, ny), (nx + 0.3, ny - 0.55), (nx - 0.3, ny - 0.55)], shake, nx, ny))) else: # the cork flies off k = i - 8 cx, cy = nx + k * 0.45, ny - 0.4 - k * 0.9 + k * k * 0.05 s.append(P(rotate([(cx - 0.25, cy), (cx + 0.25, cy), (cx + 0.3, cy - 0.55), (cx - 0.3, cy - 0.55)], k * 0.9, cx, cy))) if k < 2: s.append(K("yellow")) s.append(P(star(nx, ny - 0.3, 1.2, 0.4, 8, i))) s.append(K("blue")) for m in range(9): # the fountain of fizz a = -math.pi / 2 + (m - 4) * 0.22 for q in range(2): r = (k - q * 2) * 0.55 if r > 0: x = nx + r * math.cos(a) y = ny + r * math.sin(a) + (r * 0.35) ** 2 s.append(C(x, y, 0.1 + 0.05 * ((m + q + i) % 2), 0 if (m + q) % 2 else 0.05)) u = ease_out(min(1.0, k / 8)) y = ny - 0.8 - 2.6 * u s.append(K("red")) s.append(NUM(nx + 0.12 * math.sin(i * 0.9), y, 0.7 + 1.0 * u, rot=0.12 * math.sin(i * 0.7))) return s def stamp(i, t): """A big rubber stamp hovers, rears up and comes down above the box with a KACHUNK, then lifts away: the number left stamped inside a jagged seal.""" s = [] cx, cy = 0.2, -2.6 # where it lands ys = [-3.0, -3.3, -3.6, -3.9, -3.2, -1.5, 0.0, 0.0, 0.0, 0.0, -0.3, -1.2, -2.6, -4.2, -6.0] off = ys[i] if i < len(ys) else None if i >= 7: # the imprint g = 1.12 if i == 7 else 1.0 tilt = -0.14 s.append(P(rotate(star(cx, cy, 1.9 * g, 1.68 * g, 18, i * 0.02), tilt, cx, cy), 0.1)) s.append(P(rotate(ring(cx, cy, 1.45 * g, 20, 0, 0.03), tilt, cx, cy), 0.06)) s.append(NUM(cx, cy + 0.02, 1.1 * g, rot=tilt)) if 7 <= i <= 9: s += rays(cx, cy, 2.2, 2.8 + (i - 7) * 0.3, 10, 0.3, 0.09) if off is not None: # the stamp itself y = cy + off sq = 0.82 if i == 6 else 1.0 s.append(K("blue")) s.append(P([(cx - 1.6, y - 0.2 * sq), (cx + 1.6, y - 0.2 * sq), (cx + 1.5, y - 0.75 * sq), (cx - 1.5, y - 0.75 * sq)])) s.append(P([(cx - 0.3, y - 0.75 * sq), (cx + 0.3, y - 0.75 * sq), (cx + 0.24, y - 1.7 * sq), (cx - 0.24, y - 1.7 * sq)])) s.append(C(cx, y - 2.05 * sq, 0.48)) if i == 6: s.append(K("red")) s += rays(cx, y - 0.1, 1.9, 2.4, 8, 0.0, 0.1) return s ANIMS = [ (megaphone, "yellow", "blare"), (slotmachine, "red", "jackpot"), (bannerplane, "blue", "propeller"), (cannon, "red", "kaboom"), (drum, "blue", "drumroll"), (pinata, "yellow", "whack"), (champagne, "blue", "uncork"), (stamp, "red", "kachunk"), ]