Skip to content

Instantly share code, notes, and snippets.

@Hammer2900
Created May 17, 2025 17:27
Show Gist options
  • Save Hammer2900/8bd43ff04700cdce7055100566039f94 to your computer and use it in GitHub Desktop.
Save Hammer2900/8bd43ff04700cdce7055100566039f94 to your computer and use it in GitHub Desktop.
ball animation lobster
import std
import vec
import color
import gl
let SCREEN_WIDTH = 800.0
let SCREEN_HEIGHT = 450.0
let RAIN_BASE_COLOR_RGB = float3 { 230.0/255.0, 41.0/255.0, 55.0/255.0 }
let CAPTION1 = "PRESS [R] TO RESTART"
let CAPTION2 = "PRESS [ENTER] TO PLAY AGAIN!"
let font_file="/home/izot/.local/share/fonts/InputMono-Regular.ttf"
let font_size=26
var ball_x = -100.0
var ball_radius = 20.0
var ball_alpha = 0.0
var anim_state = 0
var frame_count = 0.0
def ease_elastic_out(t:float, b:float, c:float, d:float) -> float:
if t == 0.0: return b
t /= d
if t == 1.0: return b + c
let p = d * 0.3
let a = c
let s = p / 4.0
return (a * pow(2.0, -10.0 * t) * sin(((t * d - s) * 360.0) / p) + c + b)
def ease_elastic_in(t:float, b:float, c:float, d:float) -> float:
if t == 0.0: return b
t /= d
if t == 1.0: return b + c
let p = d * 0.3
let a = c
let s = p / 4.0
return -(a * pow(2.0, 10.0 * (t - 1.0)) * sin(((t * d - s) * 360.0) / p)) + b
def ease_cubic_out(t:float, b:float, c:float, d:float) -> float:
t = t / d - 1.0
return c * (t * t * t + 1.0) + b
def main():
fatal(gl.window("Lobster Easings Ball Anim", int(SCREEN_WIDTH), int(SCREEN_HEIGHT)))
gl.set_target_delta_time(1.0 / 60.0)
check(gl.set_font_name(font_file) and gl.set_font_size(font_size), "can\'t load font!")
while gl.frame() and gl.button("escape") != 1:
if anim_state == 0: // Ball moves right
frame_count += 1.0
ball_x = ease_elastic_out(frame_count, -100.0, SCREEN_WIDTH / 2.0 + 100.0, 200.0)
if frame_count >= 200.0:
frame_count = 0.0
anim_state = 1
elif anim_state == 1: // Ball radius increases
frame_count += 1.0
ball_radius = ease_elastic_in(frame_count, 20.0, 500.0, 200.0) // Change in radius is 50 (from 20 to 70)
if frame_count >= 200.0:
frame_count = 0.0
anim_state = 2
elif anim_state == 2: // Ball becomes opaque (alpha increases)
frame_count += 1.0
ball_alpha = ease_cubic_out(frame_count, 0.0, 1.0, 200.0)
if frame_count >= 200.0:
frame_count = 0.0
anim_state = 3
elif anim_state == 3: // Wait for Enter to restart
if gl.button("return") == 1:
ball_x = -100.0
ball_radius = 20.0
ball_alpha = 0.0
anim_state = 0
frame_count = 0.0
if gl.button("r") == 1:
frame_count = 0.0
anim_state = 0
ball_x = -100.0
ball_radius = 20.0
ball_alpha = 0.0
gl.clear(color_white)
if anim_state >= 2:
gl.color(color_green)
gl.rect(float2 { SCREEN_WIDTH, SCREEN_HEIGHT })
gl.color(float4 { RAIN_BASE_COLOR_RGB.x, RAIN_BASE_COLOR_RGB.y, RAIN_BASE_COLOR_RGB.z, (1.0 - ball_alpha) })
gl.translate(float2 { ball_x, 200.0 }):
gl.circle(ball_radius, 30)
gl.color(color_black)
gl.translate(float2 {((SCREEN_WIDTH - float(gl.text_size(CAPTION1))) / 2).x, SCREEN_HEIGHT * 0.02 }):
gl.text(CAPTION1)
if anim_state >= 3:
gl.color(color_black)
gl.translate(float2 {((SCREEN_WIDTH - float(gl.text_size(CAPTION2))) / 2).x, SCREEN_HEIGHT * 0.4 }):
gl.text("PRESS [ENTER] TO PLAY AGAIN!")
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment