Dice with Random


Step 1 /10

Introduction

Dice are a great way to play games. Using CodeBug’s random block, you can make a user controlled dice in minutes.


Step 2 /10

Checking for a button press

You want the person rolling the dice to be able to roll it again and again. This means you need to loop your code and check if the user has pressed a button

  • From the Loops menu drag in a repeat while true loop.

  • From the If menu drag in an if block and snap it inside your repeat while true loop.

  • From the Inputs/Outputs menu drag in a button A pressed block and snap it to the puzzle piece of your if block.


Step 3 /10

Using variables

You want to be able to store a random number to check what number it is.

  • From the Variables menu, drag in a set item to block and snap it inside your if block.

  • Click on the drop down arrow of your set item to block and select rename variable.

  • In the window that appears type random number and click OK.


Step 4 /10

Getting a random number

You want to get a random number between 1 and 6 for the 6 sides of a regular dice.

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

  • Click on the first 0 block in your random integer block and type 1.

  • Click on the second 0 block and type 6.


Step 5 /10

Conditional code

You now need to check the value of your random number variables.

  • From the If menu, drag in an if block and snap it below your set random number to block.

  • Click on the blue cog on your new if block.

  • In the window that appears, snap five else if blocks below the if block on the right of the window.

  • Click on the blue cog again to close this window.


Step 6 /10

Checking the variable

Now that your if is set up, you need to check the random number variable against different numbers.

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

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

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

  • Right click on your = block and select Duplicate five times.

  • Drag these new = blocks to each of the puzzle pieces of your if block.

  • Change the numbers in the second gaps of each of your = blocks to the numbers 1 to 6, so the first = block contains the number 1, the second = block contains the number 2, and so on.


Step 7 /10

Drawing the dice faces

Dice show the numbers 1 to 6 pictographically, you will do this by drawing sprites.

  • From the Sprite menu, drag in a draw sprite block and snap it inside the first gap of your inner if block (below random number = 1).

  • Again from the Sprite menu, drag in a build sprite block and snap it to the puzzle piece of your draw sprite block.

  • Right click on your draw sprite block and select duplicate five times.

  • Drag each of these draw sprite blocks into the other gaps of your inner if block.

  • Click on the checkboxes of the build sprite blocks to represent the random number in dots, (e.g. click three boxes for the random number = 3 section and five boxes for the random number = 3 section).


Step 8 /10

Slowing down your program

Currently your code will rush through lots of dice rolls when a user presses the button, so you need to pause after they roll the dice.

  • From the Basics menu, drag in a pause for time block and snap it inside your outer if block, below your inner if block.


Step 9 /10

Run your code

Test that your code works correctly.

  • Click the play button.

  • Click on the button A on the emulator a few times.


Step 10 /10

Download your code

  • Click the Download button to download your code.

  • Load it onto CodeBug.