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 Battle for the Solar System (Complete) The Pandoran war machine ravaged the galaxy, driving the human race to the brink of destruction. Seven men and women stood in its way. This is their story. |
— 2D Santa game — Note: this tutorial assumes knowledge of C, as well as prior tutorials.
Introduction This wouldn't be Christmas Eve without Santa hurling a load of gifts down chimneys (or coal, if you're on the Naughty List). In this part, we're going to implement code to allow the player to deploy such items from his sleigh, raining them down onto the dwellings below. Extract the archive, run cmake CMakeLists.txt, followed by make, and then use ./santa05 to run the code. You will see a window open like the one above, with the scene moving from right to left. Steer Santa's sleigh with the WASD control scheme, and use J and L to throw a gift or a lump of coal. At the moment, these two items are unlimited. The objects will be removed from the game when they hit the ground and cannot, as yet, enter chimneys. When you're finished, close the window to exit. Inspecting the code Adding in our gifts and coal is yet again an easy task; they are just entities that have `tick` and `draw` functions. We need only spawn the entity, assign all the relevant details, and have our game loop take things from there. Once again, you can see that this is straightforward to do, so let's hop to it. Starting with defs.h:
We've added two new enums - ET_GIFT and ET_COAL, to identify gift and coal, respectively. Now over to gifts.c, the new compilation unit we've created to handle both our gift and coal entities. Once more, we'll see that implementing these objects is very simple. Starting with initGift:
As this is a shared function for both the gifts and coal, we're passing over the type we want to create (either ET_GIFT or ET_COAL). We're loading our textures and spawning an entity, while setting the `type` to the value we passed into the function. Note how the entity has an initial `dy` of -6, so that it doesn't fall immediately. Instead, it's going to move up the screen a little, as if Santa has tossed it. We're then randomly assigning a gift or coal texture from either the giftTextures array or the coalTextures array, depending in the `type`, and lastly assigning the `tick` and `draw` functions. Simple enough. The `tick` function follows:
Here, we're applying gravity to the gift by increasing its `dy`, and then adding that to the gift's `y`, so that it accelerates. We're then testing to see if the bottom of the gift (according to the height of the texture) has struck the ground, and setting its `dead` flag to 1 if so. We basically want to destroy these objects if they land on the ground. Next up is `draw`:
A standard drawing function here, where we're just rendering the gift, using its `texture`. The last function we have is loadTextures:
Once more, we're just loading a series of textures: gfx/gift01.png, gfx/gift02.png, gfx/coal01, etc. That's all there is to gifts.c for now! We now need a way to drop the gifts and coal, so it's over to player.c for the final tweaks. Starting with `tick`:
We're now calling a function named dropGift:
This is a simple function that tests if we've pressed either J or L, and then calling initGift, passing over ET_GIFT or ET_COAL depending on whether we want to deploy a gift or a lump of coal. In both instances, we're clearing the control, so that the player needs to press it again to issue another deployment. That's all there is to it! Once again, because of our ground work, adding in our gifts and coal is a simple process. Of course, we need the gifts and coal to actually do something, other than be removed when they impact the ground. The point of our game is to throw these things down chimneys. So, in the next part we're finally going to implement our scoring mechanism, and allow gifts and coal to enter chimneys. 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 |