Welcome to mission control, start the video below to hear the first transmission from the spaceship.
Count your points!
The way chips and computers developed on our planet was greatly do to entertainment, games!
The most fun games let you set high scores! Let’s make a code that will add a point to a score when a sensor is triggered.
We will need to attach a sensor to the Easyboard, let’s use the distance sensor for now.
For counting something, we need to make what’s called a Variable, this is basically just a number that the chip remembers. To make a simple counter and show this on the micro:bit LEDs we can use the following code.
Before you can make this code, you need to create a Variable, we named this one ‘Var’. With this code, the counter is adding one, every time it is done displaying the number on the micro:bit. Now we want the code to add one, everytime a sensor is triggered, so the input signal is higher than for example 500. Can you come up with the code you need to make this work?
Upload the code and come close to the distance sensor to trigger the counter. Change the distance sensor for another sensor and try again, what kind of games can you come up with?
Score a goal!
We can use the code you just made to create a simple aiming game. Let’s make a simple counter that makes you score a point every time a marble is going in the goal.
It will look something like this:
The sensor is aiming down, play with the height of the goal (physically) and the strength of the signal (in your code, 500 in the example above), to finetune your goal and make sure it works correctly.
First take a piece of cardboard and fold it 4 times as shown in the image.
Then use the split pens to attach the sensor to the middle of the cardboard and to attach the folded piece to another piece of flat cardboard.
Attach the sensor to the Easyboard on port 0 and connect the micro:bit, upload the code you made earlier and try to score points!
As the code works now, if the ball remains in the goal, it will just keep getting points. We want it to just score one point at a time and only able to score another one once it is out of the goal again. We can make sure this happens using something called Flagging. We basically pull up a flag every time a goal is scored and do not let it happen again while the flag is up. Of course we need to make sure the flag is brought down when it needs to.
In code this means we make a new variable (let’s call it Flag). When the sensor is triggered we check whether the flag is down (Flag = 0). If so, the score variable (Var) gets + 1 and this time, the flag is put up (Flag = 1). As long as the sensor is triggered, the flag stays 1 and no new points are added. Once the sensor is no longer triggered (signal goes below 500) the flag is set to 0 again. Enabling the scoring of points once again.
Try out the code and fine tune if necessary!
What if you score twice within 1 minute? Should you get rewarded with more points? If yes, let’s make that happen.
In order to do that we have added two variables. First the PointMultiplier variable, everytime you score a goal, the points are multiplied with the PointMultiplier variable. This variable is set to 1 on start. The second variable will keep time and is called Timer. In order to keep time we can use a block called Running Time. It gives us the time the program has been running. Comparing to this time will help to see when goals are scored soon after one another. If you can figure out this code on your own, super! If not, no problem, this one is very tricky.
Note that in this code, the scores need to follow up within 5 seconds (5000 ms). This is done to test the code, you can change it later to a time you want.
Also note that the timer is being set to -5000 this is to prevent the sensor triggering the first time.
The changing and showing of the score takes some time, especially when the score grows bigger than double digits. During this time, the code is on pause. To prevent this from getting in the way of your game you check the score when a button is pressed, saving you precious time during fast games.
A Button Pressed block can go out of the On Start or Forever blocks.
What if there were more ways to score points and what if it worked together? Let’s build a pin ball machine and use the different sensors to score points. Maybe even get some output parts in there to shake things up a bit!
Show us your creation!
Be sure you are logged in to receive your space tokens!
Upload a video of your creation and send us the link!