Name: Randomly spawning enemies that double in number
Description: This pattern adds enemies to our game that appear in random places. The number of enemies on the screen also increases as the game progresses.
Need for Pattern: Having a randomly spawning enemies that double in number is a way of increasing the Challenge in our game. It is also an example of a feedback loop.
Related Game Patterns: Add Static Enemy [related],
Links to other Computing Patterns: Systems Dynamics, Reinforcing Feedback Loops
We will create our randomly appearing enemy by creating a forever loop which creates an enemy of a type projectile that starts at the side of the screen and moves left. So the enemy doesn’t always start in the same place we will make the height value (y) random. See the screen shot below. Add in a pause to the forever loop so it this new projectile happens every 5 seconds to start with.
We now code a listener event that creates game over if there is an overlap.
A common pattern is to increase the spawn time of our enemies to increase the challenge. To do this we will first create a new variable at the start of our game called spawnTime. Replace our value in the previous fover loop with this variable.
We now need to start to decrease this spawnTime variable. One easy way of doing that is to half the value every time the event runs by dividing it by two. This creates a reinforcing feedback loop so things might get crowded with projectiles very soon.
Your player will have to be fast.
Test your game to check that your changes have the desired behaviour and that there are no side effects.
To check that you are making the most of this pattern you can ask yourself the following questions:
This Game Pattern is one of many allowing you to make improvements to your platform game and to learn coding and wider computing concepts. Find more on the Game Pattern page.