SPACE SHOOTER
WAVES
MAKING WAVES
MAIN
Wave Counter
First we'll need to add a global variable to the top of our program called wave. Start it at zero.
int wave = 0;
Next, we'll want to display this in our program. Add code to your render() method to display it:
textSize(24);
fill(255);
text("Wave: " + wave, 50, 50);
✓ Test your program - it should be displaying "Wave - 0" at the top.
Advancing Waves
Create a new method called nextWave() and call it from update(). The next wave method does the following:
Looks through each object, checking if it is an enemy
If it finds a single enemy, quits the method early
Increases the wave count by one
void nextWave()
{
for(int i = 0; i < objects.size(); i++)
{
if(objects.get(i) instanceof Enemy)
{
return;
}
}
wave++;
}
✓ Test your program - it should be displaying wave at the top. When you run out of enemies on the screen, it should rapidly increase forever.
Spawning Enemies
Write a new method that is in charge of spawning enemies. We'll call this method spawnEnemies() and call it from nextWave(), directly after the line that increases wave by one.
This method is basically a long set of conditionals checking which level it is. On the appropriate level, it will spawn a wave of enemies. The code to spawn an enemy is the same as what you had in setup. Make sure you remove all code in setup that created enemies before testing this.
void spawnEnemies()
{
if(wave == 1)
{
for(int i = 0; i < 5; i++)
{
objects.add(new EvilSquare(random(width), random(0, 100)));
}
}
}
✓ Test your program - it should advance to wave 1, spawn these enemies, then advance forever once the wave is completed. Try adding in more waves and make sure it works as intended.
STREAMLINING YOUR CODE
MAIN
Helper Methods
In the code above, we have a whole bunch of stuff inside each wave. Imagine if you were creating 3 different types of enemies across 5-10 waves. It's a lot of clutter!
Let's simplify this by making a helper method for spawning the EvilSquare.
void spawnEvilSquare(int count)
{
for(int x = 0; x < count; x++)
{
objects.add(new EvilSquare(random(width), random(0, 100)));
}
}
Revised SpawnEnemies
This lets us make spawnEnemies() way simpler. Look at how simple the new version is by comparison:
void spawnEnemies()
{
if(wave == 1)
{
spawnEvilSquare(5);
}
}
SMOOTHER ENTRY
MAIN
Spawn Above The Screen
Consider the position that the default evil square spawns at:
objects.add(new EvilSquare(random(width), random(0, 100)));
This means the squares spawn at the top of the screen, but they're actually spawning on the screen. If you look closely, you can see enemies "pop" on screen between waves. Instead, considering making them appear off screen.
objects.add(new EvilSquare(random(width), random(-200, -50)));
Other Sides
Consider having enemies that come from different directions. You could have a group of enemies spawn on the left side, move to the right, and wrap in that direction.
Bouncing Enemies
Careful! If you have an enemy that "bounces" off all four sides, you won't (easily) be able to have it spawn off the screen. You can do so with some careful coding, but may want to consider just having them appear already on the screen.