2017-12-07 01:39:55 +01:00
# Hot or Cold
2017-06-24 08:55:04 +02:00
2017-09-07 22:42:08 +02:00
## @description A Hot-or-Cold treasure hunt game
2017-06-24 08:55:04 +02:00
2017-09-07 22:42:08 +02:00
## ~avatar avatar
2017-06-24 08:55:04 +02:00
2017-06-25 17:28:21 +02:00
Find the hidden @boardname @ before the other players!
2017-06-24 08:55:04 +02:00
2017-09-07 22:42:08 +02:00
## ~
2017-06-24 08:55:04 +02:00
https://youtu.be/nbRfNug-RkY
## Beacons
2017-12-07 01:39:55 +01:00
In this game, players are looking for a hidden @boardname @ that emits a radio signal.
Hidden @boardname@s are called **beacons** .
2017-06-24 08:55:04 +02:00
2017-12-07 01:39:55 +01:00
### Setting up the radio
2017-06-24 08:55:04 +02:00
2017-12-07 01:39:55 +01:00
We set the radio group to ``1`` and all the players use the same group.
The @boardname @ transmits its serial number (that's a unique number that identifies it)
so that players can tell one beacon apart from another. Also, the power of the antenna is reduced to shorten the range of transmission.
2017-06-24 08:55:04 +02:00
```block
radio.setGroup(1)
radio.setTransmitSerialNumber(true)
radio.setTransmitPower(6)
```
2017-12-07 01:39:55 +01:00
### Beacon gotta beam
2017-06-24 08:55:04 +02:00
2017-12-07 01:39:55 +01:00
The beacon just needs to send a radio message every now and then. So, to pace the transmits and give some visual feedback, we add some ``||basic:show icon||`` blocks to animate the screen.
2017-06-24 08:55:04 +02:00
```blocks
basic.forever(() => {
radio.sendNumber(0)
basic.showIcon(IconNames.Heart)
basic.showIcon(IconNames.SmallHeart)
})
radio.setGroup(1)
radio.setTransmitSerialNumber(true)
radio.setTransmitPower(6)
```
2017-12-07 01:39:55 +01:00
### Hide the beacons
2017-06-24 08:55:04 +02:00
2017-12-07 01:39:55 +01:00
Download the code to each beacon @boardname @ and hide them!
2017-06-24 08:55:04 +02:00
## Hunters
2017-12-07 01:39:55 +01:00
The hunter @boardname @ looks for beacons.
2017-06-24 08:55:04 +02:00
2017-12-07 01:39:55 +01:00
### Is the beacon close?
2017-06-24 08:55:04 +02:00
2017-12-07 01:39:55 +01:00
To determine how far away or how close they are, we use the signal strength of each radio packet sent by the beacons. The signal strength ranges from ``-128db`` (weak) to ``-42db`` (very strong).
2017-06-24 08:55:04 +02:00
```blocks
2018-10-16 00:32:09 +02:00
let signal = 0;
radio.onReceivedNumber(function (receivedNumber) {
signal = radio.receivedPacket(RadioPacketProperty.SignalStrength)
basic.showNumber(signal);
2017-06-24 08:55:04 +02:00
});
radio.setGroup(1)
```
2017-12-07 01:39:55 +01:00
Test and record the signal values as you move around a beacon, moving closer and farther away:
| | |
|-|-|
| Hot signal value: | ``_________________`` |
| Warm signal value: | ``_________________`` |
| Cold signal value: | ``_________________`` |
### Hot or cold?
2017-06-24 08:55:04 +02:00
2017-12-07 01:39:55 +01:00
The hunter's screen will display:
2017-06-24 08:55:04 +02:00
2017-12-07 01:39:55 +01:00
* ``SmallDiamond``: if the beacon is far (cold).
* ``Diamond``: if the beacon is relatively close (warm).
* ``Square``: if the beacon is really close (hot).
2017-06-24 08:55:04 +02:00
2017-12-07 01:39:55 +01:00
Use the ``signal`` values collected in the previous step to determine when to show each icon.
2017-06-24 08:55:04 +02:00
2018-11-20 22:26:26 +01:00
Here is an example that uses ``-95`` or less for cold, between ``-95`` and ``-80`` for warm, and ``-80`` or above for hot. You can change these values to account for your room setup or conditions of your hiding place.
2017-06-24 08:55:04 +02:00
2017-12-07 01:39:55 +01:00
To make the program more responsive, add a ``||led:stop animation||`` to cancel icon animations when a new beacon packet comes in.
2017-06-24 08:55:04 +02:00
```blocks
2018-10-16 00:32:09 +02:00
let signal = 0;
radio.onReceivedNumber(function (receivedNumber) {
signal = radio.receivedPacket(RadioPacketProperty.SignalStrength)
2017-06-24 08:55:04 +02:00
if (signal < -90 ) {
basic.showIcon(IconNames.SmallDiamond)
} else if (signal < -80 ) {
basic.showIcon(IconNames.Diamond)
} else {
basic.showIcon(IconNames.Square)
}
})
radio.setGroup(1)
```
Download the code and play the game!
2017-12-07 01:39:55 +01:00
## Expand the game: multiple beacons
2017-06-24 08:55:04 +02:00
2017-12-07 01:39:55 +01:00
We'll making the game more interesting by counting how many beacons the hunting player has seen so far.
2017-06-24 08:55:04 +02:00
2017-12-07 01:39:55 +01:00
### Remember the beacons
2017-06-24 08:55:04 +02:00
2017-12-07 01:39:55 +01:00
Do you remember that the beacon was configured to transmit its serial number? We can use this information
to determine if a beacon is new or if it's one we've seen before.
2017-06-24 08:55:04 +02:00
2017-12-07 01:39:55 +01:00
To do so, we add an ** [array ](/types/array )** variable that will hold all the beacon serial numbers seen so far.
2017-06-24 08:55:04 +02:00
```block
let beacons: number[] = [0]
```
2017-12-07 01:39:55 +01:00
Whenever we receive a new packet, we are going to check if the ``beacons`` array already
contains the serial number. If not, we add the serial number at the end of ``beacons`` and increment the ``||game:score||``.
2017-06-24 08:55:04 +02:00
2017-12-07 01:39:55 +01:00
To check if an array contains an certain element, we use the ``||arrays:find index of||`` block which returns ``-1`` if the element is not found.
2017-06-24 08:55:04 +02:00
```blocks
let beacons: number[] = [0]
2018-10-16 00:32:09 +02:00
let signal = 0;
let serialNumber = 0;
radio.onReceivedNumber(function (receivedNumber) {
signal = radio.receivedPacket(RadioPacketProperty.SignalStrength)
serialNumber = radio.receivedPacket(RadioPacketProperty.SerialNumber)
if (signal > -50 & & beacons.indexOf(serialNumber) < 0 ) {
beacons.push(serialNumber)
2017-06-24 08:55:04 +02:00
game.addScore(1)
basic.showNumber(game.score())
}
})
```
2017-12-07 01:39:55 +01:00
### Show my score
2017-06-24 08:55:04 +02:00
2017-12-07 01:39:55 +01:00
To see the current score, we add an ``||input:on button pressed||`` that displays the score on the screen when the **A** button is pressed.
2017-06-24 08:55:04 +02:00
```block
input.onButtonPressed(Button.A, () => {
basic.showNumber(game.score())
})
```
2017-12-07 01:39:55 +01:00
### All together
2017-06-24 08:55:04 +02:00
2017-12-07 01:39:55 +01:00
The hunter code with all th pieces together looks like this now. Download and try it out with multiple beacons!
2017-06-24 08:55:04 +02:00
```blocks
let beacons: number[] = [0];
2018-10-16 00:32:09 +02:00
let signal = 0;
let serialNumber = 0;
radio.onReceivedNumber(function (receivedNumber) {
signal = radio.receivedPacket(RadioPacketProperty.SignalStrength)
serialNumber = radio.receivedPacket(RadioPacketProperty.SerialNumber)
2017-06-24 08:55:04 +02:00
led.stopAnimation();
if (signal < -95 ) {
basic.showIcon(IconNames.SmallDiamond)
} else if (signal < -80 ) {
basic.showIcon(IconNames.Diamond)
} else {
basic.showIcon(IconNames.Square)
2018-10-16 00:32:09 +02:00
if (signal > -50 & & beacons.indexOf(serialNumber) < 0 ) {
beacons.push(serialNumber)
2017-06-24 08:55:04 +02:00
game.addScore(1)
basic.showNumber(game.score())
}
}
})
input.onButtonPressed(Button.A, () => {
basic.showNumber(game.score())
})
radio.setGroup(1)
2017-06-25 17:28:21 +02:00
```
2017-08-18 17:34:01 +02:00
```package
radio
2018-11-20 22:26:26 +01:00
```