![]() | |
PC Games
• Orb Tutorials
• 2D shoot 'em up Latest Updates
SDL2 Quest game tutorial
SDL2 Versus game tutorial
Download keys for SDL2 tutorials on itch.io
The Legend of Edgar 1.37
SDL2 Santa game tutorial 🎅
Tags • android (3) • battle-for-the-solar-system (10) • blob-wars (10) • brexit (1) • code (6) • edgar (9) • games (44) • lasagne-monsters (1) • making-of (5) • match3 (1) • numberblocksonline (1) • orb (2) • site (1) • tanx (4) • three-guys (3) • three-guys-apocalypse (3) • tutorials (18) • water-closet (4) Books ![]() A North-South Divide For over a hundred years, messenger Duncan has wandered the world, searching for the missing pieces of an amulet that will rid him of his curse; a curse that has burdened him with an extreme intolerance of the cold, an unnaturally long life, and the despair of watching all he knew and loved become lost to the ravages of time. But now, Duncan is close to the end of his long quest. |
— Simple 2D quest game — Note: this tutorial assumes knowledge of C, as well as prior tutorials.
Introduction With our towns built and populated, it's about time we made it so that we can fully explore the world. The only thing preventing us from doing so is the ability to enter and exit the towns. We'll fix that now. As you can see, this is quite a short part, since we've already put together everything else we need. Basically, we just need to connect the dots! Extract the archive, run cmake CMakeLists.txt, followed by make, and then use ./quest10 to run the code. As before, you can move around with the WASD control scheme. To enter a town, simply walk into it on the overworld map. To leave a town, walk through the gap in the surrounding wall. You're free to explore the world and towns as you like, sailing boats to reach new islands. When you're finished, close the window to exit. Inspecting the code As already stated, this is a simple update, with just a few minor tweaks required to allow us to enter and exit towns. We'll start first by the update we've made to the Town struct in structs.h:
We've added a field called `discovered`. This flag will be used to track whether the player has entered the town. Right now, this will be used to decorate the Town upon first entry. In future, it will be used as part of the extended exploration. Over next to settlement.c (a reminder that this is the entity that exists on the overworld, and represents a town). Here, we've added in a new `touch` function:
When the player makes contact with the town, we'll call the enterTown function, and pass over the town entity itself (`self`). This function, as we saw in a previous part, is responsible for transitioning us from the overworld to the town. So, walking into a town on the world map will allow us to enter it. Now over to player.c, where we've updated moveTown:
Before, we were restricting the player's movements to the Town interior; attempting to move outside of the map bounds would simply halt our movement. Now, we're checking if the player has moved outside of the map area. If so, we're going to reset their position to the Town's entrance, and then call enterOverworld. The reason we're resetting the player's position is to make navigating a bit easier when returning to the town; when first entering a town, the adventurer entity is set at the entrance (a little way in from the bounds) for this reason. We also want to ensure the player character remains restricted to the town map. Again, nothing taxing or unexpected. Remember that due to the surrounding walls, it's impossible to walk outside of the map bounds, except via the entrance. Now over to overworld.c, where we've made an important change to enterOverworld:
Since we're now in the overworld, we want to clear Game's `town` variable. Remember that the variable is used to track whether we're inside a town, and therefore process different logic in player.c. To ensure we begin processing the overworld logic once more, we need to null Game's `town` variable. Finally, it's over to town.c, where we've also updated enterTown:
As we want to decorate the Town only when we first enter, we need to track this. We're doing so with the new `discovered` flag. If it's 0, we'll set it to 1, and then call `decorate`. This ensures our town is only deocrated once. And that's all there is to it. We now have a fully explorable world, with places to go and people to meet. But what would make it even better is if we could have tasks to fulfil while we're here, in the form of quests. Before all that, though, it would be good to implement some much needed quality of life improvements. We're unable, for example, to locate ourselves on the map when viewing the Quest Log. Marking discovered towns, boats, etc. on the map would also be nice. So, in the next part we're going to add in a hud, improve our map, and add in some exploration data. Purchase The source code for all parts of this tutorial (including assets) is available for purchase, as part of the SDL2 tutorials bundle: From itch.io |