Learn how to make a space shooter for the Sega Mega Drive using SGDK! I highly recommend working through the Megapong Tutorial first, as that explains a lot of the basics of MD coding.

Megalaga 8 - Spam Protection

Posted April 16, 2020

Last time I mentioned that there is a potential issue concerning our game which was related to the bullet pool. And now I’m going to reveal the secret (and of course fix the issue)!

The issue stems from the fact that both the player and all enemies draw from the same bullet pool. The player isn’t actually limited in how many bullets they can fire; before a bullet is fired, we only check whether bulletsOnScreen < MAX_BULLETS is true. This means that if the player keeps firing continuously, there will almost never be a free bullet for an enemy to shoot, because the player is using all bullets available in the pool!

This of course makes the game super easy, which is not what we want. I mean, if we wanted an easy game, we wouldn’t have let the enemies shoot in the first place, right? So let’s fix that.

Start off with a new define and a new variable near the top of main.c:

u16 shotByPlayer = 0;

The variable shotByPlayer will keep track of how many of the currently active bullets have been shot by the player. We will compare this value to MAX_PLAYER_BULLETS whenever the player tries to fire. This way some of the bullets in the pool will be reserved for the enemies.

Let’s move on to shootBullet() now to implement this! You’ll see that first of all, we check whether a bullet has been fired by the player and store that result in fromPlayer. Then we check whether we have reached the maximum amount of bullets allowed on screen. We want to leave both of these checks unchanged, but we will add a new one right at the start of the if (bulletsOnScreen < MAX_BULLETS) body:

if(fromPlayer == TRUE){
    if(shotByPlayer >= MAX_PLAYER_BULLETS){

So now the player can only fire if a) there is still room on screen for another bullet and b) if MAX_PLAYER_BULLETS hasn’t been reached yet. If the player has fired all of their allotted bullets, we simply return out of shootBullet() without firing anything. The player will then have to wait until one of the bullets they have fired hits an enemy or goes offscreen.

But for this to work we need to keep shotByPlayer updated. Let’s start doing this in shootBullet() while we’re still here!

After we use reviveEntity to revive the bullet we want to fire, we check whether the bullet is going to be fired by the player to set the velocity accordingly. Looks like the perfect spot to increase our counter! So add a line to make the statement look like this:

if(fromPlayer == TRUE){
    b->vely = -3;
} else{ ... }

This way shotByPlayer will only get incremented if the player shoots a bullet, not an enemy. But of course we’ll also have to decrease the value somewhere! We’ll do this twice. The first time is in positionBullets(). Inside of the if-statement where we check whether the current bullet has left the top of the screen, add a line to make it look like this:

if (b->y + b->h < 0)

This will free up a bullet in the pool when the player misses an enemy. And now we of course have to decrease the value when the player doesn’t miss! This will happen inside of handleCollisions().

Inside the innermost if-statement body of that function (so inside of if (collideEntities(b, e)){}), add the line so it looks like this:


And there we go, that’s all we need to do! Now we keep track of how many bullets the player has fired and prevent them from firing more once the limit has been reached. This way there is no way to cheese the game by spamming bullets and keeping the pool empty for the enemies. You can of course decrease MAX_PLAYER_BULLETS to make the game tougher on players if you want!

Our game is even more polished now, but one thing is still missing: Sound! It wouldn’t be a space shooter if you didn’t hear the pew pew of lasers and the krakoom of exploding enemies. So next time we’ll talk about sound drivers and how to make our game less quiet! Until then, be excellent to each other!

If you've got problems or questions, join the official SGDK Discord! It's full of people a lot smarter and skilled than me. Of course you're also welcome to just hang out and have fun!

Download the Project Files!

All patrons on Patreon get the complete source code for this tutorial, as well as other perks such as early access! And Patreon support also ensures that I can keep working on tutorials like this one. Become a Patron!
Just Want to Buy Me a Coffee?

Check out the rest of this tutorial series!

  • Megalaga 1 - Space
  • Megalaga 2 - Entities
  • Megalaga 3 - Enemies
  • Megalaga 4 - Enemy Movement and Input
  • Megalaga 5 - Bullets
  • Megalaga 6 - Collision and HUD
  • Megalaga 7 - Enemy Bullets
  • Megalaga 8 - Spam Protection
  • Megalaga 9 - Sound
  • Megalaga BONUS - Powerup
  • Get Words in Your Inbox!

    Be oldschool and sign up for my newsletter to get updates! Just enter your email address, prove you're not part of Skynet and you're good to go!

    Powered by CleverReach. I will not send you spam or sell/give your email address to someone else.  You can of course unsubscribe at any time. By clicking the subscribe button above, you confirm that you have read and agreed to our privacy policy.

    By using the Disqus service you confirm that you have read and agreed to the privacy policy.

    comments powered by Disqus

    Related Posts

    HaxeFlixel Tutorials!

    If you’ve popped over to the tutorial section recently you might have noticed that I’ve added my very first HaxeFlixel tutorial! It shows how to implement a simple, pixel-perfect 2D water shader which I used for Go! Go! PogoGirl. But a few of you might be wondering what a HaxeFlixel is. Well, it’s a 2D game framework that is as powerful as it is underrated! It runs on the (also underrated) Haxe language, is extremely well documented, open source, and has built-in functions for almost anything you’d need.
    Read More

    Streets of Was

    As I’m sure many of you will remember, the original Streets of Rage for the Mega Drive had multiple endings. The real canonical ending has you beat the crap out of Mr. X, thereby ending his reign of terror forever (yeah, right). However, if you confronted Mr. X with a buddy in tow, a new possible path unlocked. A quick refresher is in order. When you confront Mr. X he will ask you to join his organization.
    Read More

    Streets of Rage 2 Design Docs

    A few years ago, Yuzo Koshiro posted a pile of old game design documents for Bare Knuckle 2 aka Streets of Rage 2 on the Ancient blog to commemorate the release of Streets of Rage 2 3D on the Nintendo 3DS. These documents gave a deep insight into the game’s inner workings, technical aspects, designs and even some cut content. They were an awesome resource for one of the most awesome games ever created.

    Read More