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 Firmware HF-Tech's chips have changed the world. Embedded into the heads of over 90% of the world's population, they have cured autism, dementia, provided intelligence boosts, and helped to ease some of the more mundane tasks in life. Daniel Blair, hacker and Workshop member, however is not convinced that everything is as rosy as it seems. But is he looking in all the wrong places..? |
— Mission-based 2D shoot 'em up — Note: this tutorial assumes knowledge of C, as well as prior tutorials.
Introduction It's time for our second mission, where we're going to introduce not only a new enemy, but also a bunch of new features. We'll allow access to an in-game menu, handle the player death, and also add a fancy bit where the player exits the stages in dramatic fashion after finishing the mission..! Extract the archive, run cmake CMakeLists.txt, followed by make, and then use ./shooter3-25 to run the code. You will see a window open displaying the intermission's planets screen. Select Satsuma, then enter the comms section to start the mission. Play the game as normal. You may repeat the mission as often as you like. Remember that you can change the fighter's damage, weapons, output, and health by editing game.c, if you wish to get ahead of things early. During gameplay you may also press Escape to access the settings menu. If you are killed, you will have the option to restart the game or return to the intermission section. Once you're finished, close the window to exit. Inspecting the code We've added the second mission in this part, which involves collection 1,000 catnip during a mission. Since we already had the code available to support this, we're going to add in a load more features, including the ability for the player to access in-game settings, and also to be killed during play. We're adding in a new enemy, too. As we've seen how to handle the setting menus (during our work on the intermission, we'll be taking a high level overview of these. We'll start by looking at defs.h:
Our new enemy (the blue fighter) sports dual guns. We've therefore added AI_WPN_DUAL to support this. We've added in a new MS enum next:
MS_FAILED will be used to denote that our mission has been failed. At this time it only happens upon the death of the player, but this will be expanded upon in later missions. Okay, let's look at the new compilation unit, greebleDualFighter.c. This is where we'll find all the functions related to the new enemy. Many will look familiar, as they'll be quite similar to the first enemy. Starting with initGreebleDualFighter:
As expected in this factory function, we're setting the fighter's attributes. Note that we're setting its ai's weaponType to AI_WPN_DUAL. Also note that it has a [weak] shield, meaning it will be a little more resilient to our attacks than its red cousin. We'll skip the `tick`, `draw`, and `destroy` functions, as they are identical to those of greebleLightFighter, and look at the `die` function:
This is mostly the same as the `die` function for greebleLightFighter, except that we're dropping a little more catnip, and are sending over "greebleDualFighter" to the updateObjective function. These two fighters make a good case for a common set of shared functions. If we were to introduce another fighter (maybe a green one, called greebleTripleFighter), we could have a compilation unit called greebleFighter.c that would provide all these functions in one place, that the sub fighters would then reference. These are the sort of things that tend to be refactored as a game evolves and opportunities such as this present themselves. That's all for the fighter. It will behave like the original enemy, thanks to using the same AI routines. It will just be tougher to fight, thanks to its shield and weapons. Now, let's look at the updates we've made to the existing code. Starting with scripts.c. We've updated doScript to support a couple of new commands:
We're now supporting a command called "DELAY". This command is used in conjunction with the ScriptFunction's `delay` value. When reading the DELAY value, we're placing it into one of our new intParams indexes (via sscanf). intParams is an array of ints, added to allow us to read integer values. We'll then multiply it by FPS, so that our delays are resolved to the second (e.g., DELAY 3 means wait 3 seconds). The other new command is "REMOVE_CATNIP". This is called at the end of the Satsuma mission. After Leo has successfully finished the mission to collect 1,000 catnip, we're removing it from the player. It was never theirs to keep..! Once again, we'll read this into our intParams, and the remove the value from Game's catnip. Had we only been updating the code for our new mission, we'd be stopping here. However, we've gone a step further with our presentation, that we'll now step into. Firstly, we've updated player.c with tweaks to several of the functions. Remember that the player can now die. This needs to be supported. You might have also noticed that, upon successfully finishing the mission, the KIT-E now flies away, instead of the game simply pausing, and displaying the completed objectives. This is handled in player.c, as well as camera.c. We'll look at the changes to player.c first, starting with initPlayer:
We're setting the player's `die` pointer to the `die` function we've made (and will see in a bit). We're also setting the value of a control variable called missionCompleteBoost to 0. This controls the player leaving the stage, including the sound effect that plays, and the music jingle that sounds. The updates to `tick` come next:
Okay, so we've got two main pieces of logic here, contained in if-statements. The first if-stateemnt handles regular play, and will be called if the missionCompleteTimer is still counting down (it won't do so while the mission is incomplete) or if the mission is failed. The former will come into full use in later missions, when it is possible to fail a mission without being killed. In this clause, the player will control the KIT-E as normal. If this isn't the case, and the mission is complete, we'll take control away from the player, and force the KIT-E to face right. Next, we'll test if the missionCompleteBoost flag is still 0, and if the player has moved off the left-hand side of the screen (again, we'll see in a bit how we're updating the camera tracking). If this is true, we'll update missionCompleteBoost to 1, play the boost and win jingle sounds, and stop the music playing. Finally, we'll check if missionCompleteBoost is set, and increase the player's `x` value, so they move across the screen. So, in summary, if the mission is on-going or have been failed (and the player is still alive), we'll allow the player to control the fighter as normal. Otherwise, if the mission is now complete, we'll have the player fly away, in dramatic fashion. Finally, we have our `die` function:
This looks very much like the `die` functions for the enemies, except that when the player is dead we'll stop playing the music, play a sound effect to reflect they have been killed (an ominous low hum), set Stage's `status` to MS_FAILED, and also call the "MISSION_FAILED" script function (if it exists). This allows us to react to the player's death. Maybe we want to do something story-related, like not actually kill the player, etc. Again, our scripting system makes the possibilities endless. That's player.c done. We can now look at the changes to camera.c. There's only one function here, doCamera:
The camera is no longer always centered on the player; we're now testing three different states. First, if the player is dead, we're going to make the camera slow to a halt, as though it was tracking, and suddenly lost its target. The next clause will centre the camera on the player as usual, if the mission is still in progress or has recently been failed. Finally, if the mission is complete, the camera will move to the right, while also adjusting Stage's `ssx` and `ssy`. This is seen before the player flies away from the stage (from left to right). We can now move onto the changes made to stage.c. There have been quite a few changes here, due to us now supporting the player's death, and also the ability to access the in-game menu system, to change settings, etc. We'll be performing high level overviews of the widget stuff. So, starting with initStage:
We're making a copy of the player's game. oldGame (static in stage.c) will contain all the data from the main Game object. We're doing this so that we can reset aspects of the player's game upon them restarting the mission (either manually or after their death). We want to preserve the amount of catnip, ammo, health, etc. they had upon starting the mission (from the intermission), so that we can reset it when they start over. If the player earned 400 catnip before then being killed, we're not going to let them keep that. Likewise, if they came into the fight with full ammo, and consumed half of it, we'll restore them to full ammo. Basically, they'll be reset to whatever they had before the mission started. We'll see this in use later. Next up are the updates to `logic`:
We're now handling SHOW_STAGE, SHOW_PAUSE, SHOW_MISSION_OPTIONS, and SHOW_OPTIONS for our `show` value. When in SHOW_STAGE, the game will play as normal, allowing us to pause it or access the in-game menu. We'll be calling doStage and doStatus here, as before (some of our logic has moved into doStatus). When in SHOW_PAUSE, we'll allow the player to unpause the game. SHOW_MISSION_OPTIONS will call doStageOptions, while SHOW_OPTIONS will call doOptions (in options.c). The updates to doStatus follow:
We've made an update to how we're handling MS_COMPLETE. We're now waiting until we no longer have any collectables floating around, have no hud messages displaying, do not have a script running, and aren't showing a message box before we decrease the value of Stage's missionCompleteTimer. Basically, we don't want the mission to end while we're in a state where there are items of interest to the player. The MS_FAILED case is a new one. Unlike MS_COMPLETE, we'll always decrease the value of missionCompleteTimer. Once it falls to MISSION_FAILED_STATUS_TIME or less, we'll increase the value of fadeValue, show the mouse pointer, and process our "missionFailed" widgets. Along with the updates to `logic`, this means that the game will pause and the screen darken a few moments after we fail the mission (including when the player is killed). Okay, let's look at the rendering updates. Starting with `draw`:
We're now testing for various states before choosing what the draw. SHOW_PAUSE will call drawPause, SHOW_MISSION_OPTIONS will call drawStageOptions, while SHOW_OPTIONS will call drawOptions (from options.c). It will darken the screen a little before doing so, to make everything a little easier to see. We're also testing our Stage's `status`, and calling drawMissionComplete and drawMissionFailed in line with their logic timers. These will render the familiar ending screens. drawMissionFailed is new, but very similar to those that have come before:
As we can see, we're simply drawing "MISSION FAILED!" in red, and then listing our objectives list. While we're not going to cover all the widget functions (as they are largely similar to those in intermission), we should consider the ones most relevant to our mission. So, starting with `retry`:
This function is invoked whenever the player opts to restart the mission. We're keeping a copy of Stage's `mission`, since we'll be resetting Stage completely and will thus lose this pointer. Once we've cleared Stage, we're setting it back. Before that, we're calling resetGameData (we'll see this in a bit). Once all is done, we're calling initStage to start over. Notice how we're setting show to SHOW_STAGE. This is so we don't get held up by the objectives list again, which could become annoying if we fail the mission a lot. Doing this allows us to jump straight back into the action. The `quit` function follows:
Here, we're calling resetGameData, clearing the stage data, and then calling initIntermission, to return to the intermission. Unlike when we complete a mission, we're resetting the player's state details, so they don't keep anything they earned during play (or waste ammo.!). Finally, the resetGameData function:
Here, we're copying oldGame's `catnip` and `kite` data back into `game`, to reset everything to how it was prior to the mission starting. Notice that we're not resetting the stats. We want the player to keep a record of how many shots they fired, etc. even upon failing a mission or quitting. And that's our second mission inserted. There was actually quite a bit more to this part than originally thought, but it was mostly down to adding in the in-game menu and supporting the player's death. Our next parts will be shorter as they can now focus exclusively on the missions themselves. Our next mission will do something a bit more special - a new form of AI. We'll be introducing yet another new enemy, one that will act defensively, and aims to keep away from the player, while still attacking. 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: |