Created
May 17, 2025 09:45
-
-
Save Hammer2900/f47572fe4bf1d16d82d22f943698e2db to your computer and use it in GitHub Desktop.
comet particles lobster
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import std | |
import vec | |
import color | |
import gl | |
let WIDTH = 800 | |
let HEIGHT = 600 | |
let MAX_TAIL_LENGTH = 50 | |
// Particle class to represent parts of the comet's tail | |
class Particle: | |
x:float | |
y:float | |
alpha:float | |
offset_y:float // For wavy motion | |
speed:float // Speed of wavy motion | |
amplitude:float// Amplitude of wavy motion | |
def main(): | |
fatal(gl.window("Comet with Dynamic Tail", WIDTH, HEIGHT)) | |
gl.set_target_delta_time(1.0 / 60.0) | |
// Comet properties | |
var comet_x = 0.0 | |
let comet_y = HEIGHT / 2.0 | |
let comet_speed = 5.0 | |
// Tail: a vector of Particle objects | |
let tail:[Particle] = [] | |
// Main game loop | |
while gl.frame() and gl.button("escape") != 1: | |
// Move comet | |
comet_x += comet_speed | |
if comet_x > WIDTH: | |
comet_x = 0.0 | |
// Add new particle to the head of the tail | |
let new_particle = Particle { | |
x: comet_x, | |
y: comet_y, | |
alpha: 1.0, | |
offset_y: 0.0, | |
speed: rnd_float() * (0.5 - 0.1) + 0.1, | |
amplitude: rnd_float() * (5.0 - 1.0) + 1.0 | |
} | |
insert(tail, 0, new_particle) | |
// Limit tail length | |
if tail.length > MAX_TAIL_LENGTH: | |
tail.truncate(MAX_TAIL_LENGTH) | |
// Update tail particles | |
let current_time = gl.time() | |
for(tail) p, i: // Iterate with particle (p) and index (i) | |
// Fade out particle | |
p.alpha -= 0.02 | |
p.alpha = max(0.0, p.alpha) // Clamp alpha to be >= 0 | |
// Add wavy motion | |
let wave_angle_deg = current_time * p.speed * 50.0 + float(i) * 10.0 | |
p.offset_y = sin(wave_angle_deg) * p.amplitude | |
// Update particle's y position with wavy motion and slight random jitter | |
p.y = comet_y + p.offset_y + (rnd_float() * 2.0 - 1.0) // random.uniform(-1, 1) | |
// --- Draw --- | |
gl.clear(color_black) | |
// Draw tail | |
for(tail) p, i: | |
if p.alpha > 0.0: // Only draw visible particles | |
let particle_color = float4 { 1.0, 1.0, 1.0, p.alpha } // White with alpha | |
gl.color(particle_color) | |
// Particle size decreases with distance from comet head | |
let particle_radius = max(1.0, 5.0 - float(i) * 0.1) | |
gl.translate(float2 { p.x, p.y }): | |
gl.circle(particle_radius, 12) // 12 segments for small circles | |
// Draw comet head | |
gl.color(color_white) | |
gl.translate(float2 { comet_x, comet_y }): | |
gl.circle(10.0, 30) // 30 segments for a smoother comet head | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment