added various tutorials

This commit is contained in:
Peli de Halleux 2016-05-09 10:32:02 -07:00
parent 5fcf9165ea
commit 13f42f5892
10 changed files with 166 additions and 68 deletions

View File

@ -12,17 +12,14 @@ Answer the questions while completing the tutorial. Pay attention to the dialogu
## 1. Write the code that will store the global variable named 'action' and returns a random number between 0 and 2 ## 1. Write the code that will store the global variable named 'action' and returns a random number between 0 and 2
<br/> ```blocks
let action = Math.random(3)
```
action = Math.random(3)
``` ```
## 2. Write the code that will display the string, "PUSH A" if the global variable called 'action' is equal to 0 ## 2. Write the code that will display the string, "PUSH A" if the global variable called 'action' is equal to 0
<br /> ```blocks
let action = Math.random(3)
```
if (action == 0) { if (action == 0) {
basic.showString("PUSH A", 150) basic.showString("PUSH A", 150)
} }
@ -30,10 +27,9 @@ if (action == 0) {
## 3. Write the code that increments the score if button A is pressed when the global variable called 'action' is equal to 1 ## 3. Write the code that increments the score if button A is pressed when the global variable called 'action' is equal to 1
<br /> ```blocks
```
input.onButtonPressed(Button.A, () => { input.onButtonPressed(Button.A, () => {
let action = Math.random(3)
if (action == 0) { if (action == 0) {
game.addScore(1) game.addScore(1)
} }
@ -42,9 +38,8 @@ input.onButtonPressed(Button.A, () => {
## 4. Write the code that will display the string "LOGO DOWN" if the global variable called 'action' is equal to 1 ## 4. Write the code that will display the string "LOGO DOWN" if the global variable called 'action' is equal to 1
<br /> ```blocks
let action = Math.random(3)
```
if (action == 1) { if (action == 1) {
basic.showString("LOGO DOWN", 150) basic.showString("LOGO DOWN", 150)
} }
@ -52,10 +47,9 @@ if (action == 1) {
## 5. Write the code that increments the score if the BBC micro:bit logo is tilted down when the global variable called 'action' is equal to 1 ## 5. Write the code that increments the score if the BBC micro:bit logo is tilted down when the global variable called 'action' is equal to 1
<br /> ```blocks
```
input.onLogoDown(() => { input.onLogoDown(() => {
let action = Math.random(3)
if (action == 1) { if (action == 1) {
game.addScore(1) game.addScore(1)
} }
@ -64,9 +58,8 @@ input.onLogoDown(() => {
## 6. Write the code that will display the string "SHAKE" if the global variable called 'action' is equal to 2 ## 6. Write the code that will display the string "SHAKE" if the global variable called 'action' is equal to 2
<br /> ```blocks
let action = Math.random(3)
```
if (action == 2) { if (action == 2) {
basic.showString("SHAKE", 150) basic.showString("SHAKE", 150)
} }
@ -74,13 +67,11 @@ if (action == 2) {
## 7. Write the code that increments the score if the BBC micro:bit is shaken when the global variable called 'action' is equal to 2 ## 7. Write the code that increments the score if the BBC micro:bit is shaken when the global variable called 'action' is equal to 2
<br/> ```blocks
```
input.onLogoDown(() => { input.onLogoDown(() => {
let action = Math.random(3)
if (action == 1) { if (action == 1) {
game.addScore(1) game.addScore(1)
} }
}) })
``` ```

View File

@ -11,6 +11,7 @@ Variables
## Quick Links ## Quick Links
* [activity](/lessons/catch-the-egg-game/activity) * [activity](/lessons/catch-the-egg-game/activity)
* [tutorial](/lessons/catch-the-egg-game/tutorial)
* [quiz](/lessons/catch-the-egg-game/quiz) * [quiz](/lessons/catch-the-egg-game/quiz)
* [quiz answers](/lessons/catch-the-egg-game/quiz-answers) * [quiz answers](/lessons/catch-the-egg-game/quiz-answers)
@ -20,18 +21,21 @@ Learn how to create a catch the egg game game with **plot**, `led->plot` , **unp
## Documentation ## Documentation
* **variables** : [read more...](/reference/variables/var) ```cards
* **forever** : [read more...](/reference/basic/forever) let x = 2;
* **unplot** : [read more...](/reference/led/unplot) led.unplot(0, 0);
* **plot** : [read more...](/reference/led/plot) basic.forever(() => {});
* **if** : [read more...](/reference/logic/if) x += 1;
* **acceleration** : [read more...](/reference/input/acceleration) led.plot(0, 0);
* **math minimum number** : [read more...](/reference/math) basic.pause(300);
* **math maximum number** : [read more...](/reference/math) input.acceleration(Dimension.X);
* **math random number** : [read more...](/reference/math) Math.min(0,0);
* **math modulus** : [read more...](/reference/math) Math.max(0,1);
* **show number** : [read more...](/reference/basic/show-number) Math.random(5);
* **pause** : [read more...](/reference/basic/pause) game.addScore(1);
game.score();
game.removeLife(1);
```
## Objectives ## Objectives

View File

@ -1,7 +1,5 @@
# catch the egg game challenges # catch the egg game challenges
Coding challenges for catch the egg game.
## Before we get started ## Before we get started
Your starting code should look like this: Your starting code should look like this:

View File

@ -0,0 +1,29 @@
# catch the egg game tutorial
### Rebuild the game!
The blocks have been shuffled! Put them back together so that...
* an egg LED falls from the top of the screen, row by row.
* a basket LED is on the bottom row and can be moved by using the accelerometer `X` data.
* if the egg LED reaches the last row, reset the egg position to the first row.
```shuffle
let basketX = 2
let eggX = 2
let eggY = 0
basic.forever(() => {
led.unplot(basketX, 4)
led.unplot(eggX, eggY)
eggY = eggY + 1
led.plot(eggX, eggY)
basic.pause(300)
let accX = input.acceleration(Dimension.X)
basketX = 2 + Math.min(2, Math.max(-2, accX / 200))
led.plot(basketX, 4)
if (eggY > 4) {
eggY = -1
eggX = Math.random(5)
}
basic.pause(300)
})
```

View File

@ -9,6 +9,7 @@ Acceleration
## Quick Links ## Quick Links
* [activity](/lessons/glowing-pendulum/activity) * [activity](/lessons/glowing-pendulum/activity)
* [tutorial](/lessons/glowing-pendulum/tutorial)
* [challenges](/lessons/glowing-pendulum/challenges) * [challenges](/lessons/glowing-pendulum/challenges)
* [quiz](/lessons/glowing-pendulum/quiz) * [quiz](/lessons/glowing-pendulum/quiz)
* [quiz answers](/lessons/glowing-pendulum/quiz-answers) * [quiz answers](/lessons/glowing-pendulum/quiz-answers)

View File

@ -4,10 +4,29 @@ Construct a pendulum that glows using acceleration.
Welcome! This activity will teach how to construct a pendulum that glows using acceleration. Let's get started! Welcome! This activity will teach how to construct a pendulum that glows using acceleration. Let's get started!
Turn on all the LEDs.
```blocks
basic.showLeds(`
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
`)
```
Create a **forever** loop that will constantly display the appropriate brightness on the LED display. Create a **forever** loop that will constantly display the appropriate brightness on the LED display.
```blocks ```blocks
basic.showLeds(`
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
`)
basic.forever(() => { basic.forever(() => {
}) })
@ -16,6 +35,13 @@ basic.forever(() => {
Now let's measure the acceleration on the `y` axis and store that value in a variable. The `acceleration(y)` function will provide the value. Now let's measure the acceleration on the `y` axis and store that value in a variable. The `acceleration(y)` function will provide the value.
```blocks ```blocks
basic.showLeds(`
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
`)
basic.forever(() => { basic.forever(() => {
let acceleration = input.acceleration(Dimension.Y); let acceleration = input.acceleration(Dimension.Y);
}); });
@ -25,9 +51,15 @@ Since the micro:bit will be swinging back and forth, the acceleration will only
```blocks ```blocks
let acceleration = 0; basic.showLeds(`
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
`)
basic.forever(() => { basic.forever(() => {
acceleration = input.acceleration(Dimension.Y); let acceleration = input.acceleration(Dimension.Y);
acceleration = Math.abs(acceleration) acceleration = Math.abs(acceleration)
}); });
``` ```
@ -35,6 +67,13 @@ basic.forever(() => {
The function `acceleration(y)` returns a number between 0 and 1024. We want to use this value for the brightness of the micro:bit, but the `set brightness()` only accepts a value between 0 and 256. Thus, we need to divide the acceleration by 4 to ensure we will be in the appropriate range. The function `acceleration(y)` returns a number between 0 and 1024. We want to use this value for the brightness of the micro:bit, but the `set brightness()` only accepts a value between 0 and 256. Thus, we need to divide the acceleration by 4 to ensure we will be in the appropriate range.
```blocks ```blocks
basic.showLeds(`
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
`)
basic.forever(() => { basic.forever(() => {
let acceleration = input.acceleration(Dimension.Y); let acceleration = input.acceleration(Dimension.Y);
acceleration = Math.abs(acceleration); acceleration = Math.abs(acceleration);
@ -46,27 +85,6 @@ basic.forever(() => {
Now let's use our acceleration value to set the brightness on the micro:bit. Now let's use our acceleration value to set the brightness on the micro:bit.
```blocks ```blocks
basic.forever(() => {
let acceleration = input.acceleration(Dimension.Y);
acceleration = Math.abs(acceleration);
acceleration = acceleration / 4;
led.setBrightness(acceleration)
});
```
Let's show what the brightness of the micro:bit is by turning all the LEDs on!
```blocks
basic.forever(() => {
let acceleration = input.acceleration(Dimension.Y);
acceleration = Math.abs(acceleration);
acceleration = acceleration / 4;
led.setBrightness(acceleration)
basic.showLeds(` basic.showLeds(`
# # # # # # # # # #
# # # # # # # # # #
@ -74,9 +92,12 @@ basic.forever(() => {
# # # # # # # # # #
# # # # # # # # # #
`) `)
basic.forever(() => {
let acceleration = input.acceleration(Dimension.Y);
acceleration = Math.abs(acceleration);
acceleration = acceleration / 4;
led.setBrightness(acceleration)
}); });
``` ```
### ~avatar avatar ### ~avatar avatar

View File

@ -0,0 +1,29 @@
# glowing pendulum block tutorial
The glowing pendulum changes the screen brightness based on the acceleration measured on the BBC micro:bit.
### Rebuild the game!
The blocks have been shuffled! Put them back together so that...
* all LEDs are turned on
* the BBC micro:bit repeats code **forever** that
* reads the acceleration along the ``y`` axis,
* calculate the absolute value of the acceleration
* scales down the acceleration value by a factor of `4`
* uses the scaled value to set the screen **brightness**
```blocks
basic.showLeds(`
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
`)
basic.forever(() => {
let acceleration = input.acceleration(Dimension.Y);
acceleration = Math.abs(acceleration);
acceleration = acceleration / 4;
led.setBrightness(acceleration)
});
```

View File

@ -11,6 +11,7 @@ Math - Pick Random
## Quick links ## Quick links
* [activity](/lessons/guess-the-number/activity) * [activity](/lessons/guess-the-number/activity)
* [tutorial](/lessons/guess-the-number/tutorial)
* [challenges](/lessons/guess-the-number/challenges) * [challenges](/lessons/guess-the-number/challenges)
* [quiz](/lessons/guess-the-number/quiz) * [quiz](/lessons/guess-the-number/quiz)
* [quiz answers](/lessons/guess-the-number/quiz-answers) * [quiz answers](/lessons/guess-the-number/quiz-answers)

View File

@ -19,12 +19,11 @@ input.onButtonPressed(Button.A, () => {
``` ```
Create a local variable of type number `x` and set it to a random number using `pick random`. `pick random` 9 generates a random number between `0` and `09`. Create a local variable of type number `x` and set it to a random number using `pick random`. `pick random` 9 generates a random number between `0` and `9`.
```blocks ```blocks
input.onButtonPressed(Button.A, () => { input.onButtonPressed(Button.A, () => {
let x = Math.random(9) let x = Math.random(10)
}) })
``` ```
@ -34,7 +33,7 @@ Show the random number on the screen.
```blocks ```blocks
input.onButtonPressed(Button.A, () => { input.onButtonPressed(Button.A, () => {
let x = Math.random(9) let x = Math.random(10)
basic.showNumber(x) basic.showNumber(x)
}) })

View File

@ -0,0 +1,25 @@
# guess the number tutorial
### ~avatar avatar
### @video td/videos/guess-the-number-0
This tutorial will help you create a guess the number game! Let's get started!
### ~
### Rebuild the game!
The blocks have been shuffled! Put them back together so that...
* when the user presses button ``A``,
* generate a random number
* show the number on screen
```shuffle
input.onButtonPressed(Button.A, () => {
let x = Math.random(10)
basic.showNumber(x)
})
```