This chapter covers
- Generating random numbers
- Adding the logic to generate and display new enemy asteroids
- Making the asteroids move down toward the player
- Removing asteroids as they reach the bottom of the screen
In previous chapters, we placed the player’s ship on the screen and allowed the player to move the ship and fire bullets. Now we need to introduce some enemy objects for the player to shoot at and dodge away from as they move down the screen. Just like in the original Astro Smash game, our main enemy objects will be asteroids of various sizes that move down the screen at different speeds.
8.1 Generating random numbers
Before we start working directly on the enemy objects, it would be nice to have some randomness as to when and where any new enemies appear. To do this, we need to make a new function that will create random numbers that we can call at various places in our game.
It is not actually that easy to generate truly random numbers on any computer, but we can generate some random-like number sequences based on a formula. The key, especially with 8-bit computers, is to not have the formula take up too many of our precious CPU cycles. Here we will cover two random number routines, one based on shifting values and the other based on merging two linear number sequences together.