How to Make Dice
This article will walk through creating a simple die with Scratch. When finished a user should be able to press space and a random dice face should show on the screen. Start by creating a new project and delete the Scratch Cat sprite.
Creating the Costumes
Before coding is begun, several costumes have to be made. For this, 6 costumes are required, each to represent a face of the dice. It does not have to be the traditional style, it could even be just numbers. However, make sure they are all different. Once that is finished, name the costumes from 1-6. This will be necessary later on.
Coding the Dice
Now that all the costumes have been created, it is time to code the dice to add functionality.
Starting the Program
Unlike manyШаблон:Citation needed programs on Scratch, which start with the when green flag clicked
block, this program will start with the when this sprite clicked
block. Just to make sure that the dice displays the output in the centre of the stage, use the go to x: () y: ()
block. The code should look like this:
when this sprite clicked go to x: (0) y: (0)
Rolling the Dice
Now that the program can start, it is time to create the main mechanism. The code needs to be added to randomise the outcome. For this, the switch costume to ( v)
and pick random () to ()
blocks are needed. The random block with pick a random number between 1 and 6, which will make the sprite will switch its costume to the costume named with the number. Now join them together like so:
switch costume to (pick random (1) to (6))//do not change any of this, it could mess up the program
Now join all the blocks together to form this script:
when this sprite clicked go to x: (0) y: (0) switch costume to (pick random (1) to (6))
This tutorial is now complete.