2017-06-24 08:55:04 +02:00
# Hot Or Cold
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
In this game, players are looking for hidden @boardname @ that emit radio signals.
The hidden @boardname @ are called **beacons** .
2017-09-07 22:42:08 +02:00
## Setting up the radio
2017-06-24 08:55:04 +02:00
We set the radio group to ``1`` to make sure all the players are using the same group.
We also tell the @boardname @ to transmit its serial number (that's a unique number that identifies it)
so that the player can tell apart each beacon. We also reduce the power of the antenna to reduce the range of transmission.
```block
radio.setGroup(1)
radio.setTransmitSerialNumber(true)
radio.setTransmitPower(6)
```
2017-09-07 22:42:08 +02:00
## Beacon gotta beam
2017-06-24 08:55:04 +02:00
The beacon simply needs to send a radio message every now and then. To pace it out,
we add some ``|show icon|`` blocks to animate the screen.
```blocks
basic.forever(() => {
radio.sendNumber(0)
basic.showIcon(IconNames.Heart)
basic.showIcon(IconNames.SmallHeart)
})
radio.setGroup(1)
radio.setTransmitSerialNumber(true)
radio.setTransmitPower(6)
```
2017-09-07 22:42:08 +02:00
## Hide the beacons
2017-06-24 08:55:04 +02:00
Download the code to your beacon @boardname @ and hide them!
## Hunters
The hunter @boardname @ look for beacons.
2017-09-07 22:42:08 +02:00
## Is the beacon close?
2017-06-24 08:55:04 +02:00
To determine how far or close, we use the signal strength of each radio packet sent by the beacons. The signal
strength ranges from ``-128db`` (weak) to ``-42db`` (very strong).
```blocks
radio.onDataPacketReceived( ({ receivedNumber, signal }) => {
basic.showNumber(signal)
});
radio.setGroup(1)
```
Take notes of the values as you move around the beacon.
* hot signal value: ``_________________``
* mild signal value: ``_________________``
* cold signal value: ``_________________``
2017-09-07 22:42:08 +02:00
## Hot or cold?
2017-06-24 08:55:04 +02:00
The hunter screen displays ``SmallDiamond`` on the screen if the beacon is far, ``Diamond`` mildly close and ``Square`` if it is close. Use the ``signal`` values collected in the previous step to determine when to show those letters.
Here is an example that uses ``-95`` and less for cold, between ``-95`` and ``-80`` for mild and above ``-80`` for hot. Use your own values based on the room setup or the hidding place.
To make the program more responsive, we add a ``|led stop animation|`` to cancel any icon animation when a new beacon packet comes.
```blocks
radio.onDataPacketReceived( ({ receivedNumber, signal }) => {
led.stopAnimation();
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!
## Extra: Multiple beacons
We're making the game more interresting by counting how many beacons a player has seen so far.
2017-09-07 22:42:08 +02:00
## Remember the beacons
2017-06-24 08:55:04 +02:00
Remember that the beacon was configured to transmit its serial number? We can use this information
to determine if we've visited a beacon for the first time.
To do so, we are going to add an **array** variable that will hold all the beacon serial numbers seen so far.
```block
let beacons: number[] = [0]
```
Whenever we receive a new packet, we are going to check if the ``beacons`` already
contains the serial number. If not, we add the serial number at the end of ``beacons`` and increment the ``|game score|``.
To check if an ``array`` contains an element, we use the ``|find index of|`` block which returns ``-1``
if the value is not found.
```blocks
let beacons: number[] = [0]
radio.onDataPacketReceived( ({ receivedNumber, signal, serial }) => {
if (signal > -50 & & beacons.indexOf(serial) < 0 ) {
beacons.push(serial)
game.addScore(1)
basic.showNumber(game.score())
}
})
```
2017-09-07 22:42:08 +02:00
## Show my score
2017-06-24 08:55:04 +02:00
To see the current score, we add a ``|on button pressed|`` that displays the score on screen when ``A`` is pressed.
```block
input.onButtonPressed(Button.A, () => {
basic.showNumber(game.score())
})
```
2017-09-07 22:42:08 +02:00
## All together
2017-06-24 08:55:04 +02:00
The hunter code with all th pieces together looks like this now. Download it and try it out with multiple beacons!
```blocks
let beacons: number[] = [0];
radio.onDataPacketReceived( ({ receivedNumber, signal, serial }) => {
led.stopAnimation();
if (signal < -95 ) {
basic.showIcon(IconNames.SmallDiamond)
} else if (signal < -80 ) {
basic.showIcon(IconNames.Diamond)
} else {
basic.showIcon(IconNames.Square)
if (signal > -50 & & beacons.indexOf(serial) < 0 ) {
beacons.push(serial)
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
```