Minecraft Modding For Kids For Dummies
Book image
Explore Book Buy On Amazon

When you code in Minecraft, you can use a random integer to make your code behave in random ways. An integer is just a number; the random integer Math block in LearnToMod chooses a random number between two numbers (1 and 10, for example) every time the code is run.

Using integers in Minecraft.
Using integers in Minecraft.

The first time you run this code, the result might be 5; the second time, it might be 8; and so on.

You can use this random integer block to spawn creepers or materials at random places in your Minecraft world.

The capability to generate a block at a random location can be useful for making the Capture the Flag game more exciting (where players have to race one another to find an object hidden somewhere on the map) because the flags are placed in random spots, making them more difficult to find.

Random spawn locations can also help build randomized mazes or enemy spawn locations to make games more interesting.

You should have a basic knowledge of how drones work so that it’s easier for you to spawn blocks or entities.

To use a drone to spawn blocks or entities, follow these steps:

  1. Add a main function and create a drone named d inside it, like this:

    Create your drone.
    Create your drone.
  2. Make two new functions named moveDrone and setForwardAndRight:

    Name your functions.
    Name your functions.

    These functions help the drone move forward and right a random number of steps.

  3. To make the drone move random distances, use the random integer block, found under Math:

    See? Math can be fun.
    See? Math can be fun.
  4. Set the values from –10 to 10 (though the range can be larger or smaller) so that the drone moves within a 10 x 10 square around its current location:

    Creating your 10 × 10 space.
    Creating your 10 × 10 space.
  5. Create two new variables (FORWARD and RIGHT), and set them equal to the random integer block.

    Each variable is now a random number between –10 and 10:

    Set your variables.
    Set your variables.

    Each time you call setForwardAndRight, the variables FORWARD and RIGHT get a new random number between –10 and 10.

  6. Make the drone move forward the random FORWARD amount and move to the right the random RIGHT amount:

    You’re in control of your drone.
    You’re in control of your drone.

    Now your code should look like this:

    Keep that drone moving!
    Keep that drone moving!
  7. Create another function in which you tell the drone to spawn a creeper at its new location. Then call the spawnCreeper function from main:

    Tell your drone what to do. You’re the boss.
    Tell your drone what to do. You’re the boss.

    If you want to repeat this process to spawn multiple creepers, make sure that the drone returns to the original location after it has spawned an entity.

    If the drone doesn’t return, it may continue to jump further and further away from you until it leaves the map, which is never good.

    By telling the drone that it can move a maximum of only ten steps in any direction, and that it must return to you when it’s done, you control the area within which creepers will be spawned.

  8. Create a function named returnDrone where you tell the drone to move backward the FORWARD number of steps, and to move to the left the RIGHT number of steps:

    Your code must tell your drone what it can do.
    Your code must tell your drone what it can do.

    This step basically makes the drone retrace its steps and return to where it started.

  9. Call returnDrone from main after you spawn the creeper:

    Spawn your creepers.
    Spawn your creepers.

    Then you can repeat all four function calls as many times as you want to spawn creepers in a 10 x 10 space around you:

    Spawning creepers again and again.
    Spawning creepers again and again.

If you call setForwardAndRight between moveDrone and returnDrone, like this:

Pay close attention to your code.
Pay close attention to your code.

The drone doesn’t return to its original location.

Suppose that FORWARD is set to 5 and RIGHT is set to 8 the first time you call setForwardAndRight. Your drone will move forward 5 squares, move to the right 8 spots, and then spawn a creeper. When you call setForwardAndRight again, suppose that the Forward setting is 6 and the Right setting is 9: Your drone moves backward 6 squares and right 9 squares, leaving it a block off diagonally from where it started.

About This Article

This article is from the book:

About the book authors:

Sarah Guthals, Ph.D. is the CTO of ThoughtSTEM and has dedicated her life to coding education.

Stephen Foster, Ph.D. is the CEO of ThoughtSTEM, a company that teaches computer science to kids across America.

Lindsey Handley, Ph.D. is the COO of ThoughtSTEM and has hundreds of hours of experience as a classroom instructor for Minecraft based science and computer science classes.

This article can be found in the category: