Activity 12.2 - Modifying the game

Topic

In this Activity you will modify the game so that if the robot reaches the star portal a “whoosh” noise is played and the robot vanishes.

Resources

A sound file woosh.wav has been added to the res folder for the project.

This is where the code you are going to write will expect to find it.

Instructions

  1. In the project Ex12_2 begin by adding a new method void whoosh() to the class Game. A convenient place to locate this method is following the method endTune() that appears at the end of the class definition. The body of the method should play the “whoosh sound”. The code needed for this can be found in Section 6.1 under Non-musical sounds. The createPayer() method it invokes can throw an exception, so you need to enclose the code in a try with a corresponding catch block.
  2. At the end of the game loop (i.e. at the end of the loop controlled by the statement while (gameTicks > 0) insert an if statement which uses the collidesWith() method to check if the sprite referenced by robot has collided with the one referenced by starPortal. The second argument to collidesWith() should be false – see Collision detection in Section 4.3.

  3. If the robot has collided with the star portal, that means it has escaped the maze! Your code should then:

  4. Run the game and verify that the whoosh is played correctly and the robot vanishes. If you have trouble getting the robot to the star portal in time, you can cheat a little by altering the initial number of game ticks to a bigger number.

Notes

  1. Ensure that you insert the code that checks if the robot is out at the end of the game loop, not at the end of the run() method, otherwise it won’t work as intended.
  2. If you do not include the return statement mentioned in Instruction 2 the whoosh sound will get played repeatedly and may cause an OutOfMemoryError.
  3. The ending is rather crude as it stands. If the robot escapes the message displayed should change from “Time units left …” to something more appropriate. If you are interested and have time you might like to try making this improvement.