This is just a little thing I threw together in a day. I've been thinking of doing a tank-battle type of game, but I needed a way to set shot angle and power, and than let the program plot the trajectory under gravity from there. Vector math to the rescue! That lead to setting random angles and power levels, and plotting LOTS of projectiles in this little volcano program. How many particles can your rig comfortably maintain? To maximize rendering speed, I do no keyboard or mouse checks. Press Esc to exit, or just wait for the cycle count to complete. Happy Coding!
-CW
/////////////////////////////////////////////////////////////////////////
// VOLCANO
// Written by Craig Waterman
// 01/25/13
// ----------------------------------------------------------------------
// This project uses vector math to animate a small volcano.
/////////////////////////////////////////////////////////////////////////
//////////////////////////// Initialize ///////////////////////////////
//
// Change PARTICLE_COUNT to set number of particles to use.
// (Try 0 to 100000, or more. How many can your machine handle?)
CONSTANT PARTICLE_COUNT = 3000
GLOBAL sx,sy
GETSCREENSIZE sx, sy // Volcano is positioned in the center of the screen. (Screen can be resized.)
LOCAL Altitude%,Horizontal_Location%
LOCAL Path$=GETCURRENTDIR$()
LOADFONT Path$+"MyFont1.png",1
SETFONT 1
TYPE Particle
Particle_Number#
X_Velocity = 0.0
X_Birth_Tick# = 0 // Hold the clock tick when a particle is born. Trajectory is based on time from birth.
Y_Velocity = 0.0
Color% = 0.0
ENDTYPE
GLOBAL Particles[] AS Particle,Surge
DIM Particles[PARTICLE_COUNT+1] // Up to PARTICLE_COUNT particles will simultaneously erupt from the volcano.
// Note that the count begins with zero, but the dim needs one extra to properly format.
// Format: Particles[Particle Number][X_Velocity][Y_Velocity][Color of particle in RGB(value,value,value)]
GOSUB Initialize_Particles
GOSUB DrawLandscape
/////////////////////////////// BODY ////////////////////////////////////////
FOR Tick = 0 TO 1000 //Tick is the number of clock cycles in the full animation.
FOR P_Inumerate = 0 TO PARTICLE_COUNT
Altitude = INTEGER(Particles[P_Inumerate].Y_Velocity*(Tick-Particles[P_Inumerate].X_Birth_Tick));IF Altitude <-13 THEN Altitude = -13
Particles[P_Inumerate].Y_Velocity=Particles[P_Inumerate].Y_Velocity - .32
IF Altitude >-13 // Top of volcano is 13 pixels high, so ground is at -13, and any particle at -13 needs to be reset.
Horizontal_Location = (Particles[P_Inumerate].X_Velocity*(Tick-Particles[P_Inumerate].X_Birth_Tick) + (sx/2) )
SETPIXEL Horizontal_Location,400-Altitude,Particles[P_Inumerate].Color
ELSE
Initialize_Particle(P_Inumerate,Tick)
ENDIF
NEXT // P_Inumerate
Surge = Surge + 1
SHOWSCREEN
NEXT // Tick
PRINT "Finished!",sx/2-60,sy/2+75
PRINT "Press Key",sx/2-60,sy/2+100
SHOWSCREEN
KEYWAIT
END
///////////////////////////////////////////////////////////////////
//////////////////////// Functions ///////////////////////////////
// ------------------------------------------------------------- //
// --- VECTORCOMPONENT ---
// ------------------------------------------------------------- //
FUNCTION Initialize_Particle: Particle_Num,BirthTick
LOCAL Angle#,Power%
Angle = RND(60)+60
Power = RND(1000)+525+INTEGER(COS(Surge*12)*75) //To add visual interest, I allow the projectile power to surge.
Particles[Particle_Num].X_Velocity = COS(Angle)*Power/100
Particles[Particle_Num].Y_Velocity = SIN(Angle)*Power/75
Particles[Particle_Num].Color = RGB(255,RND(100),255)// Violet color with a random greenish component.
Particles[Particle_Num].X_Birth_Tick = BirthTick
RETURN
ENDFUNCTION // VECTORCOMPONENTX
///////////////////////////////////////////////////////////////////
////////////////////////// Subs /////////////////////////////////
// ------------------------------------------------------------- //
// --- DRAWLANDSCAPE --- I like a Minimalist Style.
// ------------------------------------------------------------- //
SUB DrawLandscape:
CLEARSCREEN
DRAWLINE 10,414,(sx-10),414,RGB(0,255,0) // Draw Ground
LOCAL VolcanoColor% = RGB(255,0,0)
DRAWLINE (sx/2-13),413,(sx/2-5),396,VolcanoColor;DRAWLINE (sx/2+13),413,(sx/2+5),396,VolcanoColor // Draw Volcano
DRAWLINE (sx/2-5),396,(sx/2+5),396,VolcanoColor// Draw Volcano
USEASBMP;SHOWSCREEN;SLEEP 500 //Delay .5 seconds to allow user to admire the landscape before the eruption.
ENDSUB // DRAWLANDSCAPE
// ------------------------------------------------------------- //
// --- INITIALIZE_PARTICLES ---
// ------------------------------------------------------------- //
SUB Initialize_Particles:
FOR Counter = 0 TO PARTICLE_COUNT
Initialize_Particle(Counter,0)
NEXT
ENDSUB // INITIALIZE_PARTICLES
///////////////////////////////////////////////////////////////////