I was inspired recently to write a computer program which draws pretty images. Here are a few screenshots.
Getting the Code
I have uploaded the code to bit bucket. If you have mercurial installed, you can get it by typing:
hg clone http://bitbucket.org/talljosh/swirls
You will need to have python and pygame installed to run the script.
How it Works
- There are invisible “singularities” drifting around the screen (you can make them visible by replacing
SINGULARITY.VISIBLE = Falsewith
SINGULARITY.VISIBLE = Truein the code).
- There are a fixed number of flares, which actually do the drawing. The flares are affected by gravitational effects from the singularities.
- At any given point in time, every flare is leaving a trail of the same colour. Over time, the colour cycles through the various colours.
- To prevent slingshot effects being too extreme, there is a radius around each singularity within which flares don’t feel any gravity from that singularity.
- Singularities cannot leave the screen. If they hit an edge, they change directions.
- Flares can leave the screen, but cannot go more than half the screen’s width or height beyond the screen boundaries. If they do, they reflect back and their velocity is damped.
- While the left mouse button is pressed down, the flares feel a gravitational effect towards the mouse pointer, which is twice as strong as the effect of one singularity.
Settings to Play With
There are a few settings that you can tweak to get different visual effects:
SINGULARITY.START_SPEED—the initial speed of each singularity in pixels per second
SINGULARITY.COUNT—the total number of singularities
SINGULARITY.VISIBLE—whether to show the singularities or not
FLARE.START_SPEED—the initial speed of each flare in pixels per second
FLARE.COUNT—the number of flares
FLARE.MAX_SPEED—flares are not allowed to travel faster than this speed (in pixels per second)
FLARE.FADE_TO_WHITE—controls whether the tip of the flare trail is white or black
GRAVITY_CONSTANT—controls the overall strength of gravity
NEUTRAL_DISTANCE—how close flares can get to singularities and still feel gravity; for super slingshotting, set to 0
COOLING_FACTOR—controls how quickly the white tip of the flare fades to the colour
DIM_HALFLIFE—the time in seconds that it takes for the colours to half fade to black
WALL_DAMPING—controls how much slower flares go after they bounce off the “walls” that are past the edges of the screen
MAX_FRAME_RATE—the number of frames per second that the script aims to achieve