CODE CREATIVE
SUBMARINE ATTACK
Sprite collision detection
TASK #1
Watch the video below to see an example of the "Submarine Attack" program.


CHALLENGE EXERCISE #1
  1. Define the following variables: bombX and bombY.
  2. Define and initialize the variables worldWidth and worldHeight. Each should be set to 300 units.
  3. Define and initialize the variables subWidth and subHeight. Remember that the sub's width must be a value between 40-70 units and the sub's height must be a value between 10-20 units, inclusive.
  4. Define and initialize the variables subX and subY. Remember that these represent the top left corner of the sub, the sub must be randomly placed, and no parts of the sub can exist outside of the world.
  5. Define and initialize the variable blastRadius. Set this variable to 30 units.




TASK #2
TASK:
Take in the bomb's x and y position as user inputs. Remember that these values must ensure that the entire blast radius is within the world and must error if outside the world.

HINT:
You need to typecast the numerical string to an integer using Integer.parseInt(). You also need to use a while loop to force the user to re-enter the bomb's position if it results in any part of the bomb blast being outside of the world.

CHECK UP:
Watch the video below to check that you completed the task.
TASK #3
TASK:
Watch the following video for a breakdown of the problem of how to detect a hit between the bomb and the sub.

CHECK UP:
Watch the video below to check that you completed the task.


CHALLENGE EXERCISE #2
  1. Check if the bomb has hit the submarine top left corner of the submarine. Remember that you can use Math.sqrt() along with Math.pow() in your calculations.




TASK #4
TASK:
Adjust the algorithm described in "Challenge Exercise #2" to only occur if the bomb is in the top left quadrant.

HINT:
To check if the bomb is in the top left quadrant, you will need to compare the bomb's x position with the left side of the sub and the bomb's y position with the top of the sub.

CHECK UP:
Watch the video below to check that you completed the task.
TASK #5
TASK:
Check if the bomb has hit the top of the submarine.

HINT:
You will need to check if the bomb is directly above the submarine and find the closest distance from the bomb to the submarine.

CHECK UP:
Watch the video below to check that you completed the task.


CHALLENGE EXERCISE #3
  1. Check if the bomb has hit the submarine top right corner of the submarine.
  2. Check if the bomb has hit the right side of the submarine.
  3. Check if the bomb has hit the submarine bottom right corner of the submarine.
  4. Check if the bomb has hit the bottom of the submarine.
  5. Check if the bomb has hit the submarine bottom left corner of the submarine.
  6. Check if the bomb has hit the left of the submarine.
  7. Remember to thoroughly check that each of the above challenge exercises accurately detects a hit! The best way to do this is to set the sub's x and y position to static values and check the edge cases.






CHALLENGE EXERCISE #4
  1. Define a method called checkHit() that will return a boolean value based on whether the bomb has hit the submarine.






CHALLENGE EXERCISE #5
  1. Give the player 5 bombs to try and hit the submarine.
  2. Remember when looping, you must choose between using a for loop or a while loop.




CHALLENGE EXERCISE #6
  1. Two indirect hits will also sink the submarine. An indirect hit is defined as a bomb blast that is greater than 30 feet away and less than or equal to 50 units away from any part of the submarine.
  2. Remember that if the bomb doesn't provide a direct hit, you will need to check if the bomb hits indirectly. To do this, you can use reuse the checkHit() method by passing a different parameter. You will also need to count how many times an indirect hit has occurred.
CHALLENGE EXERCISE #7
  1. Once the user has destroyed the submarine, prompt them that another sub has been spotted and play the game again with the sub in a new position.
.
.
Congratulations, you have completed the Submarine Attack project!