Adds and edits to redlight greenlight (#1440)
* Adds and edits to redlight greenlight * Add block category for - forever
This commit is contained in:
parent
36718093c6
commit
0fd5570dea
@ -1,20 +1,20 @@
|
||||
# Red Light Green Light
|
||||
|
||||
This is a classic game where the game master is looking the opposite direction and saying commands to player.
|
||||
When the game master says "red light" and turns around, all players should stop. If the game master stops a moving player, he has to go back to the start.
|
||||
When the game master turns around and says "green light", all players move and try to touch the game master.
|
||||
This is the classic "Red Light, Green Light" game where one person is a virtual stoplight and gives commands to the other players to either stop or go.
|
||||
|
||||
In this remake, we will use @boardname@, the radio and the accelerometer to enforce those rules!
|
||||
## Rules of play
|
||||
|
||||
## The game master
|
||||
The player chosen as the current the stoplight says "Green Light!" and turns away from the other players. The other players move toward the stoplight player, from a distance set at the beginning of the game, and try to touch them. The stoplight player can at any time say "Red light!" and then turn around to face the other players. If the stop light player sees anyone still moving, they call them out and they are finished playing until a new game is started. The stoplight player repeats the red light, green light cycle. If one of the other players happens to touch the stop light player before they can turn around when saying "Red Light!", then the current stoplight player moves to the beginning of the course and the other player becomes the stoplight. The game continues until only the the stoplight player remains.
|
||||
|
||||
Let's start with the code running on the game master @boardname@. Don't use this code for players!
|
||||
In this remake of the game, we will use a @boardname@, its radio, and the accelerometer to enforce these rules!
|
||||
|
||||
## Creating the stoplight
|
||||
|
||||
Let's start with the code running on the stoplight's @boardname@. Don't use this code for the other players!
|
||||
|
||||
### States
|
||||
|
||||
We define two states, ``GREENLIGHT`` and ``REDLIGHT`` and a ``state`` variable to store the current game state.
|
||||
When the game master presses ``A``, the game goes into "green light" mode. When he presses ``B``,
|
||||
the state goes in "red light" mode.
|
||||
We define two _states_, or game conditions, called ``GREENLIGHT`` and ``REDLIGHT``. A variable named ``state`` will store the current game state. When the stoplight player presses ``A``, the game goes into "green light" mode. When they press ``B``, the state goes into "red light" mode.
|
||||
|
||||
```blocks
|
||||
let REDLIGHT = 0
|
||||
@ -26,7 +26,7 @@ REDLIGHT = 2
|
||||
|
||||
### Communication
|
||||
|
||||
The radio group is set to ``1``. We will set the same group in the player code. The game state is streamed in a forever loop so that players receive constantly.
|
||||
The radio group for all game players is set to ``1``. We will set the same group in the player's code too. The game state is streamed in a ``||basic:forever||`` loop so that players continuously receive it.
|
||||
|
||||
```blocks
|
||||
let state = 0;
|
||||
@ -38,9 +38,9 @@ basic.forever(function () {
|
||||
|
||||
### Red light, green light
|
||||
|
||||
Use the ``||radio:on button pressed`` block to run code when button ``A`` and ``B`` are pressed.
|
||||
Use the ``||radio:on button pressed||`` block to run code when button ``A`` and ``B`` are pressed.
|
||||
When ``A`` is pressed, the game goes into ``GREENLIGHT`` mode. When ``B`` is pressed, the game
|
||||
goes into ``B`` mode. We also use ``||basic:show icon||`` to display the current state.
|
||||
goes into ``REDLIGHT`` mode. We also use ``||basic:show icon||`` to display the current game state.
|
||||
|
||||
```blocks
|
||||
let REDLIGHT = 0
|
||||
@ -58,9 +58,9 @@ GREENLIGHT = 1
|
||||
REDLIGHT = 2
|
||||
```
|
||||
|
||||
### Game master code
|
||||
### Stoplight code
|
||||
|
||||
All together the game master code looks like this:
|
||||
All together the stoplight code looks like this:
|
||||
|
||||
```blocks
|
||||
let REDLIGHT = 0
|
||||
@ -84,36 +84,36 @@ basic.forever(function () {
|
||||
|
||||
### ~ hint
|
||||
|
||||
Make sure to rename this program to ``game master`` or something so that you don't confuse it with the player program!
|
||||
Make sure to rename this program to ``stoplight`` or something similar so that you don't confuse it with the player program!
|
||||
|
||||
### ~
|
||||
|
||||
### Improve the game
|
||||
|
||||
* Use ``||music:ring tone||`` to play a sound while the game is in ``GREENLIGHT`` mode.
|
||||
* Attach a servo and move the arm based on the game state
|
||||
* Attach a servo and move the arm based on the game state.
|
||||
|
||||
## The players
|
||||
|
||||
The code for players needs to listen for the game master state.
|
||||
The code for the other players needs to listen for the stoplight's state.
|
||||
|
||||
### States
|
||||
|
||||
First, we redefine the state constant, ``GREENLIGHT``, ``REDLIGHT`` and set the radio to ``1``.
|
||||
First, we again define the state constants ``GREENLIGHT``, ``REDLIGHT``, and set the radio group to ``1``.
|
||||
We also add a ``state`` variable that will store the state of the game.
|
||||
|
||||
```blocks
|
||||
let RED = 0
|
||||
let REDLIGHT = 0
|
||||
let state = 0
|
||||
let GREEN = 0
|
||||
GREEN = 1
|
||||
RED = 2
|
||||
let GREENLIGHT = 0
|
||||
GREENLIGHT = 1
|
||||
REDLIGHT = 2
|
||||
radio.setGroup(1)
|
||||
```
|
||||
|
||||
### Communication
|
||||
|
||||
We use the ``radio:on received number`` block to store the game master state into the ``state`` variable.
|
||||
We use the ``||radio:on received number||`` block to store the stoplight state into the ``state`` variable.
|
||||
|
||||
```blocks
|
||||
let state = 0
|
||||
@ -124,7 +124,7 @@ radio.onReceivedNumber(function (receivedNumber) {
|
||||
|
||||
### Display
|
||||
|
||||
In a forever loop, we display different icons based on the game state. Use a ``||logic:if||`` and
|
||||
In a ``||basic:forever||`` loop, we display different icons based on the game state. Use a ``||logic:if||`` and
|
||||
``||basic:show icon||`` blocks to display the game state.
|
||||
|
||||
```blocks
|
||||
@ -144,13 +144,15 @@ basic.forever(function () {
|
||||
|
||||
If the ``state`` is equal to ``REDLIGHT``, we need to check that the player is not moving.
|
||||
This is where the accelerometer comes into play. The accelerometer measures forces applied to the @boardname@.
|
||||
If the player moves, it is likely that some small forces will be applied and the accelerometer will detect it.
|
||||
At all times, gravity is applied to the @boardname@, so the acceleration strength at rest always near ``1000``mg.
|
||||
If the acceleration strength is far from that value, say ``1100`` or ``900``, we can assume that the player is moving. To compute this we use this formula:
|
||||
If the player moves, it's likely that the accelerometer will detect any small forces applied to the @boardname@.
|
||||
At all times, gravity is applied to the @boardname@, so the acceleration strength at rest is always near ``1000`` mg.
|
||||
If the acceleration strength is far from that value, say ``1100`` or ``900``, we can assume that the player is moving. To compute this we use the formula:
|
||||
|
||||
moving = | acc strength - 1000 | > 1000
|
||||
```
|
||||
moving = | acc strength - 1000 | > 1000
|
||||
```
|
||||
|
||||
Now that we know the math, we can turn this into code.
|
||||
Now that we know the math for it, we can turn this into code.
|
||||
|
||||
```block
|
||||
let REDLIGHT = 0
|
||||
@ -167,7 +169,7 @@ if (state == REDLIGHT) {
|
||||
|
||||
### Player code
|
||||
|
||||
All together:
|
||||
All together, the code for the players is:
|
||||
|
||||
```blocks
|
||||
let moving = false
|
||||
@ -197,16 +199,16 @@ basic.forever(function () {
|
||||
|
||||
### ~ hint
|
||||
|
||||
Make sure to rename this program to ``player`` or something so that you don't confuse it with the game master program!
|
||||
Make sure to rename this program to ``player`` or something like that so you don't confuse it with the stoplight program!
|
||||
|
||||
### ~
|
||||
|
||||
|
||||
### Tuning
|
||||
|
||||
Does the movement check work? Try changing the ``100`` value to tune the detection sensivity. Try ``64``.
|
||||
Does the movement check work? Try changing the ``100`` value to tune the detection sensivity. Try ``64`` maybe.
|
||||
|
||||
### Improve the game
|
||||
|
||||
* Use ``||music:ring tone||`` to play a tone while in green mode.
|
||||
* Use the packet signal strength to detect that you've reached the game master.
|
||||
* Use the packet signal strength to detect that you've reached the stoplight.
|
||||
|
Loading…
Reference in New Issue
Block a user