Coin Flipper

Step 1

Place a ||input:on button pressed|| block to run code when button A is pressed.

input.onButtonPressed(Button.A, () => {
})

Step 2

Place a if block and check the value returned by the ||Math:pick random true or false|| block.

||Math:pick random true or false|| returns true or false randomly.

input.onButtonPressed(Button.A, () => {
    if (Math.randomBoolean()) {
    } else {
    }
});

Step 3

Place a ||basic:show icon|| block under the if and pick one of the images.

input.onButtonPressed(Button.A, () => {
    if (Math.randomBoolean()) {
        basic.showIcon(IconNames.Skull)
    } else {
        basic.showIcon(IconNames.Square)
    }
});

Step 4

Click |Download| to transfer your code in your micro:bit and press button A to try it out.

Step 5

Place multiple ||basic:show icon|| blocks before the if to create a coin flipping animation.

input.onButtonPressed(Button.A, () => {
    basic.showIcon(IconNames.Diamond)
    basic.showIcon(IconNames.SmallDiamond)
    basic.showIcon(IconNames.Diamond)
    basic.showIcon(IconNames.SmallDiamond)
    if (Math.randomBoolean()) {
        basic.showIcon(IconNames.Skull)
    } else {
        basic.showIcon(IconNames.Square)
    }
})

Step 6

Click |Download| to transfer your code in your micro:bit and press button A to try it out!