Step 6: Adding Rock Collision

Goal:


Let’s code the rocks in the game.

We want to constantly check if the “Sharky” sprite touched the rocks.

If a rock touches the Shark, then we want to subtract one from the player’s life and then broadcast a new “HealthLost!” message.

Then we need to check that if the player’s Lives is less than 1, then the “GameOver!” message will be sent.

Steps:

  1. Click on the “Rocks” sprite create the shown code.
  2. From Events, place a when green flag clicked block.
  3. From Control, place a forever block and place an if block inside it.
  4. From Sensing, place a touching mouse-pointer block and change it to touching Sharky.
  5. From Variables, place a change by block and set it to Lives: and set the number to -1.
  6. From Events, place a broadcast block and change it to HealthLost!
  7. From Looks, place a hide block.
  8. From Control, place an if block.
  9. From Operators, place a < block.
  10. From Variables place a Lives: block on the left side of the < block and set the right side to 1.
  11. From Control place a braodcast block and set it to GameOver!

Code: