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 Honour of the Knights (First Edition) (The Battle for the Solar System) When starfighter pilot Simon Dodds is enrolled in a top secret military project, he and his wingmates begin to suspect that there is a lot more to the theft of a legendary battleship and an Imperial nation's civil war than either the Confederation Stellar Navy or the government are willing to let on. |
— 2D Shoot 'Em Up Tutorial —
Introduction Note: this tutorial builds upon the ones that came before it. If you aren't familiar with the previous tutorials in this series you should read those first. We now have collision detection and can shoot the enemies to destroy them! Extract the archive, run cmake CMakeLists.txt, followed by make to build. Once compiling is finished type ./shooter07 to run the code. A 1280 x 720 window will open, with a near-black background. A spaceship sprite will also be shown, as in the screenshot above. The ship can now be moved using the arrow keys. Up, down, left, and right will move the ship in the respective directions. You can also fire by holding down the left control key. Enemies (basically red versions of the player's ship) will spawn from the right and move to the left. Shoot enemies to destroy them. Close the window by clicking on the window's close button. Inspecting the code To start with we're going to update defs.h to add a side declaration. This will come into effect when firing bullets.
In order to test whether or not a bullet has hit an enemy, we need to do some collision detection. We've added a new compilation unit called util.c, which contains a single function - collision:
This function takes 8 arguments, essentially two rectangles. It tests to see if the rectangles overlap and if so, returns 1. This is a very common test, so we won't go into details as to how it works (there are many, many such collision detection routines to be found around the web). Next, we'll take a look at some other minor changes, starting with structs.h. In the Entity struct, we've added a new variable called side. This will use one of the SIDE_ declarations from defs.h.
We see this side variable in action in stage.c, where we've added it to the initPlayer, spawnEnemies, and fireBullet functions (some context of each omitted):
Next, we want to update doFighters, to tell the function to delete a fighter if its health is 0. This is a simple change:
We then add a new test to doBullets. As well as deleting a bullet if it leaves the right-hand side of the screen, we'll call a new function called bulletHitFighter, passing over the bullet. This function will be used to, as the name suggests, test to see if the bullet has hit made contact with a fighter.
Let's take a look at the bulletHitFighter function:
What this code does is step through all the fighters in our stage linked list and calls our collision function. We pass over the bullet's x, y, w, and h, as well as those of the fighter's, to the function. These represent the two objects' rectangles and will be used to check for an overlap. If the two rectangles do overlap, we set both the bullet's and fighter's health to 0, thus eliminating them from the game. One very, very important additional check is to see who the bullet belongs to. If we don't do this then the bullet will kill the issuing fighter as soon as it is fired. In effect, the player will press fire to die! This is why we added the side variable to Entity, to prevent such a thing happening. To sum up, if we see that the bullet is on the same side as the fighter that issued it, we'll ignore it (there will be further consequences for this in the next tutorial). Finally, we need to ensure that the enemy fighters have health. If we don't do this they'll be removed the instant they are spawned.
We can now shoot the enemies and defeat them! Great. In the next tutorial we'll add some challenge to the game have the enemies shoot back at (and destroy!) the player. Exercises
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: |