Step 8: Finishing the Initial Rock Behavior

Goal:


Now let’s code where the rocks will reappear on the game screen after it has touched the Shark.

We want the rock to spawn between 100 and 200 units away from “Sharky:”.

We do this because if we don’t, there is a chance that the rocks can spawn inside “Sharky:” and it would be very unfair if that were to happen.

Once the rocks have picked a random spawn point away from the shark, then it will appear once more on the game screen.

Steps:

  1. Stay on the “Rocks” sprite.
  2. Underneath the last if statement, from Control place a wait 1 seconds block.
  3. From Variables place a set to block and change it to newX.
  4. From Operators, place a pick random block.
  5. From Operators, place a + (addition) block to each side of the pick random block.
  6. From Sensing place a distance to block to the left side of each + block.
  7. On the first + block, set the right side to 100 and on the second + block set the right side to 200.
  8. Now use your mouse to hover over the set newX block and right click on the mouse. A drop down menu will appear. Click on duplicate. Set the duplicated code underneath the set newX block.
  9. Change the duplicated set newX code to set newY.
  10. From Motion, place a go to x/y block.
  11. From Variables, place a round newX block and a round newY block into the go to x/y block.
  12. From Looks place a show block.

Code:


Click to replay