In this part of the project, we'll add the ability to shoot by pressing the spacebar.
The player's newly created projectiles are added to the playerBullets list, while those that have flown off the screen are removed from it.
To remove the projectiles, you can either use an iterator, or create a copy of the playerBullets list and run through it in a loop. If we determine that a projectile needs to be removed, we'll call playerBullets.remove(bullet).
To make the game a little harder, we'll also limit the number of projectiles the player can have on the screen. For convenience, we'll put this number in a variable.
We'll override the Game class's setCellValueEx method so that it only works with valid coordinates.
- The SpaceInvadersGame class must have a private static final int PLAYER_BULLETS_MAX field, initialized to 1 when it is declared.
- The SpaceInvadersGame class's onKeyPress(Key) method must call the fire() method on the player's ship if the passed argument is Key.SPACE.
- If the fire() does not return null and the number of objects in the playerBullets list is less than PLAYER_BULLETS_MAX, you need to add the object returned by the fire() method to the playerBullets list.
- In the SpaceInvadersGame class's removeDeadBullets() method, you need to remove all projectiles from the playerBullets list if the projectile is "dead" OR the sum of the y coordinate and the projectile's height field is less than zero.
- In the SpaceInvadersGame class, the setCellValueEx(int, int, Color, String) method of the Game parent class must be overridden.
- The setCellValueEx(int x, int y, Color, String) must not do anything if method parameter x or y is off the playing field.
- The setCellValueEx(int, int, Color, String) method must call the parent class's method using the super keyword.