Introduction

You are going to make a game in which players have to guide a wand along a course without making contact. Making contact will add one to the player’s score - the player with the lowest score wins!

screenshot
screenshot

For this project you’ll need some additional items:

  • Metal wire (approx. 50cm);
  • Modelling clay (Plasticine or similar, not air-drying, needs to be non-conductive);
  • Electrical tape (optional);
  • Crocodile clip leads (optional).
screenshot
screenshot

Step 1: Storing fails

Let’s start by creating a place to store the number of fails.

Activity Checklist

  • Go to jumpto.cc/mb-new to start a new project in the Code Kingdoms editor. Call your new project ‘Frustration’.

  • Drag the onStart event (along with the comment above it) into the bin.

screenshot
screenshot
  • A new game should start when the player presses button A. Click Add Event and then onPressA.
screenshot
screenshot
  • A variable is a place to store data, such as the number of times the player has touched the wire. To create a new variable, click the ‘Library’ tab and then click ‘Globals’.
screenshot
screenshot
  • Click the ‘+’ button, and create a new variable called fails.
screenshot
screenshot
  • You’ll need to set the number of fails to 0 at the start of a new game. To do this, first drag your fails variable into your onPressA event.
screenshot
screenshot
  • You can then set the number of fails by clicking the arrow on your fails block, clicking ‘Number’ and then typing 0.
screenshot
screenshot
  • Finally, you can display the number of fails on your micro:bit. To do this, first drag a say block to the end of your script.

    screenshot
    screenshot
  • To display the number of fails, click the arrow on your say block and click on your variable.

    screenshot
    screenshot

    Here’s how your code should look:

    screenshot
    screenshot
  • Click ‘run’ to test your script. Clicking button A should display the number of fails, which has been set to 0.

screenshot
screenshot

Challenge: Display an image

Can you display an image for 1 second (1000ms) before the number of fails are displayed?

screenshot
screenshot

You’ll need to use the following blocks do to this:

screenshot
screenshot

Step 2: Keeping track of fails

Let’s add code to keep track of fails.

Activity Checklist

  • You’re going to add 1 to your fails variable every time a connection is made on Pin0. To do this, Click ‘Add Event’, and choose onPressPin0.
screenshot
screenshot
  • Next, add 2 blocks to display a cross for 1 second when Pin0 is pressed.
screenshot
screenshot
  • You’ll then need to add 1 to your fails variable. To do this, click the ‘Library’ tab and then click ‘Globals’ and drag in your fails variable.
screenshot
screenshot
  • Click the update arrow, and choose left + right.
screenshot
screenshot
  • Click the left arrow and choose your fails variable.
screenshot
screenshot
  • Click the right arrow and type 1. This means that your fail variable will add 1 to its old value. Here’s how your block should look.
screenshot
screenshot
  • Finally, you can add code to display the updated number of fails. Here’s how your code should look.
screenshot
screenshot
  • Click ‘run’ to test your code, and press button A to start your game. Each time you press Pin0 you should see your fails variable increase by 1.
screenshot
screenshot
  • Click ‘compile’ and transfer your script onto your micro:bit. You can press Pin0 by completing a circuit. To do this, place your right thumb on the ground pin (GND) and then tap Pin0 with your left thumb.
screenshot
screenshot

Step 3: Building your game

Now that you’ve coded your game, let’s put it all together!

Activity Checklist

  • First, let’s make your wand. Take a piece of wire about 20cm long and bend it in half, making a loop at the top.
screenshot
screenshot
  • You can then twist the two pieces of wire together.
screenshot
screenshot
  • To make the course, take another piece of wire about 30cm long and bend the middle part of the wire into shape. You should bend up one end of the course.
screenshot
screenshot
  • If you have some electrical tape, wrap some around the two ends of the wire, leaving some exposed metal at both ends.
screenshot
screenshot
  • Slide your wand through your course, and push the ends of your wire into some clay to stand it up.
screenshot
screenshot
  • You can now connect your game to your micro:bit using a crocodile clip lead or some wire. Firstly, connect the ground pin (GND) to one end of your course.
screenshot
screenshot
  • You can then connect Pin0 to your wand.
screenshot
screenshot
  • Test your game. Press button A and your score should be set to 0. Each time your wand touches the course, the circuit is completed and your micro:bit should add 1 to your number of fails.
screenshot
screenshot

Challenge: Cheat mode!

Can you add a cheat to your game, so that pressing button B reduces your score by 1?

Challenge: Personalise your game

Get some friends to try out your game. If your game is too easy, you can make it harder by:

  • Creating a longer course;
  • Adding more bends to your course;
  • Making a wand with a smaller gap.