Galaxy Shooter — Making Powerups Rare

Joshua Nielsen
4 min readJul 23, 2021

Greetings, and welcome to this special series of articles. In this series, the intention is to take a solid functional base game and add some extra features that (hopefully) make it just a little more special.

The base game we have here is called Galaxy Shooter. It’s a Galaga-style 2D shmup that I put together while going through the 2D Game Development course for GameDevHQ. It has a controllable player ship, enemies, powerups, sound effects, music, and more. It is, as stated above, a fully functional game that can already be played and enjoyed in its current state.

But let’s see what else we can make it do.

Our next goal is to create an enemy seeking missile powerup. But we want this powerup to be a little more special, so we need to figure out how to cause some powerups to spawn more rarely than others.

The initial state and the path forward

Right now we have a collection of powerups in our spawn manager script. Every so often a powerup is spawned into the game. Every powerup in the collection has an equal chance of being spawned.

This is what we need to change. We want the different powerups to have different odds of being spawned.

For this problem, I’m going with what I am calling a “raffle ticket” model. For those unfamiliar, the way raffle tickets work is that each individual raffle ticket has an equal chance of being the winner, but the participants in the contest can have one or more tickets. That means each participant could have different odds of winning.

For example, let’s say we have a raffle with two people. Person A has just one ticket while person B has four tickets, making five tickets in the raffle total. That means person A has a 20% chance of winning while person B has a 80% chance of winning.

So the plan is to make each powerup a “participant” in our raffle, and each powerup will be assigned a certain number of “tickets”. With our goal being to make our upcoming homing missile powerup rare in comparison to the existing powerups, this means we’re going to give our new powerup less tickets than the rest.

Implementing the script

In order to make this change, the first thing we’ll need to do is create a new serialized field in the powerup script. This field will be an integer and represent the number of tickets the powerup has in our raffle. With the field being serialized, this allows us to easily modify this value for every different type of powerup we have in our game.

The rest of this update will be in the script doing the spawning of the powerups. At the start of game, we want this script to build us a collection that can show us what ranges of values apply to which powerup.

For example, let’s say we have three powerups. Powerup A has seven tickets, powerup B has five tickets, and powerup C has three tickets. That is fifteen tickets in total. So what we’re looking for is something that can tell us easily, when we have a random value from 0–14, that a value of 0–6 will be for powerup A, 7–11 powerup B, and 12–14 powerup C.

Creating the ranges of each powerup’s “winning” values

Once we have these ranges in place, all that we have left to do is get a random value between zero and the total number of “tickets” in the system, and then match that value to the appropriate powerup.

Determining the “winner”

Because we created a sorted list earlier, all we need to do is see which value our random number is lower than and we have the powerup we’re looking for.

Great! Now that we’ve made our new powerup a little more rare (and by extension a little more special), next we’re going to need to actually implement it. Until then, good luck and happy coding!

--

--