# Catch the dot

This is going to be even more fun! We’re going to code a game onto the micro:bit.

You’ll learn how to

• Use sprites.
• Move the sprites.
• Delete a sprite.
• Use the direction of the micro:bit as an input for a program.
• Check if two sprites are in the same position.

Log on before starting the exercise.

### ORIENTATION

Take a look at the example and consider the following questions:

• What should I do?
• What are the goals?
• What are the expectations?
• What can I already do?
• What do I still have to discover?

### PREPARATION

View the steps under 'perform'. Then answer the following questions:

• Which steps should I follow?
• Which steps can I follow?
• How much time do I need?
• What tools do I need?

### PERFORM

Perform the steps one by one. Do you not remember how to do it? View the hints or download the solution.

# Make a sprite

As usual, we’ll start by making a variable. Make a variable with the name “player”.
We can turn a variable into a sprite on your micro:bit. Go to “Game” (first, click on Advanced)
Drag these blocks from Game and Variable into your work field and piece them together.

This sprite is one light on the screen of your micro:bit.

This way, you can control all the LEDs on your micro:bit. The ‘x’ and ‘y’ you that see above, show the position of the LEDs. The image below will make that clear. For a dot in the left-hand corner, x=0 and y=0.

Try to make a sprite when starting up the micro:bit. When testing out the code, you will see it appear.

## Move by using the buttons

You can make your sprite move by changing the values of x and y. Delete the code you made in the last step, and make a LED light up in the middle of your Micro:Bit. (X = 2 and Y = 2)
Search for this block in the menu “Game” (you find it in "advanced"). You can use this to make a led go from left to right.

Can you make the sprite move to the left by using the A-button, and to the right by using the B-button?

## Step 3

### MOVING A LED LIGHT BY TILTING THE MICRO:BIT

Let’s take it a step further! If we tilt the micro:bit to the left, the LED has to move to the left. If we tilt the micro:bit to the right, the LED also has to move to the right. The position of the micro:bit determines the way our sprite moves.
In order to do that, we’ll use the following block from the menu Input:

This block uses a number to show the way the micro:bit is tilted. The following image will make that clear:

The value of the acceleration can be between -2048 and 2048.
We can now use this to move our sprite. If the acceleration in a certain direction is large enough, then the dot will move.
Experiment with the value you’re going to use. Will you test the code?

You’ll notice that the sprite now reacts very quickly. To solve this, you can insert a break.

## Step 4

### A DOT TO CATCH

When a button is pushed, we want to start the game. A dot will appear in a random position. The player has to try to make their sprite ‘catch’ this dot.
First, you’ll have to set up a new variable as the dot. Can you think of a fitting name?

Make sure that when you push the A-button, a LED lights up. The blocks you’ll need for this:
• When the A-button is pushed.
• Your new variable: set ... to.
• make sprite at X ... Y ... (from menu Game)
• Pick a random number from ... to ... (from menu Maths)

## Step 5

### CATCHING THE DOT

Now we’ll try to use our Player to catch this Dot. You’ll have to check if the player has already caught the dot. You can use the following block to do this:

As long as the dot HASN’T been caught, the sprite has to keep moving. We will need a loop: “while ... do...” .

What we want to know, is when the player and the dot do NOT touch each other. That’s why you need a ‘not’-block, which you can find in the ‘logic’ group:

Start by fitting these blocks together.

For the moment, nothing is moving. We’ll try to make the dot move randomly.

Our code already works! If you want to replay the game, however, you’ll notice that the previous dot has not disappeared. You can solve this by using the following block:

### REFLECT

Look back at the project you just made. Ask yourself the following questions:

• Does the program work as I expected? Why / why not?
• What works well?
• What works less well?
• How can I approach it differently?