• 2D shoot 'em up
SDL2 Santa game tutorial 🎅
SDL2 Shooter 3 tutorial
The Legend of Edgar 1.36
SDL2 map editor tutorial [UPDATED]
TBFTSS: The Pandoran War - Amiga OS4 Port
— Mission-based 2D shoot 'em up —
Beam weapons are another staple of shoot 'em ups. They tend to be devestating weapons that cause tremendous damage and strike many targets are once. In this part, we're going to implement such a weapon.
Extract the archive, run cmake CMakeLists.txt, followed by make, and then use ./shooter3-09 to run the code. You will see a window open like the one above. Use the WASD control scheme to move the fighter around. Hold J to fire your main guns. Press I to fire your secondary weapon (red beam). Play the game as normal. Using the red beam consumes ammo. Targets will take more damage the longer they are held in the path of the beam. Notice how the beam cuts through all targets, not just the first one it strikes. Once you're finished, close the window to exit.
Inspecting the code
Our red beam is one of the easiest beams to define, implement, and draw. And, once again, because we have already put in all the leg work for bullets and our weapons systems, it's even easier to setup.
First, let's look at the update to defs.h:
As expected, we've added in a new enum - SW_RED_BEAM will represent our red beam weapon.
Now, over to bullets.c, where we've added in all the necessary functions. To begin with, we've added fireRedBeam:
We're simply creating the bullet we need, setting its `health` (3 seconds), and assigning its `tick` and `draw` functions. Notice how we're not doing anything else - our beam has no velocity, damage, etc. This is because we'll be handling all of this in the tick function.
Let's look at the redBeamTick function now:
The first thing we're doing is determining how much damage we want to cause, by making use of RED_BEAM_DPS and our delta time. RED_BEAM_DPS defines how much damage the beam will cause per second (DPS being short of Damage Per Second). It is set to 15 (the define itself says 15.0 / FPS, to do the maths correctly, when used with our delta time). Next, we're calling a function named getRedReamRect, passing across the bullet, and an SDL_Rect into which the result will be placed. Basically, this function will calculate the rectangular area the red beam occupies, so that it can be used for both rendering and collision detection. With our rectangle determined (`r`), we're then looping through all entities in the stage, checking for enemies of the owner that can be hurt, and testing for a collision between them and the rectangle. Upon success, we're calling the entity's takeDamage function, passing over the entity itself, and the damage we calculated earlier.
Very simple! Let's look at redBeamDraw next:
This couldn't get any easier to understand - we're once again calling getRedBeamRect, testing if the resulting rectangle is on screen, and then calling drawRect, passing over the rectangle's attributes. For the colour, we're randomly choosing a value between 128 and 255, so that our beam appears to pulse.
Lastly, let's see how getRedBeamRect works:
You will notice how the red beam sticks to the player's fighter as it moves around. We're determining this here. The rectangle's (`r`) `y` value is set to the vertical middle of the bullet's owner. Next, we're setting the width and height to be half a screen wide, and 3 pixels tall. Finally, we're testing the direction the owner is facing, and positioning the beam accordingly. If the owner is facing right, we'll position the beam's origin on its right-hand side. If left, we'll position the beam to be the owner's x location less the rectangle's width. This properly aligns the beam when it comes to drawing.
An advantage of using getRedBeamRect is that it gives us a single version of the truth for the beam, rather than doing some calculations in one function, and the rest in another.
We're almost done! Just two more updates remaining and our weapon is ready. Heading over to player.c, we've once again updated fireSecondary:
We've added SW_RED_BEAM to our switch statement, where we're now calling the new fireRedBeam function.
Finally, let's tweak initGame in game.c:
We're setting SW_RED_BEAM as our secondary weapon.
There we go. We now have three secondary weapons to play with, all of which have different properties and behaviours. The final weapon we're going to add are mines. These, again, will be very easy to work with, and will float about, moving towards enemies, and exploding with tremendous power.
The source code for all parts of this tutorial (including assets) is available for purchase:
It is also available as part of the SDL2 tutorial bundle: