In this homework we'll create a scroller / shooter game where a
spaceship floats in space and can be controlled by the arrow keys, and
faces a number of opponents that it can avoid or shoot. Download the
following archive to find a started project and the resources used so
far:
shooter.zip
Ex. 1. Ship movement. Add 3 more functions to the class Rocket that make the ship move right, up, down. In each of them, make sure to prevent the ship form going out of the stage. Modify the function moveRocket to call these 3 functions you defined. This should give full functionality to the rocket movement.
Ex. 2. Dangers. Create a couple more enemy objects images. Modify the class Danger to allow for an array of enemy objects instead of a single one. The variables bombSprite, bombWidth, bombHeight, bombX, bombY, will need to be arrays. Add another one for the speed. You will need a counter for the number of dangers.
Modify the constructor for the class Dangers to add another parameter designating the number of enemy objects to be created. Make sure to generate a combination of dangers using all of images you created. I will leave the organizing details (1 of each? random?), and their speed up to you, considering that the speed is in pixels.
Modify the function dropBomb and add a loop that moves all of the bombs in the array. For each of them, instead of simply incrementing the value of bombY, add the value of the speed to it.
Ex. 3. Bullets. In the class Rocket, add four class variables: bulletX, bulletY, bulletSprite, bulletCount. The first two will be arrays containing the bullet coordinates. The third one will be an array containing a sprite for each bullet. The fourth one will be a counter for how many have been fired. We'll have a limit of 20 for the number of bullets that can be fired at the same time, so you can initialize all of these arrays with that number of elements. Create a sprite for the bullet and initialize all the sprites with the same bitmap. Add all the sprites to the stage but make them all invisible.
To fire a bullet, check first if the number of them already fired has not yet reached the limit. If that's the case, then add a new one at the end of the array by assigning the x and y based on the position of the rocket (plus some quantities depending on the size of the rocket to place it at the top center of the rocket) and by making the sprite visible. Note that you'll need to position the sprite itself based on the x and y that you just computed. Increment the bullet count.
To erase a bullet, make its sprite invisible, swap all 3 values (x, y, sprite) with the one at the end of the array, and then make that sprite invisible. Then decrement the bullet count.
Ex. 4. Collision detection. Add a timer object to the class Rocket to animate the bullets. This function should move all the bullets from 0 to bulletCount-1 up by 1 position (considering that the top corner is 0, this means decreasing y). Then you need to check for collision detection for all the objects in the game. This means checking for collisions between every bomb and every bullet, and between the rocket and every bomb.
For the purpose of the collision detection, we'll consider that all the objects involved in the game are spherical. For the example bomb, its radius is equal to 15, half of 30. For the rocket that would mean a radius of 25, but because of its shape we'll consider its radius somewhat smaller, let's say 20, so that bombs don't hit it too easily. Then two objects of radius r1 and r2 are colliding if the distance between their centers c1 and c2 is less than or equal to the sum of the radii. The center of the object is obtained by adding the radius to both corner coordinates.
When a collision happens, play the exploding (gunshot). If it involves a bullet and a bomb, then erase the bullet (as described above), and re-randomize the bomb. If a collision between a bomb and the rocket is detected, then the game is lost.
Ex. 5. Game goal. Note the countDown variable declared in the Rocket class. Every time a bullet hits a bomb, decrease this counter and update the text field. If the counter reaches 0, then declare the game won and display the information in the text field (you may need it to be larger). Otherwise if the rocket is hit by a bomb, then declare the game lost and display it in the text field. In both cases, you need to stop the bullet and bomb timers.
Extra credit. Up to 5 points for any creative addition to the game functionality.
Turn in: the source file .fla, the compiled version .swf, and all the resource files and classes that you create and modify.