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 Alysha When her village is attacked and her friends and family are taken away to be sold as slaves, Alysha Tanner sets out on a quest across the world to track them down and return them home. Along the way, she is aided by the most unlikely of allies - the world's last remaining dragon. |
— Mission-based 2D shoot 'em up — Note: this tutorial assumes knowledge of C, as well as prior tutorials.
Introduction The next section we're going to look into is our statistics display. This is really just for fun, for the curious types that like to know how much of everything they have been doing during the game. In this part, we'll be listing the stats on their own screen. Extract the archive, run cmake CMakeLists.txt, followed by make, and then use ./shooter3-19 to run the code. You will see a window open like the one above. Use the mouse to control the cursor. Aside from watching the time played counter increase, there is not much more to say about this screen. Once you're finished, close the window to exit. Inspecting the code Okay, so this is a stats screen. It displays stats. Not much more to say, so let's jump into the code and see what we've done to achieve this. Starting with structs.h:
To our Game struct, we've added an inner struct called `stats`, that will hold all the values we are interested it. The names (in camel case) should already describe which statistic the variable represents, so we'll just move right on. stats.c is the new compilation unit that we've created to handle our statistics viewing. It's a very simple file, as we'll see. We'll start by looking at initStats:
This first zeros a variable called timePlayedTimer (static in stats.c). This timer is used to control how often we update the text that represents the Time Played display. Since we're only resolving the time played down to the second, we don't want to be generating the string all the time. Next, we're zeroing a variable call `accuracy` (also a static variable in stats.c), before testing if any shots have been fired (shotsFired), and then calculating the player's accuracy by dividing by shotsHit, and converting a percentage. We do it here so we don't keep calculating it each time it needs to be drawn to the screen. Next up is doStats:
This is the main logic processing function of the section. Here we're decreasing the value of timePlayedTimer, and then, if it's 0 or less, we're using sprintf to generate our "Time played" string. We're producing seconds, minutes, and hours(!) as the time here. That's all there is to the logic, so we can move onto the drawing. drawStats is where we render the data for this section:
All this function does is renders each stat to the screen, with the help of a function named drawStat. We're using this function to help with alignment and vertical spacing of our stats. Otherwise, there isn't much else to say about this function..! Let's look at drawStat, to see how it works:
Again, pretty simple. The function takes the stat `name`, `value`, a variable called isPercent to control whether we want to render the output as a percentage (for formatting purposes, such as with accuracy), and also a pointer to our vertical position on screen (`y`). We'll then test if we want to format the output text to include a percent (%), before then drawing the name and the value, aligned around the middle of the screen (with padding determined by STAT_X_SPACING). We'll then increase the value of `y` by the value of STAT_Y_SPACING. Again, the function exists to help with the layout and keep things consistent (and our code neat). That's it for the stats page, so let's move onto incorporating it into the main intermission screen. So, over to intermission.c we go. Starting with initIntermission:
We've added in the call to initStats. Next, we update `logic`:
We've added the IS_STATS case, to call doStats. We also update `draw`:
A similar IS_STATS statement has been added, to call drawStats: That's it for the screen in our intermission. All that needs adding are the stats updates themselves. We've sprinkled these throughout the code in various places, where appropriate to the stat itself. We did a similar thing in the Medals tutorial (#2, #3, #4), so we won't repeat ourselves here (examples can be found in bullets.c, fighters.c, and collectables.c). That's it for this screen. In summary, this is just a simple stats display screen, with the stats themselves being increased by various functions and events during gameplay. Ultimately, this isn't very impactful, but something that's nice to see. Something far more impactful is the ability for us to load and save our game. While our game isn't long, it would still be nice to save and reload our progress at a time of our choosing. In the next part, we're going to look at introducing the save system. 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: |