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 Project Starfighter In his fight back against the ruthless Wade-Ellen Asset Protection Corporation, pilot Chris Bainfield finds himself teaming up with the most unlikely of allies - a sentient starfighter known as Athena. |
— Creating a simple roguelike — Note: this tutorial assumes knowledge of C, as well as prior tutorials.
Introduction The time has come to add in the primary baddie of our game - The Mouse King. The naughty mouse can be found hiding on the top floor of the dungeon, where he waits for the player to confront him. Since this part only involves adding in the King, it is fairly short. Extract the archive, run cmake CMakeLists.txt, followed by make, and then use ./rogue19 to run the code. You will see a window open displaying the player character in a small room, with just a stair case (leading up). Play the game as normal and reach floor 13, to battle the Mouse King (or just hack the code, to cheat your way there, since it can prove difficult otherwise!). Once the King is dead, the game will end and display the highscore table, with your entry at the top. Once you're finished, close the window to exit. Inspecting the code We've made a number of misc. changes to the code to support the Mouse King. Nothing huge, thankfully. Starting with structs.h:
We've added in an Entity pointer called `mouseKing`, to track the Mouse King when he's added to the dungeon, in much the same way as the player. Heading over to monsters.c, we've added in a new function named initMouseKing:
This is a standard entity init function, for adding in the Mouse King. The Mouse King himself is very, very weak..! He is actually weaker than a Micro Mouse. Who knew? The King has 1 `hp`, 1 `defence`, 1 `minAttack`, 2 `maxAttack`, a very, very high visRange (the entire map width), and is worth 1 `xp`. We're setting his `name`, `description`, and `texture`, and then assigning `e` to dungeon's mouseKing variable. This allows us to track the Mouse King and test his state. We've also added in a function called addMouseKing:
This function simply adds the Mouse King to the dungeon. Turning next to dungeon.c, we've made a number of changes to support the Mouse King. To begin with, we've updated initDungeon:
We're setting a new variable called mouseKingDeathAlpha (static within dungeon.c) to half a second. This will work in the same way as playerDeathAlpha, as we'll see shortly. Next, we've tweaked createDungeon:
We're setting dungeon's mouseKing pointer to NULL before setting up the dungeon proper, to make sure he's not being tracked by default (this prevents dangling pointer references, that could lead to crashes). We're also testing whether we're on the top floor of the dungeon, by testing whether dungeon's `floor` equals MAX_FLOORS. If so, we'll be calling addMouseKing, to add the Mouse King to the floor. Turning to `logic` next, we've reworked the function a bit:
After decreasing our floorChangeTimer, we're now testing if the player is dead, and handling that condition if so. Otherwise, we're now testing to see if dungeon's mouseKing pointer is not NULL and whether the mouseKing is dead. If this is true, we're increasing the value of mouseKingDeathAlpha, much the same way as we do with playerDeathAlpha when the player is killed. If mouseKingDeathAlpha reaches 128 or higher, we're setting game's highscore's defeatedMouseKing flag to 1, setting the killedBy string to "A ripe old age", and then calling initGameOver. Yes, defeating the Mouse King will immediately end the game. Ultimately, this will mean that we will earn one of the top places on the highscore table, since it is sorted first by those who have defeated the King. The rest of the `logic` function continues as before. The final tweak is to the `draw` function:
We're now testing at the end of the function if the Mouse King has been defeated and whether mouseKingDeathAlpha is greater than 0. If so, we'll begin to turn the screen white, in the same way as we turn it red when the player is killed. Finally, we need to ensure that The Mouse King can be created. To do this, we just add a line to entityFactory.c, in initEntityFactory:
We'll be calling initMouseKing when asked to create "Mouse King". And that's our game pretty much finished. Our final part, the finishing touches, will involve adding in some sound effects, music, widgets, and a title screen, all of which are simple tasks. 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: |