Web Blocks Tutorial 2

Using scripts to make block patterns

① Colourful blocks

Bored of the standard block colours? We can create our own colours! For this we'll write a script.

Scripts work a little differently from the Console. Instead of running the commands as we type them, they are all run at once when we press the Run button. This is great because it lets us run our script as many times as we want to!

➡ Go to webblocks.uk in your browser
➡ Press the ESC key on your keyboard
➡ Click on the Script tab
➡ Click on the New button
➡ Enter the following line of code:

setBlock(100,10,100,Colour,0);  

➡ Click on the Save button, type in magic colours and click Save again.
➡ Click on the Run button

Do you see the block appear on the screen? Notice that the block has a colour?

➡ Try changing the last number in the command from 0 to another number (i.e. 50, 100, 200)
➡ Click on the Run button each time you make a change to your script

💡 You can use the ESC key to toggle the script editor so you can better see your world.

What do you notice? Does the block change colour?

② A strip of blocks

In scripts we can use loops to automatically repeat sections of our code. We will adapt our script to include a loop.

We will add a for loop to run the setBlock command 20 times.

➡ Enter the following code (notice how the middle line is the same):

for (let i = 0; i < 20; i++) {  
  setBlock(100,10,100,Colour,0);
}

➡ Click on the Run button

Does anything different happen? No? That's because we are creating the same block 20 times in the same location! Let's fix that.

➡ Change the first number in the setBlock command from 100 to 100 + i and Run your script

That's more like it. We have changed the x co-ordinate from being fixed at 100 to a number that goes 100, 101, 102..., getting bigger for every cycle of the loop. But why are they the same colour?

➡ Change the last number in the setBlock command from 0 to 10 * i and Run your script

Do you see a rainbow of colours? We changed the colour number from 0 to a number that goes 0, 10, 20, 30...

➡ Do some further experimentation, changing how we use i in the script to make different effects