• 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
— 2D Shoot 'Em Up Tutorial —
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're now ready to do something important in our tutorial: firing a bullet. Extract the archive, run cmake CMakeLists.txt, followed by make to build. Once compiling is finished type ./shooter04 to run the code.
A 1280 x 720 window will open, coloured a light shade of blue. 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 a bullet by pressing the left control key. Due to the nature of this tutorial, you'll need to wait until the bullet has reached the right-hand side of the screen before you can fire again. Close the window by clicking on the window's close button.
Inspecting the code
To start with, let's add an extra variable to app in structs.h, one to track if we're pressing the fire control. We'll call it fire:
Next, we need to update the Entity structure for firing our bullet (a bullet will be an Entity):
We've added three new variables: dx, dy, and health. These variables will handle the delta x and y of the entity (for movement) and whether or not the thing is alive. A health of zero would mean the entity is dead and could be deleted, etc. Next, we'll make a couple of changes to input.c.
In doKeyDown we've added a check to see if the left control key is pressed. If so, we'll set the app.fire variable to 1. We'll also update doKeyUp to reset it to 0 when the key is released:
With those simple changes in place, let's update main.c to deal with our bullet:
We have a new bullet variable declaration (defined again in main.h), which, like player, is an Entity. We'll use memset to zero everything and then load a texture into it. Next, we'll add some code to test if we want to fire the bullet. For this, we'll test two things: whether app.fire is set to 1 and whether the bullet's health is 0. The second part of this test restricts us from firing the bullet again until its health is 0. If both these checks are true we'll set the bullet object's x and y to that of player, set it's dx to 16 and it's health to 1.
We'll always be attempting to move the bullet, whether it is alive or not. We do this by adding the bullet dx and dy to its x and y. Once the bullet reaches the right-hand side of the screen (bullet.x > SCREEN_WIDTH), we set its health to 0. Finally, we want to draw the bullet, using our blit function. We only do this if the bullet's health is greater than 0.
That's it for firing a bullet. Not a great number of changes, but we now have the ability to fire a bullet by holding down the left control key.
We've made some great steps so far, but we'll need to refactor and tune some of the code in order to make it more useful to us going forward; for one, you can only fire one shot at a time, which isn't going to be too exciting. We're also putting a lot of stuff into main, which really should be separated out into their own compilation units. The next tutorial will lay the foundations for creating something more fully fledged, allowing the tutorials that follow to ramp up significantly.
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: