Project 327: Exploder Course

9. Randomize the explosion

The brick is exploding constantly right now, which would make the game very difficult. Let's have it only explode when a random timer says it's ready to explode. 

Starting point file for this challenge

Your goal

Steps

1. Only explode if "ready" is true

We're going to wrap the code we wrote into an if-statement.

Before the explosion instance is made, write an if-statement that checks if a variable called "ready" is true.

After the explosion instance is made, write "end" to end your if-statement.
if ready == true then end

2. Turn the if-statement into an if-then-else

After the explosion instructions, write "else" to create an if-then-else statement. 

Hit return to create an empty space, then hit return again and type "end" to create an end to your if-then-else statement. 

3. Clean up the formatting

Try using the tab key and the delete key on your keyboard to organize your code closer to the picture example. This will make it much easier to read. 

4. Pick a random number if ready ISN'T true

Tell the computer to pick a random number between 1 and 6 using math.random. 

math.random(1,6)

5. Label the random number

Remember to keep track of your numbers! We'll create a variable just called "randomNumber" and set it equal to the random number the computer picked. 

randomNumber = math.random(1,6)

6. Check if that random number is 1

Create a new if-statement, nested inside the "else" of the first if-statement. 

See if "randomNumber" is equal to 1. 

if randomNumber == 1 then

7. Hit return to create an "end" to your new if-statement

Anything written in between "then" and "end" will be the "then" blank of your new if-statement. 

8. If the random number is 1, set ready to true

This means the computer has a 1 in 6 chance of making your brick ready to explode. 

ready = true

9. Set ready back to false after its done exploding

Remember, boolean variables (true/false variables) are like a light switch - if we turn them on, we have to turn them OFF again at some point. Otherwise, they'd just stay on forever. 

ready = false

10. Test it out

How are the bricks exploding now?