Challenge 9: Auto-Fire

Pressing the fire button over and over might get a bit tedious. Auto-fire would be a nice bonus for the player. Give it as a reward for the player's ability to navigate effectively through space.

Add a blue orb (the image was in the downloaded project files) that comes down the screen occasionally. If the player is able to collect this orb (by colliding with it), then they will receive autofire ability. Here is an example in which the blue orb appears after about every 16 seconds and auto-fire continues for about 8 seconds after having collected the orb.

While not included in my example, add ambiant background music as well as sound effects for firing bullets, obtaining the blueOrb, and explosions.

Try completing this with no help... but if you get stuck, use the suggested steps below.

Suggested Steps

  1. Get Auto-Fire to work by calling the fire() function every other time through the gameloop. Calling fire() every time through the gameloop will make the ship fire too frequently. To make fire() occur every other time, first add currentCount as a global variable with an initial value of zero to keep track of how many times the gameloop has been executed. Then in gameloop(), add the code to increase the currentCount by 1, and then use the mod (%) operator to fire only when currentCount is an even number:
    if(currentCount%2 == 0) fire();

    The mod operator is simply the remainder of a division. So
    100%2 is 0, because 100/2 is 50 with a remainder of 0;
    101%2 is 1, because 101/2 is 50 with a remainder of 1;
    102%2 is 0, because 102/2 is 51 with a remainder of 0;
    103%2 is 1, because 103/2 is 51 with a remainder of 1;

  2. Create autofire as a global variable and add an if statement around the code that is causing the autofire so that it only fires when autofire is true. Initialize autofire to true to test it out. Once it is working properly, change the initial value of autofire to false.

  3. Similar to the enemy ships, dynamically create a blueOrb IMG element when the page loads, create a placeBlueOrb() function that positions the blueOrb high above the gameScreen, and add into the gameloop the code that moves the blueOrb. The higher up the blueOrb is placed, the longer it will take for it to appear on the screen.

  4. Add a hittest to determine when the ship collides with the blueOrb, and at that moment the blueOrb should be placed to a new location and autofire should be set to true.

  5. Add autofireCount as a global variable with an initial value of 0 to keep track of how many times through the gameloop that auto-fire has been on. In the gameloop, add to the conditional statement that causes the autofire to occur (the one with the % operator); increase autofireCount, and check if autofireCount becomes greater than the number of bullets you want to be automatically fired when the blueOrb is obtained. If so, set autofire back to false and autofireCount back to 0.

  6. Once all is working well, add the music and sound effects.

Of course, you are always encouraged to go above and beyond as these students have done:

Spring 2016

Spring 2015