fireflew activity (#410)

This commit is contained in:
Peli de Halleux 2017-06-04 10:42:52 -07:00 committed by GitHub
parent 699697301e
commit 59c5e2ea9a
4 changed files with 117 additions and 1 deletions

View File

@ -31,6 +31,10 @@ Fun games to build with your @boardname@.
"name": "Coin Flipper",
"url":"/projects/coin-flipper",
"imageUrl": "/static/mb/projects/coin-flipper.png"
}, {
"name": "Fireflies",
"url": "/projects/fireflies",
"imageUrl": "/static/mb/projects/fireflies.png"
}, {
"name": "Infection",
"url": "/projects/infection",
@ -117,4 +121,5 @@ Fun games to build with your @boardname@.
### See Also
[Flashing Heart](/projects/flashing-heart), [Smiley Buttons](/projects/smiley-buttons), [Love Meter](/projects/love-meter), [Rock Paper Scissors](/projects/rock-paper-scissors), [Compass](/projects/compass), [Hack your headphones](/projects/hack-your-headphones), [Banana keyboard](/projects/banana-keyboard), [Telegraph](/projects/telegraph), [Guitar](/projects/guitar), [Wallet](/projects/wallet), [Watch](/projects/watch),
[Milk Monster](/projects/milky-monster), [Karel the LED](/projects/karel)
[Milk Monster](/projects/milky-monster), [Karel the LED](/projects/karel), [Infection](/projects/infection),
[Fireflies](/projects/fireflies)

109
docs/projects/fireflies.md Normal file
View File

@ -0,0 +1,109 @@
# Fireflies
### ~ avatar
Turn your @boardname@ into fireflies.
### ~
https://youtu.be/ZGvtnE1Wy6U
## How do Fireflies synchronise?
Go to http://ncase.me/fireflies/ and read about the fireflies synchronization phenomenon.
## Code
The goal of this project is to create virtual fireflies using @boardname@ that mimic the real fireflies.
After reading the article, there are a few sentences that stand out to help.
### "Each firefly has its own individual internal clock"
A clock in this case is like a counter, so we will start by adding a ``clock`` variable to our program.
```block
// the clock ticker
let clock = 1
```
### "and every time the clock “strikes twelve”, it flashes."
So we need a [forever](/reference/basic/forever) loop to increment the clock.
When the clock reaches "noon" (let's pick 8), we turn on the screen breifly.
```block
// the clock ticker
let clock = 0
basic.forever(() => {
// if clock "hits noon", flash the screen
if (clock >= 8) {
// flash
game.addScore(1)
// wait for 2 ticks + a tiny bit
basic.pause(220)
// reset the clock
clock = 0
} else {
// just wait a bit
basic.pause(100)
// increment the clock
clock += 1
}
})
```
### Step 1: when you see a nearby firefly flash, nudge your clock a little bit forward.
We will use radio messages to simulate the vision of fireflies.
* When a firefly flashes, it also sends a number over radio using [radio send number](/reference/radio/send-number).
* When a firefly receives a radio packet, it increments its clock by one just like the fireflies.
```block
// the clock ticker
let clock = 0
radio.onDataPacketReceived(() => {
// advance clock to catch up neighbors
clock += 1
})
```
### Putting all together
https://youtu.be/XzZeB4yYnEw
Now that we have all the parts needed to build our firefly simulator, we can assemble it into a program.
We've add a [radio set group](/reference/radio/set-group) block to specify which group the firefly will communicate on. Download this program on as many @boardname@ as you can find and try it out in a dark room!
```blocks
// the clock ticker
let clock = 0
radio.onDataPacketReceived(() => {
// advance clock to catch up neighbors
clock += 1
})
basic.forever(() => {
// if clock hits noon, flash the screen
if (clock >= 8) {
// notify neighbors
radio.sendNumber(clock)
// flash
game.addScore(1)
// wait for 2 ticks + a tiny bit
basic.pause(220)
// reset the clock
clock = 0
} else {
// just wait a bit
basic.pause(100)
// increment the clock
clock += 1
}
})
// short range radio transmission
radio.setTransmitPower(1)
radio.setGroup(12)
```

View File

@ -9,6 +9,8 @@ There is a disease outbreak! Will you find patient zero?!?
**Infection** is a distributed game which simulates
the propagation of an illness. **The goal is to stop the outbreak before every player dies!**
* **Number of players:** 1 Master, 4 or more players. Each Master and players need a @boardname@ with battery pack.
In this game, a master @boardname@ infects a "patient zero" player
with the sickness. The infected player will be contagious immediately but won't show any sign
during the incubation time. The sickness gets transmitted when two @boardname@ get close to each other.

BIN
docs/static/mb/projects/fireflies.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB