PC Games
• Orb Tutorials
• 2D shoot 'em up Latest Updates
SDL2 Versus game tutorial
Download keys for SDL2 tutorials on itch.io
The Legend of Edgar 1.37
SDL2 Santa game tutorial 🎅
SDL2 Shooter 3 tutorial
Tags • android (3) • battle-for-the-solar-system (10) • blob-wars (10) • brexit (1) • code (6) • edgar (9) • games (43) • lasagne-monsters (1) • making-of (5) • match3 (1) • numberblocksonline (1) • orb (2) • site (1) • tanx (4) • three-guys (3) • three-guys-apocalypse (3) • tutorials (17) • water-closet (4) Books The Honour of the Knights (Second Edition) (Battle for the Solar System, #1) When starfighter pilot Simon Dodds is enrolled in a top secret military project, he and his wingmates begin to suspect that there is a lot more to the theft of a legendary battleship and the Mitikas Empire's civil war than the Helios Confederation is willing to let on. Somewhere out there the Pandoran army is gathering, preparing to bring ruin to all the galaxy... |
— Mission-based 2D shoot 'em up — Note: this tutorial assumes knowledge of C, as well as prior tutorials.
Introduction Our final secondary weapon is a mine. It's yet another weapon that is easy to implement, and also one with unique properties compared to the others. When deployed, our mines will move towards the nearest enemy (at a slow speed), exploding with an area effect when they are close. Thus, this weapon can affect multiple enemies at once. Extract the archive, run cmake CMakeLists.txt, followed by make, and then use ./shooter3-10 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 deploy a mine. Play the game as normal. Deploying mines consumes ammo. Luring enemies into proximity of the mines will cause them to detonate, dealing them damage. The explosions cannot hurt the player. Once you're finished, close the window to exit. Inspecting the code As already stated, adding in our mines is a simple task, with only a few tweaks needed, as well as the logic updates. As always, let's first turn to defs.h:
We've now added in the SW_MINE enum, to represent our mine seconday weapon. Now, over to structs.h, where we've updated the Bullet struct:
We've added a new field - thinkTime. This will be used by the mine, to determine how it will wander around, and look for enemy targets. With those done, we can now make the required updates and changes to bullets.c. First of all, we've updated initBullets:
We're loading the texture for our mine (mineTexture). Easy. Now for the first of our mine function. We've added a new function called deployMine, that will be responsible for dropping a mine:
A standard factory function. Our mines will be spawned in the middle of the owner, and live for 5 minutes. Yep, a long time. We don't want our mines to expire after just a few seconds. We're setting the mine's `texture`, and its `tick` and `draw` functions to mineTick and standardDraw. Let's look at mineTick now. This is where we do all the interesting stuff:
We're decreasing the value of the mine's thinkTime. Our mine will only take action sometimes, so we need the thinkTime to have hit 0 or less. If so, we're going to have the mine first look for a target. This works very much like the logic for the homing missile. If the mine has a target, it will make its way towards it (by making use of calcSlope, and its `dx` and `dy` variables). Unlike our homing missile, our mine can change direction at will, and doesn't need to slow down. Next, we're scanning all the entities in the stage, looking for enemies (those on a different side to the player, and can take damage). If we find one that's less than 175 pixels away, we'll add it to an array of entities called `candidates`. Our array is limited to MAX_MINE_TARGETS (which is defined as 256). With our list of nearby enemies created, we test the number that were nearby (`n`). If we found 1 or more, the mine will detonate. We'll set the mine's `health` to 0, and then loop through our list of candidates. For each one, we'll call the takeDamage function, passing over 65 as the damage. We'll then call addMineExplosions to create a nice explosion effect. The last thing we'll do is set the mine's thinkTime back to a quarter of a second; our mines will only scan for things that can set it off every so often. So, in summary, our mine will move towards the nearest enemy target, exploding when it comes into ranage of any opposing enemy, and explode, dealing 65 points of damage to all those in range. That's it for bullets.c. Our mines are using the standard drawing routine, as they're not doing anything special with their rendering! On to player.c, to update fireSecondary:
We've added in SW_MINE to our switch statement, which will call deployMine. Finally, we've updated initGame, so that we can use our mines:
There we have it. All of our secondary weapons have been added in. As already stated, these weapons will be available to the player to purchase from an in-game shop, and can be swapped at will between missions. We've one last add-on to implement - an energy shield. This will technically be classed as a seconday weapon, since the player will need to equip it in place of the rockets, beam, etc. So, in our next part we'll look into how this is setup. Purchase The source code for all parts of this tutorial (including assets) is available for purchase: From itch.io It is also available as part of the SDL2 tutorial bundle: |