Lesson 18: Particle Effects
In lesson 14 you were given a sneak peak at particle effects with the Hocus Pocus example highlighting the use of color. In this lesson we'll cover the mechanics of creating particle effects and how to implement them in games. Particle effects add fun to any game they are incorporated into. Many mobile games incorporate an overkill of particle effects to hide the fact that they are fairly dull otherwise. The flashing colors and objects flying around make games seem more engaging. Of course with an excellent game particle effects are icing on the cake.
Pixel Effects
The simplest particle effects use nothing more than controlled pixels. Each pixel contains an x and y location, x and y vectors, and a speed at a minimum. The following example is a particle effect I often use called sparks. It's an example of the minimum code needed and I often use it as a basis to build other effects upon.
( This code can be found at .\tutorial\Lesson18\SparkDemo.bas )
'* Particle effects demo - Sparks
CONST FALSE = 0, TRUE = NOT FALSE ' truth detectors
TYPE XYPAIR ' x,y pair definition
x AS SINGLE ' x value
y AS SINGLE ' y value
END TYPE
TYPE SPARK ' single spark definition
Location AS XYPAIR ' location of spark
Vector AS XYPAIR ' spark vector
Velocity AS SINGLE ' velocity of spark (speed)
Fade AS INTEGER ' intensity of spark
Lifespan AS INTEGER ' lifespan of spark
END TYPE
REDIM Spark(0) AS SPARK ' dynamic array to hold sparks
DIM SparkNum AS INTEGER ' number of sparks to create at a time
DIM SparkLife AS INTEGER ' life span of spark in frames
SCREEN _NEWIMAGE(640, 480, 32) ' graphics screen
SparkNum = 10 ' 10 sparks at a time
SparkLife = 30 ' 1/2 second spark life span
DO ' begin main loop
_LIMIT 60 ' 60 frames per second
CLS ' clear screen
LOCATE 2, 20: PRINT "Press left mouse button to make sparks fly!"; ' display directions
LOCATE 4, 29: PRINT USING "Sparks Created: #######"; UBOUND(Spark); ' display array size
WHILE _MOUSEINPUT: WEND ' get latest mouse update
IF _MOUSEBUTTON(1) THEN ' user pressing left mouse button?
MakeSparks _MOUSEX, _MOUSEY ' yes, make the sparks fly at cursor location
END IF
UpdateSparks ' maintain sparks on screen
_DISPLAY ' update screen with changes
LOOP UNTIL _KEYDOWN(27) ' leave when ESC pressed
SYSTEM ' return to operating system
'------------------------------------------------------------------------------------------------------------
SUB MakeSparks (x AS INTEGER, y AS INTEGER) ' spark initiator
'* Add sparks to the Spark() array
'x, y - location of new sparks
'Note: DO ... LOOPs used for speed. Much faster than FOR ... NEXT statements.
SHARED Spark() AS SPARK ' dynamic array to hold sparks
SHARED SparkNum AS INTEGER ' number of sparks to create at a time
SHARED SparkLife AS INTEGER ' life span f spark in frames
DIM CleanUp AS INTEGER ' TRUE is no life left in array
DIM Count AS LONG ' spark counter
DIM TopSpark AS LONG ' highest index in array
DIM NewSpark AS LONG ' index in array to start adding new sparks
CleanUp = TRUE ' assume no life left in array
DO ' begin spark life check
IF Spark(Count).Lifespan <> 0 THEN ' is this spark alive?
CleanUp = FALSE ' yes, array still has life
END IF
Count = Count + 1 ' increment spark counter
LOOP UNTIL Count > UBOUND(Spark) OR CleanUp = FALSE ' leave when life found or end of array reached
IF CleanUp THEN REDIM Spark(0) AS SPARK ' if no life found then reset dynamic array
TopSpark = UBOUND(Spark) ' identify array index starting point
REDIM _PRESERVE Spark(TopSpark + SparkNum + 1) AS SPARK ' increase array while saving living sparks
Count = 0 ' reset spark counter
RANDOMIZE TIMER ' seed RND generator
DO ' begin add spark loop
Count = Count + 1 ' increment spark counter
NewSpark = TopSpark + Count ' next array index to use
Spark(NewSpark).Lifespan = SparkLife ' set spark life span
Spark(NewSpark).Location.x = x ' set spark location
Spark(NewSpark).Location.y = y
Spark(NewSpark).Fade = 255 ' set spark intensity
Spark(NewSpark).Velocity = INT(RND * 6) + 6 ' set random spark velocity
Spark(NewSpark).Vector.x = RND - RND ' set random spark vector
Spark(NewSpark).Vector.y = RND - RND
LOOP UNTIL Count = SparkNum ' leave when all sparks added
END SUB
'------------------------------------------------------------------------------------------------------------
SUB UpdateSparks () ' spark maintainer
'* Maintains any live sparks contained in the dynamic array
SHARED Spark() AS SPARK ' dynamic array to hold sparks
DIM Count AS LONG ' spark counter
DIM FC0 AS _UNSIGNED LONG ' spark fade colors
DIM FC1 AS _UNSIGNED LONG
DIM FC2 AS _UNSIGNED LONG
DIM Fade0 AS INTEGER ' spark fade color values
DIM Fade1 AS INTEGER
DIM Fade2 AS INTEGER
DIM CleanUp AS INTEGER ' TRUE if no life left in array
IF UBOUND(Spark) = 0 THEN EXIT SUB ' leave if array cleared
CleanUp = TRUE ' assume no life left in array
DO ' begin spark maintenance loop
Count = Count + 1 ' increment spark counter
IF Spark(Count).Lifespan THEN ' is this spark alive?
CleanUp = FALSE ' yes, array still has life
Fade0 = Spark(Count).Fade ' set the intensity values
Fade1 = Spark(Count).Fade \ 2
Fade2 = Spark(Count).Fade \ 4
FC0 = _RGB32(Fade0, Fade0, Fade0) ' create the intensity colors
FC1 = _RGB32(Fade1, Fade1, Fade1)
FC2 = _RGB32(Fade2, Fade2, Fade2)
PSET (Spark(Count).Location.x, Spark(Count).Location.y), FC0 ' create pixels with intensities
PSET (Spark(Count).Location.x + 1, Spark(Count).Location.y), FC1
PSET (Spark(Count).Location.x - 1, Spark(Count).Location.y), FC1
PSET (Spark(Count).Location.x, Spark(Count).Location.y + 1), FC1
PSET (Spark(Count).Location.x, Spark(Count).Location.y - 1), FC1
PSET (Spark(Count).Location.x + 1, Spark(Count).Location.y + 1), FC2
PSET (Spark(Count).Location.x - 1, Spark(Count).Location.y - 1), FC2
PSET (Spark(Count).Location.x - 1, Spark(Count).Location.y + 1), FC2
PSET (Spark(Count).Location.x + 1, Spark(Count).Location.y - 1), FC2
Spark(Count).Fade = Spark(Count).Fade - 128 / Spark(Count).Lifespan ' decrease spark intensity
'* update spark location
Spark(Count).Location.x = Spark(Count).Location.x + Spark(Count).Vector.x * Spark(Count).Velocity
Spark(Count).Location.y = Spark(Count).Location.y + Spark(Count).Vector.y * Spark(Count).Velocity
Spark(Count).Velocity = Spark(Count).Velocity * .9 ' decrease spark velocity
Spark(Count).Lifespan = Spark(Count).Lifespan - 1 ' decrease spark life span
END IF
LOOP UNTIL Count = UBOUND(Spark) ' leave when last index reached
IF CleanUp THEN REDIM Spark(0) AS SPARK ' reset dynamic array if no life
END SUB
'------------------------------------------------------------------------------------------------------------
Figure 1: Let the sparks fly!
When creating effects such as this there are two routines needed, an initiator and a maintainer. In the previous example the MakeSparks() subroutine is the initiator. When MakeSparks() is called it creates new entries in the Spark() dynamic array that are waiting to be be acted upon. In other words MakeSparks() is filling fields with values in the array spreadsheet. The UpdateSparks() subroutine then maintains and updates the entries residing within the Spark() dynamic array. Each spark is created using a TYPE identifier that resides in the Spark() dynamic array.
TYPE SPARK ' single spark definition
Location AS XYPAIR ' location of spark
Vector AS XYPAIR ' spark vector
Velocity AS SINGLE ' velocity of spark (speed)
Fade AS INTEGER ' intensity of spark
Lifespan AS INTEGER ' lifespan of spark
END TYPE
Spark().Lifespan is used as a count down timer that is given a value when a spark is created. With each passing frame Spark().Lifespan is decreased by one in effect giving the spark a finite time to live. The Spark().Location.x and Spark().Location.y variables are the current coordinate locations of the spark. The Spark().Vector.x and Spark().Vector.y variables are vector quantities that control the direction of travel for the spark. Spark().Velocity is the vector multiplier that controls the speed of each spark. Spark().Fade is used to control the intensity of the spark throughout its life span. As the spark gets older the intensity value decreases giving the illusion of fading sparks.
Line 18 of the code creates an empty dynamic array named Spark(0). This dynamic array will grow and shrink in size as needed to maintain the number of sparks currently alive within the array.
When the code recognizes that the user is pressing the left mouse button in line 31 the MakeSparks() subroutine is called and the current mouse cursor location is passed to it.
MakeSparks _MOUSEX, _MOUSEY ' yes, make the sparks fly at cursor location
The MakeSparks() subroutine first performs some array maintenance in lines 55 through 61. If there are no live sparks found within the Spark() array it is reset back to a zero index. This ensures that the array does not grow so large as to cause an out of memory error. The spark counter seen on the screen is actually the current size of the Spark() array and will reset back to zero when new sparks are not being made.
The largest index value within the array is then identified in line 62 so line 63 can increase the size of the array accordingly to handle the new sparks about to be made. The variable SparkNum controls how many sparks are made with each mouse click and this number is used to increase the array size by that amount.
Lines 66 through 76 are used to populate the new array indexes with initial values needed to create a spark. These new entries are now ready to be called upon by the UpdateSparks() maintainer subroutine which is located within the main program loop.
Line 94 within the UpdateSparks() subroutine is used to detect if the Spark() array is currently empty. If it is an immediate exit from the subroutine is performed. There is no reason to scan the Spark() array if it doesn't contain any data. UpdateSparks() also performs some array maintenance duties since it is always called up in the main program loop unlike MakeSparks() which is only called when needed. Line 95 sets a flag that assumes the array contains no live sparks. If this flag is still set in line 123 the Spark() array is reset to a zero index.
Each spark is drawn with a bright center pixel surround by 4 pixels at half intensity and 4 more pixels at one quarter intensity. This gives each pixel a hot glow effect. Figure 2 below shows what the pixels look like zoomed in at 800%.
Figure 2: Sparks zoomed in 800%
Because the pixels move so fast and fade quickly the user can't tell that each spark is actually a 6x6 grid of pixels with different intensities. They simply blend together to appear as a single glowing pixel moving through space. Go ahead and REM out lines 107 through 114 to see each spark as a single pixel. It's not nearly as clean looking in my opinion.
Throughout the life span of the spark it's intensity and velocity are decreased until finally the life span timer reaches zero at which point the spark will no longer be acted upon. Multiplying each spark's velocity by 0.9 in line 119 effectively slows its speed by 10% with each frame. This is why the sparks move very fast at first but quickly slow exponentially afterwards.
Simulated Gravity
Adding simulated gravity to particle effects is a simple matter of tinkering with the y vector of each particle. By slowly increasing the y vector value any particles heading upward with a negative y vector value will eventually turn around and start heading downward. By increasing the y vector speed at the same time the particles will appear to speed up as they travel downward simulating a gravitational pull. Keep in mind that the next example simply simulates gravity which in many cases will look "good enough". This keeps the math involved to a bare minimum while still yielding a believable effect.
The following example is the previous example slightly altered to introduce simulated gravity. Any line of code that has been changed or added has been noted at the right-hand side of the code.
( This code can be found at .\tutorial\Lesson18\GravitySparkDemo.bas )
'* Particle effects demo - Sparks
'* Simulated gravity added
CONST FALSE = 0, TRUE = NOT FALSE ' truth detectors
TYPE XYPAIR ' x,y pair definition
x AS SINGLE ' x value
y AS SINGLE ' y value
END TYPE
TYPE SPARK ' single spark definition
Location AS XYPAIR ' location of spark
Vector AS XYPAIR ' spark vector
Velocity AS XYPAIR ' velocity of spark vectors (speed) ** CHANGED
Fade AS INTEGER ' intensity of spark
Lifespan AS INTEGER ' lifespan of spark
END TYPE
REDIM Spark(0) AS SPARK ' dynamic array to hold sparks
DIM SparkNum AS INTEGER ' number of sparks to create at a time
DIM SparkLife AS INTEGER ' life span of spark in frames
SCREEN _NEWIMAGE(640, 480, 32) ' graphics screen
SparkNum = 10 ' 10 sparks at a time
SparkLife = 60 ' 1/2 second spark life span ** CHANGED
DO ' begin main loop
_LIMIT 60 ' 60 frames per second
CLS ' clear screen
LOCATE 2, 20: PRINT "Press left mouse button to make sparks fly!"; ' display directions
LOCATE 4, 29: PRINT USING "Sparks Created: #######"; UBOUND(Spark); ' display array size
WHILE _MOUSEINPUT: WEND ' get latest mouse update
IF _MOUSEBUTTON(1) THEN ' user pressing left mouse button?
MakeSparks _MOUSEX, _MOUSEY ' yes, make the sparks fly at cursor location
END IF
UpdateSparks ' maintain sparks on screen
_DISPLAY ' update screen with changes
LOOP UNTIL _KEYDOWN(27) ' leave when ESC pressed
SYSTEM ' return to operating system
'------------------------------------------------------------------------------------------------------------
SUB MakeSparks (x AS INTEGER, y AS INTEGER) ' spark initiator
'* Add sparks to the Spark() array
'x, y - location of new sparks
'Note: DO ... LOOPs used for speed. Much faster than FOR ... NEXT statements.
SHARED Spark() AS SPARK ' dynamic array to hold sparks
SHARED SparkNum AS INTEGER ' number of sparks to create at a time
SHARED SparkLife AS INTEGER ' life span of spark in frames
DIM CleanUp AS INTEGER ' TRUE is no life left in array
DIM Count AS LONG ' spark counter
DIM TopSpark AS LONG ' highest index in array
DIM NewSpark AS LONG ' index in array to start adding new sparks
CleanUp = TRUE ' assume no life left in array
DO ' begin spark life check
IF Spark(Count).Lifespan <> 0 THEN ' is this spark alive?
CleanUp = FALSE ' yes, array still has life
END IF
Count = Count + 1 ' increment spark counter
LOOP UNTIL Count > UBOUND(Spark) OR CleanUp = FALSE ' leave when life found or end of array reached
IF CleanUp THEN REDIM Spark(0) AS SPARK ' if no life found then reset dynamic array
TopSpark = UBOUND(Spark) ' identify array index starting point
REDIM _PRESERVE Spark(TopSpark + SparkNum + 1) AS SPARK ' increase array while saving living sparks
Count = 0 ' reset spark counter
RANDOMIZE TIMER ' seed RND generator
DO ' begin add spark loop
Count = Count + 1 ' increment spark counter
NewSpark = TopSpark + Count ' next array index to use
Spark(NewSpark).Lifespan = SparkLife ' set spark life span
Spark(NewSpark).Location.x = x ' set spark location
Spark(NewSpark).Location.y = y
Spark(NewSpark).Fade = 255 ' set spark intensity
Spark(NewSpark).Velocity.x = INT(RND * 6) + 9 ' set random spark velocity ** CHANGED
Spark(NewSpark).Velocity.y = INT(RND * 6) + 6 ' set random spark velocity ** ADDED
Spark(NewSpark).Vector.x = RND - RND ' set random spark vector
Spark(NewSpark).Vector.y = RND - RND
LOOP UNTIL Count = SparkNum ' leave when all sparks added
END SUB
'------------------------------------------------------------------------------------------------------------
SUB UpdateSparks () ' spark maintainer
'* Maintains any live sparks contained in the dynamic array
SHARED Spark() AS SPARK ' dynamic array to hold sparks
DIM Count AS LONG ' spark counter
DIM FC0 AS _UNSIGNED LONG ' spark fade colors
DIM FC1 AS _UNSIGNED LONG
DIM FC2 AS _UNSIGNED LONG
DIM Fade0 AS INTEGER ' spark fade color values
DIM Fade1 AS INTEGER
DIM Fade2 AS INTEGER
DIM CleanUp AS INTEGER ' TRUE if no life left in array
IF UBOUND(Spark) = 0 THEN EXIT SUB ' leave if array cleared
CleanUp = TRUE ' assume no life left in array
DO ' begin spark maintenance loop
Count = Count + 1 ' increment spark counter
IF Spark(Count).Lifespan THEN ' is this spark alive?
CleanUp = FALSE ' yes, array still has life
Fade0 = Spark(Count).Fade ' set the intensity values
Fade1 = Spark(Count).Fade \ 2
Fade2 = Spark(Count).Fade \ 4
FC0 = _RGB32(Fade0, Fade0, Fade0) ' create the intensity colors
FC1 = _RGB32(Fade1, Fade1, Fade1)
FC2 = _RGB32(Fade2, Fade2, Fade2)
PSET (Spark(Count).Location.x, Spark(Count).Location.y), FC0 ' create pixels with intensities
PSET (Spark(Count).Location.x + 1, Spark(Count).Location.y), FC1
PSET (Spark(Count).Location.x - 1, Spark(Count).Location.y), FC1
PSET (Spark(Count).Location.x, Spark(Count).Location.y + 1), FC1
PSET (Spark(Count).Location.x, Spark(Count).Location.y - 1), FC1
PSET (Spark(Count).Location.x + 1, Spark(Count).Location.y + 1), FC2
PSET (Spark(Count).Location.x - 1, Spark(Count).Location.y - 1), FC2
PSET (Spark(Count).Location.x - 1, Spark(Count).Location.y + 1), FC2
PSET (Spark(Count).Location.x + 1, Spark(Count).Location.y - 1), FC2
Spark(Count).Fade = Spark(Count).Fade - 128 / Spark(Count).Lifespan ' decrease spark intensity
'* update spark location
'* The following 2 lines have been changed ** CHANGED
Spark(Count).Location.x = Spark(Count).Location.x + Spark(Count).Vector.x * Spark(Count).Velocity.x
Spark(Count).Location.y = Spark(Count).Location.y + Spark(Count).Vector.y * Spark(Count).Velocity.y
Spark(Count).Velocity.x = Spark(Count).Velocity.x * .9 ' decrease x velocity ** CHANGED
Spark(Count).Velocity.y = Spark(Count).Velocity.y * 1.03 ' increase y velocity ** ADDED
Spark(Count).Vector.y = Spark(Count).Vector.y + .04 ' slowly increase y vector ** ADDED
Spark(Count).Lifespan = Spark(Count).Lifespan - 1 ' decrease spark life span
END IF
LOOP UNTIL Count = UBOUND(Spark) ' leave when last index reached
IF CleanUp THEN REDIM Spark(0) AS SPARK ' reset dynamic array if no life
END SUB
'------------------------------------------------------------------------------------------------------------
The code now needs the ability to separately control the speed of the x and y vectors. This was achieved in line 13 by changing .Velocity from a single type value to an XYPAIR TYPE. Since the vertical speed of the sparks will need to increase independent of the horizontal speed as they travel downward two variables will be required, Spark().Velocity.x and Spark().Velocity.y.
Lines 74 and 75 in the MarkSparks() subroutine now creates separate x and y vector velocities for each spark. Lines 120 and 121 in the UpdateSparks() subroutine can now have these velocities independently applied to the x and y vectors of each spark.
Lines 123 and 124 is where the calculations for the simulated gravity take place. In line 123 the vertical speed is slightly increased to make each spark appear to move faster downward as the frames progress. Line 120 adds a small positive value to the y vector with each passing frame. This has the effect of turning around any particles moving in an upward direction to a downward direction during their life span.
Again, this effect looks "good enough" for most situations because of the small size and fleeting life span of the particles. For larger and slower moving objects the correct physics may need to be calculated instead.
Particle Fountains
Pixels are great for effects but images do even better. By pointing your objects in a desired direction you can achieve a particle fountain. Particle fountains are useful for things such as engine exhaust, water fountains, and bullet hell games. Here's a piece of code that creates a Mario fountain complete with animation and sound, because, well why not! Pressing the space bar will release one Mario from the fountain. Holding the ENTER key will open the fountain for a flood of Marios. Holding the ENTER key and then holding the space bar is just insane! You'll get over 270 Marios on your screen at once. How's that for QB64 power! The code named MarioFountain.BAS can be found in your .\tutorial\Lesson18\ directory. Go ahead and load it into your IDE now.
Figure 3: Release the Marios!
This code is a slight modification from the second code example with gravity. The key differences are:
The MakeMario subroutine in lines 80 through 114 no longer resets the object array, Mario(), when there is inactivity on the screen. Since the frame rate is 60 and up to two Marios can be created during each frame it can be assumed that a maximum of 120 Marios can be created by the user per second. Given some time for the Marios to fly and run off the screen the number of maximum array objects at any given time should be manageable. The code proves this out by showing the maximum number of Marios seen at a time which rarely exceeds 270. So the code was written to increase the size of the array as needed and reuse free indexes in the array when they become available.
Lines 90 through 96 scans the Mario() array for an index that is not in use. If one is found that index is used. If one is not found then line 98 increases the array size and that new index value is used.
Lines 106 through 108 set vector quantities for a 30 degree arc from -15 degrees to +15 degrees using a radian value. 15 degrees converted to a radian value is .2617994 so using a radian value from -.262 to +.262 will result in the 30 degree upward facing arc. Line 106 creates that range of radian values. The radian value is then converted to vector quantities in lines 107 and 108.
The UpdateMario subroutine scans the Mario() array for any live Marios by checking Mario().Liv. When a Mario is found to be alive a series of events happens to guide Mario through the animation sequence. A two dimensional array named Sprite() has been created that holds 9 sprite images, 3 for flying, 3 for landing, and another 3 for running. The first index in the Sprite() array indicates the action, FLYING, LANDING, and RUNNING, and the second index holds the series of three sprites needed for that action's animation.
When a Mario is created with MakeMario the array value Mario().Act is set to the action FLYING. The SELECT CASE statement in line 127 looks at the value in Mario().Act and uses that value to animate the current action Mario is going through.
When the CASE is FLYING the y vector of Mario is looked at. If the y vector is less than zero (going upward) the first sprite in the FLYING series is used. When the y vector has gone above zero but still less than .5 then Mario is beginning the downward arc and the second sprite in the series is used. When Mario has gone beyond the value of .5 in the y vector the third sprite in the series in used. Mario's location is then updated in lines 136 and 137 and the y vector is increased slightly in line 138. When Mario's y location exceeds 199 Mario's action is then set to LANDING, the sprite reset to one, a frame counter is set to 6, and Mario's y location is forced to 200. At this point the landing sound is played.
When the CASE is LANDING a frame counter is used to determine how long each sprite in the LANDING sequence should be shown. The frame count is decreased by one in line 147. When the frame count reaches zero the next sprite in the sequence is set and the frame counter is reset to a value of 6. When the sprite sequence reaches 4 that means all sprites in the LANDING sequence have been shown. Mario's action is now set to RUNNING, the sprite is reset back to one and the frame counter is reset to 6. Mario's running direction is determined by taking the sign of the x vector, negative or positive, and multiplying that by a velocity of 2 in line 155.
When the CASE is RUNNING Mario's x location is updated. If Mario has run off the screen then Mario().Liv is set to FALSE releasing the array index for the next Mario that comes along. If Mario is still RUNNING then the frame counter is decreased by one. When the frame counter reaches zero the next sprite in the sequence is set and the frame counter reset back to 6.
By incorporating frame counters into animation sequences you can precisely control when a change in the sprite image will occur. The main program loop runs at 30 frames per second. The RUNNING sequence changes Mario's sprite image every 6th frame in effect stepping down Mario's RUNNING animation to 5 frames per second. Mario running at 30 frames per second would create nothing but a blur.
Finally in lines 171 through 177 the Mario sprite based on action and sprite number, Sprite(Mario(count).Act, Mario(count).Spr), is drawn in the correct direction as determined by line 171. If the x vector of Mario is positive he must be running to the right, if the x vector is negative he's running to the left. The _PUTIMAGE statements are used to orient the sprite image as required.
The only change to Mario's trajectory needed was in line 138 with the y vector unlike the code in the second example. Mario's jumping arc simply needs to be a smooth up and down action without an increase in velocity or a change in the x vector. In fact most platform games that utilize a character that jumps around uses this same procedure, simply modifying the y vector after a velocity and vector quantity have been established for the jump. The x vector is typically the velocity the character happens to be running in.
This demo also uses the concepts of a sprite sheet. The background image and sprites have been combined into one image called MarioFountain.PNG as seen in Figure 4 below.
Figure 4: MarioFountain.PNG
The LoadAssets subroutine is used to create and load the images, sounds, parse the sprites from MarioFountain.PNG, and then remove them so the main image can be used as a static background.
Finally, the ReleaseAssets subroutine is used to remove all loaded images and sounds from RAM before exiting the program. It's always a good idea to have your software perform this sort of cleanup routine before exiting back to the operating system.
Putting It All Together
Let's finish off this lesson with a game that includes everything learned up to this point. You can find Falcon9.BAS in your .\tutorial\Lesson18\ directory. Load the program into your IDE now.
Figure 5: Landing the Falcon 9 stage 1 booster
The game starts out with a one minute and 30 second cinematic introduction explaining the events that lead up to the premise of the game: You need to land the stricken Falcon 9 stage 1 booster manually. After the introduction (which you can skip by pressing any key) you are greeted by the CEO explaining the dire situation the company is facing. Pressing the UP ARROW key fires the main engine. The RIGHT and LEFT ARROW keys burst the side thrusters allowing you to maneuver the stricken spacecraft toward the awaiting barge. Land the booster and you are praised, crash it and, well, you're not. You can press the ESC key at any time to immediately exit the game.
Landing the rocket is hard! But after just a few minutes of playing you'll be able to achieve about a 50% proficiency in your landings, even better if you keep at it. Go easy on the side thrusters as there is a very limited supply of nitrogen on-board. There's no real physics calculations going on in this game. Everything is simulated using concepts learned in lesson 17. It's a good example of how simulated can be "good enough". Real physics calculations would of course be ideal but we'll get to those in a later lesson.
The source code of this game is a little over 1000 lines long which is rather small for a game that incorporates everything seen. Particle fountains are used to simulate the thrust blasts from the rocket's side thrusters. Another particle fountain is used to simulate exhaust smoke from the main engine as well as fire smoke coming from the rocket when it crashes. The concepts of an initiator and maintainer program are used to control the fading text sequences seen in the cinematic intro to the game. And finally simulated gravity is used to control the motion of the rocket as you try to maneuver and land it on the floating barge.
The Intro subroutine shows how you can set up an introduction sequence using a frame counter to keep track of key entry points for actions or events. The MakeSmoke and UpdateSmoke subroutines are once again just modified versions of the sparks demo shown earlier as are the MakeBurst and UpdateBurst subroutines.
The LoadAssets subroutine is used to load the game's sounds and graphics. A master sprite sheet is loaded and picked apart into the various graphics images needed for game play. Finally a subroutine called ExitGame is called upon when the user wishes to quit the game. ExitGame releases all sound and graphics assets from RAM before returning to the operating system. Always remember to clean up after yourself before exiting.
The code is well documented so make sure to take a good look at it. All of the concepts from this and previous lessons have purposely been included in the game. It's not a highly polished game and could use some improvements. A difficulty level selector and more polished graphics would be nice place to start. If you decide to improve upon the game make sure to upload it to the QB64 forum for others to enjoy!
This game took me a little more than 3 days to write. If you stick with game programming and have a genuine interest in learning it you'll be doing the same in a surprisingly short amount of time.