Back to Activity
Tutorial
Random answer
{% trans 'Sorry, your browser does not support canvas -- please either update your browser or submit your requirements by phone or email.' %}

Accelerometer

Loading Blockly...

Step 1 /9

Introduction

CodeBug can display messages randomly using the random block and scroll sprite blocks.

Step 2 /9

Random number variable

  • From the Variables menu take a set item to block and snap it to your start block.

  • Click the dropdown menu and select rename variable.

  • Give this variable the name "random-number".

  • From the Maths menu, drag in a random integer block and snap it to your variable block.

  • Change the second number in this random block to "3".

Step 3 /9

User control

  • From the Basics menu, drag in a sleep until button changes block.

This will let the user decide when they want CodeBug to answer their question by pressing one of the buttons.

Step 4 /9

Conditional code

  • From the If menu, drag in an if block and snap this below your sleep until button changes block.

  • Click on the blue cog of this block.

  • In the window that appears, drag three else if blocks under the if block on the other side of the window.

  • Click on the cog again to close the window.

Step 5 /9

Checking the number

  • From the Logic menu, drag in an equals block (=) and snap it to the first puzzle gap of your if block.

  • From the Variables menu drag in your random number variable block and snap it inside the first gap of your = block.

  • From the Maths menu, drag in a 0 block and snap it into the second gap of your = block.

  • Right-click on your = block and select Duplicate and snap the block that appears into the second puzzle gaps of your if block.

  • Repeat this for the remaining puzzle gaps of your if block.

  • Click on the 0 blocks in your = blocks and change them so you the first one is "0" the second is “1” the third is “2” and the fourth is “3”.

These will check what your random number is.

Step 6 /9

Mystic messages

Now to create the mystic messages.

  • Click on the Sprite menu and drag in a scroll sprite block, snap this inside the first gap of your if block.

  • Again from the Sprite menu, drag in a get string sprite block and snap it to your scroll sprite block.

  • Change the "Hello world!" text to “Yes”.

  • Right-click on the scroll sprite block and duplicate it three times.

  • Drag each of the duplicates under a different gap in your if block and change their messages to "No", “Maybe” and “I’ve got a bad feeling about this”.

Step 7 /9

Running your code

  • Click run on the emulator, then click either the A or B button on the emulator. CodeBug will give you an answer.

Step 8 /9

Looping

We want CodeBug to be able to answer lots of questions.

  • Click on the Loops menu and drag in a repeat while true loop and snap this to your start block.

  • Click on your set random number block and drag it (and the rest of your code) into your repeat loop. Now your program will respond with an answer each time you press the button.

Step 9 /9

Download your code

  • Download your code and load it onto your CodeBug.