• 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
— Creating an in-game achievement system —
Now that we have a system whereby we can award Medals to players for performing certain tasks, we should take a brief look at how to incorporate it into gameplay. It's really quite simple, as we'll see. In this part, we won't be looking at all the gameplay code (entity management, collisions, etc), but only those aspects that touch on the Medals integration.
Extract the archive, run cmake CMakeLists.txt, followed by make, and then use ./medals02 to run the code. You will see a window open like the one above, showing a little blue alien and a host of floating batteries. The goal is to collect all the batteries. Guide the little alien around using the WASD control scheme. Medals will be unlocked as you pickup the batteries, for lots of 1, 10, 20, and 25. The Ruby medal is awarded for collecting all the other medals. Close the window to exit.
Inspecting the code
Incorporating the Medal code into our game is very straightforward; we saw in the first part how unlocking a Medal is simply a case of calling one function, which is largely what we'll be doing here. We'll be tracking the number of batteries collected by use of a stat.
Starting with defs.h:
We've added an enum to handle our stats. We've only got the one - STAT_BATTERIES_COLLECTED, which will be used to track the number of batteries picked up.
Moving onto structs.h now, we've updated the Game struct:
We've added a field called `stats`, which is an array of ints, of STAT_MAX size. This array will be used to track our stat values (of which we only have one right now).
There are quite a few files in this project now: entities.c, entityFactory.c, player.c, etc. However, these are irrelevent to our Medal and stat tracking, so we won't be talking about them (if you're not familiar with the other tutorials where the game engine style is created, it is recommended you go back and read all those). Instead, we will focus on batteries.c, and the `touch` function in particlar:
We're checking to see what (`other`) has touched the battery (`self`). If it's the player, we're going to increase the value of our STAT_BATTERIES_COLLECTED stat, flag the batttery as dead, and play a sound. After that, we're going to test the value of the STAT_BATTERIES_COLLECTED stat. If it's 1, we'll call awardMedal, passing over "batteries1", to unlock that Medal. If it's 10, we'll call awardMedal and pass over "batteries10". For 20 and 25, we'll pass over "batteries20" and "batteriesAll" to the function. So, as we collect more batteries, we're unlocking more Medals.
That's it for batteries.c. As you can see, it's very simple to unlock the Medals.
If we quickly turn to stage.c, we see how we setup the gameplay demonstration.
Starting with initStage:
We're memsetting `stage`, calling initEntities, addEntities, and assigning the logic and draw delegates to the `logic` and `draw` functions found in this file.
addEntities is next:
Here, we're creating a Player and placing them at the top of the screen, in the middle. We're then creating 25 batteries, of random type, and placing them in random locations about the screen.
Our `logic` function is simple:
We're just calling doEntities, to process our entities (which can be found in entities.c).
Likewise, our `draw` function makes one single call:
We're calling drawEntities, to draw our entities (again, found in entities.c).
And that's it for this part. As you can see, incorporating our Medals into gameplay is very easy. What we'll look at in our next part is saving our progress, so that we can return to a game in progress, and continue with collecting our Medals.
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: