Build and Play this BBC micro:bit Board Game
Check out this quick and easy project, made me, Sam (and inspired by this BBC tutorial), which will teach you to use some of Block Editor's nifty tricks. Replace that boring, plastic, six-sided cube you usually use when playing board games and replace it with your brand new micro:bit!
I've even designed a special BBC micro:bit snakes and ladders board for you to print out and use.
This project might appear simple but it will, in fact, teach you important fundamental rules about coding. And, because it uses Block Editor, it’s easy to visualise how the code functions. Once you feel like you've got it, why not try and recreate the code using one of the other languages!
Before we get to the game playing, we need to build a dice code, using If statements to run code run code depending on whether a condition is true or not. You can find a handy tutorial below. Or, if you’re feeling lazy, you can download a code we've written.
But before we let you download the code we're going to show you to how it works! Don't worry, it won't take long and you'll gain some really valuable understanding about how to code the micro:bit using Block Editor. So get on a computer, fire up Block Editor over on the BBC website and let's get going!
First, we'll need to create a condition for if the micro:bit is shaken.
It looks like this and is hiding somewhere amongst the Inputs. This is because the accelerometer is an input, just like pressing either of the buttons.
Next, we need to have the micro:bit show a random value between 1 and 6 when the micro:bit is shook. What's the point of having a shake condition if you aren't going to do anything with it? We set a local variable and call it 'roll', and for 'roll' to set the micro:bit to pick a number between 0 and 5. (Make sure you don't put it between 0 and 6: 'roll' has a range of 0 to 5, which is six!)
That part looks like this: .
Now that the local variable is set, when the shake input is registered the roll variable will be set in action.
Now, time to set up a condition for if roll is 5. Use the micro:bit's LEDs to show what looks like the sixth face of a die. Get to know the if conditionals!
To get your code looking like ours you'll have to take a look in the logic, maths and variable sections. Having set the parameters that local variable roll enacts pick random number from 0 to 5, you now want to start setting the results if roll = 5, if roll = 4, and so on. Confusingly, show leds is under the basic tab rather than LED.
Add an else if conditional to your first if for roll = 4. It should look something like this.
Now it's time to repeat steps 4 and 5, to show the LEDs for 4, 3, 2 and 1. Add another if to the first, and set it for roll = 3. Then repeat step 5 with an else if for roll = 2.
Eventually you'll end up with a code that looks like this:
You've come all this way, but you still might not quite get how the code works. Or maybe our instructions weren't clear enough! Or you've skipped ahead because you're lazy! Either way, here is our promised pre-made code to flash onto your micro:bit. You can check out how the code works by importing it into Block Editor, and while you're there why not play around with it. Turn that six-sided dice into a twenty-faced dice for all the Dungeons & Dragons fans out there.
Now you just need to print off the handy snakes and ladders board made by yours truly (available for download soon) or find one on your chosen search engine. You can also use this code for your favourite dice-based board game.