SDL_Chomik: Ten Monkeys

I was born in 1973, in Cracow, Poland. I am a software engineer.
Using Ranges and Random Generators in SDL_Chomik
In the previous SDL_Chomik article, we animated a single blue monkey drifting across the screen. This time, we will take the idea one step further. Instead of one, we’ll unleash ten monkeys! Each monkey starts at a random position and moves steadily toward the right edge of the window. When a monkey exits the screen, it respawns at a fresh random vertical position, starting again from the left.
This small change introduces two powerful features of Chomik:
Integer ranges:
<initialize blue monkey (MONKEY:1..10)>creates ten monkeys in a compact, declarative way.Random number streams: two separate streams provide randomized x and y positions for initializing and resetting our monkeys.
The Code
<create new image "chomik.png">;let chomik image index = <the created image index>;<create new image "background.png">;let background image index = <the created image index>;<create new image "blue_monkey.png">;let blue monkey image index = <the created image index>;
<create new input random number stream "integer" 0 <the sdl window height>>;let my random vertical stream index=<the created stream index>;<create new input random number stream "integer" 0 <the sdl window width>>;let my random horizontal stream index=<the created stream index>;
let initialize blue monkey (M:integer) = value code{let monkey index = value integer [(M:integer)];let the read from stream source stream index = <my random vertical stream index>;<read from stream "integer">;let blue monkey <monkey index> y = <the read from stream result "integer">;
let the read from stream source stream index = <my random horizontal stream index>;<read from stream "integer">;let blue monkey <monkey index> x = <the read from stream result "integer">;};
<initialize blue monkey (MONKEY:1..10)>;
let reset blue monkey (M:integer) on greater = value code{let monkey index = value integer [(M:integer)];let blue monkey <monkey index> x = value integer -128;let the read from stream source stream index = <my random vertical stream index>;<read from stream "integer">;let blue monkey <monkey index> y = <the read from stream result "integer">;};
let reset blue monkey (M:integer) if needed = value code{let monkey index = value integer [(M:integer)];<compare "integer" <blue monkey <monkey index> x> <the sdl window width>>;<reset blue monkey <monkey index> on <the compare result>>;};
let move blue monkey (M:integer) = value code{let monkey index = value integer [(M:integer)];<add "integer" <blue monkey <monkey index> x> 3>;let blue monkey <monkey index> x = <the add result "integer">;<reset blue monkey <monkey index> if needed>;};
let sdl loop body = value code{<show image <background image index> 0 0 >;<show image <chomik image index> 0 0 >;<show image <blue monkey (MONKEY:1..10) x> <blue monkey (MONKEY:1..10) y>>;<move blue monkey (MONKEY:1..10)>;};<sdl loop>;
How It Works
Random generators: We create two streams:
one for vertical positions (0 .. window height),
one for horizontal positions (0 .. window width)
Each monkey pulls its starting coordinates from these streams.
Initialization: With <initialize blue monkey (MONKEY:1..10)>, ten monkeys are defined at once. Each monkey gets its own x and y values.
Movement: Each monkey moves three pixels to the right in every frame:
<add "integer" <blue monkey <monkey index> x> 3>;
If it goes past the window width, we reset its x to -128 and pick a new random y.Rendering:
The background, base "chomik" image, and all ten monkeys are drawn in each loop iteration.
What to Notice
Integer ranges make it natural to express “ten monkeys” without repetitive code.
Streams provide a clean source of randomness—ideal for procedural animation.
This example highlights how SDL_Chomik’s declarative style lets you think in terms of behaviors rather than loops or conditions. You define what happens when a monkey moves, reaches an edge, or respawns—and the runtime handles the rest.
Next Steps
From here, you can:
Vary each monkey’s speed by adding another random generator.
Use different sprites for different monkeys.
Make the monkeys bounce instead of respawn.
The combination of ranges, randomness, and reset behaviors gives you a solid toolbox for creating lively, unpredictable animations in SDL_Chomik.





