Migrate docs from the other repo
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
# Clear Screen
|
||||
|
||||
Turn off all the LED lights on the [LED screen](/microbit/device/screen).
|
||||
|
||||
```sig
|
||||
basic.clearScreen()
|
||||
```
|
||||
|
||||
### Example: vanishing heart
|
||||
|
||||
The following code displays a heart on the screen and then turns off all the LED lights using `clear screen`:
|
||||
|
||||
```blocks
|
||||
basic.showLeds(`
|
||||
. # . # .
|
||||
# # # # #
|
||||
# # # # #
|
||||
. # # # .
|
||||
. . # . .
|
||||
`)
|
||||
basic.clearScreen()
|
||||
```
|
||||
|
||||
### Lessons
|
||||
|
||||
[blink](/microbit/lessons/blink), [flashing heart](/microbit/lessons/flashing-heart), [screen wipe](/microbit/lessons/screen-wipe)
|
||||
|
||||
### See also
|
||||
|
||||
[set brightness](/microbit/reference/led/set-brightness), [unplot](/microbit/reference/led/unplot), [plot](/microbit/reference/led/plot), [Image](/microbit/reference/image/image), [clear](/microbit/reference/basic/clear-screen)
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
# Forever
|
||||
|
||||
Repeat code [in the background](/microbit/reference/control/in-background) forever.
|
||||
|
||||
```sig
|
||||
basic.forever(() => {
|
||||
})
|
||||
```
|
||||
|
||||
### Example: compass
|
||||
|
||||
The following example constantly checks the [compass heading](/microbit/reference/input/compass-heading) and updates the screen with the direction.
|
||||
|
||||
```blocks
|
||||
basic.forever(() => {
|
||||
let heading = input.compassHeading()
|
||||
if (heading < 45) {
|
||||
basic.showString("N", 100)
|
||||
} else if (heading < 135) {
|
||||
basic.showString("E", 100)
|
||||
}
|
||||
else if (heading < 225) {
|
||||
basic.showString("S", 100)
|
||||
}
|
||||
else {
|
||||
basic.showString("W", 100)
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### Example: counter
|
||||
|
||||
The following example continually shows the current value of a global variable:
|
||||
|
||||
```blocks
|
||||
let num = 0
|
||||
basic.forever(() => {
|
||||
basic.showNumber(num, 150)
|
||||
})
|
||||
input.onButtonPressed("A", () => {
|
||||
num = num + 1
|
||||
})
|
||||
```
|
||||
|
||||
### Contention for the LED display
|
||||
|
||||
If you have multiple processes that each show something on the LED screen, you may get unexpected results. Try, for example:
|
||||
|
||||
```blocks
|
||||
basic.forever(() => {
|
||||
basic.showNumber(6789, 150)
|
||||
})
|
||||
input.onButtonPressed(Button.A, () => {
|
||||
basic.showNumber(2, 150)
|
||||
})
|
||||
```
|
||||
|
||||
### Lessons
|
||||
|
||||
[blink](/microbit/lessons/blink), [bounce-image](/microbit/lessons/bounce-image), [snowflake-fall](/microbit/lessons/snowflake-fall), [flashing-heart](/microbit/lessons/flashing-heart)
|
||||
|
||||
### See also
|
||||
|
||||
[while](/microbit/js/while), [on button pressed](/microbit/reference/input/on-button-pressed), [in background](/microbit/reference/control/in-background)
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
# Pause
|
||||
|
||||
Pause program execution for the specified number of milliseconds. This function is helpful when you need to slow down your program's execution.
|
||||
|
||||
```sig
|
||||
basic.pause(400)
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
* ``ms`` - the number of milliseconds that you want to pause (100 = 1/10 second, 1000 milliseconds = 1 second)
|
||||
|
||||
### Example: diagonal line
|
||||
|
||||
The following example code turns on LED `0, 0` thru `4, 4`, pausing 500 milliseconds after each LED. Without `pause`, the code would run so fast that you wouldn't see each individual LED turning on.
|
||||
|
||||
```blocks
|
||||
for (let i = 0; i < 5; i++) {
|
||||
led.plot(i, i)
|
||||
basic.pause(500)
|
||||
}
|
||||
```
|
||||
|
||||
### Lessons
|
||||
|
||||
[blink](/microbit/lessons/blink), [lucky 7](/microbit/lessons/lucky-7), [smiley](/microbit/lessons/smiley), [flashing heart](/microbit/lessons/flashing-heart)
|
||||
|
||||
### See also
|
||||
|
||||
[while](/microbit/js/while), [running time](/microbit/reference/input/running-time), [for](/microbit/reference/loops/for)
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
# Show Animation
|
||||
|
||||
Show a series of image frames on the [LED screen](/microbit/device/screen), pausing the specified time after each frame.
|
||||
|
||||
```sig
|
||||
basic.showAnimation(`
|
||||
. . # . . . # # # . . # # # .
|
||||
. # # . . . . . # . . . . # .
|
||||
. . # . . . . # . . . # # # .
|
||||
. . # . . . # . . . . . . # .
|
||||
. . # . . . # # # . . # # # .
|
||||
`)
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
* `leds` - [String](/microbit/reference/types/string); a series of LED on/off states
|
||||
* `interval` - [Number](/microbit/reference/types/number); the number of milliseconds to pause after each image frame
|
||||
|
||||
### Show a series of image frames
|
||||
|
||||
```
|
||||
basic.showAnimation(`
|
||||
. . # . . . # # # . . # # # .
|
||||
. # # . . . . . # . . . . # .
|
||||
. . # . . . . # . . . # # # .
|
||||
. . # . . . # . . . . . . # .
|
||||
. . # . . . # # # . . # # # .
|
||||
`)
|
||||
```
|
||||
|
||||
### ~hint
|
||||
|
||||
If the series of images appear too fast, increase the value of the *interval* parameter.
|
||||
|
||||
### ~
|
||||
|
||||
### Example: animating frames
|
||||
|
||||
The following example creates an image with six frames and then shows each frame o the screen, pausing 500 milliseconds after each frame:
|
||||
|
||||
```
|
||||
basic.showAnimation(`
|
||||
. . . . . # . . . . . . . . . . . . . # . . . . . # . . . .
|
||||
. . # . . . . . . . . . # . . . . . . . . . # . . . . . . .
|
||||
. # . # . . . # . . . # . # . . . # . . . # . # . . . # . .
|
||||
. . # . . . . . . . . . # . . . . . . . . . # . . . . . . .
|
||||
. . . . . . . . . # . . . . . # . . . . . . . . . . . . . #
|
||||
`, 500)
|
||||
```
|
||||
|
||||
### ~hint
|
||||
|
||||
Use [forever](/microbit/reference/basic/forever) to continually repeat an animation
|
||||
|
||||
### ~
|
||||
|
||||
### Lessons
|
||||
|
||||
[smiley](/microbit/lessons/smiley), [bounce image](/microbit/lessons/bounce-image), [snowflake fall](/microbit/lessons/snowflake-fall), [rotation animation](/microbit/lessons/rotation-animation)
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
# Show LEDs
|
||||
|
||||
Display an image on the BBC micro:bit's [LED screen](/microbit/device/screen).
|
||||
|
||||
```sig
|
||||
basic.showLeds(`
|
||||
. . . . .
|
||||
. # . # .
|
||||
. . # . .
|
||||
# . . . #
|
||||
. # # # .
|
||||
`
|
||||
)
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
* ``leds`` - a series of LED on/off states that form an image (see steps below)
|
||||
* (optional) ``ms`` - [Number](/microbit/reference/types/number) - time to wait after displaying image. In blocks, ``ms`` is 400 by default.
|
||||
|
||||
### Example - Block Editor
|
||||
|
||||
1. Open the `basic` category and select the `show leds` blocks.
|
||||
|
||||
```blocks
|
||||
basic.showLeds(`
|
||||
# # . # #
|
||||
# # . # #
|
||||
. # # # .
|
||||
. # . # .
|
||||
. # . # .
|
||||
`
|
||||
)
|
||||
```
|
||||
|
||||
In JavaScript, the led off is represented by a `.` and the led on by a `#` character.
|
||||
|
||||
### Lessons
|
||||
|
||||
[smiley](/microbit/lessons/smiley), [flashing heart](/microbit/lessons/flashing-heart), [magic logo](/microbit/lessons/magic-logo)
|
||||
|
||||
### See also
|
||||
|
||||
[plot leds](/microbit/reference/led/plot-leds), [show animation](/microbit/reference/led/show-animation)
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
# Show Number
|
||||
|
||||
Show a number on the [LED screen](/microbit/device/screen), one digit at a time (scrolling from left to right)
|
||||
|
||||
~~~~sig
|
||||
basic.showNumber(2, 150)
|
||||
~~~~
|
||||
|
||||
### Parameters
|
||||
|
||||
* value - a [Number](/microbit/reference/types/number)
|
||||
* (optional) interval (ms) - [Number](/microbit/reference/types/number); the time (in milliseconds) before scrolling by one LED; the larger the number, the slower the scroll
|
||||
|
||||
### ~
|
||||
|
||||
To display the number 10:
|
||||
|
||||
~~~~blocks
|
||||
basic.showNumber(10)
|
||||
~~~~
|
||||
|
||||
To display the number stored in the `x` variable:
|
||||
|
||||
~~~~blocks
|
||||
let x = 1
|
||||
basic.showNumber(x)
|
||||
~~~~
|
||||
|
||||
### Example: count to 5
|
||||
|
||||
This example uses a [for](/microbit/reference/loops/for) loop to show numbers ``1`` through ``5`` on the screen:
|
||||
|
||||
~~~~blocks
|
||||
for (let i = 0; i < 5; i++) {
|
||||
basic.showNumber(i + 1)
|
||||
basic.pause(200)
|
||||
}
|
||||
~~~~
|
||||
|
||||
### Other show functions
|
||||
|
||||
* use [show string](/microbit/reference/basic/show-string) to show a string on the screen
|
||||
* use [show animation](/microbit/reference/basic/show-animation) to show a series of images on the screen
|
||||
|
||||
### Lessons
|
||||
|
||||
* [lucky 7](/microbit/lessons/lucky-7)
|
||||
|
||||
### See also
|
||||
|
||||
[show string](/microbit/reference/basic/show-string), [show animation](/microbit/reference/basic/show-animation), [Number](/microbit/reference/types/number), [math library](/microbit/js/math)
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
# Show String
|
||||
|
||||
Show a string on the [LED screen](/microbit/device/screen) one character at a time (scrolling from left to right).
|
||||
|
||||
```sig
|
||||
basic.showString("Hello!")
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
* `text` - a [String](/microbit/reference/types/string)
|
||||
* (optional) `ms` - [Number](/microbit/reference/types/number); the time (in milliseconds) before scrolling left by one LED; the larger the number, the slower the scroll
|
||||
|
||||
### Examples:
|
||||
|
||||
To display Hello:
|
||||
|
||||
```blocks
|
||||
basic.showString("Hello")
|
||||
```
|
||||
|
||||
To display the content of a string variable:
|
||||
|
||||
```blocks
|
||||
let s = "Hi"
|
||||
basic.showString(s)
|
||||
```
|
||||
|
||||
### Other show functions
|
||||
|
||||
* use [show number](/microbit/reference/basic/show-number) to show a number on the screen
|
||||
* use [show animation](/microbit/reference/basic/show-animation) to show a series of images on the screen
|
||||
|
||||
### Lessons
|
||||
|
||||
[answering machine](/microbit/lessons/answering-machine), [rock paper scissors](/microbit/lessons/rock-paper-scissors), [love meter](/microbit/lessons/love-meter), [digital pet](/microbit/lessons/digital-pet)
|
||||
|
||||
### See also
|
||||
|
||||
[String](/microbit/reference/types/string), [string functions](/microbit/reference/types/string-functions), [show number](/microbit/reference/basic/show-number), [show animation](/microbit/reference/basic/show-animation)
|
||||
|
||||
@@ -0,0 +1,141 @@
|
||||
# From Block Editor to Touch Develop
|
||||
|
||||
#docs
|
||||
|
||||
The Block Editor and Touch Develop programming languages provide similar features, but are not identical in their functionality. This presents a learning opportunity for teachers and students: to understand a few basic concepts and how they are expressed in different programming languages. The objective is to make students better able to navigate the sea of programming languages they will encounter later.
|
||||
|
||||
## Concept 1: Inclusive and exclusive intervals
|
||||
|
||||
In mathematics, numeric intervals are a useful shorthand for expressing a sequence of numbers. For example, the notation [0,9] represents the sequence of ten numbers 0,1,2,3,4,5,6,7,8,9. This is known as an "inclusive" interval because the sequence includes the endpoints of the interval, namely 0 and 9. On the other hand, the interval (0,9) represents the sequence of eight numbers 1,2,3,4,5,6,7,8 and is known as "exclusive".
|
||||
|
||||
In the interval notation, the brackets "[" and "]" represent inclusive endpoints and the parentheses "(" and ")" represent exclusive endpoints. Brackets can be mixed and matched, so [0,9) represents the sequence of nine numbers 0,1,2,3,4,5,6,7,8 while (0,9] represents the sequence of nine numbers 1,2,3,4,5,6,7,8,9. Let's call the former interval "inclusive-exclusive" and the latter interval "exclusive-inclusive".
|
||||
|
||||
### Block Editor for loop uses a 0-based inclusive interval
|
||||
|
||||
Numeric intervals arise in the context of for loops, both in the Block Editor and Touch Develop. Here's a Block Editor for loop to draw a diagonal line from the top-left corner of the [LED screen](/microbit/device/screen) to the bottom-right corner. The loop iteration variable *i* ranges "from 0 to 4":
|
||||
|
||||

|
||||
|
||||
What interval does "from 0 to 4" represent? The answer is the inclusive interval [0,4], meaning that the loop iteration variable `i` will take on the values 0,1,2,3,4 over the *five* iterations of the loop. Experiments have shown that the *inclusive internal* is most familiar to students with no previous programming experience.
|
||||
|
||||
### TouchDevelop for loop uses a 0-based inclusive-exclusive interval
|
||||
|
||||
To achieve the same result in Touch Develop, we write the for loop slightly differently because the upper bound of the 0-based loop is *exclusive* rather than inclusive:
|
||||
|
||||
```
|
||||
for (let i = 0; i < 5; i++) {
|
||||
led.plot(i, i)
|
||||
}
|
||||
```
|
||||
|
||||
If we translated the Block Editor loop directly into Touch Develop, we would have:
|
||||
|
||||
```
|
||||
for (let i1 = 0; i1 < 4; i1++) {
|
||||
led.plot(i1, i1)
|
||||
}
|
||||
```
|
||||
|
||||
which would result in the loop iteration variable taking on values in the interval [0,4), namely 0,1,2,3.
|
||||
|
||||
### ~hint
|
||||
|
||||
The use of an exclusive upper-bound in for loops is standard practice in most programming languages. The basic reason for this is that with a 0-based inclusive lower bound, an exclusive upper bound U conveniently happens to be the length of the sequence represented by [0,U). This, of course, begs the question of why we count by starting with zero (0) in programming whereas we learn to count with one (1) in math.
|
||||
|
||||
### ~
|
||||
|
||||
## Concept 2: variable scope
|
||||
|
||||
A variable's *scope* is defined by two other concepts: its *lifetime* and *visibility*. Imagine program execution like a timeline with each point in the timeline being a step in the program's execution.
|
||||
|
||||
* A variable's *lifetime* can be thought of as an interval [birth, death) in the program execution during which the variable exists and has a value. Within that interval of the program execution, we say the variable is *alive*.
|
||||
* A variable is visible if its value can be read/written at a point in program execution. Visibility often is based on program structure and where the current point of program execution is. Imagine program structure like a house with only doors and no windows: if you are outside the house, you cannot see the objects (variables) inside the house - they are not visible to you even though they may exist; however, if you enter the house through the door, you can see the variables inside the house. The house itself may be divided into rooms, each of which defines another space in which certain variables are visible and others are not visible.
|
||||
|
||||
A variable is "in scope" at a program point if it is both alive and visible at that point. A variable is "not in scope" if it is not alive or if it is not visible.
|
||||
|
||||
### The Block Editor has variables with only global scope
|
||||
|
||||
In the Block Editor, all variables are *global* variables, which means that all variables are alive and visible during the entire program execution. Consider the following Block Editor program:
|
||||
|
||||

|
||||
|
||||
This program will draw a diagonal line, wait for one second and then show the value of global variable `i`. What number will be shown on the LED screen? After the fifth iteration of the for loop, the value of variable `i` is 4. At the end of this iteration, the variable `i` is incremented and takes on the value 5. Since 5 is greater than the upper (inclusive) endpoint of 4, the loop terminates with the value of `i` at 5.
|
||||
|
||||
### Problems with global variables: unintended interference
|
||||
|
||||
The Block Editor program belows shows a problem with having only variables with global scope. The intent of the program below is fairly clear: if the user presses button A, slowly draw a diagonal line from top-left to lower-right; if the user presses button B, slowly draw a diagonal line from top-right to lower-left. Pressing both buttons should lead to an X being displayed on the screen.
|
||||
|
||||

|
||||
|
||||
The problem with the above program is that we have two loops using the same global variable *i* as the loop iteration variable. If the user first presses button A and then quickly presses button B, the loops execute concurrently, both reading and writing global variable *i* - this can cause unexpected results (in particular, you won't necessarily end up with an X displayed on the screen). You can see this more clearly by pressing the convert button in the Block Editor and examining the Touch Develop code that implements the Block Editor semantics:
|
||||
|
||||

|
||||
|
||||
### JavaScript has variables with both local and global scope
|
||||
|
||||
In Touch Develop, in contrast to the Block Editor, the for-loop iteration variable has scope that is local to the loop:
|
||||
|
||||
```
|
||||
for (let i2 = 0; i2 < 5; i2++) {
|
||||
led.plot(i2, i2)
|
||||
}
|
||||
```
|
||||
|
||||
This means that:
|
||||
|
||||
1. the loop iteration variable *i* comes into existence just before the loop begins and goes out of existence just after the loop terminates.
|
||||
|
||||
2. the variable `i` only is visible from the code that appears textually between the `do` and `end` keywords of the for loop (this is known as lexical scoping).
|
||||
|
||||
The value of the loop iteration variable is completely determined by the semantics of the for loop. As such, Touch Develop doesn't allow the programmer to overwrite the value of a loop iteration variable, as shown below:
|
||||
|
||||
```
|
||||
for (let i3 = 0; i3 < 5; i3++) {
|
||||
led.plot(i3, i3)
|
||||
i3 = 42
|
||||
}
|
||||
```
|
||||
|
||||
### Why is local scope useful?
|
||||
|
||||
Local scope allows you to use the same variable name in different parts of a program without concern about interference (as with variables with global scope). Here's the Touch Develop program that implements the "X" program without interference:
|
||||
|
||||
```
|
||||
input.onButtonPressed("A", () => {
|
||||
for (let i4 = 0; i4 < 5; i4++) {
|
||||
led.plot(i4, i4)
|
||||
basic.pause(1000)
|
||||
}
|
||||
})
|
||||
input.onButtonPressed("B", () => {
|
||||
for (let i5 = 0; i5 < 5; i5++) {
|
||||
led.plot(4 - i5, i5)
|
||||
basic.pause(1000)
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
Even though the same variable name (i) appears in both loops, these are different variables, each with their own lifetime and visibility (as defined by the for-loop).
|
||||
|
||||
## Concept 3: static types
|
||||
|
||||
A variable has a *static type* if it holds the same kind of value (integer, string, Boolean) everywhere that it is in scope. If a variable can hold values of different types at different program locations, then it does not have a static type.
|
||||
|
||||
### Block Editor blocks not plugged to an event will run
|
||||
|
||||
Blocks not plugged to an event will run. Blocks are running even if they are not inside of an `event`. As shown below, ``show string`` *Hello* will show a string on the LED screen one character at a time (scrolling from left to right).
|
||||
|
||||

|
||||
|
||||
### Google's Blockly variables do not have static types
|
||||
|
||||
In Blockly, a variable can hold different types of values at different program locations. As shown below, the global variable *Count* can be first set to a number and later to a string:
|
||||
|
||||

|
||||
|
||||
### Block Editor and Touch Develop variables have static types
|
||||
|
||||
In the Block Editor (based on Blockly) and Touch Develop, each variable has a static type. This means that some programs don't make sense, such as:
|
||||
|
||||

|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
# Comment
|
||||
|
||||
A note in code. #docs #comment #language
|
||||
|
||||
### @parent blocks/statement
|
||||
|
||||
A comment is a line of code that contains text, usually an explanation or a note. All comments are ignored during script execution.
|
||||
|
||||
### Block
|
||||
|
||||
Right click on any block and add a comment
|
||||
|
||||
### ~hint
|
||||
|
||||
To find out how to insert comments using the Blocks editor, see [the Blocks editor](/microbit/blocks/editor).
|
||||
|
||||
### ~
|
||||
|
||||
### Sample code with comments
|
||||
|
||||

|
||||
|
||||
### Commenting out code
|
||||
|
||||
During the debugging process, you may want to comment out a section of your code so that it doesn't run.
|
||||
|
||||
To comment out a block of code:
|
||||
|
||||
1. Right click on any block of code that you want to comment out.
|
||||
|
||||
1. Select add comment
|
||||
|
||||
When you want to uncomment your code, right click the on the comment, and then click delete block.
|
||||
|
||||
### See also
|
||||
|
||||
[Block editor](/microbit/blocks/editor)
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
# In Background
|
||||
|
||||
Run code in the background as a separate process or thread; for more information on this advanced construct, see [the micro:bit - a reactive system](/microbit/device/reactive).
|
||||
|
||||
```
|
||||
control.inBackground(() => {
|
||||
})
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
The example below shows how a background process can be used to display the current value of the global variable `num`, while code (like the `on button pressed` handler) can change the value of the variable.
|
||||
|
||||
```
|
||||
let num = 0
|
||||
control.inBackground(() => {
|
||||
while (true) {
|
||||
basic.showNumber(num, 150)
|
||||
basic.pause(100)
|
||||
}
|
||||
})
|
||||
input.onButtonPressed("A", () => {
|
||||
num++;
|
||||
})
|
||||
```
|
||||
|
||||
The code below using the `forever` loop is equivalent to the code above
|
||||
|
||||
```
|
||||
let num = 0
|
||||
basic.forever(() => {
|
||||
basic.showNumber(num, 150)
|
||||
})
|
||||
input.onButtonPressed("A", () => {
|
||||
num++;
|
||||
})
|
||||
```
|
||||
|
||||
### Contention for the LED display
|
||||
|
||||
If you have multiple processes that each show something on the LED screen, you may get unexpected results. Try, for example:
|
||||
|
||||
```
|
||||
basic.forever(() => {
|
||||
basic.showNumber(6789, 150)
|
||||
})
|
||||
input.onButtonPressed("A", () => {
|
||||
basic.showNumber(2, 150)
|
||||
})
|
||||
```
|
||||
|
||||
### See also
|
||||
|
||||
[while](/microbit/reference/loops/while), [forever](/microbit/reference/basic/forever), [on button pressed](/microbit/reference/input/on-button-pressed)
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
# Reset
|
||||
|
||||
Reset the BBC micro:bit (as if you pushed the reset button on the back of the device), which causes the program to start again.
|
||||
|
||||
```sig
|
||||
control.reset()
|
||||
```
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
# On Gamepad Button
|
||||
|
||||
Register code to run when the micro:bit receives a command from the paired gamepad.
|
||||
|
||||
## Bluetooth required
|
||||
|
||||
The functions in the ``devices`` namespace allow the BBC micro:bit to communicate with a separate (remote) device, such as a smartphone, over Bluetooth (Smart).
|
||||
|
||||
The set of supported events will depend on the remote device and the BBC micro:bit apps available for the remote device.
|
||||
|
||||
### Block Editor
|
||||
|
||||

|
||||
|
||||
### KindScript
|
||||
|
||||
```
|
||||
export function onGamepadButton(name: string, body:td.Action)
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
* ``body``: Action code to run when the the micro:bit receives a command from the paired gamepad.
|
||||
|
||||
### See Also
|
||||
|
||||
[tell remote control to](/microbit/reference/devices/tell-remote-control-to), [raise alert to](/microbit/reference/devices/raise-alert-to), [on notified](/microbit/reference/devices/on-notified), [signal strength](/microbit/reference/devices/signal-strength), [on signal strength changed](/microbit/reference/devices/on-signal-strength-changed)
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
# On Signal Strength Changed
|
||||
|
||||
The `on signal strength changed` function. #docs #devices #ble
|
||||
|
||||
Register code to run when the signal strength of the paired device changes.
|
||||
|
||||
## Bluetooth required
|
||||
|
||||
The functions in the ``devices`` namespace allow the BBC micro:bit to communicate with a separate (remote) device, such as a smartphone, over Bluetooth (Smart).
|
||||
|
||||
The set of supported events will depend on the remote device and the BBC micro:bit apps available for the remote device.
|
||||
|
||||
### Block Editor
|
||||
|
||||

|
||||
|
||||
### KindScript
|
||||
|
||||
```
|
||||
export function onSignalStrengthChanged(body:td.Action)
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
* ``body``: code to run when the signal strength changes.
|
||||
|
||||
### Examples
|
||||
|
||||
Display the signal strength on screen:
|
||||
|
||||
```
|
||||
devices.onSignalStrengthChanged(() => {
|
||||
basic.showNumber(devices.signalStrength(), 150)
|
||||
})
|
||||
```
|
||||
|
||||
### See Also
|
||||
|
||||
[tell remote control to](/microbit/reference/devices/tell-remote-control-to), [raise alert to](/microbit/reference/devices/raise-alert-to), [on notified](/microbit/reference/devices/on-notified), [signal strength](/microbit/reference/devices/signal-strength)
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
# raise alert to
|
||||
|
||||
The raise alert to function. #docs #antenna #ble
|
||||
|
||||
Raise an alert on a remote device.
|
||||
|
||||
##
|
||||
|
||||
The functions in the ``devices`` namespace allow the BBC micro:bit to communicate with a separate (remote) device, such as a smartphone, over Bluetooth (Smart).
|
||||
|
||||
The set of supported events will depend on the remote device and the BBC micro:bit apps available for the remote device.
|
||||
|
||||
### KindScript
|
||||
|
||||

|
||||
|
||||
### KindScript
|
||||
|
||||
```
|
||||
export function raiseAlertTo(event: string)
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
* event - an event identifier
|
||||
|
||||
### Examples
|
||||
|
||||
To tell the connected device to display toast
|
||||
|
||||
```
|
||||
devices.raiseAlertTo("display toast")
|
||||
```
|
||||
|
||||
To tell the connected device to vibrate
|
||||
|
||||
```
|
||||
devices.raiseAlertTo("vibrate")
|
||||
```
|
||||
|
||||
To tell the connected device to play a sound
|
||||
|
||||
```
|
||||
devices.raiseAlertTo("play sound")
|
||||
```
|
||||
|
||||
To tell the connected device to play a ringtone
|
||||
|
||||
```
|
||||
devices.raiseAlertTo("play ringtone")
|
||||
```
|
||||
|
||||
To tell the connected device to find my phone
|
||||
|
||||
```
|
||||
devices.raiseAlertTo("find my phone")
|
||||
```
|
||||
|
||||
To tell the connected device to ring alarm
|
||||
|
||||
```
|
||||
devices.raiseAlertTo("ring alarm")
|
||||
```
|
||||
|
||||
### See also
|
||||
|
||||
[tell remote control to](/microbit/reference/devices/tell-remote-control-to), [tell camera to](/microbit/reference/devices/tell-camera-to), [on notified](/microbit/reference/devices/on-notified)
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
# Receive Number
|
||||
|
||||
The broadcast function. #docs #ble #radio
|
||||
|
||||
Reads the next radio packet as a number data packet.
|
||||
|
||||
## Important Security Consideration
|
||||
|
||||
The functions in the ``radio`` namespace allow the BBC micro:bit to communicate with other micro:bits.
|
||||
|
||||
This API does not contain any form of encryption, authentication or authorization. It's purpose is solely for use as a teaching aid to demonstrate how simple communications operates, and to provide a sandpit through which learning can take place.
|
||||
|
||||
For serious applications, BLE should be considered a substantially more secure alternative.
|
||||
|
||||
### Block Editor
|
||||
|
||||

|
||||
|
||||
### KindScript
|
||||
|
||||
```
|
||||
export function receiveNumber() : number
|
||||
```
|
||||
|
||||
### Returns
|
||||
|
||||
* packet - a number received.
|
||||
|
||||
### Examples
|
||||
|
||||
Broadcasts the value of ``acceleration`` x to other micro:bits.
|
||||
|
||||
```
|
||||
radio.onDataReceived(() => {
|
||||
led.plotBarGraph(radio.receiveNumber(), 1023)
|
||||
})
|
||||
```
|
||||
|
||||
### See also
|
||||
|
||||
[send number](/microbit/reference/radio/send-number), [receive number](/microbit/reference/radio/receive-number), [on data received](/microbit/reference/radio/on-data-received), [set group](/microbit/reference/radio/set-group)
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
# Signal Strength
|
||||
|
||||
The `signal strength` function. #docs #antenna #ble
|
||||
|
||||
Returns the signal strength reported by the paired device from ``0`` (no signal) to ``4`` (full strength).
|
||||
|
||||
## Bluetooth required
|
||||
|
||||
The functions in the ``devices`` namespace allow the BBC micro:bit to communicate with a separate (remote) device, such as a smartphone, over Bluetooth (Smart).
|
||||
|
||||
The set of supported events will depend on the remote device and the BBC micro:bit apps available for the remote device.
|
||||
|
||||
### Block Editor
|
||||
|
||||

|
||||
|
||||
### KindScript
|
||||
|
||||
```
|
||||
export function signalStrength() : number
|
||||
```
|
||||
|
||||
### Returns
|
||||
|
||||
* the signal strength from ``0`` (no signal) to ``4`` (full strength).
|
||||
|
||||
### Examples
|
||||
|
||||
Display the signal strength on screen:
|
||||
|
||||
```
|
||||
devices.onSignalStrengthChanged(() => {
|
||||
basic.showNumber(devices.signalStrength(), 150)
|
||||
})
|
||||
```
|
||||
|
||||
### See Also
|
||||
|
||||
[tell remote control to](/microbit/reference/devices/tell-remote-control-to), [raise alert to](/microbit/reference/devices/raise-alert-to), [on notified](/microbit/reference/devices/on-notified), [on signal strength changed](/microbit/reference/devices/on-signal-strength-changed)
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
# tell camera to
|
||||
|
||||
The tell camera to function. #docs #antenna #ble
|
||||
|
||||
Access the photo/video-taking functionality of a remote device using the ``tell camera to`` function.
|
||||
|
||||
## Bluetooth required
|
||||
|
||||
The functions in the ``devices`` namespace allow the BBC micro:bit to communicate with a separate (remote) device, such as a smartphone, over Bluetooth (Smart).
|
||||
|
||||
The set of supported events will depend on the remote device and the BBC micro:bit apps available for the remote device.
|
||||
|
||||
### Block Editor
|
||||
|
||||

|
||||
|
||||
### KindScript
|
||||
|
||||
```
|
||||
export function tellCameraTo(event: string)
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
* event - an event identifier
|
||||
|
||||
### Examples
|
||||
|
||||
To tell the connected device to take a picture:
|
||||
|
||||
```
|
||||
devices.tellCameraTo("take photo")
|
||||
```
|
||||
|
||||
To tell the connected device to start recording a video
|
||||
|
||||
```
|
||||
devices.tellCameraTo("start video capture")
|
||||
```
|
||||
|
||||
To tell the connected device to stop recording a video
|
||||
|
||||
```
|
||||
devices.tellCameraTo("stop video capture")
|
||||
```
|
||||
|
||||
To tell the connected device to toggle front-rear
|
||||
|
||||
```
|
||||
devices.tellCameraTo("toggle front-rear")
|
||||
```
|
||||
|
||||
To tell the connected device to launch photo mode
|
||||
|
||||
```
|
||||
devices.tellCameraTo("launch photo mode")
|
||||
```
|
||||
|
||||
To tell the connected device to launch video mode
|
||||
|
||||
```
|
||||
devices.tellCameraTo("launch video mode")
|
||||
```
|
||||
|
||||
To tell the connected device to stop photo mode
|
||||
|
||||
```
|
||||
devices.tellCameraTo("stop photo mode")
|
||||
```
|
||||
|
||||
To tell the connected device to stop video mode
|
||||
|
||||
```
|
||||
devices.tellCameraTo("stop video mode")
|
||||
```
|
||||
|
||||
### See Also
|
||||
|
||||
[tell remote control to](/microbit/reference/devices/tell-remote-control-to), [raise alert to](/microbit/reference/devices/raise-alert-to), [on notified](/microbit/reference/devices/on-notified)
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
# tell microphone to
|
||||
|
||||
The tell microphone to function. #docs #antenna #ble
|
||||
|
||||
Access the audio recording capabilities of the device using the ``tell microphone to`` function.
|
||||
|
||||
The functions in the antenna namespace allow the BBC micro:bit to communicate with a separate (remote) device, such as a smartphone, over Bluetooth (Smart). The set of supported events will depend on the remote device and the BBC micro:bit apps available for the remote device.
|
||||
|
||||
### Block Editor
|
||||
|
||||

|
||||
|
||||
### KindScript
|
||||
|
||||
```
|
||||
export function tellMicrophoneTo(event: string)
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
* event - an event identifier
|
||||
|
||||
### Event values
|
||||
|
||||
* play
|
||||
* stop
|
||||
* pause
|
||||
* forward
|
||||
* rewind
|
||||
* volume up
|
||||
* volume down
|
||||
* previous track
|
||||
* next track
|
||||
|
||||
### Examples
|
||||
|
||||
To tell the connected device to start recording audio
|
||||
|
||||
```
|
||||
antenna.tellMicrophoneTo("start capture")
|
||||
```
|
||||
|
||||
To tell the connected device to stop recording audio
|
||||
|
||||
```
|
||||
antenna.tellMicrophoneTo("stop capture")
|
||||
```
|
||||
|
||||
### Other show functions
|
||||
|
||||
* use [tell remote control to](/microbit/reference/devices/tell-remote-control-to) to control presentation of media content
|
||||
* use [tell camera to](/microbit/reference/devices/tell-camera-to) to control the photo/video recording of connected devices
|
||||
* use [raise alert to](/microbit/reference/devices/raise-alert-to) to control the microphone of connected devices
|
||||
|
||||
### See also
|
||||
|
||||
[Antenna](/microbit/js/antenna)
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
# tell remote control to
|
||||
|
||||
The tell remote control to function. #docs #antenna #ble
|
||||
|
||||
Control the presentation of media content available on a remote device using the `tell remote control` to function.
|
||||
|
||||
##
|
||||
|
||||
The functions in the ``devices`` namespace allow the BBC micro:bit to communicate with a separate (remote) device, such as a smartphone, over Bluetooth (Smart).
|
||||
|
||||
The set of supported events will depend on the remote device and the BBC micro:bit apps available for the remote device.
|
||||
|
||||
### Block Editor
|
||||
|
||||

|
||||
|
||||
### KindScript
|
||||
|
||||
```
|
||||
export function tellRemoteControlTo(event: string)
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
* event - an event identifier
|
||||
|
||||
### Event values
|
||||
|
||||
* play
|
||||
* stop
|
||||
* pause
|
||||
* forward
|
||||
* rewind
|
||||
* volume up
|
||||
* volume down
|
||||
* previous track
|
||||
* next track
|
||||
|
||||
### Examples
|
||||
|
||||
To tell the connected device to start playing:
|
||||
|
||||
```
|
||||
devices.tellRemoteControlTo("play")
|
||||
```
|
||||
|
||||
To tell the connected device to stop playing
|
||||
|
||||
```
|
||||
devices.tellRemoteControlTo("stop")
|
||||
```
|
||||
|
||||
To tell the connected device to go to next track
|
||||
|
||||
```
|
||||
devices.tellRemoteControlTo("next track")
|
||||
```
|
||||
|
||||
To tell the connected device to go to previous track
|
||||
|
||||
```
|
||||
devices.tellRemoteControlTo("previous track")
|
||||
```
|
||||
|
||||
To tell the connected device to go forward
|
||||
|
||||
```
|
||||
devices.tellRemoteControlTo("forward")
|
||||
```
|
||||
|
||||
To tell the connected device to rewind
|
||||
|
||||
```
|
||||
devices.tellRemoteControlTo("rewind")
|
||||
```
|
||||
|
||||
To tell the connected device volume up
|
||||
|
||||
```
|
||||
devices.tellRemoteControlTo("volume up")
|
||||
```
|
||||
|
||||
To tell the connected device volume down
|
||||
|
||||
```
|
||||
devices.tellRemoteControlTo("volume down")
|
||||
```
|
||||
|
||||
### See also
|
||||
|
||||
[tell camera to](/microbit/reference/devices/tell-camera-to), [raise alert to](/microbit/reference/devices/raise-alert-to), [on notified](/microbit/reference/devices/on-notified)
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
# Change Score By
|
||||
|
||||
The game library
|
||||
|
||||
The game library supports simple single-player time-based games. The player will ** add points to score**.
|
||||
|
||||
## Block Editor
|
||||
|
||||
The code below shows a simple game where the user gets to press the button ``A`` as much times as possible and the score will display on the screen.
|
||||
|
||||

|
||||
|
||||
## KindScript
|
||||
|
||||
The code below shows a simple game where the user gets to press the button ``A`` as much times as possible in 10 seconds.
|
||||
|
||||
```
|
||||
input.onButtonPressed("A", () => {
|
||||
game.addScore(1)
|
||||
})
|
||||
game.startCountdown(10000)
|
||||
```
|
||||
|
||||
### Score
|
||||
|
||||
When a player achieves a goal, you can increase the game score
|
||||
|
||||
* add score points to the current score
|
||||
|
||||
```
|
||||
export function addScore(points: number)
|
||||
```
|
||||
|
||||
* get the current score value
|
||||
|
||||
```
|
||||
export function score() : number
|
||||
```
|
||||
|
||||
### Lessons
|
||||
|
||||
[bop it](/microbit/lessons/bop-it) | [game of chance](/microbit/lessons/game-of-chance) | [game counter](/microbit/lessons/game-counter)
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
# Change
|
||||
|
||||
The game library
|
||||
|
||||
### Change
|
||||
|
||||
Sprite will change the x position by this number
|
||||
|
||||
## Block Editor
|
||||
|
||||

|
||||
|
||||
## KindScript
|
||||
|
||||
Sprite will change the x position by this number
|
||||
|
||||
```
|
||||
export function changeXBy(_this: micro_bitSprites.LedSprite, x: number)
|
||||
```
|
||||
|
||||
Sprite will change the y position by this number
|
||||
|
||||
```
|
||||
export function changeYBy(_this: micro_bitSprites.LedSprite, y: number)
|
||||
```
|
||||
|
||||
### Lessons
|
||||
|
||||
[game of chance](/microbit/lessons/game-of-chance) | [game counter](/microbit/lessons/game-counter)
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
# Clear
|
||||
|
||||
The clear function for images. #clear #docs
|
||||
|
||||
Turn off all the pixels in an [Image](/microbit/reference/image/image).
|
||||
|
||||
### KindScript
|
||||
|
||||
```
|
||||
export function clear(img: micro_bit.Image)
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
* none
|
||||
|
||||
### Example
|
||||
|
||||
The following example turns off the pixels of `img` when the A input button is pressed:
|
||||
|
||||
```
|
||||
let img = images.createImage(`
|
||||
. . . . .
|
||||
. # # # .
|
||||
. # # # .
|
||||
. # # # .
|
||||
. . . . .
|
||||
`)
|
||||
img.showImage(0)
|
||||
input.onButtonPressed("A", () => {
|
||||
img.clear()
|
||||
img.showImage(0)
|
||||
})
|
||||
```
|
||||
|
||||
### See also
|
||||
|
||||
[Image](/microbit/reference/image/image), [show animation](/microbit/reference/basic/show-animation), [show image](/microbit/reference/images/show-image), [scroll image](/microbit/reference/images/scroll-image), [create image](/microbit/reference/images/create-image)
|
||||
|
||||
@@ -0,0 +1,161 @@
|
||||
# Game Library
|
||||
|
||||
The game library #docs
|
||||
|
||||
The game library supports simple single-player time-based games. The player has a **sprite**, number of **lives** and a **score**. The game has a sprite, number of **levels** and a **countdown clock**. The general goal of a game will be to move the sprite and achieve a top score before time runs out or the number of lives goes to zero.
|
||||
|
||||
## Block Editor
|
||||
|
||||

|
||||
|
||||
## KindScript
|
||||
|
||||
The code below shows a simple game where the user gets to press the button ``A`` as much times as possible in 10 seconds.
|
||||
|
||||
```
|
||||
input.onButtonPressed("A", () => {
|
||||
game.addScore(1)
|
||||
})
|
||||
game.startCountdown(10000)
|
||||
```
|
||||
|
||||
### [Create sprite](/microbit/functions/game-library/create-sprite)
|
||||
|
||||
Create sprite with x, y coordinates and returns a LED Sprite. Create a new LED sprite.
|
||||
|
||||

|
||||
|
||||
```
|
||||
export function createSprite(x: number, y: number) : micro_bitSprites.LedSprite
|
||||
```
|
||||
|
||||
### [Move](/microbit/functions/game-library/move)
|
||||
|
||||
Sprite move by a certain number
|
||||
|
||||

|
||||
|
||||
```
|
||||
export function move(_this: micro_bitSprites.LedSprite, leds: number)
|
||||
```
|
||||
|
||||
### [Turn](/microbit/functions/game-library/turn)
|
||||
|
||||
Rotates a sprite to the right by a certain number of degrees
|
||||
|
||||

|
||||
|
||||
```
|
||||
export function turnRight(_this: micro_bitSprites.LedSprite, degrees: number)
|
||||
```
|
||||
|
||||
Rotates a sprite to the left by a certain number of degrees
|
||||
|
||||
```
|
||||
export function turnLeft(_this: micro_bitSprites.LedSprite, degrees: number)
|
||||
```
|
||||
|
||||
### [Change](/microbit/functions/game-library/change)
|
||||
|
||||
Sprite will change the x position by this number
|
||||
|
||||

|
||||
|
||||
```
|
||||
export function changeXBy(_this: micro_bitSprites.LedSprite, x: number)
|
||||
```
|
||||
|
||||
Sprite will change the y position by this number
|
||||
|
||||
```
|
||||
export function changeYBy(_this: micro_bitSprites.LedSprite, y: number)
|
||||
```
|
||||
|
||||
### [Set](/microbit/functions/game-library/set)
|
||||
|
||||
Sprite will change the x position by this number
|
||||
|
||||
```
|
||||
export function setX(_this: micro_bitSprites.LedSprite, x: number)
|
||||
```
|
||||
|
||||
Sprite will change the y position by this number
|
||||
|
||||

|
||||
|
||||
```
|
||||
export function changeYBy(_this: micro_bitSprites.LedSprite, y: number)
|
||||
```
|
||||
|
||||
### [If on edge, bounce](/microbit/functions/game-library/if-on-edge-bounce)
|
||||
|
||||
Sprite - If the sprite is on the edge, the sprite will bounce
|
||||
|
||||

|
||||
|
||||
```
|
||||
export function ifOnEdge_Bounce(_this: micro_bitSprites.LedSprite)
|
||||
```
|
||||
|
||||
### [Change score by](/microbit/functions/game-library/change-score-by)
|
||||
|
||||
When a player achieves a goal, you can increase the game score
|
||||
|
||||
* add score points to the current score
|
||||
|
||||

|
||||
|
||||
```
|
||||
export function addScore(points: number)
|
||||
```
|
||||
|
||||
### [Score](/microbit/functions/game-library/score)
|
||||
|
||||
* set the current score to a particular value.
|
||||
|
||||
```
|
||||
export function setScore(value: number)
|
||||
```
|
||||
|
||||
* get the current score value
|
||||
|
||||

|
||||
|
||||
```
|
||||
export function score() : number
|
||||
```
|
||||
|
||||
### [Countdown](/microbit/functions/game-library/start-countdown)
|
||||
|
||||
If your game has a time limit, you can start a countdown in which case `game->current time` returns the remaining time.
|
||||
|
||||
* start a countdown with the maximum duration of the game in milliseconds.
|
||||
|
||||

|
||||
|
||||
```
|
||||
export function startCountdown(ms: number)
|
||||
```
|
||||
|
||||
### [Game over](/microbit/functions/game-library/game-over)
|
||||
|
||||
If the `life` reaches zero or the time expires (see countdown), the game enters the **game over** mode. When the game is over, `game->is running` returns false
|
||||
|
||||
* check if the game still running.
|
||||
|
||||
```
|
||||
let running = game.isRunning()
|
||||
```
|
||||
|
||||
You can also end the game by calling the `game -> game over` function:
|
||||
|
||||

|
||||
|
||||
```
|
||||
game.gameOver()
|
||||
```
|
||||
|
||||
### Lessons
|
||||
|
||||
[game of chance](/microbit/lessons/game-of-chance) | [game counter](/microbit/lessons/game-counter)
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
# Game Over
|
||||
|
||||
The game library
|
||||
|
||||
The game library supports simple single-player time-based games. The game can end the game by calling the `game over` function
|
||||
|
||||
## Block Editor
|
||||
|
||||
You can end the game by calling the `game over ` function. In this example, if BBC micro:bit's answer to the question is GAME OVER, GAME OVER will be displayed to end the game.
|
||||
|
||||

|
||||
|
||||
## KindScript
|
||||
|
||||
You can end the game by calling the `game -> game over` function:
|
||||
|
||||
```
|
||||
game.gameOver()
|
||||
```
|
||||
|
||||
### Lessons
|
||||
|
||||
[game of chance](/microbit/lessons/game-of-chance)
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
# Move
|
||||
|
||||
The game library
|
||||
|
||||
### Move
|
||||
|
||||
Sprite move by a certain number
|
||||
|
||||
## Block Editor
|
||||
|
||||

|
||||
|
||||
## KindScript
|
||||
|
||||
```
|
||||
export function move(_this: micro_bitSprites.LedSprite, leds: number)
|
||||
```
|
||||
|
||||
### Lessons
|
||||
|
||||
[game of chance](/microbit/lessons/game-of-chance) | [game counter](/microbit/lessons/game-counter)
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
# Position
|
||||
|
||||
The game library
|
||||
|
||||
### Create sprite
|
||||
|
||||
Reports the x or y position of a sprite on the LED screen
|
||||
|
||||
## Block Editor
|
||||
|
||||
Reports the x position of a sprite on the LED screen
|
||||
|
||||

|
||||
|
||||
## KindScript
|
||||
|
||||
Reports the x position of a sprite on the LED screen
|
||||
|
||||
```
|
||||
export function x(_this: micro_bitSprites.LedSprite) : number
|
||||
```
|
||||
|
||||
Reports the y position of a sprite on the LED screen
|
||||
|
||||
```
|
||||
export function y(_this: micro_bitSprites.LedSprite) : number
|
||||
```
|
||||
|
||||
### Lessons
|
||||
|
||||
[game of chance](/microbit/lessons/game-of-chance) | [game counter](/microbit/lessons/game-counter)
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
# Reports
|
||||
|
||||
The game library
|
||||
|
||||
### Reports
|
||||
|
||||
Reports the x or y position, the current direction of a sprite, or the brightness of a sprite on the LED screen
|
||||
|
||||
## Block Editor
|
||||
|
||||
Reports the x position of a sprite on the LED screen
|
||||
|
||||

|
||||
|
||||
Reports the y position of a sprite on the LED screen
|
||||
|
||||

|
||||
|
||||
Reports the brightness of a sprite on the LED screen
|
||||
|
||||

|
||||
|
||||
Reports the direction of a sprite on the LED screen
|
||||
|
||||

|
||||
|
||||
## KindScript
|
||||
|
||||
Reports the x position of a sprite on the LED screen
|
||||
|
||||
```
|
||||
export function x(_this: micro_bitSprites.LedSprite) : number
|
||||
```
|
||||
|
||||
Reports the y position of a sprite on the LED screen
|
||||
|
||||
```
|
||||
export function y(_this: micro_bitSprites.LedSprite) : number
|
||||
```
|
||||
|
||||
Reports the brightness of a sprite on the LED screen
|
||||
|
||||
```
|
||||
export function brightness(_this: micro_bitSprites.LedSprite) : number
|
||||
```
|
||||
|
||||
Reports the current direction of a sprite on the LED screen
|
||||
|
||||
```
|
||||
export function direction(_this: micro_bitSprites.LedSprite) : number
|
||||
```
|
||||
|
||||
### Lessons
|
||||
|
||||
[game of chance](/microbit/lessons/game-of-chance) | [game counter](/microbit/lessons/game-counter)
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
# Score
|
||||
|
||||
The game library #docs
|
||||
|
||||
The game library supports simple single-player games. The player has a **score**.
|
||||
|
||||
## Block Editor
|
||||
|
||||
The code below shows a simple game where the user gets to press the button ``A`` and adds 1 point to score that will be displayed on the BBC micro:bit screen
|
||||
|
||||

|
||||
|
||||
## KindScript
|
||||
|
||||
The code below shows a simple game where the user gets to press the button ``A`` as much times as possible in 10 seconds.
|
||||
|
||||
```
|
||||
input.onButtonPressed("A", () => {
|
||||
game.addScore(1)
|
||||
})
|
||||
game.startCountdown(10000)
|
||||
```
|
||||
|
||||
### Score
|
||||
|
||||
When a player achieves a goal, you can increase the game score
|
||||
|
||||
* add score points to the current score
|
||||
|
||||
```
|
||||
export function addScore(points: number)
|
||||
```
|
||||
|
||||
* set the current score to a particular value.
|
||||
|
||||
```
|
||||
export function setScore(value: number)
|
||||
```
|
||||
|
||||
* get the current score value
|
||||
|
||||
```
|
||||
export function score() : number
|
||||
```
|
||||
|
||||
### Countdown
|
||||
|
||||
If your game has a time limit, you can start a countdown in which case `game->current time` returns the remaining time.
|
||||
|
||||
* start a countdown with the maximum duration of the game in milliseconds.
|
||||
|
||||
```
|
||||
export function startCountdown(ms: number)
|
||||
```
|
||||
|
||||
### Lessons
|
||||
|
||||
[bop it](/microbit/lessons/bop-it) | [game of chance](/microbit/lessons/game-of-chance) | [game counter](/microbit/lessons/game-counter)
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
# Start Countdown
|
||||
|
||||
The game library #docs
|
||||
|
||||
The game library supports simple single-player time-based games. The general goal of a game will be to achieve a top score before time runs out of time.
|
||||
|
||||
## Block Editor
|
||||
|
||||

|
||||
|
||||
## KindScript
|
||||
|
||||
The code below shows a simple game where the user gets to press the button ``A`` as much times as possible in 10 seconds.
|
||||
|
||||
```
|
||||
input.onButtonPressed("A", () => {
|
||||
game.addScore(1)
|
||||
})
|
||||
game.startCountdown(10000)
|
||||
```
|
||||
|
||||
### Score
|
||||
|
||||
When a player achieves a goal, you can increase the game score
|
||||
|
||||
* add score points to the current score
|
||||
|
||||
```
|
||||
export function addScore(points: number)
|
||||
```
|
||||
|
||||
* set the current score to a particular value.
|
||||
|
||||
```
|
||||
export function setScore(value: number)
|
||||
```
|
||||
|
||||
* get the current score value
|
||||
|
||||
```
|
||||
export function score() : number
|
||||
```
|
||||
|
||||
### Countdown
|
||||
|
||||
If your game has a time limit, you can start a countdown in which case `game->current time` returns the remaining time.
|
||||
|
||||
* start a countdown with the maximum duration of the game in milliseconds.
|
||||
|
||||
```
|
||||
export function startCountdown(ms: number)
|
||||
```
|
||||
|
||||
### Lessons
|
||||
|
||||
[bop it](/microbit/lessons/bop-it) | [game of chance](/microbit/lessons/game-of-chance) | [game counter](/microbit/lessons/game-counter)
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
# Touching
|
||||
|
||||
The game library
|
||||
|
||||
### Touching
|
||||
|
||||
Reports true if sprite is touching specified sprite
|
||||
|
||||
## Block Editor
|
||||
|
||||

|
||||
|
||||
## KindScript
|
||||
|
||||
Reports true if sprite is touching specified sprite
|
||||
|
||||
```
|
||||
export function isTouching(_this: micro_bitSprites.LedSprite, other: micro_bitSprites.LedSprite) : boolean
|
||||
```
|
||||
|
||||
### Lessons
|
||||
|
||||
[game of chance](/microbit/lessons/game-of-chance) | [game counter](/microbit/lessons/game-counter)
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
# Turn
|
||||
|
||||
The game library
|
||||
|
||||
Rotates a sprite to the right by a certain number of degrees
|
||||
|
||||
## Block Editor
|
||||
|
||||
Rotates a sprite to the right by a certain number of degrees
|
||||
|
||||

|
||||
|
||||
## KindScript
|
||||
|
||||
Rotates a sprite to the right by a certain number of degrees
|
||||
|
||||
```
|
||||
export function turnRight(_this: micro_bitSprites.LedSprite, degrees: number)
|
||||
```
|
||||
|
||||
Rotates a sprite to the left by a certain number of degrees
|
||||
|
||||
```
|
||||
export function turnLeft(_this: micro_bitSprites.LedSprite, degrees: number)
|
||||
```
|
||||
|
||||
### Lessons
|
||||
|
||||
[game of chance](/microbit/lessons/game-of-chance) | [game counter](/microbit/lessons/game-counter)
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
# Create Image
|
||||
|
||||
Create an [Image](/microbit/reference/image/image) to show on the [LED screen](/microbit/device/screen).
|
||||
|
||||
```sig
|
||||
images.createImage(`
|
||||
. . # . .
|
||||
. # # # .
|
||||
# # # # #
|
||||
. # # # .
|
||||
. . # . .
|
||||
`)
|
||||
```
|
||||
|
||||
### Example: rock, paper, scissors
|
||||
|
||||
The following example shows one of three images (rock, paper, or scissors) when you shake the micro:bit:
|
||||
|
||||
```
|
||||
input.onGesture(Gesture.Shake, () => {
|
||||
let rockpaper = images.createImage(`
|
||||
. . . . . # # # # # . . . . #
|
||||
. # # # . # . . . # # # . # .
|
||||
. # # # . # . . . # . # # . .
|
||||
. # # # . # . . . # # # . # .
|
||||
. . . . . # # # # # . . . . #
|
||||
`)
|
||||
rockpaper.showFrame(Math.random(3))
|
||||
})
|
||||
```
|
||||
|
||||
### Lessons
|
||||
|
||||
[rock paper scissors](/microbit/lessons/rock-paper-scissors), [digital pet](/microbit/lessons/digital-pet), [offset-image](/microbit/lessons/offset-image)
|
||||
|
||||
### See also
|
||||
|
||||
[show animation](/microbit/reference/basic/show-animation), [image](/microbit/reference/image/image), [show image](/microbit/reference/image/show-image), [scroll image](/microbit/reference/image/scroll-image)
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
# Image
|
||||
|
||||
An image for the micro:bit screen. #docs #image #screen #LED
|
||||
|
||||
### @parent blocks/language
|
||||
|
||||
An *Image* is a matrix of pixels to show on the [LED screen](/microbit/device/screen)
|
||||
|
||||
### Block Editor: Show LEDs
|
||||
|
||||
To display an image using the [Block Editor](/microbit/blocks/editor):
|
||||
|
||||
* click `Basic` , `Show LEDs`, and tap on the LEDs`
|
||||
* when you're done, return to your code
|
||||
|
||||

|
||||
|
||||
You should see code similar to this:
|
||||
|
||||
### Creating an image
|
||||
|
||||
To create an image that you can later modify, see the [create image](/microbit/reference/images/create-image) function.
|
||||
|
||||
### Block editor: create and show images
|
||||
|
||||
To create images using the [Block editor](/microbit/blocks/editor):
|
||||
|
||||
1. Click the **Images** category on the left.
|
||||
|
||||
2. Drag and drop the **show image** block into your code.
|
||||
|
||||
3. Drag and drop the **create image** or **create big image** block onto the **show image** block so that they connect.
|
||||
|
||||
4. Make an image on the **create image** block by clicking on the squares.
|
||||
|
||||
You should see code similar to this:
|
||||
|
||||

|
||||
|
||||
### Image functions
|
||||
|
||||
* [create image](/microbit/reference/images/create-image): create an image from a series of on/off LED states
|
||||
* [clear](/microbit/reference/basic/clear-screen): turn off all the pixels in an image
|
||||
* [pixel](/microbit/reference/images/pixel): get the state of a pixel in an image
|
||||
* [show-leds](/microbit/reference/basic/show-leds): show a single-frame image on the LED screen
|
||||
* [show image](/microbit/reference/images/show-image): show an image on the screen
|
||||
* [scroll image](/microbit/reference/images/scroll-image): scroll an image on the screen
|
||||
|
||||
### Lessons
|
||||
|
||||
* [smiley](/microbit/lessons/smiley)
|
||||
|
||||
### See also
|
||||
|
||||
[Show LEDs](/microbit/reference/basic/show-leds), [create image](/microbit/reference/images/create-image), [show image](/microbit/reference/images/show-image), [LED screen](/microbit/device/screen)
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
# Pixel
|
||||
|
||||
The pixel function. #pixel #image #docs
|
||||
|
||||
Get the state of a pixel in an [Image](/microbit/reference/image/image).
|
||||
|
||||
### KindScript
|
||||
|
||||
```
|
||||
export function pixel(_this: micro_bit.Image, x: number, y: number) : boolean
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
* x - [Number](/microbit/reference/types/number); the *x coordinate* or horizontal position of a pixel in an [image](/microbit/reference/image/image)
|
||||
* y - [Number](/microbit/reference/types/number); the *y coordinate* or vertical position of a pixel in an [image](/microbit/reference/image/image)
|
||||
|
||||
### x, y coordinates?
|
||||
|
||||
To figure out the ``x``, ``y`` coordinates, see [LED screen](/microbit/device/screen).
|
||||
|
||||
### Returns
|
||||
|
||||
* [Boolean](/microbit/reference/types/boolean) - `true` for on and `false` for off
|
||||
|
||||
### Example
|
||||
|
||||
This example gets the state of pixel `0, 0` in the `img` variable:
|
||||
|
||||
### ~hide
|
||||
|
||||
```
|
||||
let img = images.createImage(`
|
||||
. . # . . . . . . .
|
||||
. # . # . . . # . .
|
||||
. . # . . . . . . .
|
||||
. # . # . . . # . .
|
||||
. . # . . . . . . .
|
||||
`)
|
||||
```
|
||||
|
||||
### ~
|
||||
|
||||
```
|
||||
let state = img.pixel(0, 0)
|
||||
```
|
||||
|
||||
### See also
|
||||
|
||||
[set pixel](/microbit/reference/images/set-pixel), [show image](/microbit/reference/images/show-image), [image](/microbit/reference/image/image), [create image](/microbit/reference/images/create-image), [scroll image](/microbit/reference/images/scroll-image)
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
# Plot Frame
|
||||
|
||||
The plot frame function. #plotframe #docs #image #screen #LED
|
||||
|
||||
Display an [Image](/microbit/reference/image/image) on the BBC micro:bit's [LED screen](/microbit/device/screen)
|
||||
|
||||
### KindScript
|
||||
|
||||
```
|
||||
export function plotFrame(_this: micro_bit.Image, index: number)
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
* index - [Number](/microbit/reference/types/number); which frame of the image to display
|
||||
|
||||
### Difference from `plot image`
|
||||
|
||||
The `plot frame` function takes the index of the frame (if there are two frames, then the possible indices are 0 and 1), whereas `plot image` accepts an offset (if there are two frames, the offset would range between 0 and 9).
|
||||
|
||||
### Example
|
||||
|
||||
```
|
||||
let img = images.createImage(`
|
||||
# . . . # # . . . #
|
||||
. # . # . . # # # .
|
||||
. . # . . . # # # .
|
||||
. # . # . . # # # .
|
||||
# . . . # # . . . #
|
||||
`)
|
||||
img.plotFrame(1)
|
||||
```
|
||||
|
||||
### Lessons
|
||||
|
||||
[smiley](/microbit/lessons/smiley), [flashing heart](/microbit/lessons/flashing-heart), [magic logo](/microbit/lessons/magic-logo)
|
||||
|
||||
### See also
|
||||
|
||||
[create image](/microbit/reference/images/create-image), [show animation](/microbit/reference/basic/show-animation), [image](/microbit/reference/image/image), [show image](/microbit/reference/images/show-image), [scroll image](/microbit/reference/images/scroll-image)
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
# Plot Image
|
||||
|
||||
The plot image function. #plotimage #docs #image #screen #LED
|
||||
|
||||
Display an [Image](/microbit/reference/image/image) on the BBC micro:bit's [LED screen](/microbit/device/screen)
|
||||
|
||||
### KindScript
|
||||
|
||||
```
|
||||
export function plotImage(_this: micro_bit.Image, xOffset: number)
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
* x offset - [Number](/microbit/reference/types/number); the horizontal starting point of an image; use 0 for the first frame of the image, 5 for the second frame of the image, 10 for the third frame and so on.
|
||||
|
||||
### Difference from `show image`
|
||||
|
||||
The `show image` function has a built in delay of 400ms after display of the image, whereas `plot image` has no built-in delay.
|
||||
|
||||
### Example
|
||||
|
||||
```
|
||||
let img = images.createImage(`
|
||||
# . . . # # . . . #
|
||||
. # . # . . # # # .
|
||||
. . # . . . # # # .
|
||||
. # . # . . # # # .
|
||||
# . . . # # . . . #
|
||||
`)
|
||||
img.plotImage(0)
|
||||
```
|
||||
|
||||
### Lessons
|
||||
|
||||
[smiley](/microbit/lessons/smiley), [flashing heart](/microbit/lessons/flashing-heart), [magic logo](/microbit/lessons/magic-logo)
|
||||
|
||||
### See also
|
||||
|
||||
[create image](/microbit/reference/images/create-image), [show animation](/microbit/reference/basic/show-animation), [image](/microbit/reference/image/image), [show image](/microbit/reference/images/show-image), [scroll image](/microbit/reference/images/scroll-image)
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
# Scroll Image
|
||||
|
||||
The scroll image function. #scrollimage #image #docs
|
||||
|
||||
Scrolls the frames within an [Image](/microbit/reference/image/image) on the [LED screen](/microbit/device/screen).
|
||||
|
||||
### Block Editor
|
||||
|
||||

|
||||
|
||||
### KindScript
|
||||
|
||||
```
|
||||
export function scrollImage(_this: micro_bit.Image, xOffsetPerStep: number, interval: number)
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
* x offset per step : [Number](/microbit/reference/types/number) - the number of columns to scroll at a time (horizontal offset). Use a positive number to scroll an image to the right and a negative number to scroll left. To jump from one image frame to the next, use an offset of 5 or -5.
|
||||
* interval (ms) : [Number](/microbit/reference/types/number) - the time (in milliseconds) before scrolling by `x offset per step`; the larger the number, the slower the scroll.
|
||||
|
||||
### ~hide
|
||||
|
||||
```
|
||||
let img = images.createImage(`
|
||||
. . # . . . # # # . . # # # .
|
||||
. . # . . . . . # . . . . # .
|
||||
. . # . . . . # . . . # # # .
|
||||
. . # . . . # . . . . . . # .
|
||||
. . # . . . # # # . . # # # .
|
||||
`)
|
||||
```
|
||||
|
||||
### ~
|
||||
|
||||
To scroll an image 1 column at a time to the right:
|
||||
|
||||
```
|
||||
img.scrollImage(1, 1000)
|
||||
```
|
||||
|
||||
To scroll an image 5 columns at a time (skip from frame to frame):
|
||||
|
||||
```
|
||||
img.scrollImage(5, 1000)
|
||||
```
|
||||
|
||||
To scroll an image 1 column at a time to the left:
|
||||
|
||||
```
|
||||
img.scrollImage(-1, 500)
|
||||
```
|
||||
|
||||
### Example: scroll through frames
|
||||
|
||||
This example creates an image with 3 frames, then scrolls through the 3 frames:
|
||||
|
||||
```
|
||||
img = images.createImage(`
|
||||
. . # . . . # # # . . # # # .
|
||||
. . # . . . . . # . . . . # .
|
||||
. . # . . . . # . . . # # # .
|
||||
. . # . . . # . . . . . . # .
|
||||
. . # . . . # # # . . # # # .
|
||||
`)
|
||||
img.showImage(0)
|
||||
img.scrollImage(5, 1000)
|
||||
```
|
||||
|
||||
### See also
|
||||
|
||||
[show image](/microbit/reference/images/show-image), [image](/microbit/reference/image/image), [create image](/microbit/reference/images/create-image), [show animation](/microbit/reference/basic/show-animation)
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
# Set Pixel
|
||||
|
||||
The set pixel function. #set pixel #image #docs
|
||||
|
||||
Set the on/off state of pixel in an [Image](/microbit/reference/image/image).
|
||||
|
||||
### KindScript
|
||||
|
||||
```
|
||||
export function setPixel(_this: micro_bit.Image, x: number, y: number, value: boolean)
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
* x - [Number](/microbit/reference/types/number); the *x coordinate* or horizontal position of a pixel in an [image](/microbit/reference/image/image)
|
||||
* x - [Number](/microbit/reference/types/number); the *y coordinate* or vertical position of a pixel in an [image](/microbit/reference/image/image)
|
||||
* value -[Boolean](/microbit/reference/types/boolean); the on/off state of a pixel; `true` for on, `false` for off
|
||||
|
||||
### x, y coordinates?
|
||||
|
||||
To figure out the ``x``, ``y`` coordinates, see [LED screen](/microbit/device/screen).
|
||||
|
||||
### Example
|
||||
|
||||
The following example creates an image and stores it in the `img` variable. The `set pixel` function sets the centre pixel off, before `img` is shown using `show image`.
|
||||
|
||||
```
|
||||
let img = images.createImage(`
|
||||
. . # . .
|
||||
. # . # .
|
||||
. . # . .
|
||||
. # . # .
|
||||
. . # . .
|
||||
`)
|
||||
img.setPixel(2, 2, false)
|
||||
img.showImage(0)
|
||||
```
|
||||
|
||||
### See also
|
||||
|
||||
[pixel](/microbit/reference/images/pixel), [show image](/microbit/reference/images/show-image), [image](/microbit/reference/image/image), [create image](/microbit/reference/images/create-image), [scroll image](/microbit/reference/images/scroll-image)
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
# Show Frame
|
||||
|
||||
The show frame function. #showframe #docs #image #screen #LED
|
||||
|
||||
Display an [Image](/microbit/reference/image/image) on the BBC micro:bit's [LED screen](/microbit/device/screen)
|
||||
|
||||
### KindScript
|
||||
|
||||
```
|
||||
export function showFrame(img: micro_bit.Image, frame: number)
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
* index - [Number](/microbit/reference/types/number); which frame of the image to display
|
||||
|
||||
### Difference from `plot frame`
|
||||
|
||||
The `show frame` function is the same as [plot frame](/microbit/reference/image/plot-frame), but contains a built-in delay after the LED screen has been updated (whereas `plot frame` has no built-in delay)
|
||||
|
||||
### Example
|
||||
|
||||
```
|
||||
let img = images.createImage(`
|
||||
# . . . # # . . . #
|
||||
. # . # . . # # # .
|
||||
. . # . . . # # # .
|
||||
. # . # . . # # # .
|
||||
# . . . # # . . . #
|
||||
`)
|
||||
img.showFrame(1)
|
||||
```
|
||||
|
||||
### Lessons
|
||||
|
||||
[smiley](/microbit/lessons/smiley), [flashing heart](/microbit/lessons/flashing-heart), [magic logo](/microbit/lessons/magic-logo)
|
||||
|
||||
### See also
|
||||
|
||||
[create image](/microbit/reference/images/create-image), [show animation](/microbit/reference/basic/show-animation), [image](/microbit/reference/image/image), [show image](/microbit/reference/images/show-image), [scroll image](/microbit/reference/images/scroll-image)
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
# Show Image
|
||||
|
||||
The show image function. #showimage #docs #image #screen #LED
|
||||
|
||||
Show an [Image](/microbit/reference/image/image) on the [LED screen](/microbit/device/screen), followed by a 400ms pause.
|
||||
|
||||
### Block Editor
|
||||
|
||||

|
||||
|
||||
### KindScript
|
||||
|
||||
```
|
||||
export function showImage(_this: micro_bit.Image, xOffset: number)
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
* x offset - [Number](/microbit/reference/types/number); the horizontal starting point of an image; use 0 for the first frame of the image, 5 for the second frame of the image, 10 for the third frame and so on.
|
||||
|
||||
### Create image and show image
|
||||
|
||||
Use the [image editor](/microbit/reference/image/image) to create images using the [create image](/microbit/reference/image/create-image) function, and then use `show image` like this:
|
||||
|
||||
```
|
||||
let img = images.createImage(`
|
||||
. . # . .
|
||||
. # . # .
|
||||
. . # . .
|
||||
. # . # .
|
||||
. . # . .
|
||||
`)
|
||||
img.showImage(0)
|
||||
```
|
||||
|
||||
### Example: display numbers 1-5
|
||||
|
||||
The following example creates an image with 5 frames and then uses a [for loop](/microbit/reference/loops/for) to show each frame on the screen:
|
||||
|
||||
```
|
||||
let img2 = images.createImage(`
|
||||
. . # . . . # # # # . # # # . . . . # . . # # # .
|
||||
. # # . . . . . . # . . . # . . . # # . . # . . .
|
||||
. . # . . . . . # . . . # . . . # # # # . # # # .
|
||||
. . # . . . . # . . . . . # . . . . # . . . . # .
|
||||
. . # . . . # # # # . # # # . . . . # . . # # # .
|
||||
`)
|
||||
for (let i = 0; i < 5; i++) {
|
||||
img2.showImage(i * 5)
|
||||
basic.pause(1000)
|
||||
}
|
||||
```
|
||||
|
||||
### Lessons
|
||||
|
||||
[rock paper scissors](/microbit/lessons/rock-paper-scissors), [digital pet](/microbit/lessons/digital-pet), [offset-image](/microbit/lessons/offset-image)
|
||||
|
||||
### See also
|
||||
|
||||
[show animation](/microbit/reference/basic/show-animation), [image](/microbit/reference/image/image), [create image](/microbit/reference/images/create-image), [scroll image](/microbit/reference/images/scroll-image)
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
# Width
|
||||
|
||||
The width function. #width #image #docs
|
||||
|
||||
Get the width of an [Image](/microbit/reference/image/image) in columns.
|
||||
|
||||
### KindScript
|
||||
|
||||
```
|
||||
export function width(_this: micro_bit.Image) : number
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
* none
|
||||
|
||||
### Returns
|
||||
|
||||
* [Number](/microbit/reference/types/number) - the number of columns in a image. This function returns 5 if the image has 1 frame, 10 for 2 frames, 15 for 3 frames and so on. Divide the number of columns by 5 to find out how many frames an image has (see example below).
|
||||
|
||||
The following example gets the width of `img` and stores it in the `w` variable:
|
||||
|
||||
### ~hide
|
||||
|
||||
```
|
||||
let img = images.createImage(`
|
||||
. . # . . . . . . .
|
||||
. # . # . . . # . .
|
||||
. . # . . . . . . .
|
||||
. # . # . . . # . .
|
||||
. . # . . . . . . .
|
||||
`)
|
||||
```
|
||||
|
||||
### ~
|
||||
|
||||
```
|
||||
let w = img.width()
|
||||
```
|
||||
|
||||
### Example: show each frame
|
||||
|
||||
The following example uses the `width` function with a [for](/microbit/reference/loops/for) loop to show each image frame on the screen:
|
||||
|
||||
```
|
||||
let img2 = images.createImage(`
|
||||
. . # . . . # # # # . # # # .
|
||||
. # # . . . . . . # . . . # .
|
||||
. . # . . . . . # . . . # . .
|
||||
. . # . . . . # . . . . . # .
|
||||
. . # . . . # # # # . # # # .
|
||||
`)
|
||||
for (let i = 0; i < img2.width() / 5; i++) {
|
||||
img2.showImage(i * 5)
|
||||
basic.pause(1000)
|
||||
}
|
||||
```
|
||||
|
||||
### See also
|
||||
|
||||
[show image](/microbit/reference/images/show-image), [image](/microbit/reference/image/image), [create image](/microbit/reference/images/create-image), [scroll image](/microbit/reference/images/scroll-image), [show animation](/microbit/reference/basic/show-animation)
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
# Acceleration
|
||||
|
||||
Get the acceleration value (milli g-force), in one of three specified dimensions.
|
||||
|
||||
```sig
|
||||
input.acceleration(Dimension.X);
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
* dimension : [String](/microbit/reference/types/string) - one of three values specifying the axis of acceleration: ``x`` (left/right); ``y`` (forward/backwards); ``z`` (up/down)
|
||||
|
||||
### Returns
|
||||
|
||||
* [Number](/microbit/reference/types/number) - acceleration, in milli-gravities. When the micro:bit is laying flat with the screen up, x=0, y=0 and z=-1023.
|
||||
|
||||
### Example: bar chart
|
||||
|
||||
Use the ``plot bar chart`` to visual the acceleration on the LED screen.
|
||||
|
||||
```blocks
|
||||
basic.forever(() => {
|
||||
led.plotBarGraph(input.acceleration("x"), 1023)
|
||||
})
|
||||
```
|
||||
|
||||
### Example: micro:bit leveller
|
||||
|
||||
The following example uses the `acceleration` and the `plot` function to help you move the micro:bit until it's level (the centre LED is *on* when the device is level). When running this code in a web browser, move your mouse to simulate the accelerometer.
|
||||
|
||||
```blocks
|
||||
basic.forever(() => {
|
||||
let ax = input.acceleration(Dimension.X)
|
||||
let x = pins.map(-1023, 1023, 0, 4, ax)
|
||||
let ay = input.acceleration("y")
|
||||
let y = pins.map(-1023, 1023, 0, 4, ay)
|
||||
basic.clearScreen()
|
||||
led.plot(x, y)
|
||||
})
|
||||
```
|
||||
|
||||
### Lessons
|
||||
|
||||
[zoomer](/microbit/lessons/zoomer)
|
||||
|
||||
### See also
|
||||
|
||||
[compass-heading](/microbit/input/compass-heading), [lightlevel](/microbit/input/lightlevel)
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
# Button Is Pressed
|
||||
|
||||
Get the state of an input button. The micro:bit has two input buttons: A and B.
|
||||
|
||||
```sig
|
||||
input.buttonIsPressed(Button.A);
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
* name - [String](/microbit/reference/types/string); input button "A", "B", or "A+B" (both input buttons)
|
||||
|
||||
### Returns
|
||||
|
||||
* [Boolean](/microbit/reference/types/boolean) - `true` if pressed, `false` if not pressed
|
||||
|
||||
### Example
|
||||
|
||||
The following code uses an [if](/microbit/reference/logic/if) statement to run code, depending on whether or not the A button is pressed:
|
||||
|
||||
```blocks
|
||||
basic.forever(() => {
|
||||
let pressed = input.buttonIsPressed(Button.A)
|
||||
if (pressed) {
|
||||
// this code runs if the A button is pressed
|
||||
basic.showNumber(1, 150)
|
||||
} else {
|
||||
// this code runs if the A button is *not* pressed
|
||||
basic.showNumber(0, 150)
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### Lessons
|
||||
|
||||
[zoomer](/microbit/lessons/zoomer)
|
||||
|
||||
### See also
|
||||
|
||||
[on button pressed](/microbit/input/on-button-pressed), [if](/microbit/reference/logic/if), [forever](/microbit/basic/forever)
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
# Compass Heading
|
||||
|
||||
Get the compass heading of the micro:bit in degrees. Your micro:bit has a built-in **magnetometer** so it can your direction with respect to the North Magnetic Pole.
|
||||
|
||||
```sig
|
||||
input.compassHeading();
|
||||
```
|
||||
|
||||
### Returns
|
||||
|
||||
* [Number](/microbit/reference/types/number) - the heading in degrees (0 to 360 degrees). If the compass is calibrating, it returns ``-1003``.
|
||||
|
||||
## Simulator
|
||||
|
||||
Calibration does not work on the simulator.
|
||||
|
||||
### Example
|
||||
|
||||
The following code gets the compass heading and stores it in the `degrees` variable:
|
||||
|
||||
```blocks
|
||||
let degrees = input.compassHeading()
|
||||
```
|
||||
|
||||
### ~hint
|
||||
|
||||
When running code with this function in a web browser, click and drag the on-screen compass needle to change heading.
|
||||
|
||||
### ~
|
||||
|
||||
### Example: compass
|
||||
|
||||
The following example gets the `compass heading` and then displays a letter depending on the value of `degrees`: N for north, E for East, S for South, and W for West.
|
||||
|
||||
```blocks
|
||||
basic.forever(() => {
|
||||
let degrees = input.compassHeading()
|
||||
if (degrees < 45)
|
||||
basic.showString("N")
|
||||
else if (degrees < 135)
|
||||
basic.showString("E")
|
||||
else if (degrees < 225)
|
||||
basic.showString("S")
|
||||
else basic.showString("W")
|
||||
})
|
||||
```
|
||||
|
||||
### Calibration
|
||||
|
||||
On the first use of the compass, the **calibration** procedure will automatically start. The user must draw a circle with the device until it is fully calibrated.
|
||||
|
||||
An enclosure made from metal, or using in proximity of metal objects, might affect the accuracy of the reading and calibration.
|
||||
|
||||
During calibration, ``compass heading`` returns ``-1003``.
|
||||
|
||||
### Lessons
|
||||
|
||||
[compass](/microbit/lessons/compass)
|
||||
|
||||
### See also
|
||||
|
||||
[acceleration](/microbit/reference/input/acceleration)
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
# Light Level
|
||||
|
||||
Gets the light level from ``0`` (dark) to ``255`` (bright). The light is measured by using various LEDs from the screen.
|
||||
|
||||
This function will return ``0`` on the first call to this method, a light reading will be available after the display has activated the light sensor for the first time.
|
||||
|
||||
```sig
|
||||
input.lightLevel();
|
||||
```
|
||||
|
||||
### Returns
|
||||
|
||||
* [Number](/microbit/reference/types/number) - light level from ``0`` (dark) to ``255`` (bright).
|
||||
|
||||
### Example: chart light level
|
||||
|
||||
Use `plot bar chart` to visual the influence of various light source on the light level.
|
||||
|
||||
```blocks
|
||||
basic.forever(() => {
|
||||
led.plotBarGraph(input.lightLevel(), 255)
|
||||
})
|
||||
```
|
||||
|
||||
### Lessons
|
||||
|
||||
### See also
|
||||
|
||||
[acceleration](/microbit/reference/input/acceleration), [compass-heading](/microbit/input/compass-heading)
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
# Magnetic Force
|
||||
|
||||
Get the magnetic force (micro Teslas), in one of three specified dimensions.
|
||||
|
||||
```sig
|
||||
input.magneticForce(Dimension.X);
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
* dimension : [String](/microbit/reference/types/string) - one of three values specifying the axis of the force: ``x`` (left/right); ``y`` (forward/backwards); ``z`` (up/down); ``strength`` (the length of the vector)
|
||||
|
||||
### Returns
|
||||
|
||||
* [Number](/microbit/reference/types/number) - magnetic force, in micro-Teslas.
|
||||
|
||||
### Example: metal detector
|
||||
|
||||
The following example uses the `magnetic force` to control the brightness of the screen. When the magnetic force increases, the center LED will appear brighter.
|
||||
|
||||
```blocks
|
||||
led.plot(2, 2)
|
||||
basic.forever(() => {
|
||||
let f = input.magneticForce(Dimension.X)
|
||||
led.setBrightness(f / 2000)
|
||||
})
|
||||
```
|
||||
|
||||
### See also
|
||||
|
||||
[compass heading](/microbit/input/compass-heading)
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
# On Button Pressed
|
||||
|
||||
Register an [event handler](/microbit/reference/event-handler) that will execute whenever an input button (A, B, or A and B together) is pressed during program execution. When [running code](/microbit/js/simulator) with this function in a web browser, click an on-screen input button - labelled A or B.
|
||||
|
||||
```sig
|
||||
input.onButtonPressed(Button.A, () => {})
|
||||
```
|
||||
|
||||
### Example: count button clicks
|
||||
|
||||
This example counts how many times the left or right input button is pressed. Each time a button is pressed, the global count variable is increased by 1 and displayed on the screen.
|
||||
|
||||
```blocks
|
||||
let count = 0
|
||||
basic.showNumber(count)
|
||||
input.onButtonPressed(Button.A, () => {
|
||||
count++;
|
||||
basic.showNumber(count);
|
||||
})
|
||||
```
|
||||
|
||||
### Example: roll a dice
|
||||
|
||||
This example generates a random number when you press the B input button, and then displays a random die image:
|
||||
|
||||
```blocks
|
||||
input.onButtonPressed(Button.B, () => {
|
||||
let dice = Math.random(6)
|
||||
basic.showNumber(dice)
|
||||
})
|
||||
```
|
||||
|
||||
### Lessons
|
||||
|
||||
[smiley](/microbit/lessons/smiley), [answering machine](/microbit/lessons/answering-machine), [screen wipe](/microbit/lessons/screen-wipe), [rotation animation](/microbit/lessons/rotation-animation)
|
||||
|
||||
### See also
|
||||
|
||||
[button is pressed](/microbit/reference/input/button-is-pressed), [forever](/microbit/reference/basic/forever)
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
# On Gesture
|
||||
|
||||
Register an [event handler](/microbit/reference/event-handler) that will execute whenever the user executes a gesture withthe BBC micro:bit.
|
||||
|
||||
```sig
|
||||
input.onGesture(Gesture.Shake,() => {
|
||||
})
|
||||
```
|
||||
|
||||
## Gestures
|
||||
|
||||
|
||||
|
||||
### Example: random number
|
||||
|
||||
The following example displays a number from 0-9 on the screen when you shake the BBC micro:bit.
|
||||
|
||||
```blocks
|
||||
input.onGesture(Gesture.Shake,() => {
|
||||
let x = Math.random(10)
|
||||
basic.showNumber(x, 100)
|
||||
})
|
||||
```
|
||||
|
||||
### Example: rock, paper, scissors
|
||||
|
||||
The following example shows one of three images (rock, paper, or scissors) when you shake the BBC micro:bit.
|
||||
|
||||
```blocks
|
||||
input.onGesture(Gesture.Shake,() => {
|
||||
let img = images.createImage(`
|
||||
. . . . . # # # # # . . . . #
|
||||
. # # # . # . . . # # # . # .
|
||||
. # # # . # . . . # . # # . .
|
||||
. # # # . # . . . # # # . # .
|
||||
. . . . . # # # # # . . . . #
|
||||
`)
|
||||
img.showFrame(Math.random(3))
|
||||
})
|
||||
```
|
||||
|
||||
### Lessons
|
||||
|
||||
[bounce image](/microbit/lessons/bounce-image), [rock paper scissors](/microbit/lessons/rock-paper-scissors)
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
# On Pin Pressed
|
||||
|
||||
Register an [event handler](/microbit/reference/event-handler) that will execute whenever the user holds the `GND` pin with one hand, and presses pin `0`, `1`, or `2` with the other hand, thus completing a circuit; when you run a script with this function in a web browser, click pins 0 , 1, or 2 on the simulator.
|
||||
|
||||
*Note* that this function works best when the BBC micro:bit is powered by AAA battery.
|
||||
|
||||
```sig
|
||||
input.onPinPressed(TouchPin.P0, () => {
|
||||
})
|
||||
```
|
||||
|
||||
### Example: pin pressed counter
|
||||
|
||||
This example counts how many times the P0 pin is pressed. Each time the pin is pressed, the global count variable is increased by 1 and displayed on the screen.
|
||||
|
||||
```blocks
|
||||
let count = 0
|
||||
basic.showNumber(count, 100)
|
||||
input.onPinPressed(TouchPin.P0, () => {
|
||||
count = count + 1
|
||||
basic.showNumber(count, 100)
|
||||
})
|
||||
```
|
||||
|
||||
### Lessons
|
||||
|
||||
[love meter](/microbit/lessons/love-meter)
|
||||
|
||||
### See also
|
||||
|
||||
[BBC micro:bit pins](/microbit/device/pins), [pin is pressed](/microbit/input/pin-is-pressed), [analog read pin](/microbit/pins/analog-read-pin), [analog write pin](/microbit/pins/analog-write-pin), [digital read pin](/microbit/pins/digital-read-pin), [digital write pin](/microbit/pins/digital-write-pin)
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
# Pin Is Pressed
|
||||
|
||||
Gets the pin state (pressed or not pressed), by detecting when the user holds the `GND` pin with one hand, and presses pin `0`, `1`, or `2` with the other hand, thus completing a circuit.
|
||||
|
||||
*Note* that this function works best when the BBC micro:bit is powered by AAA battery.
|
||||
|
||||
```sig
|
||||
input.pinIsPressed(TouchPin.P0);
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
* name - [String](/microbit/reference/types/string); the pin name ("P0", "P1", or "P2")
|
||||
|
||||
### returns
|
||||
|
||||
* [Boolean](/microbit/reference/types/boolean) - `true` if pressed, `false` if not pressed
|
||||
|
||||
### Example
|
||||
|
||||
This example displays 1 if P0 is pressed, and 0 if P0 is not pressed:
|
||||
|
||||
```blocks
|
||||
basic.forever(() => {
|
||||
if (input.pinIsPressed(TouchPin.P0)) {
|
||||
basic.showNumber(1, 150)
|
||||
} else {
|
||||
basic.showNumber(0, 150)
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### See also
|
||||
|
||||
[BBC micro:bit pins](/microbit/device/pins), [on pin pressed](/microbit/input/on-pin-pressed), [analog read pin](/microbit/reference/pins/analog-read-pin), [analog write pin](/microbit/reference/pins/analog-write-pin), [digital read pin](/microbit/reference/pins/digital-read-pin), [digital write pin](/microbit/reference/pins/digital-write-pin)
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
# Rotation
|
||||
|
||||
Get a rotation angle in degrees inferred from the accelerometer readings.
|
||||
|
||||
```sig
|
||||
input.rotation(Rotation.Roll);
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
* kind: [String](/microbit/reference/types/string) - one of values specifying the kind of rotation: ``pitch`` (up/down around the ``x`` axis); ``roll`` (left/right around the ``y`` axis)
|
||||
|
||||
### Returns
|
||||
|
||||
* [Number](/microbit/reference/types/number) - angle, in degrees.
|
||||
|
||||
### Example: micro:bit leveller
|
||||
|
||||
The following example uses the `rotation` and the `plot leds` function to help you move the BBC micro:bit until it's level: when it is level, a smiley shows up on the screen. When running this code in a web browser, move your mouse to simulate the rotation.
|
||||
|
||||
```sig
|
||||
basic.forever(() => {
|
||||
let pitch = input.rotation(Rotation.Pitch)
|
||||
let roll = input.rotation(Rotation.Roll)
|
||||
if (Math.abs(pitch) < 10 && Math.abs(roll) < 10) {
|
||||
basic.plotLeds(`
|
||||
. . . . .
|
||||
. # . # .
|
||||
. . . . .
|
||||
# . . . #
|
||||
. # # # .
|
||||
`)
|
||||
} else {
|
||||
basic.plotLeds(`
|
||||
# . . . #
|
||||
. # . # .
|
||||
. . # . .
|
||||
. # . # .
|
||||
# . . . #
|
||||
`)
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### See also
|
||||
|
||||
[acceleration](/microbit/reference/input/acceleration), [compass-heading](/microbit/reference/input/compass-heading)
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
# Running Time
|
||||
|
||||
Get the number of milliseconds elapsed since the script began. 1,000 milliseconds = 1 second.
|
||||
|
||||
```sig
|
||||
input.runningTime();
|
||||
```
|
||||
|
||||
### Returns
|
||||
|
||||
* [Number](/microbit/reference/types/number)
|
||||
|
||||
### Example: elapsed time
|
||||
|
||||
This code gets the elapsed time since the start of the program execution and displays it on the screen.
|
||||
|
||||
```blocks
|
||||
let now = input.runningTime()
|
||||
basic.showNumber(now)
|
||||
```
|
||||
|
||||
### Lessons
|
||||
|
||||
[speed button](/microbit/lessons/speed-button)
|
||||
|
||||
### See also
|
||||
|
||||
[show number](/microbit/reference/basic/show-number), [pause](/microbit/reference/basic/pause)
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
# Temperature
|
||||
|
||||
Get the ambient temperature (degree Celsius °C). The temperature is inferred from the the surface temperature of the various chips on the micro:bit.
|
||||
|
||||
```sig
|
||||
input.temperature();
|
||||
```
|
||||
|
||||
### Returns
|
||||
|
||||
* [Number](/microbit/reference/types/number) - temperature in degree Celsius °C.
|
||||
|
||||
### How does it work?
|
||||
|
||||
The BBC micro:bit does not have a dedicated temperature sensor. Instead, the temperature provided is actually the temperature of the silicon die on the main CPU. As the processor generally runs cold though (it is a high efficiency ARM core), the temperature is a good approximation of the ambient temperature... you might warm up if you give the processor a lot of work to do though, and don't [sleep](/microbit/reference/basic/pause)!
|
||||
|
||||
The temperature sensor has a high precision, but isn't trimmed for accuracy. In other words, it can sense changes in temperature very well, but there may be (and probably is) base line offset. i.e. it might return 20 degrees when it's actually 17, but it would return 21 when it is 18 etc.
|
||||
|
||||
### Example: micro:bit thermometer
|
||||
|
||||
The following example uses the `temperature` and the `show number` to display the room temperature.
|
||||
|
||||
```sig
|
||||
basic.forever(() => {
|
||||
let temp = input.temperature()
|
||||
basic.showNumber(temp)
|
||||
})
|
||||
```
|
||||
|
||||
### Lessons
|
||||
|
||||
### See also
|
||||
|
||||
[compass-heading](/microbit/reference/input/compass-heading), [acceleration](/microbit/reference/input/acceleration)
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
# Antenna Library
|
||||
|
||||
The events library #docs
|
||||
|
||||
The functions in the ``antenna`` namespace allow the BBC micro:bit to communicate with a separate (remote) device, such as a smartphone, over Bluetooth (Smart). The set of supported events will depend on the remote device and the BBC micro:bit apps available for the remote device.
|
||||
|
||||
### Remote control
|
||||
|
||||
Control the presentation of media content available on a remote device using the `tell remote control to` function
|
||||
|
||||
```
|
||||
export function tellRemoteControlTo(event: string)
|
||||
```
|
||||
|
||||
The remote control specific events include:
|
||||
|
||||
* play
|
||||
* pause
|
||||
* stop
|
||||
* next track
|
||||
* previous track
|
||||
* forward
|
||||
* rewind
|
||||
* volume up
|
||||
* volume down
|
||||
|
||||
### Camera
|
||||
|
||||
Access the photo/video-taking functionality of a remote device using the *camera* function:
|
||||
|
||||
```
|
||||
export function tellCameraTo(event: string)
|
||||
```
|
||||
|
||||
The camera-specific events include:
|
||||
|
||||
* toggle front-rear
|
||||
* launch photo mode
|
||||
* take photo
|
||||
* stop photo mode
|
||||
* launch video mode
|
||||
* start video capture
|
||||
* stop video capture
|
||||
* stop video mode
|
||||
|
||||
### Alert
|
||||
|
||||
Raise an alert on a remote device using the `raise alert to` function
|
||||
|
||||
```
|
||||
export function raiseAlertTo(event: string)
|
||||
```
|
||||
|
||||
The set of alerting-specific events include:
|
||||
|
||||
* display toast
|
||||
* vibrate
|
||||
* play sound
|
||||
* play ringtone
|
||||
* find my phone
|
||||
* alarm 1
|
||||
* alarm 2
|
||||
* alarm 3
|
||||
* alarm 4
|
||||
* alarm 5
|
||||
* alarm 6
|
||||
|
||||
### Microphone
|
||||
|
||||
Access the audio recording capabilities of the device using the `tell microphone to` function
|
||||
|
||||
```
|
||||
export function tellMicrophoneTo(event: string)
|
||||
```
|
||||
|
||||
The set of audio recorder events include:
|
||||
|
||||
* launch
|
||||
* start capture
|
||||
* end capture
|
||||
* stop
|
||||
@@ -0,0 +1,54 @@
|
||||
# Assignment Operator
|
||||
|
||||
Set the value for local and global variables #docs #assignment #language #var #data
|
||||
|
||||
### @parent js/operators
|
||||
|
||||
|
||||
Set or change the value of a variable
|
||||
|
||||
### Block Editor
|
||||
|
||||

|
||||
|
||||
### Touch Develop
|
||||
|
||||
Use the assignment operator (:=) to set or change the value of a [local variable](/microbit/reference/variables/var) or a [global variable](/microbit/js/data).
|
||||
|
||||
### Declare a variable
|
||||
|
||||
Declare a new *local* variable using the [var](/microbit/reference/variables/var) statement and the assignment operator (`:=`). Like this:
|
||||
|
||||
```blocks
|
||||
let num1 = 7
|
||||
let name = "Joe"
|
||||
```
|
||||
|
||||
The variable's name is on the left of the assignment operator (`:=`) and the variable's value is on the right:
|
||||
|
||||
*variable name* `:=` *value*
|
||||
|
||||
See [global variable](/microbit/js/data) for info on declaring a global variable.
|
||||
|
||||
### Change a variable
|
||||
|
||||
After a global or local variable is defined, use the assignment operator (`:=`) to change the variable's value.
|
||||
|
||||
```
|
||||
g = 42
|
||||
num1 = 42
|
||||
```
|
||||
|
||||
### Notes
|
||||
|
||||
* Don't confuse the assignment operator `:=` with the equality operator `=`, which is used to compare values.
|
||||
* You can use the assignment operator `:=` with variables of each of the supported [types](/microbit/js/types).
|
||||
|
||||
### Lessons
|
||||
|
||||
[counter](/microbit/lessons/counter), [rotation animation](/microbit/lessons/rotation-animation), [digital pet](/microbit/lessons/digital-pet), [offset image](/microbit/lessons/offset-image)
|
||||
|
||||
### See also
|
||||
|
||||
[local variables](/microbit/reference/variables/var), [global variables](/microbit/js/data), [types](/microbit/js/types)
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
# Bits Library
|
||||
|
||||
Functions in the Bits library. #docs #bits #32bit #signed #unsigned
|
||||
|
||||
### @parent td/language
|
||||
|
||||
The binary numeral system represents numeric values using values 0 and 1. This is how almost all modern computers store data. Each 0 or 1 digit is called a binary digit, or bit for short.
|
||||
|
||||
The Bits library includes functions for bit-level manipulation of integers. In the [Touch Develop editor](/microbit/js/editor), click `bits` to see the following bit functions:
|
||||
|
||||
## Bitwise and, or, and xor functions
|
||||
|
||||
#### Syntax
|
||||
|
||||
bits `->` *and/or/xor* uint32 (x : [Number](/microbit/reference/types/number), y : [Number](/microbit/reference/types/number)) *returns* [Number](/microbit/reference/types/number)
|
||||
|
||||
#### Parameters
|
||||
|
||||
* x - an unsigned 32 bit integer [Number](/microbit/reference/types/number)
|
||||
* y - another unsigned 32 bit integer [Number](/microbit/reference/types/number)
|
||||
|
||||
### and uint32
|
||||
|
||||
performs bitwise AND; returns `1` at position i if both bits *x[i]* and *y[i]* are `1`, otherwise returns `0`.
|
||||
|
||||
### or uint32
|
||||
|
||||
performs bitwise OR; returns `1` at position *i* if either bit *x[i]* or *y[i]* is `1`, otherwise returns `0`.
|
||||
|
||||
### xor uint32
|
||||
|
||||
performs bitwise exclusive XOR; returns `1` at position *i* if *x[i]=1 and y[i]=0* or *x[i] = 0 and y[i] =1*; returns `0` otherwise
|
||||
|
||||
## Rotate left and rotate right
|
||||
|
||||
Rotate bits to the left or the right, by the specified number of positions.
|
||||
|
||||
#### Syntax
|
||||
|
||||
bits `->` rotate left unint32 (x : [Number](/microbit/reference/types/number), bits : [Number](/microbit/reference/types/number)) *returns* [Number](/microbit/reference/types/number)
|
||||
|
||||
bits `->` rotate right unint32 (x : [Number](/microbit/reference/types/number), bits : [Number](/microbit/reference/types/number)) *returns* [Number](/microbit/reference/types/number)
|
||||
|
||||
#### Parameters
|
||||
|
||||
* x - [Number](/microbit/reference/types/number);
|
||||
* bits - [Number](/microbit/reference/types/number);
|
||||
|
||||
## Shift left and shift right
|
||||
|
||||
Shift bits to the left or the right, by the specified number of positions.
|
||||
|
||||
#### Syntax
|
||||
|
||||
bits `->` shift left unint32 (x : [Number](/microbit/reference/types/number), bits : [Number](/microbit/reference/types/number)) *returns* [Number](/microbit/reference/types/number)
|
||||
|
||||
bits `->` shift right unint32 (x : [Number](/microbit/reference/types/number), bits : [Number](/microbit/reference/types/number)) *returns* [Number](/microbit/reference/types/number)
|
||||
|
||||
#### Parameters
|
||||
|
||||
* x - [Number](/microbit/reference/types/number);
|
||||
* bits - [Number](/microbit/reference/types/number);
|
||||
|
||||
### See also
|
||||
|
||||
[statements and operators](/microbit/js/statements), [math functions](/microbit/js/math), [Number](/microbit/reference/types/number)
|
||||
|
||||
@@ -0,0 +1,122 @@
|
||||
# Boolean
|
||||
|
||||
true or false.
|
||||
|
||||
### @parent js/language
|
||||
|
||||
A Boolean has one of two possible values: `true`; `false`. Boolean (logical) operators (*and*, *or*, *not*) take Boolean inputs and yields a Boolean value. Comparison operators on other types ([numbers](/microbit/reference/types/number), [strings](/microbit/reference/types/string)) yields a Boolean value.
|
||||
|
||||
### Block Editor
|
||||
|
||||
In the Block Editor, the following blocks represent the true and false Boolean values, which can be plugged in anywhere a Boolean value is expected:
|
||||
|
||||

|
||||
|
||||
The next three blocks represent the three Boolean (logic) operators:
|
||||
|
||||

|
||||
|
||||
The next six blocks represent comparison operators that yield a Boolean value. Most comparisons you will do involve [numbers](/microbit/reference/types/number):
|
||||
|
||||

|
||||
|
||||
### Touch Develop
|
||||
|
||||
### ~hide
|
||||
|
||||
```
|
||||
let condition = true
|
||||
let condition2 = true
|
||||
```
|
||||
|
||||
### ~
|
||||
|
||||
Boolean values and operators are often used with an [if](/microbit/reference/logic/if) or [while](/microbit/js/while) statement to determine which code will execute next. For example:
|
||||
|
||||
```
|
||||
if (condition && condition2) {
|
||||
// This code runs if both `condition` and `condition2` are `true`
|
||||
} else {
|
||||
// This code runs if either `condition` or `condition2` is `false`
|
||||
}
|
||||
```
|
||||
|
||||
### Functions that return a Boolean
|
||||
|
||||
Some functions return a Boolean value, which you can store in a Boolean variable. For example, the following code gets the on/off state of `point (1, 2)` and stores this in the Boolean variable named `on`. Then the code clears the screen if `on` is `true`:
|
||||
|
||||
```
|
||||
let on = led.point(1, 2)
|
||||
if (on) {
|
||||
basic.clearScreen()
|
||||
}
|
||||
```
|
||||
|
||||
### Boolean operators
|
||||
|
||||
Boolean operators take Boolean inputs and evaluate to a Boolean output:
|
||||
|
||||
### Conjunction: `A and B`
|
||||
|
||||
`A and B` evaluates to `true` if-and-only-if both A and B are true:
|
||||
|
||||
- `false and false` = `false`
|
||||
|
||||
- `false and true` = `false`
|
||||
|
||||
- `true and false` = `false`
|
||||
|
||||
- `true and true` = `true`
|
||||
|
||||
### Disjunction: `A or B`
|
||||
|
||||
`A or B` evaluates to `true` if-and-only-if either A is true or B is true:
|
||||
|
||||
- `false or false` = `false`
|
||||
|
||||
- `false or true` = `true`
|
||||
|
||||
- `true or false` = `true`
|
||||
|
||||
- `true or true` = `true`
|
||||
|
||||
### Negation: `not A`
|
||||
|
||||
`not A` evaluates to the opposite (negation) of A:
|
||||
|
||||
* `not false` = `true`
|
||||
* `not true` = `false`
|
||||
|
||||
### Example
|
||||
|
||||
This example turns on LED `3 , 3`, if LEDs `1 , 1` and `2 , 2` are both on:
|
||||
|
||||
```
|
||||
if (led.point(1, 1) && led.point(2, 2)) {
|
||||
led.plot(3, 3)
|
||||
}
|
||||
```
|
||||
|
||||
### Comparisons of numbers and strings
|
||||
|
||||
When you compare two Numbers, you get a Boolean value, such as the comparison `x < 5` in the code below:
|
||||
|
||||
```
|
||||
let x = Math.random(10)
|
||||
if (x < 5) {
|
||||
basic.showString("Low", 150)
|
||||
} else {
|
||||
basic.showString("High", 150)
|
||||
}
|
||||
```
|
||||
|
||||
See the documentation on [Numbers](/microbit/reference/types/number) for more information on comparing two Numbers. You can also [compare strings](/microbit/reference/types/string-functions) using the `equals` function.
|
||||
|
||||
### Lessons
|
||||
|
||||
[rotation animation](/microbit/lessons/rotation-animation), [digi yoyo](/microbit/lessons/digi-yoyo), [love meter](/microbit/lessons/love-meter), [zoomer](/microbit/lessons/zoomer)
|
||||
|
||||
### See also
|
||||
|
||||
[if](/microbit/reference/logic/if), [while](/microbit/js/while), [number](/microbit/reference/types/number)
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
# Break
|
||||
|
||||
Break statement; exit a for or while loop. #docs #break #language #loop #for #while
|
||||
|
||||
### @parent js/language
|
||||
|
||||
|
||||
Exit a [while](/microbit/js/while) or [for](/microbit/reference/loops/for) loop before the loop is complete.
|
||||
|
||||
### Touch Develop syntax
|
||||
|
||||
**break**
|
||||
|
||||
### Example: count to a random number
|
||||
|
||||
The following example counts from 0 to a random number from 0-9. When the for loop counter equals the random number (`i = x`), the `break` statement exits the loop:
|
||||
|
||||
```
|
||||
let x = Math.random(10)
|
||||
for (let i = 0; i < 10; i++) {
|
||||
if (i == x) {
|
||||
break
|
||||
} else {
|
||||
basic.showNumber(i, 0)
|
||||
basic.pause(500)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Lessons
|
||||
|
||||
[jailbreak](/microbit/lessons/jailbreak)
|
||||
|
||||
### See also
|
||||
|
||||
[for](/microbit/reference/loops/for), [while](/microbit/js/while)
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
# Call a Function
|
||||
|
||||
How to call a function in your code. #docs #function #call #language
|
||||
|
||||
### @parent js/language
|
||||
|
||||
|
||||
Type a function name in your code to call an existing [function](/microbit/js/function) in your script.
|
||||
|
||||
### Call a function
|
||||
|
||||
1. In the Touch Develop editor, click a line of code to open the on-screen [Code Keyboard](/microbit/js/editor).
|
||||
|
||||
2. Click `code` to see the functions in your script.
|
||||
|
||||
2. Click the function that you want to call.
|
||||
|
||||
3. Click `store in var` to store the return value in a variable.
|
||||
|
||||
### Example: the square function
|
||||
|
||||
Here's a function called `square`, with a [Number](/microbit/reference/types/number) input parameter:
|
||||
|
||||
```
|
||||
/**
|
||||
* // returns the square of the input parameter x
|
||||
* @param x TODO
|
||||
*/
|
||||
export function square(x: number) : number {
|
||||
let result: number
|
||||
return x * x
|
||||
return result
|
||||
}
|
||||
```
|
||||
|
||||
The following code calls the `square` function, passing it an input parameter (`x`), and storing the return value in the `result` variable:
|
||||
|
||||
### ~hide
|
||||
|
||||
```
|
||||
let x1 = 2
|
||||
```
|
||||
|
||||
### ~
|
||||
|
||||
```
|
||||
let result1 = square(x1)
|
||||
```
|
||||
|
||||
Or this code, which displays the result of the `square` function (without first storing the value in a variable):
|
||||
|
||||
```
|
||||
basic.showNumber(square(x1), 150)
|
||||
```
|
||||
|
||||
### See all your functions
|
||||
|
||||
To see a list of the functions in a script, open the script and then click `script` (in the upper-right corner). All of the functions appear under the **code** heading. Click on a function to open it in the editor.
|
||||
|
||||
### See also
|
||||
|
||||
[function parameters](/microbit/js/functionparameters), [create a function](/microbit/js/function), [return statement](/microbit/js/return)
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
# function
|
||||
|
||||
A function with inputs and outputs. #docs #function #language
|
||||
|
||||
### @parent js/language
|
||||
|
||||
|
||||
To add a **functions** to your script, click the `script` button, then click the `+` `add new function` button
|
||||
|
||||
### functions
|
||||
|
||||
A **function** takes [inputs](/microbit/actionparameters), runs code and (optionally) returns an output.
|
||||
|
||||
TouchDevelop functions are similar to `mathematical functions`. Consider the function that computes the square of `x`: `square(x) = x*x`. In code, it would look like this:
|
||||
|
||||
```
|
||||
export function square(x: number) : number {
|
||||
let result: number
|
||||
return x * x
|
||||
return result
|
||||
}
|
||||
```
|
||||
|
||||
### private function
|
||||
|
||||
An function can be marked as **private** in the properties. A private function is not visible outside a library (if the script is a library)
|
||||
|
||||
### documentation
|
||||
|
||||
The comment(s) at the beginning of a function used to provide a description of its purpose. This text will show in the help area when the function is called from the code editor. This is particularly useful for [libraries](/microbit/libraries).
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
# The Collections Library
|
||||
|
||||
#docs
|
||||
|
||||
A collection allows you to store an arbitrary number of elements. If you have a collection of numbers, you can add new numbers to the collection; remove numbers; check if a number is in there; iterate over all the numbers in the collection.
|
||||
|
||||
## Creation
|
||||
|
||||
A collection is created as follows. Note that we are providing the *type* of the elements that are meant to go in the collection.
|
||||
|
||||
```
|
||||
let c = (<number[]>[])
|
||||
```
|
||||
|
||||
At the moment, you can create collections of numbers, booleans, strings, and any of the object types that you defined.
|
||||
|
||||
**Important:** if your collection is a global variable, make sure you initialise it at the beginning of your script.
|
||||
|
||||
```
|
||||
c = (<boolean[]>[])
|
||||
```
|
||||
|
||||
Trying to use an uninitialised collection will crash the simulator, and display a sad face on the device.
|
||||
|
||||
## Adding and finding elements; counting
|
||||
|
||||
```
|
||||
c.push(3)
|
||||
```
|
||||
|
||||
The line above just added the number `3` to the collection. One can think of a collection as a list, where `add` appends the element at the end of the list.
|
||||
|
||||
At this stage, our collection has size `1`, meaning that the line below will display `1`.
|
||||
|
||||
```
|
||||
basic.showNumber(c.length, 150)
|
||||
```
|
||||
|
||||
We can add another number as follows.
|
||||
|
||||
```
|
||||
c.push(5)
|
||||
```
|
||||
|
||||
At this stage, the count of elements in the collection is `2`. We mentioned earlier that a collection is like a list: adding elements appends them at the end of the list. This means that, at this point in the program, the first element in the list is 3 (we added it earlier), and the second element in the list is 5 (we just added it).
|
||||
|
||||
```
|
||||
basic.showNumber(c[0], 150)
|
||||
```
|
||||
|
||||
Can you guess what the line above does? Remember that in computing, indexing starts at zero. This function takes the *first element* in the list. This means that the line above displays `3`.
|
||||
|
||||
We can ask questions such as: "what is the index of this element"? The line below displays `1`, meaning that the number `5` is first found at index `1` (it is the *second* element in the list).
|
||||
|
||||
```
|
||||
basic.showNumber(c.indexOf(5, 0), 150)
|
||||
```
|
||||
|
||||
## Iterating over the elements
|
||||
|
||||
A classic pattern consists in iterating over all the elements in the collection. Here's one way to do it:
|
||||
|
||||
```
|
||||
for (let i = 0; i < c.length; i++) {
|
||||
basic.showString("The element at index " + i.toString() + " is " + c[i].toString(), 150)
|
||||
}
|
||||
```
|
||||
|
||||
The code above will first print `The element at index 0 is 3`, then `The element at index 1 is 5`.
|
||||
|
||||
## Modifying and removing elements
|
||||
|
||||
One can modify an existing collection using `set at`, which changes the element *at a given index*.
|
||||
|
||||
```
|
||||
c[0] = 7
|
||||
```
|
||||
|
||||
The line above modifies the collection `c` so that, after the line above, the first element of the collection is now `7`.
|
||||
|
||||
Removing elements can be done in two different ways. We can remove the *first occurrence* of an element.
|
||||
|
||||
```
|
||||
c.remove(5)
|
||||
```
|
||||
|
||||
This removes the first occurrence of `5` in the list. At this point in the program, our list now has just one element (at index 0). If we wish, we can use `remove at` to remove the element at a given index.
|
||||
|
||||
```
|
||||
c.splice(0, 1)
|
||||
```
|
||||
|
||||
Now, the collection is empty.
|
||||
|
||||
## Complete example
|
||||
|
||||
This program will record the current acceleration measured on `x` when you press `A`; when you press `B`, the program will print out all the acceleration values that were measured, then will clear the collection.
|
||||
|
||||
```
|
||||
let accelerations = (<number[]>[])
|
||||
input.onButtonPressed("A", () => {
|
||||
accelerations.push(input.acceleration("x"))
|
||||
})
|
||||
input.onButtonPressed("B", () => {
|
||||
for (let i1 = 0; i1 < accelerations.length; i1++) {
|
||||
basic.showString(accelerations[i1].toString(), 150)
|
||||
}
|
||||
accelerations.clear()
|
||||
})
|
||||
```
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
# Comment
|
||||
|
||||
A note in code. #docs #comment #language
|
||||
|
||||
### @parent js/statement
|
||||
|
||||
|
||||
A comment is a line of code that contains text, usually an explanation or a note. All comments are ignored during script execution.
|
||||
|
||||
### Block
|
||||
|
||||
Right click on any block and add a comment
|
||||
|
||||
### Touch Develop syntax
|
||||
|
||||
To insert a comment in a Touch Develop script:
|
||||
|
||||
1. Click a line in your script.
|
||||
|
||||
2. Click `+`.
|
||||
|
||||
3. Click `// comment` and then type some text (your comment).
|
||||
|
||||
### ~hint
|
||||
|
||||
To find out how to insert comments using the Blocks editor, see [the Blocks editor](/microbit/blocks/editor).
|
||||
|
||||
### ~
|
||||
|
||||
### Sample function with comments
|
||||
|
||||
This function has comments that describe the purpose of the function:
|
||||
|
||||
```
|
||||
/**
|
||||
* // square function :
|
||||
* // returns the square of the input parameter x
|
||||
* @param x TODO
|
||||
*/
|
||||
export function square(x: number) : number {
|
||||
let result: number
|
||||
return x * x
|
||||
return result
|
||||
}
|
||||
```
|
||||
|
||||
### Formatting
|
||||
|
||||
Use [markdown syntax](/microbit/js/markdown) to format your comments (for example, **bold** and *italic* formatting).
|
||||
|
||||
### Commenting out code
|
||||
|
||||
During the debugging process, you may want to comment out a section of your code so that it doesn't run.
|
||||
|
||||
To comment out a block of code:
|
||||
|
||||
1. Click the first line of code that you want to comment out.
|
||||
|
||||
2. Press and hold the Shift key, and then press the Down arrow key to select a block of code.
|
||||
|
||||
3. In the block editing window, scroll down to **surround with** and click `comment out`. This adds an [if](/microbit/reference/logic/if) statement around your code, like this:
|
||||
|
||||
```
|
||||
if (false) {
|
||||
// the commented code here...
|
||||
}
|
||||
```
|
||||
|
||||
When you want to uncomment your code, click the `if false then` statement in your code, and then click `uncomment`.
|
||||
|
||||
### Library and function comments
|
||||
|
||||
* Use [comments](/microbit/js/comment) at the beginning of a library to describe the library
|
||||
* Use [comments](/microbit/js/comment) at the beginning of a [function](/microbit/js/function) to describe a function. The comment will appear in the help area of the Touch Develop editor when you insert the function
|
||||
|
||||
### See also
|
||||
|
||||
[markdown syntax](/microbit/js/markdown), [Touch Develop editor](/microbit/js/editor), [Block editor](/microbit/blocks/editor)
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
# In-browser compiler
|
||||
|
||||
The BBC micro:bit pins #docs #gnd #p1 #p2 #p3 #3V
|
||||
|
||||
## We listened to your feedback!
|
||||
|
||||
Following the feedback from teachers, the following improvements were made:
|
||||
|
||||
* compile without signing in
|
||||
* compile offline
|
||||
* save and load code using files
|
||||
|
||||
## A new in-browser compiler
|
||||
|
||||
The compilation from a script to ARM machine code is now done entirely in the browser (read the [in depth story](https://www.touchdevelop.com/docs/touch-develop-in-208-bits) about building the compiler). The new compiler is used by the Block Editor, Touch Develop and Code Kingdoms to create a .hex file solely within the confines of your web browser (no Internet connection is needed). The BBC micro:bit compilation process (see page 10 in the Quick Start book) has been updated below to reflect the new compiler architecture, shown below.
|
||||
|
||||

|
||||
|
||||
The C++ compiler now only is used to compile the micro:bit runtime - this is done offline by the micro:bit team and the precompiled runtime linked with your compiled script in the browser.
|
||||
|
||||
## Save and load code using files
|
||||
|
||||

|
||||
|
||||
The BBC micro:bit automatically saves and synchronizes scripts for signed in users through the cloud. Unfortunately, this scenario would not work always so we decided to also support files. Users are now able to import and export scripts as files. For example, they can simply email it or submit them in their classroom portal.
|
||||
|
||||

|
||||
|
||||
Compiled .hex files can also be imported back into the web site. This make it easy for a teacher to review the source of a script by simply drag and dropping the file into the editor.
|
||||
|
||||
@@ -0,0 +1,216 @@
|
||||
# Touch Develop Documentation
|
||||
|
||||
Touch Develop docs for the micro:bit #docs #contents #apis #functions #language #statements
|
||||
|
||||
### @section full
|
||||
|
||||
### @parent editor-documentation
|
||||
|
||||
### @short Touch Develop
|
||||
|
||||
### ~hint
|
||||
|
||||
**Spotty internet? No problem!** (1) When online, go to https://www.microbit.co.uk/app/ and bookmark this URL; (2) use the bookmark to reload the web app, even without the internet.
|
||||
|
||||
### ~
|
||||
|
||||
Welcome to the Touch Develop home page for the BBC micro:bit. Below you will find resources about the Touch Develop programming language and code editor. Good places to start include:
|
||||
|
||||
* [the Touch Develop Editor](/microbit/js/editor)
|
||||
* [30+ BBC micro:bit lessons](/microbit/lessonss)
|
||||
* [offline support](/microbit/offline)
|
||||
|
||||
### ~column
|
||||
|
||||
## Language {#pconst}
|
||||
|
||||
### Variables
|
||||
|
||||
* [local variables](/microbit/reference/variables/var)
|
||||
* [global variables ](/microbit/js/data)
|
||||
|
||||
### Types
|
||||
|
||||
* [Number](/microbit/reference/types/number)
|
||||
* [Boolean](/microbit/reference/types/boolean)
|
||||
* [String](/microbit/reference/types/string)
|
||||
* [Image](/microbit/reference/image/image)
|
||||
|
||||
### Statements and control structures
|
||||
|
||||
* [assignment operator](/microbit/reference/variables/assign) `:=`
|
||||
* [if](/microbit/reference/logic/if)
|
||||
* [for](/microbit/reference/loops/for)
|
||||
* [while](/microbit/js/while)
|
||||
* [break](/microbit/js/break)
|
||||
* [forever](/microbit/reference/basic/forever)
|
||||
* [in background](/microbit/reference/control/in-background)
|
||||
* [function](/microbit/js/function)
|
||||
* [return](/microbit/js/return)
|
||||
|
||||
### Maths
|
||||
|
||||
* arithmetic operators (`+`, `-`, `*`, `/`, mod) on [Numbers](/microbit/reference/types/number)
|
||||
* comparison operators (such as `>`, `=`) on [Numbers](/microbit/reference/types/number)
|
||||
* the [math](/microbit/js/math) library
|
||||
* the [bits](/microbit/js/bits) library
|
||||
|
||||
### Logical
|
||||
|
||||
* [Boolean](/microbit/reference/types/boolean) values `true` and `false`
|
||||
* Operations (`not`, `or`, `and`) on [Booleans](/microbit/reference/types/boolean)
|
||||
|
||||
### Strings
|
||||
|
||||
* [string functions](/microbit/reference/types/string-functions)
|
||||
|
||||
### Functions
|
||||
|
||||
* [create a function](/microbit/js/function)
|
||||
* [function parameters](/microbit/js/functionparameters)
|
||||
* [call a function](/microbit/js/call)
|
||||
|
||||
### Collections
|
||||
|
||||
* read the [collections tutorial](/microbit/js/collections)
|
||||
|
||||
### Custom object types
|
||||
|
||||
* see the [object types tutorial](/microbit/js/object-types)
|
||||
* read the [object disclaimer](/microbit/js/object-disclaimer) if you're an advanced user
|
||||
|
||||
### Libraries
|
||||
|
||||
* [create and use libraries](/microbit/js/libraries)
|
||||
|
||||
### Documentation
|
||||
|
||||
* [comments](/microbit/js/comment)
|
||||
* [markdown syntax](/microbit/js/markdown)
|
||||
|
||||
### ~
|
||||
|
||||
### ~column
|
||||
|
||||
## BBC micro:bit functions
|
||||
|
||||
### Basic
|
||||
|
||||
* [clear screen](/microbit/reference/basic/clear-screen)
|
||||
* [forever](/microbit/reference/basic/forever)
|
||||
* [pause](/microbit/reference/basic/pause)
|
||||
* [show leds](/microbit/reference/basic/show-leds)
|
||||
* [show animation](/microbit/reference/basic/show-animation)
|
||||
* [show number](/microbit/reference/basic/show-number)
|
||||
* [show string](/microbit/reference/basic/show-string)
|
||||
|
||||
### LED
|
||||
|
||||
* [brightness](/microbit/reference/led/brightness)
|
||||
* [fade in](/microbit/reference/led/fade-in)
|
||||
* [fade out](/microbit/reference/led/fade-out)
|
||||
* [plot](/microbit/reference/led/plot)
|
||||
* [plot all](/microbit/reference/led/plot-all)
|
||||
* [point](/microbit/reference/led/point)
|
||||
* [screenshot](/microbit/functions/screenshot)
|
||||
* [set display mode](/microbit/functions/set-display-mode)
|
||||
* [set brightness](/microbit/reference/led/set-brightness)
|
||||
* [stop animation](/microbit/reference/led/stop-animation)
|
||||
* [toggle](/microbit/reference/led/toggle)
|
||||
* [toggle all](/microbit/reference/led/toggle-all)
|
||||
* [unplot](/microbit/reference/led/unplot)
|
||||
|
||||
### Input
|
||||
|
||||
* [acceleration](/microbit/reference/input/acceleration)
|
||||
* [rotation](/microbit/functions/rotation)
|
||||
* [button is pressed](/microbit/reference/input/button-is-pressed)
|
||||
* [compass heading](/microbit/reference/input/compass-heading)
|
||||
* [temperature](/microbit/reference/input/temperature)
|
||||
* [running time](/microbit/reference/input/running-time)
|
||||
* [on shake](/microbit/reference/input/on-gesture)
|
||||
* [on button pressed](/microbit/reference/input/on-button-pressed)
|
||||
* [on logo down](/microbit/functions/on-logo-down)
|
||||
* [on logo up](/microbit/functions/on-logo-up)
|
||||
* [on pin pressed](/microbit/reference/input/on-pin-pressed)
|
||||
* [on screen down](/microbit/functions/on-screen-down)
|
||||
* [on screen up](/microbit/functions/on-screen-up)
|
||||
* [pin is pressed](/microbit/reference/input/pin-is-pressed)
|
||||
|
||||
### Image
|
||||
|
||||
* [create image](/microbit/reference/images/create-image)
|
||||
* [clear](/microbit/reference/basic/clear-screen)
|
||||
* [pixel](/microbit/reference/images/pixel)
|
||||
* [plot frame](/microbit/reference/led/plot-frame)
|
||||
* [plot image](/microbit/reference/led/plot-image)
|
||||
* [scroll image](/microbit/reference/images/scroll-image)
|
||||
* [show frame](/microbit/functions/show-frame)
|
||||
* [set pixel](/microbit/reference/images/set-pixel)
|
||||
* [show image](/microbit/reference/images/show-image)
|
||||
* [width](/microbit/functions/width)
|
||||
|
||||
### Music
|
||||
|
||||
* [play note](/microbit/functions/play-note)
|
||||
* [note](/microbit/functions/note)
|
||||
* [ring](/microbit/reference/music/ring)
|
||||
|
||||
### Pins
|
||||
|
||||
* [digital read pin](/microbit/reference/pins/digital-read-pin)
|
||||
* [digital write pin](/microbit/reference/pins/digital-write-pin)
|
||||
* [analog read pin](/microbit/reference/pins/analog-read-pin)
|
||||
* [analog write pin](/microbit/reference/pins/analog-write-pin)
|
||||
* [analog set period](/microbit/reference/pins/analog-set-period)
|
||||
* [analog pitch](/microbit/reference/pins/analog-pitch)
|
||||
* [analog set pitch pin](/microbit/reference/pins/analog-set-pitch-pin)
|
||||
* [servo write pin](/microbit/reference/pins/servo-write-pin)
|
||||
* [servo set pulse](/microbit/reference/pins/servo-set-pulse)
|
||||
* [map](/microbit/functions/map)
|
||||
|
||||
### Control
|
||||
|
||||
* [in background](/microbit/reference/control/in-background)
|
||||
* [reset](/microbit/functions/reset)
|
||||
|
||||
### Devices
|
||||
|
||||
Functions in this category require to be connected to a remote device.
|
||||
|
||||
* [tell camera to](/microbit/reference/devices/tell-camera-to)
|
||||
* [tell remote control to](/microbit/reference/devices/tell-remote-control-to)
|
||||
* [raise alert to](/microbit/reference/devices/raise-alert-to)
|
||||
* [on notified](/microbit/reference/devices/on-notified)
|
||||
|
||||
### Libraries
|
||||
|
||||
* [game library](/microbit/js/game-library)
|
||||
* [serial library](/microbit/js/serial-library)
|
||||
|
||||
### ~
|
||||
|
||||
### ~column
|
||||
|
||||
## Run
|
||||
|
||||
* [scripts in the browser](/microbit/js/simulator)
|
||||
* [scripts on your BBC micro:bit](/microbit/device/usb)
|
||||
|
||||
## Debugging
|
||||
|
||||
* use the [serial library](/microbit/js/serial-library) to print data from your micro:bit on your computer
|
||||
* learn about the [device error codes](/microbit/device/errors) that are displayed when sad faces occur
|
||||
|
||||
## Edit/Publish
|
||||
|
||||
* [the Touch Develop Editor](/microbit/js/editor)
|
||||
* [publish a script](/microbit/js/publishing)
|
||||
|
||||
## Creating Tutorials
|
||||
|
||||
* [create a tutorial](/microbit/js/create-tutorials)
|
||||
* [markdown syntax](/microbit/js/markdown)
|
||||
|
||||
### ~
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
# Global Variables
|
||||
|
||||
How to define and use global variables. #docs #data #language #variables
|
||||
|
||||
### @parent js/language
|
||||
|
||||
A variable is a place where you can store data so that you can use it later in your code. A *global* variable is accessible from every point in your code.
|
||||
|
||||
### Block Editor
|
||||
|
||||
In the Block Editor, all variables are global. See [Block Editor](/microbit/blocks/editor) for info on creating global variables in a Block Editor script. The following block is used to set (assign) global variable's value:
|
||||
|
||||

|
||||
|
||||
The block below retrieves (gets) the current value of a global variable:
|
||||
|
||||

|
||||
|
||||
### Touch Develop
|
||||
|
||||
In Touch Develop variables are either [global](/microbit/js/data) or [local](/microbit/reference/variables/var). Variables have a name, a [type](/microbit/js/types), and value:
|
||||
|
||||
* the *name* is how you'll refer to the variable
|
||||
* the *type* refers to the kind of value a variable can store
|
||||
* the *value* refers to what's stored in the variable
|
||||
|
||||
[Global variables](/microbit/js/data) are variables that are available throughout your script. Unlike [local variables](/microbit/reference/variables/var), global variables are accessible across functions and in nested code blocks.
|
||||
|
||||
### Create a global variable
|
||||
|
||||
To create a new global variable:
|
||||
|
||||
1. In the Touch Develop [editor](/microbit/js/editor), click `script` (to the right of the search box).
|
||||
|
||||
2. Click `+` **add new**.
|
||||
|
||||
3. Click `data->` **data** and then choose a [type](/microbit/js/types).
|
||||
|
||||
4. Enter a name for your global variable and click **OK**.
|
||||
|
||||
### Set and use a global variable
|
||||
|
||||
To use a global variable that you've declared (using steps above):
|
||||
|
||||
1. In the Touch Develop [editor](/microbit/js/editor), click `data-> ` **data** or `data->` + *variable name*.
|
||||
|
||||
2. Click `:=` (assignment).
|
||||
|
||||
2. Click on the right-side of `:=` and type or click what you want to store in the variable.
|
||||
|
||||
Your code should look something like this:
|
||||
|
||||
// global number variable
|
||||
|
||||
```
|
||||
counter = 2
|
||||
```
|
||||
|
||||
// global string variable
|
||||
|
||||
```
|
||||
name2 = "Mike"
|
||||
```
|
||||
|
||||
// global boolean variable
|
||||
|
||||
```
|
||||
bool = true
|
||||
```
|
||||
|
||||
(for info on creating image variables, see [Image](/microbit/reference/image/image))
|
||||
|
||||
Once you've defined a variable and set it's initial value, use the variable's name whenever you need what's stored in the variable. For example, the following code gets the value stored in the global `counter` variable and shows it on the screen:
|
||||
|
||||
```
|
||||
basic.showNumber(counter, 100)
|
||||
```
|
||||
|
||||
To change the contents of a variable use the [assignment operator](/microbit/reference/variables/assign) `:=`. The following code increments `counter` by 10:
|
||||
|
||||
```
|
||||
counter = counter + 10
|
||||
```
|
||||
|
||||
### Promote, demote, and extract
|
||||
|
||||
To **promote** a local variable to a global variable:
|
||||
|
||||
* select the local variable name and click `promote to data`. The [var](/microbit/reference/variables/var) keyword changes to the data symbol `data->`.
|
||||
|
||||
To **demote** a global variable to a local variable:
|
||||
|
||||
* select the global variable name and click `demote to var`
|
||||
|
||||
To **extract** the content of a global variable to a local variable:
|
||||
|
||||
* select the global variable name and click `extract to var`
|
||||
|
||||
### See your global variables
|
||||
|
||||
To see a list of the global variables in your script:
|
||||
|
||||
* click `script` (along the top) and scroll down to the **vars** heading
|
||||
|
||||
### Lessons
|
||||
|
||||
[counter](/microbit/lessons/counter), [rotation animation](/microbit/lessons/rotation-animation), [digital pet](/microbit/lessons/digital-pet), [offset image](/microbit/lessons/offset-image)
|
||||
|
||||
### See also
|
||||
|
||||
[local variables](/microbit/reference/variables/var), [types](/microbit/js/types), [assignment operator](/microbit/reference/variables/assign)
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
# Touch Develop Editor
|
||||
|
||||
The Touch Develop editor. #docs #editor #scripts #compile #run #keyboard
|
||||
|
||||
### @parent js/contents
|
||||
|
||||
|
||||
The Touch Develop editor is where you write and test your code. If you're new to Touch Develop, check out the [Touch Develop editor video](/microbit/getting-started/touchdevelop-editor).
|
||||
|
||||
To create a new Touch Develop script:
|
||||
|
||||
1. Go to the micro:bit website and click **Create Code** (along the top).
|
||||
|
||||
2. Under the Touch Develop editor heading, click **New project**.
|
||||
|
||||
3. Type a name for your script and click **create**.
|
||||
|
||||
An empty script with a [function](/microbit/js/function) called `main` is created.
|
||||
|
||||
## The Editor Menu Bar
|
||||
|
||||
The Touch Develop editor has a bar of options above the code area:
|
||||
|
||||

|
||||
|
||||
* `my scripts` takes you back to a list of your scripts (My Scripts). The open script is automatically saved (in the cloud) when you leave the editor.
|
||||
* `run` executes your script, showing you the results on the on-screen micro:bit device. See [run scripts in the browser](/microbit/js/simulator) for more about this.
|
||||
* `compile` sends your script to an ARM compiler, which creates a file that you can run on your micro:bit. See [run scripts on your micro:bit](/microbit/device/usb) for more info.
|
||||
* `undo` undoes changes that you made to your script.
|
||||
* `search code...` search for functions in libraries such as the micro:bit library.
|
||||
* `script` opens script options, where you can do things like publish and preview. See **script options** below.
|
||||
|
||||
Many of the above buttons aren't much use until you've written some code, so let's move on to the Code Keyboard.
|
||||
|
||||
## Code Keyboard
|
||||
|
||||
The Code Keyboard makes it easy to write code on a touch screen device or by using your mouse. You can also type code using your computer keyboard if you know what function or statement you want (see [Touch Develop documentation](/microbit/js/contents) for a complete list).
|
||||
|
||||
To open the Code Keyboard, click on a line of code:
|
||||
|
||||

|
||||
|
||||
An on-screen keyboard appears, with buttons that vary depending on what's selected.
|
||||
|
||||
### Statements
|
||||
|
||||
The first row of the Code Keyboard has Touch Develop [statements](/microbit/js/statements) that you can insert into your code. These buttons are blue and include things like [var](/microbit/reference/variables/var), [if](/microbit/reference/logic/if), [for](/microbit/reference/loops/for) , and [while](/microbit/js/while). Click `more` to see additional statements.
|
||||
|
||||
### The BBC micro:bit, math, and code buttons
|
||||
|
||||
* `micro:bit`: click to see all the [micro:bit functions](/microbit/js/contents); click `more` to scroll left to right. The micro:bit functions are also grouped together behind the following category buttons: `basic`, `control`, `input`, `image`, `led`, and`pins`
|
||||
* `code`: click to access functions you've written (see [call a function](/microbit/js/call) for more info)
|
||||
* `math`: click to see [math functions](/microbit/js/math); such as `abs` and `round`
|
||||
* `bits`: click to see functions for bit-level manipulation of integers
|
||||
|
||||
### Editing code: add, copy, paste, and cut
|
||||
|
||||
In the coding area...
|
||||
|
||||
* **add**: to add a new line, click on a line and then click a **+** to add a new line above or below the current line
|
||||
* **copy, paste, cut**: click on a line then click **copy** or **cut**. Then click on a new line, and click **paste**.
|
||||
|
||||
### Block editing
|
||||
|
||||
To copy, cut, or comment out a block of code (more than one line):
|
||||
|
||||
1. Click on a line of code.
|
||||
|
||||
2. Press and hold the `Shift` key, and then press the `Up arrow` or `Down arrow` key on your keyboard (this selects multiple lines).
|
||||
|
||||
3. Choose a block editing option like copy, cut, or [comment out](/microbit/js/comment).
|
||||
|
||||
### Script options
|
||||
|
||||
Click `script` (in the upper-right corner) to open the script options:
|
||||
|
||||

|
||||
|
||||
Here you'll find options like...
|
||||
|
||||
* `script properties`: the script name, description, and whether or not the script is a library (more info below)
|
||||
* `publish`: share a script with other users by [publishing](/microbit/js/publishing) it
|
||||
* `share`: share a link to a published script (see [publish as script](/microbit/js/publishing) for more info)
|
||||
* `preview`: preview a documentation script
|
||||
* `+` `add new`: add a new [function](/microbit/js/function), [global variable](/microbit/js/data), picture, or library to a script
|
||||
* *code*: the functions in your script; click a function to open it in the editor
|
||||
* *global vars*: the [global variables](/microbit/js/data) in your script; click a variable to go to that variable
|
||||
* *libraries*: the libraries added to your script
|
||||
* *art*: picture and video resources added to your script
|
||||
|
||||
### Script properties
|
||||
|
||||
To edit a script's properties, click `script` (in the upper-right corner), and then click the script name or script properties.
|
||||
|
||||

|
||||
|
||||
* `name`: the script's name (60 character limit)
|
||||
* `description`: a description of what your script does along with #hashtags for search (for example, #game or #maker). Hashtags are especially important if you publish your script (200 character limit).
|
||||
* `this script is a library`: click this check box to turn a script into a library
|
||||
|
||||
### Comments
|
||||
|
||||
Comments are notes within your scripts. To learn how to insert comments into your scripts, see [Comments](/microbit/js/comment). You can format your comments using [markdown syntax](/microbit/js/markdown).
|
||||
|
||||
### Share your scripts
|
||||
|
||||
Share your scripts with other people by publishing them. See [publish a script](/microbit/js/publishing) for more info.
|
||||
|
||||
### See also
|
||||
|
||||
[publish a script](/microbit/js/publishing), [Touch Develop documentation](/microbit/js/contents)
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
# event handler
|
||||
|
||||
Event handlers - how they work. #eventhandler #docs #input #button
|
||||
|
||||
An event handler is code that is associated with a particular event, such as "button A pressed". You create (or register) the association between an event and an event handler by calling a function named "on <event>". After registering an event handler with an event, then whenever that event occurs, the event handler code executes.
|
||||
|
||||
### Registering an event handler
|
||||
|
||||
Functions named "on <event>" create an association between an event and the event handler code. For example, the following code registers the event handler (the code between the `do` and `end` keywords) with the event of a press of button A:
|
||||
|
||||
```
|
||||
input.onButtonPressed("A", () => {
|
||||
basic.showString("hello", 150)
|
||||
})
|
||||
```
|
||||
|
||||
After this code executes, then whenever button A is pressed in the future, the string "hello" will be printed.
|
||||
|
||||
### Event handlers are active for the entire program execution
|
||||
|
||||
Once you have registered an event handler for an event, like above, that event handler is active for the rest of the program execution. If you want to stop the string "hello" from printing each time button A is pressed then you need to arrange for the following code to execute:
|
||||
|
||||
```
|
||||
input.onButtonPressed("A", () => {
|
||||
})
|
||||
```
|
||||
|
||||
The above code associated an event handler that does nothing with the event of a press of button A.
|
||||
|
||||
### There is only one event handler per event
|
||||
|
||||
The above example also illustrates that there is only one event handler for each event. What is the result of the following code?
|
||||
|
||||
```
|
||||
input.onButtonPressed("A", () => {
|
||||
basic.showString("hello", 150)
|
||||
})
|
||||
input.onButtonPressed("A", () => {
|
||||
basic.showString("goodbye", 150)
|
||||
})
|
||||
```
|
||||
|
||||
The answer is that whenever button A is pressed, the string "goodbye" will be printed. If you want both the strings "hello" and "goodbye" to be printed, you need to write the code like this:
|
||||
|
||||
```
|
||||
input.onButtonPressed("A", () => {
|
||||
basic.showString("hello", 150)
|
||||
basic.showString("goodbye", 150)
|
||||
})
|
||||
```
|
||||
|
||||
### To learn more
|
||||
|
||||
To learn more about how the BBC micro:bit queues up and schedules event handlers, see [the BBC micro:bit - a reactive system](/microbit/device/reactive)
|
||||
|
||||
### see also
|
||||
|
||||
[on button pressed](/microbit/reference/input/on-button-pressed), [on logo up](/microbit/functions/on-logo-up), [on logo down](/microbit/functions/on-logo-down), [on screen up](/microbit/functions/on-screen-up), [on screen down](/microbit/functions/on-screen-down), [on shake](/microbit/reference/input/on-gesture), [on pin pressed](/microbit/reference/input/on-pin-pressed)
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
# Events Library
|
||||
|
||||
The events library #docs
|
||||
|
||||
The functions in the events namespace allow the BBC micro:bit to communicate with a separate (remote) device, such as a smartphone, over Bluetooth (Smart). The set of supported events will depend on the remote device and the BBC micro:bit apps available for the remote device. The events accessible from Touch Develop are listed below.
|
||||
|
||||
### Remote control
|
||||
|
||||
Control the presentation of media content available on a remote device using the `remote control` function
|
||||
|
||||
```
|
||||
export function remoteControl(event: string)
|
||||
```
|
||||
|
||||
The remote control specific events include:
|
||||
|
||||
* play
|
||||
* pause
|
||||
* stop
|
||||
* next track
|
||||
* previous track
|
||||
* forward
|
||||
* rewind
|
||||
* volume up
|
||||
* volume down
|
||||
|
||||
### Camera
|
||||
|
||||
Access the photo/video-taking functionality of a remote device using the *camera* function:
|
||||
|
||||
```
|
||||
export function camera(event: string)
|
||||
```
|
||||
|
||||
The camera-specific events include:
|
||||
|
||||
* toggle front-rear
|
||||
* launch photo mode
|
||||
* take photo
|
||||
* stop photo mode
|
||||
* launch video mode
|
||||
* start video capture
|
||||
* stop video capture
|
||||
* stop video mode
|
||||
|
||||
### Alert
|
||||
|
||||
Raise an alert on a remote device using the `alert` function
|
||||
|
||||
```
|
||||
export function alert(event: string)
|
||||
```
|
||||
|
||||
The set of alerting-specific events include:
|
||||
|
||||
* display toast
|
||||
* vibrate
|
||||
* play sound
|
||||
* play ringtone
|
||||
* find my phone
|
||||
* alarm 1
|
||||
* alarm 2
|
||||
* alarm 3
|
||||
* alarm 4
|
||||
* alarm 5
|
||||
* alarm 6
|
||||
|
||||
### Audio recorder
|
||||
|
||||
Access the audio recording capabilities of the device using the `audio recording` function
|
||||
|
||||
```
|
||||
export function audioRecorder(event: string)
|
||||
```
|
||||
|
||||
The set of audio recorder events include:
|
||||
|
||||
* launch
|
||||
* start capture
|
||||
* end capture
|
||||
* stop
|
||||
@@ -0,0 +1,83 @@
|
||||
# For
|
||||
|
||||
Repeat code a preset number of times. #docs #for #endfor #language
|
||||
|
||||
### @parent js/language
|
||||
|
||||
|
||||
Repeat code a fixed number of times.
|
||||
|
||||
### Block Editor
|
||||
|
||||

|
||||
|
||||
The Block Editor *for* loop is different than the Touch Develop *for* loop in an important way. The above for loop will iterate *five* times, with the loop variable *i* taking on values 0, 1, 2, 3, and 4. The Touch Develop for loop shown below will iterate four times:
|
||||
|
||||
```
|
||||
for (let k = 0; k < 4; k++) {
|
||||
}
|
||||
```
|
||||
|
||||
### Touch Develop
|
||||
|
||||
### ~hide
|
||||
|
||||
```
|
||||
let upper = 5
|
||||
```
|
||||
|
||||
### ~
|
||||
|
||||
```
|
||||
for (let k1 = 0; k1 < upper; k1++) {
|
||||
// Add code to repeat here, also called the `loop body`
|
||||
}
|
||||
```
|
||||
|
||||
where
|
||||
|
||||
* `0` is initial value of the loop index variable `k`
|
||||
* the value of `k` increases by 1 after each execution of the `loop body`
|
||||
* `upper` is the number of times the loop body will repeat
|
||||
|
||||
In other words, the index variable (`k`) starts at 0 and increases by 1 each time the `loop body` executes, until `k = upper`.
|
||||
|
||||
### Example: count to 5
|
||||
|
||||
The following example displays numbers 1 through 5 on the LED screen:
|
||||
|
||||
```
|
||||
for (let i = 0; i < 5; i++) {
|
||||
basic.showNumber(i + 1, 100)
|
||||
basic.pause(500)
|
||||
}
|
||||
```
|
||||
|
||||
### Example: draw a box
|
||||
|
||||
The [LED screen](/microbit/device/screen) has a fixed number of rows and columns (5x5), which is ideal for a for loop. This example uses a for loop to turn on the LEDs along the edge of the screen, making a square.
|
||||
|
||||
```
|
||||
for (let i1 = 0; i1 < 5; i1++) {
|
||||
led.plot(0, i1)
|
||||
led.plot(4, i1)
|
||||
led.plot(i1, 0)
|
||||
led.plot(i1, 4)
|
||||
basic.pause(500)
|
||||
}
|
||||
```
|
||||
|
||||
### ~hint
|
||||
|
||||
Want to exit a loop early? The [break](/microbit/js/break) statement exits a loop before the end value is reached.
|
||||
|
||||
### ~
|
||||
|
||||
### Lessons
|
||||
|
||||
[looper](/microbit/lessons/looper), [strobe light](/microbit/lessons/strobe-light)
|
||||
|
||||
### See also
|
||||
|
||||
[while](/microbit/js/while), [break](/microbit/js/break), [if](/microbit/reference/logic/if)
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
# Create a Function
|
||||
|
||||
How to define a function with input and output parameters. #docs #function #language
|
||||
|
||||
### @parent js/language
|
||||
|
||||
|
||||
A function is a unit of code that performs a specific task and returns a result.
|
||||
|
||||
Functions are ideal when you need to perform an action multiple times. Instead of repeating a block of code in your script, you can put the code in a function and simply [call the function](/microbit/js/call) when needed.
|
||||
|
||||
*Why use functions?* Functions makes your code easier to read, debug, and update.
|
||||
|
||||
### Add a function
|
||||
|
||||
To add a function to a Touch Develop script:
|
||||
|
||||
1. Open a script and then click `script` (in the upper-right corner).
|
||||
|
||||
2. Click `+` **add new**.
|
||||
|
||||
3. Click **function()**.
|
||||
|
||||
A new function appears, like this:
|
||||
|
||||
```
|
||||
export function doStuff(p: number) {
|
||||
}
|
||||
```
|
||||
|
||||
Functions begin with the `function` keyword and end with `end function`. The function name appears after the `function` keyword (in this case, `do stuff`).
|
||||
|
||||
### ~hint
|
||||
|
||||
Click the function name to edit the function properties (i.e. change the name or add parameters - see below).
|
||||
|
||||
### ~
|
||||
|
||||
### Function components
|
||||
|
||||
Functions have three parts:
|
||||
|
||||
* [input and output parameters](/microbit/js/functionparameters)
|
||||
* the function *body* (the code that performs a task)
|
||||
|
||||
- one or more [return](/microbit/js/return) statements (the output of the function)
|
||||
|
||||
#### Example function
|
||||
|
||||
```
|
||||
/**
|
||||
* // returns the square of the input parameter x
|
||||
* @param x TODO
|
||||
*/
|
||||
export function square(x: number) : number {
|
||||
let result: number
|
||||
return x * x
|
||||
return result
|
||||
}
|
||||
```
|
||||
|
||||
In the above code...
|
||||
|
||||
* ``x `` is the [input parameter](/microbit/js/functionparameters) ([Number](/microbit/reference/types/number) type)
|
||||
* ``result`` is the [output parameter](/microbit/js/functionparameters) ([Number](/microbit/reference/types/number) type)
|
||||
* `return x * x` is the function body (which returns the value of the expression `x * x`)
|
||||
|
||||
### Add function parameters
|
||||
|
||||
1. Open your function (if needed). To do this, open your script and then click `script` (in the upper-right corner).
|
||||
|
||||
2. Under **code** click your function name.
|
||||
|
||||
3. Click the function name in the code window. This opens the function panel.
|
||||
|
||||
4. Click **add input parameter** or **add output parameter**. The parameter is added to your function.
|
||||
|
||||
Click the parameter name to rename it and click the [type](/microbit/js/types) to change the variable type. For more info, see [function parameters](/microbit/js/functionparameters).
|
||||
|
||||
### ~hide
|
||||
|
||||
### Extract code into a function
|
||||
|
||||
If you've already written some code that you'd like to have in a function, you can extract the code. Here's how:
|
||||
|
||||
1. Click the first line of code that you want to extract.
|
||||
|
||||
2. Press and hold the Shift key, and then press the Down arrow on your keyboard to select multiple lines of code.
|
||||
|
||||
3. In the block editing window, scroll down to **extract selection into function** and click `extract`.
|
||||
|
||||
### ~
|
||||
|
||||
### Function documentation
|
||||
|
||||
Use a [comment](/microbit/js/comment) at the beginning of your functions to describe the function. When you insert a function into your code, the comment text appears in the help area of the Code Keyboard.
|
||||
|
||||
### See all your functions
|
||||
|
||||
To see all the functions in a script, open the script and then click `script` (in the upper-right corner). All of the functions in your script appear under **code**.
|
||||
|
||||
### ~hide
|
||||
|
||||
### Private functions
|
||||
|
||||
If you don't want people to see the code in your function, you can make the function private. To do this, open the function, click the function name, and then mark the **private function** check box. Private functions have a locked icon instead of a play icon.
|
||||
|
||||
### ~
|
||||
|
||||
### Lessons
|
||||
|
||||
[digital pet](/microbit/lessons/digital-pet)
|
||||
|
||||
### See also
|
||||
|
||||
[function parameters](/microbit/js/functionparameters), [call a function](/microbit/js/call), [return from a function](/microbit/js/return)
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
# Function Parameters
|
||||
|
||||
How to use parameters to pass info in and out of an function. #docs #input #output #function #functionparameters
|
||||
|
||||
### @parent js/function
|
||||
|
||||
|
||||
A [function](/microbit/js/function) can have multiple input parameters and/or a single output parameter. The parameters must be one of the supported variable [types](/microbit/js/types).
|
||||
|
||||
When you first [create a function](/microbit/js/function), it looks like this:
|
||||
```
|
||||
export function doStuff() {
|
||||
}
|
||||
```
|
||||
|
||||
### Add a function parameter
|
||||
|
||||
1. Open your script (if needed) and then click `script` in the upper-right corner.
|
||||
|
||||
2. Under **code** click your function name.
|
||||
|
||||
3. Click the function name in your code (this opens the function panel).
|
||||
|
||||
4. Click **add input parameter** or **add output parameter**. The parameter is added to your function.
|
||||
|
||||
#### Input parameters
|
||||
|
||||
The default type for an input parameter is [Number](/microbit/reference/types/number):
|
||||
|
||||
```
|
||||
export function oneInput(p: number) {
|
||||
}
|
||||
```
|
||||
|
||||
To change the default type, click the type ([Number](/microbit/reference/types/number) in this case) and change it to [String](/microbit/reference/types/string), [Boolean](/microbit/reference/types/boolean), or [Image](/microbit/reference/image/image). You can add multiple input parameters to a function.
|
||||
|
||||
#### Output parameter
|
||||
|
||||
the default type for an output parameter is [Number](/microbit/reference/types/number):
|
||||
|
||||
```
|
||||
export function output() : number {
|
||||
let r: number
|
||||
return 42
|
||||
return r
|
||||
}
|
||||
```
|
||||
|
||||
To change the default type, click the type ([Number](/microbit/reference/types/number) in this case) and change it to [String](/microbit/reference/types/string), [Boolean](/microbit/reference/types/boolean), or [Image](/microbit/reference/image/image).
|
||||
|
||||
### Inputs and output function
|
||||
|
||||
The following sample function has two inputs and one output parameter (all are the Number type):
|
||||
|
||||
```
|
||||
export function inputsAndOutput(p: number, q: number) : number {
|
||||
let r: number
|
||||
return p + q
|
||||
return r
|
||||
}
|
||||
```
|
||||
|
||||
### See also
|
||||
|
||||
[call a function](/microbit/js/call), [create a function](/microbit/js/function), [return](/microbit/js/return)
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
# Gallery
|
||||
|
||||
Overview of Touch Develop lessons for the BBC micro:bit. #docs #contents
|
||||
|
||||
### @short Gallery
|
||||
|
||||
### ~column
|
||||
|
||||
|
||||
## Maker
|
||||
|
||||
* [Telegraph](/microbit/pzeagwoudd), play the telegraph game between 2 BBC micro:bits
|
||||
* [Ornament Chain](/microbit/rnvpgo), play the ornament chain game between 2 BBC micro:bits
|
||||
|
||||
### ~hide
|
||||
|
||||
* [The Watch](/microbit/lessons/the-watch), design and create The Watch
|
||||
* [Hack your headphones](/microbit/lessons/hack-your-headphones), create music on the BBC micro:bit by hacking your headphones
|
||||
* [Banana Keyboard](/microbit/lessons/banana-keyboard), create music with fruits
|
||||
|
||||
### ~
|
||||
|
||||
## Beginner
|
||||
|
||||
* [Night light](/microbit/vltwrzuqto), dim the LEDs with set brightness
|
||||
* [Beautiful image](/microbit/nudwzmphyx), show a beautiful image with show LEDs
|
||||
* [Smiley,](/microbit/zsohipimef) smiley and frowney with show animation
|
||||
* [Lucky 7](/microbit/rqhxxqppqu), show a number on the LED screen with show number
|
||||
* [Answering machine](/microbit/bnkmeqymuh), show a text message with show string
|
||||
* [Snowflake fall](/microbit/zhcfmiejlg), repeat an animation with forever
|
||||
* [Screen wipe](/microbit/hlnitnqjjk), turn off the LEDs with clear screen
|
||||
* [Flashing heart](/microbit/bwmxfwqswx), display images with a pause
|
||||
* [Blink](/microbit/jbbutifslm), turn an LED on and off with plot
|
||||
|
||||
### ~hide
|
||||
|
||||
* [Bounce image](/microbit/lessons/bounce-image), scroll an image across the screen on shake
|
||||
* [Magic logo](/microbit/lessons/magic-logo), show an image on logo up
|
||||
* [Glowing sword](/microbit/lessons/glowing-sword), make a glowing sword with fade in and fade out
|
||||
|
||||
### ~
|
||||
|
||||
### ~column
|
||||
|
||||
## Intermediate
|
||||
|
||||
* [Zoomer](/microbit/fwrohhjqql), measure the force with acceleration
|
||||
* [Strobe light](/microbit/jguqlzeayr), develop shapes with a nested for loops
|
||||
* [Digi yoyo](/microbit/lppocrbpys), create a counter with a while loop
|
||||
* [Die roll](/microbit/lzblatmknq), spin with more if statements
|
||||
* [Spinner](/microbit/dzijduruek), spin the arrow with multiple if statements
|
||||
* [Truth or dare](/microbit/filuzbwauo), a game that forces each player to reveal a secret or do something funny with if statement
|
||||
* [Love meter](/microbit/rrmlrvojfa), create a love meter with on pin pressed
|
||||
* [Guess the number](/microbit/ftsenbvqwz), guess a random number with random
|
||||
* [Magic 8](/microbit/fyjinpjuqu), a fortune teller game with the BBC micro:bit
|
||||
* [Counter](/microbit/rerlmjgjut), display a number with a variable
|
||||
* [Glowing pendulum](/microbit/xrnsveuwxj), construct a pendulum that glows using acceleration
|
||||
* [Looper](/microbit/nxcddtbizi), display a series of numbers with a for loop index
|
||||
|
||||
### ~hide
|
||||
|
||||
* [Rotation animation](/microbit/lessons/rotation-animation), control an animation with a boolean variable
|
||||
* [Offset image](/microbit/lessons/offset-image), shift an image horizontally with image offset
|
||||
* [Compass](/microbit/lessons/compass), displays the direction the BBC micro:bit is pointing
|
||||
|
||||
### ~
|
||||
|
||||
### ~column
|
||||
|
||||
## Advanced
|
||||
|
||||
* [Rock paper scissors](/microbit/tnmtbvyyma), use image offsets with local variables
|
||||
* [Digital pet](/microbit/vefocoajpb), a display of pet images with sub-functions
|
||||
* [Catch the egg](/microbit/reczlreqob), catch falling eggs in a basket with an acceleration controller
|
||||
* [Headbands](/microbit/bzrusu), create a charades game with a collection of strings that hold the words
|
||||
* [Prank WiFi](/microbit/dceikq), create fake WiFi to trick your friends
|
||||
* [Flipping bird](/microbit/lbhvywjzkv), use modulo with a conditional
|
||||
* [Runaway pac man](/microbit/loafab), construct the game pac man with the BBC micro:bit
|
||||
* [Line of Fire](/microbit/fzcoly), make a game to test hand-eye coordination
|
||||
* [The hat game](/microbit/njynsd), make a game to test your focus on the moving ball
|
||||
* [Pong](/microbit/xcenyy), a light bouncing from left to right
|
||||
* [Meteorite](/microbit/zaidka), a game where meteorites are coming for you one by one
|
||||
* [Minesweeper](/microbit/jaeeve), make a game to test your memory for placing a LED mine then finding the hidden LED mine
|
||||
* [Bop it](/microbit/zlpndm), a game where you have to keep up with the commands
|
||||
* [Letter Up](/microbit/ftlqjo), a guessing game with string operators with string at
|
||||
* [Racing Buttons](/microbit/hcuxid), racing game to determine if player 1 presses Button A faster or if player 2 presses Button B faster
|
||||
|
||||
### ~hide
|
||||
|
||||
* [Transformers](/microbit/lessons/transformers), use functions to return values
|
||||
* [Speed button](/microbit/lessons/speed-button), code a speed game with running time
|
||||
* [Jailbreak](/microbit/lessons/jailbreak), break out of a counting loop by pressing button "A"
|
||||
* [2 player pong](/microbit/bzycll), collaborate with a classmate to develop Pong on multiple BBC micro:bits
|
||||
|
||||
### ~
|
||||
|
||||
### ~hide
|
||||
|
||||
* [Number psych](/microbit/lessons/number-psych), collaborate with multiple classmates to develop a game on multiple BBC micro:bits and a breadboard
|
||||
|
||||
### ~
|
||||
|
||||
### @section full
|
||||
|
||||
The lessons promote computational thinking and computer science literacy[ read more...](/microbit/lessons/teach)
|
||||
|
||||
@@ -0,0 +1,155 @@
|
||||
# Game Library
|
||||
|
||||
The game library #docs
|
||||
|
||||
The game library supports simple single-player time-based games. The player has a number of **lives** and a **score**. The game has a number of **levels** and a **countdown clock**. The general goal of a game will be to achieve a top score before time runs out or the number of lives goes to zero.
|
||||
|
||||
## Touch Develop
|
||||
|
||||
The code below shows a simple game where the user gets to press the button ``A`` as much times as possible in 10 seconds.
|
||||
|
||||
```
|
||||
input.onButtonPressed("A", () => {
|
||||
game.addScore(1)
|
||||
})
|
||||
game.startCountdown(10000)
|
||||
```
|
||||
|
||||
### [Countdown](/microbit/js/game-library/start-countdown)
|
||||
|
||||
If your game has a time limit, you can start a countdown in which case `game->current time` returns the remaining time.
|
||||
|
||||
* start a countdown with the maximum duration of the game in milliseconds.
|
||||
|
||||
```
|
||||
export function startCountdown(ms: number)
|
||||
```
|
||||
|
||||
### [Game over](/microbit/js/game-library/game-over)
|
||||
|
||||
If the `life` reaches zero or the time expires (see countdown), the game enters the **game over** mode. When the game is over, `game->is running` returns false
|
||||
|
||||
* check if the game still running.
|
||||
|
||||
```
|
||||
export function isRunning() : boolean
|
||||
```
|
||||
|
||||
Indicates if the game is display the game over sequence.
|
||||
|
||||
```
|
||||
export function isGameOver() : boolean
|
||||
```
|
||||
|
||||
You can also end the game by calling the `game -> game over` function:
|
||||
|
||||
```
|
||||
export function gameOver()
|
||||
```
|
||||
|
||||
### Score
|
||||
|
||||
When a player achieves a goal, you can increase the game score
|
||||
|
||||
[Add Point to Score](/microbit/js/game-library/add-point-to-score)
|
||||
|
||||
* add score points to the current score
|
||||
|
||||
```
|
||||
export function addScore(points: number)
|
||||
```
|
||||
|
||||
[Score](/microbit/js/game-library/score)
|
||||
|
||||
* set the current score to a particular value.
|
||||
|
||||
```
|
||||
export function setScore(value: number)
|
||||
```
|
||||
|
||||
* get the current score value
|
||||
|
||||
```
|
||||
export function score() : number
|
||||
```
|
||||
|
||||
### Life
|
||||
|
||||
Manage the player lives. When the life count reaches 0 or less, the game is over.
|
||||
|
||||
* remove one or more lives
|
||||
|
||||
```
|
||||
export function removeLife(life: number)
|
||||
```
|
||||
|
||||
* add lives
|
||||
|
||||
```
|
||||
export function addLife(lives: number)
|
||||
```
|
||||
|
||||
* set the life to a particular value
|
||||
|
||||
```
|
||||
export function setLife(value: number)
|
||||
```
|
||||
|
||||
* get the current life value
|
||||
|
||||
```
|
||||
export function life() : number
|
||||
```
|
||||
|
||||
### Levels
|
||||
|
||||
When the game increases in difficulty, you can increase the level and use that value in your game logic.
|
||||
|
||||
* increase the level by 1
|
||||
|
||||
```
|
||||
export function levelUp()
|
||||
```
|
||||
|
||||
* get the current level
|
||||
|
||||
```
|
||||
export function level() : number
|
||||
```
|
||||
|
||||
### Time
|
||||
|
||||
The game immediately starts tracking the time from the moment the device started.
|
||||
|
||||
* get the current time
|
||||
|
||||
```
|
||||
export function currentTime() : number
|
||||
```
|
||||
|
||||
You can start the time again by using `game->start stopwatch`.
|
||||
|
||||
* start the game timer
|
||||
|
||||
```
|
||||
game.startStopwatch()
|
||||
```
|
||||
|
||||
### Blink
|
||||
|
||||
Reports the blink duration of a `sprite` .
|
||||
|
||||
```
|
||||
export function blink(_this: micro_bitSprites.LedSprite) : number
|
||||
```
|
||||
|
||||
Sets the blink duration interval in milliseconds .
|
||||
|
||||
```
|
||||
export function setBlink(sprite: micro_bitSprites.LedSprite, ms: number)
|
||||
```
|
||||
|
||||
### Lessons
|
||||
|
||||
[bop it](/microbit/lessons/bop-it) | [game of chance](/microbit/lessons/game-of-chance) | [game counter](/microbit/lessons/game-counter)
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
# Add Points to Score
|
||||
|
||||
The game library #docs
|
||||
|
||||
The game library supports simple single-player time-based games. The player will ** add points to score**.
|
||||
|
||||
## Block Editor
|
||||
|
||||
The code below shows a simple game where the user gets to press the button ``A`` as much times as possible and the score will display on the screen.
|
||||
|
||||

|
||||
|
||||
## Touch Develop
|
||||
|
||||
The code below shows a simple game where the user gets to press the button ``A`` as much times as possible in 10 seconds.
|
||||
|
||||
```
|
||||
input.onButtonPressed("A", () => {
|
||||
game.addScore(1)
|
||||
})
|
||||
game.startCountdown(10000)
|
||||
```
|
||||
|
||||
### Score
|
||||
|
||||
When a player achieves a goal, you can increase the game score
|
||||
|
||||
* add score points to the current score
|
||||
|
||||
```
|
||||
export function addScore(points: number)
|
||||
```
|
||||
|
||||
* get the current score value
|
||||
|
||||
```
|
||||
export function score() : number
|
||||
```
|
||||
|
||||
### Lessons
|
||||
|
||||
[bop it](/microbit/lessons/bop-it) | [game of chance](/microbit/lessons/game-of-chance) | [game counter](/microbit/lessons/game-counter)
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
# Change Score By
|
||||
|
||||
The game library #docs
|
||||
|
||||
The game library supports simple single-player time-based games. The player will ** add points to score**.
|
||||
|
||||
## Block Editor
|
||||
|
||||
The code below shows a simple game where the user gets to press the button ``A`` as much times as possible and the score will display on the screen.
|
||||
|
||||

|
||||
|
||||
## Touch Develop
|
||||
|
||||
The code below shows a simple game where the user gets to press the button ``A`` as much times as possible in 10 seconds.
|
||||
|
||||
```
|
||||
input.onButtonPressed("A", () => {
|
||||
game.addScore(1)
|
||||
})
|
||||
game.startCountdown(10000)
|
||||
```
|
||||
|
||||
### Score
|
||||
|
||||
When a player achieves a goal, you can increase the game score
|
||||
|
||||
* add score points to the current score
|
||||
|
||||
```
|
||||
export function addScore(points: number)
|
||||
```
|
||||
|
||||
* get the current score value
|
||||
|
||||
```
|
||||
export function score() : number
|
||||
```
|
||||
|
||||
### Lessons
|
||||
|
||||
[bop it](/microbit/lessons/bop-it) | [game of chance](/microbit/lessons/game-of-chance) | [game counter](/microbit/lessons/game-counter)
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
# Game Over
|
||||
|
||||
The game library #docs
|
||||
|
||||
The game library supports simple single-player time-based games. The game can end the game by calling the `game over` function
|
||||
|
||||
## Block Editor
|
||||
|
||||
You can end the game by calling the `game over ` function. In this example, if BBC micro:bit's answer to the question is GAME OVER, GAME OVER will be displayed to end the game.
|
||||
|
||||

|
||||
|
||||
## Touch Develop
|
||||
|
||||
You can end the game by calling the `game -> game over` function:
|
||||
|
||||
```
|
||||
game.gameOver()
|
||||
```
|
||||
|
||||
### Lessons
|
||||
|
||||
[game of chance](/microbit/lessons/game-of-chance)
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
# Score
|
||||
|
||||
The game library #docs
|
||||
|
||||
The game library supports simple single-player games. The player has a **score**.
|
||||
|
||||
## Block Editor
|
||||
|
||||
The code below shows a simple game where the user gets to press the button ``A`` and adds 1 point to score that will be displayed on the BBC micro:bit screen
|
||||
|
||||

|
||||
|
||||
## Touch Develop
|
||||
|
||||
The code below shows a simple game where the user gets to press the button ``A`` as much times as possible in 10 seconds.
|
||||
|
||||
```
|
||||
input.onButtonPressed("A", () => {
|
||||
game.addScore(1)
|
||||
})
|
||||
game.startCountdown(10000)
|
||||
```
|
||||
|
||||
### Score
|
||||
|
||||
When a player achieves a goal, you can increase the game score
|
||||
|
||||
* add score points to the current score
|
||||
|
||||
```
|
||||
export function addScore(points: number)
|
||||
```
|
||||
|
||||
* set the current score to a particular value.
|
||||
|
||||
```
|
||||
export function setScore(value: number)
|
||||
```
|
||||
|
||||
* get the current score value
|
||||
|
||||
```
|
||||
export function score() : number
|
||||
```
|
||||
|
||||
### Countdown
|
||||
|
||||
If your game has a time limit, you can start a countdown in which case `game->current time` returns the remaining time.
|
||||
|
||||
* start a countdown with the maximum duration of the game in milliseconds.
|
||||
|
||||
```
|
||||
export function startCountdown(ms: number)
|
||||
```
|
||||
|
||||
### Lessons
|
||||
|
||||
[bop it](/microbit/lessons/bop-it) | [game of chance](/microbit/lessons/game-of-chance) | [game counter](/microbit/lessons/game-counter)
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
# Start Countdown
|
||||
|
||||
The game library #docs
|
||||
|
||||
The game library supports simple single-player time-based games. The general goal of a game will be to achieve a top score before time runs out of time.
|
||||
|
||||
## Block Editor
|
||||
|
||||

|
||||
|
||||
## Touch Develop
|
||||
|
||||
The code below shows a simple game where the user gets to press the button ``A`` as much times as possible in 10 seconds.
|
||||
|
||||
```
|
||||
input.onButtonPressed("A", () => {
|
||||
game.addScore(1)
|
||||
})
|
||||
game.startCountdown(10000)
|
||||
```
|
||||
|
||||
### Score
|
||||
|
||||
When a player achieves a goal, you can increase the game score
|
||||
|
||||
* add score points to the current score
|
||||
|
||||
```
|
||||
export function addScore(points: number)
|
||||
```
|
||||
|
||||
* set the current score to a particular value.
|
||||
|
||||
```
|
||||
export function setScore(value: number)
|
||||
```
|
||||
|
||||
* get the current score value
|
||||
|
||||
```
|
||||
export function score() : number
|
||||
```
|
||||
|
||||
### Countdown
|
||||
|
||||
If your game has a time limit, you can start a countdown in which case `game->current time` returns the remaining time.
|
||||
|
||||
* start a countdown with the maximum duration of the game in milliseconds.
|
||||
|
||||
```
|
||||
export function startCountdown(ms: number)
|
||||
```
|
||||
|
||||
### Lessons
|
||||
|
||||
[bop it](/microbit/lessons/bop-it) | [game of chance](/microbit/lessons/game-of-chance) | [game counter](/microbit/lessons/game-counter)
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
# game
|
||||
|
||||
A #microbit game library.
|
||||
|
||||
Gets the current score
|
||||
|
||||
```
|
||||
init()
|
||||
return _score
|
||||
```
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
# Game Tutorials
|
||||
|
||||
Overview of Games for the BBC micro:bit. #docs #contents
|
||||
|
||||
### @short Games
|
||||
|
||||
### ~column
|
||||
|
||||
## Beginner Games
|
||||
|
||||
* [The Watch](/microbit/lessons/the-watch/activity), design and create The Watch
|
||||
* [Banana Keyboard](/microbit/lessons/banana-keyboard), create music with fruits
|
||||
|
||||
### ~hide
|
||||
|
||||
* [Smiley,](/microbit/lessons/smiley) smiley and frowney with show animation
|
||||
* [Lucky 7](/microbit/lessons/lucky-7), show a number on the LED screen with show number
|
||||
* [Snowflake fall](/microbit/lessons/snowflake-fall), repeat an animation with forever
|
||||
* [Answering machine](/microbit/lessons/answering-machine), show a text message with show string
|
||||
* [Bounce image](/microbit/lessons/bounce-image), scroll an image across the screen on shake
|
||||
* [Magic logo](/microbit/lessons/magic-logo), show an image on logo up
|
||||
* [Screen wipe](/microbit/lessons/screen-wipe), turn off the LEDs with clear screen
|
||||
* [Blink](/microbit/lessons/blink), turn an LED on and off with plot
|
||||
* [Flashing heart](/microbit/lessons/flashing-heart/tutorial), display images with a pause
|
||||
|
||||
### ~
|
||||
|
||||
* [Night light](/microbit/lessons/night-light/tutorial), dim the LEDs with set brightness
|
||||
* [Glowing sword](/microbit/lessons/glowing-sword/tutorial), make a glowing sword with fade in and fade out
|
||||
* [Guess the number](/microbit/lessons/guess-the-number/tutorial), guess a random number with random
|
||||
* [Rock paper scissors](/microbit/lessons/rock-paper-scissors/tutorial), use image offsets with local variables
|
||||
* [Counter](/microbit/lessons/counter/tutorial), display a number with a variable
|
||||
* [Love meter](/microbit/lessons/love-meter/tutorial), create a love meter with on pin pressed
|
||||
|
||||
### ~column
|
||||
|
||||
## Intermediate Games
|
||||
|
||||
* [Truth or dare](/microbit/lessons/truth-or-dare/tutorial), a game that forces each player to reveal a secret or do something funny with if statement
|
||||
* [Spinner](/microbit/lessons/spinner/tutorial), spin the arrow with multiple if statements
|
||||
* [Die roll](/microbit/lessons/die-roll/tutorial), spin with more if statements
|
||||
* [Looper](/microbit/lessons/looper/tutorial), display a series of numbers with a for loop index
|
||||
* [Strobe light](/microbit/lessons/strobe-light/tutorial), develop shapes with a nested for loops
|
||||
* [Digi yoyo](/microbit/lessons/digi-yoyo/tutorial), create a counter with a while loop
|
||||
* [Magic 8](/microbit/lessons/magic-8/tutorial), a fortune teller game with the BBC micro:bit
|
||||
* [Compass](/microbit/lessons/compass/tutorial), displays the direction the BBC micro:bit is pointing
|
||||
* [Speed button](/microbit/lessons/speed-button/tutorial), code a speed game with running time
|
||||
|
||||
### ~hide
|
||||
|
||||
* [Zoomer](/microbit/lessons/zoomer/tutorial), measure the force with acceleration
|
||||
* [Rotation animation](/microbit/lessons/rotation-animation/tutorial), control an animation with a boolean variable
|
||||
* [Offset image](/microbit/lessons/offset-image/tutorial), shift an image horizontally with image offset
|
||||
|
||||
### ~
|
||||
|
||||
### ~column
|
||||
|
||||
## Advanced Games
|
||||
|
||||
### ~hide
|
||||
|
||||
* [Digital pet](/microbit/lessons/digital-pet/tutorial), a display of pet images with sub-functions
|
||||
* [Jailbreak](/microbit/lessons/jailbreak/tutorial), break out of a counting loop by pressing button "A"
|
||||
* [Transformers](/microbit/lessons/transformers/tutorial), use functions to return values
|
||||
* [Flipping bird](/microbit/lessons/flipping-bird/tutorial), use modulo with a conditional
|
||||
|
||||
### ~
|
||||
|
||||
* [Catch the egg](/microbit/lessons/catch-the-egg-game/tutorial), catch falling eggs in a basket with an acceleration controller
|
||||
* [Headbands](/microbit/lessons/headbands/tutorial), create a charades game with a collection of strings that hold the words
|
||||
* [Pong](/microbit/lessons/pong/tutorial), a light bouncing from left to right
|
||||
* [Meteorite](/microbit/lessons/meteorite/tutorial), a game where meteorites are coming for you one by one
|
||||
* [Minesweeper](/microbit/lessons/minesweeper/tutorial), make a game to test your memory for placing a LED mine then finding the hidden LED mine
|
||||
* [Bop it](/microbit/lessons/bop-it/tutorial), a game where you have to keep up with the commands
|
||||
* [Letter Up](/microbit/lessons/letter-up/tutorial), a guessing game with string operators with string at
|
||||
* [Prank WiFi](/microbit/lessons/prank-wifi/tutorial), create fake WiFi to trick your friends
|
||||
* [Runaway pac man](/microbit/lessons/runaway-pacman/tutorial), construct the game pac man with the BBC micro:bit
|
||||
* [The hat game](/microbit/lessons/the-hat-game/tutorial), make a game to test your focus on the moving ball
|
||||
* [2 player pong](/microbit/lessons/2-player-pong/tutorial), collaborate with a classmate to develop Pong on multiple BBC micro:bits
|
||||
|
||||
### ~hide
|
||||
|
||||
* [Glowing pendulum](/microbit/lessons/glowing-pendulum/tutorial), construct a pendulum that glows using acceleration
|
||||
* [Line of Fire](/microbit/lessons/line-of-fire/tutorial), make a game to test hand-eye coordination
|
||||
* [Number psych](/microbit/lessons/number-psych/tutorial), collaborate with multiple classmates to develop a game on multiple BBC micro:bits and a breadboard
|
||||
|
||||
### ~
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
# basic LED show
|
||||
|
||||
#tutorial #docs
|
||||
|
||||
### Challenge 0
|
||||
|
||||
You have successfully following the [guided tutorial] (https://live.microbit.co.uk/td/tutorials/blink). If not, we should make sure the micro:bit script displays a blinking script on screen. We want to plot the x and y coordinates to 2, 2. Additionally, you will pause by 100 milliseconds then you will clear the screen of the micro:bit. Let's give it a go!
|
||||
|
||||
```
|
||||
while (true) {
|
||||
led.plot(2, 2)
|
||||
basic.pause(200)
|
||||
basic.clearScreen()
|
||||
basic.pause(200)
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
### Challenge 1
|
||||
|
||||
Use `basic->show string` to display text after the blink. You will be writing a series of letters to display a series of letters. Try to unravel this secret code word: HELP. This line of code is within the while scope
|
||||
|
||||
Make sure to add this line of code within the `while` scope!
|
||||
|
||||
```
|
||||
while (true) {
|
||||
led1.plot(2, 2)
|
||||
basic1.pause(200)
|
||||
basic1.clearScreen()
|
||||
basic1.pause(200)
|
||||
basic1.showString("HELP", 150) // ***
|
||||
}
|
||||
```
|
||||
|
||||
* run the code and see that it works as expected
|
||||
|
||||
### Challenge 2
|
||||
|
||||
You can also display a number on screen using `basic‐>show number`. Add code under `basic‐>show string` to display the emergency number to call in the United Kingdom. (NOTE: 999 is the historic emergency number for the United Kingdom. All calls are answered by 999 operators. Calls are always free.)
|
||||
|
||||
```
|
||||
while (true) {
|
||||
led2.plot(2, 2)
|
||||
basic2.pause(200)
|
||||
basic2.clearScreen()
|
||||
basic2.pause(200)
|
||||
basic2.showString("HELP", 150)
|
||||
basic2.showNumber(999, 150) // ***
|
||||
}
|
||||
```
|
||||
|
||||
Awesome! You have designed your message and a number to call in case of an emergency.
|
||||
|
||||
### Challenge 3
|
||||
|
||||
* tap the `run` button to view the updated script on the simulator
|
||||
|
||||
Add an associated animation after the emergency number . You can also create a cool animation on screen using `basic->show animation`. Add code under `basic->show number` to display an animation.
|
||||
|
||||
```
|
||||
while (true) {
|
||||
led3.plot(2, 2)
|
||||
basic3.pause(200)
|
||||
basic3.clearScreen()
|
||||
basic3.pause(200)
|
||||
basic3.showString("HELP", 150)
|
||||
basic3.showNumber(999, 150)
|
||||
basic3.showAnimation(`
|
||||
# # . # #
|
||||
. # . # .
|
||||
. . # . .
|
||||
# . . . #
|
||||
# # # # #
|
||||
`, 400) // ***
|
||||
}
|
||||
```
|
||||
|
||||
Awesome! We have implemented a string, number, and animation
|
||||
|
||||
* run the code and see that it works as expected.
|
||||
|
||||
### Challenge 4
|
||||
|
||||
Use the same logic `basic->string`, `basic->number`, or `basic->animation` to turn on the LEDs and display information!!!
|
||||
|
||||
* run the code and see that it works as expected
|
||||
@@ -0,0 +1,111 @@
|
||||
# blink symbols
|
||||
|
||||
#tutorial #docs
|
||||
|
||||
### Challenge 0
|
||||
|
||||
You have successfully following the [blink tutorial](/microbit/hcwxud). If not, then let's start the tutorial now. Your micro:bit script should start by displaying a blinking script on screen. We want to plot the x and y coordinates to 2, 2. Additionally, you will pause by 100 milliseconds then clear the screen of the micro:bit.
|
||||
|
||||
Let's give it a go!
|
||||
|
||||
```
|
||||
while (true) {
|
||||
led.plot(2, 2)
|
||||
basic.pause(200)
|
||||
basic.clearScreen()
|
||||
basic.pause(200)
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
### Challenge 1
|
||||
|
||||
Make a `>` greater than symbol. Start in the upper left corner of the simulator when you plot coordinates. Make sure to add the line of code `led->plot (0,0)` under the last line of code
|
||||
|
||||
```
|
||||
while (true) {
|
||||
led1.plot(2, 2)
|
||||
basic1.pause(200)
|
||||
basic1.clearScreen()
|
||||
basic1.pause(200)
|
||||
led1.plot(0, 0) // ***
|
||||
}
|
||||
```
|
||||
|
||||
Design the top half of the `>` symbol by connecting a LED to the original center coordinate `2,2` and the upper left coordinate `0,0` Make sure to add the line of code `led->plot (1,1)` under the last line of code
|
||||
|
||||
```
|
||||
while (true) {
|
||||
led2.plot(2, 2)
|
||||
basic2.pause(200)
|
||||
basic2.clearScreen()
|
||||
basic2.pause(200)
|
||||
led2.plot(0, 0)
|
||||
led2.plot(1, 1) // ***
|
||||
}
|
||||
```
|
||||
|
||||
Awesome! You have designed half of the `>` symbol. Now we should finish the lower half of the `>` symbol
|
||||
|
||||
* tap the `run` button to view the updated script on the simulator
|
||||
|
||||
Add the bottom half of the `>` symbol by plotting the most bottom - left LED first. Make sure to add the line of code `led->plot (0,5)`
|
||||
|
||||
```
|
||||
while (true) {
|
||||
led3.plot(2, 2)
|
||||
basic3.pause(200)
|
||||
basic3.clearScreen()
|
||||
basic3.pause(200)
|
||||
led3.plot(0, 0)
|
||||
led3.plot(1, 1)
|
||||
led3.plot(0, 4) // ***
|
||||
}
|
||||
```
|
||||
|
||||
Awesome! Now we must connect a LED to the original center coordinate `2,2` and the lower left coordinate `0,5` Make sure to add the line of code `led->plot (1,4)`
|
||||
|
||||
Your `main` function should look like this:
|
||||
|
||||
```
|
||||
while (true) {
|
||||
led4.plot(2, 2)
|
||||
basic4.pause(200)
|
||||
basic4.clearScreen()
|
||||
basic4.pause(200)
|
||||
led4.plot(0, 0)
|
||||
led4.plot(1, 1)
|
||||
led4.plot(0, 4)
|
||||
led4.plot(1, 3) // ***
|
||||
}
|
||||
```
|
||||
|
||||
* `run` the script and see that the program works as expected
|
||||
|
||||
Congratulations! You made a `>` symbol.
|
||||
|
||||
### Challenge 2
|
||||
|
||||
Use `led->plot` to create a exclamation design `!` Your `main` function should look like this. (notice the notation of `...` represents previous code in **Challenge 0** and **Challenge 1**
|
||||
|
||||
Make sure to add these lines of code within the `while` loop
|
||||
|
||||
Your `main` function should look like this:
|
||||
|
||||
```
|
||||
while (true) {
|
||||
// ...
|
||||
led5.plot(4, 0) // ***
|
||||
led5.plot(4, 1) // ***
|
||||
led5.plot(4, 2) // ***
|
||||
led5.plot(4, 4) // ***
|
||||
}
|
||||
```
|
||||
|
||||
* run the code and see that it works as expected.
|
||||
|
||||
### Challenge 3
|
||||
|
||||
Use the same logic `led->plot` to turn on all the LED lights!!!
|
||||
|
||||
* run the code and see that it works as expected
|
||||
@@ -0,0 +1,43 @@
|
||||
# Light Column Cascade Worksheet
|
||||
|
||||
My script. #docs
|
||||
|
||||
**Challenge 0**
|
||||
|
||||
Great Job! You have completed the Light Column Cascade tutorial having a nested for loop that plots each individual LED by column adding a delay between lighting each LED.
|
||||
|
||||
```
|
||||
for (let i = 0; i < 5; i++) {
|
||||
for (let j = 0; j < 5; j++) {
|
||||
led.plot(i, j)
|
||||
basic.pause(200)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Challenge 1**
|
||||
|
||||
Make the board light up faster by making the pause less time.
|
||||
|
||||
```
|
||||
for (let i1 = 0; i1 < 5; i1++) {
|
||||
for (let j1 = 0; j1 < 5; j1++) {
|
||||
led1.plot(i1, j1)
|
||||
basic1.pause(100) // ***
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Challenge 2**
|
||||
|
||||
Make the board light up by rows instead of by columns by changing the i to the y position and j to the x position.
|
||||
|
||||
```
|
||||
for (let i2 = 0; i2 < 5; i2++) {
|
||||
for (let j2 = 0; j2 < 5; j2++) {
|
||||
led2.plot(j2, i2) // ***
|
||||
basic2.pause(100)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
# Light Column Cascade Activity
|
||||
|
||||
My script. #docs
|
||||
|
||||
**Challenge 0**
|
||||
|
||||
Great Job! You have completed the Light Column Cascade tutorial having a nested for loop that plots each individual LED by column adding a delay between lighting each LED.
|
||||
|
||||
```
|
||||
for (let i = 0; i < 5; i++) {
|
||||
for (let j = 0; j < 5; j++) {
|
||||
led.plot(i, j)
|
||||
basic.pause(200)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Challenge 1**
|
||||
|
||||
Make the board light up faster by making the pause less time.
|
||||
|
||||
```
|
||||
for (let i1 = 0; i1 < 5; i1++) {
|
||||
for (let j1 = 0; j1 < 5; j1++) {
|
||||
led1.plot(i1, j1)
|
||||
basic1.pause(100) // ***
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Challenge 2**
|
||||
|
||||
Make the board light up by rows instead of by columns by changing the i to the y position and j to the x position.
|
||||
|
||||
```
|
||||
for (let i2 = 0; i2 < 5; i2++) {
|
||||
for (let j2 = 0; j2 < 5; j2++) {
|
||||
led2.plot(j2, i2) // ***
|
||||
basic2.pause(100)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
# Scroll Image Docs
|
||||
|
||||
My script. #docs
|
||||
|
||||
**Challenge 0**
|
||||
|
||||
This [guided tutorial](/microbit/xuhkviyyxa) introduces how to make an image look like it's scrolling across the micro:bit!
|
||||
|
||||
We can use an animation to make an image look like its moving!
|
||||
|
||||
```
|
||||
basic.forever()
|
||||
```
|
||||
|
||||
**Challenge 1**
|
||||
|
||||
Now, let's reverse the animation so it looks like the bar is bouncing off the right edge of the display.
|
||||
|
||||
```
|
||||
basic1.forever()
|
||||
```
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
# TouchDevelop Lessons
|
||||
|
||||
Overview of TouchDevelop lessons for the micro:bit. #docs #contents
|
||||
|
||||
### @section full
|
||||
|
||||
### ~column
|
||||
|
||||
### LED screen
|
||||
|
||||
* [plot guided](/microbit/hcwxud) `guided tutorial ` `video available`
|
||||
* [plots an LED](/microbit/njuzbvocit) [guided tutorial]
|
||||
* [blink symbols](/microbit/rfchtfjmag) `docs`
|
||||
* [clear screen](/microbit/jwqywu)
|
||||
* [point](/microbit/reference/led/point)
|
||||
* [set brightness](/microbit/tfrmcgdtxk)
|
||||
|
||||
## micro:bit
|
||||
|
||||
## functions
|
||||
|
||||
### Basic
|
||||
|
||||
* [show number](/microbit/doxhko)
|
||||
* [show string](/microbit/hgsfxg)
|
||||
* [forever - show image](/microbit/bniyze) `guided tutorial`
|
||||
* [forever - show animation - two frames 1a](/microbit/rwsjmubtaa)
|
||||
* [forever - show animation - two frames 1c](/microbit/fomtaxxdkk)
|
||||
* [forever - show animation - two frames 1 d](/microbit/huguhgjmmn)
|
||||
* [forever - show animation - multliple frames](/microbit/tweyhx)
|
||||
|
||||
## Language {#pconst}
|
||||
|
||||
### Variables
|
||||
|
||||
* [global variables ](/microbit/nkecii) `guided tutorial`
|
||||
* [local variable - create image, show image](/microbit/dcvnwv)
|
||||
* data types: [number](/microbit/reference/types/number), [boolean](/microbit/reference/types/boolean), [string](/microbit/reference/types/string), [image](/microbit/reference/image/image)
|
||||
|
||||
### Statements and control structures
|
||||
|
||||
* [if](/microbit/reference/logic/if)
|
||||
* [for](/microbit/reference/loops/for)
|
||||
* [for loop nested - plot](/microbit/vpvhdnaqfm) **script**
|
||||
* [while](/microbit/js/while)
|
||||
* [while - show string, show number, show animation](/microbit/bidtzqdips) `docs`
|
||||
* [while - create image ](/microbit/bnqbom)
|
||||
* [return](/microbit/js/return)
|
||||
* [break](/microbit/js/break)
|
||||
* [function](/microbit/js/function)
|
||||
* [assignment operation](/microbit/reference/variables/assign) `:=`
|
||||
|
||||
### Maths
|
||||
|
||||
* arithmetic operators (`+`, `-`, `*`, `/`, mod) on [numbers](/microbit/reference/types/number)
|
||||
* comparison operators (such as `>`, `=`) on [numbers](/microbit/reference/types/number)
|
||||
* the [math](/microbit/js/math) library
|
||||
* the [bits](/microbit/js/bits) library
|
||||
|
||||
### Logical
|
||||
|
||||
* boolean operators (`not`, `or`, `and`) on [booleans](/microbit/reference/types/boolean)
|
||||
|
||||
### Strings
|
||||
|
||||
* concat operator combines [strings](/microbit/reference/types/string)
|
||||
|
||||
### ~
|
||||
|
||||
### ~column
|
||||
|
||||
### Input
|
||||
|
||||
* [button is pressed](/microbit/reference/input/button-is-pressed)
|
||||
* [on button pressed](/microbit/reference/input/on-button-pressed)
|
||||
* [acceleration](/microbit/reference/input/acceleration)
|
||||
* [compass heading](/microbit/reference/input/compass-heading)
|
||||
* [calibrate](/microbit/functions/calibrate)
|
||||
* [running time](/microbit/reference/input/running-time)
|
||||
* [on shake](/microbit/reference/input/on-gesture)
|
||||
* [on screen up](/microbit/functions/on-screen-up)
|
||||
* [on screen down](/microbit/functions/on-screen-down)
|
||||
* [on logo up](/microbit/functions/on-logo-up)
|
||||
* [on logo down](/microbit/functions/on-logo-down)
|
||||
|
||||
### ~
|
||||
|
||||
### ~column
|
||||
|
||||
### Authoring & Other Bits
|
||||
|
||||
* [TouchDevelop editor](/microbit/js/editor)
|
||||
* [markdown](/microbit/js/markdown)
|
||||
* [creating interactive tutorials](/microbit/js/creatinginteractivetutorials)
|
||||
* [run scripts in a web browser](/microbit/js/simulator)
|
||||
* [run scripts on your micro:bit](/microbit/usb)
|
||||
* [libraries](/microbit/js/libraries)
|
||||
|
||||
### Functions and libraries
|
||||
|
||||
* [creating functions](/microbit/js/function)
|
||||
* [function parameters](/microbit/js/functionparameters)
|
||||
* [calling functions](/microbit/js/call)
|
||||
* [libraries](/microbit/js/libraries)
|
||||
|
||||
### Images
|
||||
|
||||
* [create image](/microbit/reference/images/create-image)
|
||||
* [clear](/microbit/reference/basic/clear-screen)
|
||||
* [set pixel](/microbit/reference/images/set-pixel)
|
||||
* [pixel](/microbit/reference/images/pixel)
|
||||
* [show image](/microbit/reference/images/show-image)
|
||||
* [scroll image](/microbit/reference/images/scroll-image)
|
||||
* [width](/microbit/functions/width)
|
||||
* [show animation](/microbit/reference/basic/show-animation)
|
||||
|
||||
### Pins
|
||||
|
||||
* [analog read pin](/microbit/reference/pins/analog-read-pin)
|
||||
* [analog write pin](/microbit/reference/pins/analog-write-pin)
|
||||
* [digital read pin](/microbit/reference/pins/digital-read-pin)
|
||||
* [digital write pin](/microbit/reference/pins/digital-write-pin)
|
||||
|
||||
### Accessories
|
||||
|
||||
* [forever](/microbit/reference/basic/forever)
|
||||
* [in background](/microbit/reference/control/in-background)
|
||||
|
||||
## Tutorials
|
||||
|
||||
* [Blink](/script:hcwxud)
|
||||
* [Button](/script:rxqgzy)
|
||||
* [Compass](/script:fhhhwl)
|
||||
* [Counter](/script:bqrria)
|
||||
* [Digital pet](/script:lsqwsk)
|
||||
* [Flashing heart](/script:bniyze)
|
||||
* [Glowing image](/script:hydyrp)
|
||||
|
||||
### ~
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
# Hour of Code
|
||||
|
||||
learn how to run an Hour Of Code with the BBC micro:bit. #docs
|
||||
|
||||
The BBC micro:bit can be used to run an Hour Of Code™ events for beginner of all ages. This document provides a detailed guidance on how to prepare and deliver the event in your school.
|
||||
|
||||
## preparing the room
|
||||
|
||||
1) Computers
|
||||
|
||||
* Ensure that each participant will have **a computer connected to a BBC micro:bit board via a micro-USB cable**.
|
||||
|
||||
2) Internet
|
||||
|
||||
* Ensure that each computer has access to **internet**.
|
||||
|
||||
3) Website Access
|
||||
|
||||
* [https://www.microbit.co.uk](https://www.microbit.co.uk)
|
||||
|
||||
4) Raffle tickets and prizes (optional)
|
||||
|
||||
* Reward students with raffle tickets to keep them engaged. Finishing an activity or challenge on paper should equal a raffle ticket. Perform a raffle throughout the hour and give away lots of cheap prizes (candy is always a nice choice).
|
||||
|
||||
5) Music (optional)
|
||||
|
||||
* We recommend playing the latest hits (loudly) while the students are coding. It creates a playful atmosphere and makes the entire experience more enjoyable. Many web sites offer streaming music, but be sure to try it in advance as certain sites may be blocked on your network.
|
||||
|
||||
## preparing the student handouts
|
||||
|
||||
Print the following **activities** (1 handout per student):
|
||||
|
||||
* [answering machine](/microbit/lessons/answering-machine/activity)
|
||||
* [happy birthday](/microbit/lessons/happy-birthday/activity)
|
||||
* [love meter](/microbit/lessons/love-meter/activity)
|
||||
|
||||
Print the following **challenges** (1 handout per student):
|
||||
|
||||
* [answering machine](/microbit/lessons/answering-machine/challenges)
|
||||
* [happy birthday](/microbit/lessons/happy-birthday/challenges)
|
||||
* [love meter](/microbit/lessons/love-meter/challenges)
|
||||
|
||||
## Timeline
|
||||
|
||||
* ``00:00`` students enter the website address (see step 3)
|
||||
* ``10:00`` [answering machine](/microbit/lessons/answering-machine/activity)
|
||||
* ``25:00`` [happy birthday](/microbit/lessons/happy-birthday/activity)
|
||||
* ``35:00`` [love meter](/microbit/lessons/love-meter/activity)
|
||||
* ``55:00`` raffle
|
||||
* ``60:00`` that's it!
|
||||
|
||||
## Follow up
|
||||
|
||||
After your Hour Of Code™, you will want to provide plenty of material for students to continue learning about coding. Here are some good places to start:
|
||||
|
||||
* [more challenges](/microbit/js/games) are available with BBC micro:bit Tutorials
|
||||
* [the Quick Start Guide for Teachers](http://www.slideshare.net/Microsofteduk/bbc-microbit-guide-from-hodder-education) are available within BBC micro:bit
|
||||
|
||||
The 'Hour of Code™' is a nationwide initiative by [Computer Science Education Week](http://csedweek.org) and [Code.org](http://code.org) to introduce millions of students to one hour of computer science and computer programming.
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
# Hour of Code notes
|
||||
|
||||
learn how to run an Hour Of Code with the BBC micro:bit. #docs
|
||||
|
||||
The BBC micro:bit can be used to run an Hour Of Code™ event for beginner of all ages. This document provides a detailed guidance on how to prepare and deliver the event in your school.
|
||||
|
||||
## Preparation
|
||||
|
||||
1) Computers
|
||||
|
||||
Each participant has **a computer connected to a BBC micro:bit via micro-USB**.
|
||||
|
||||
2) Internet
|
||||
|
||||
Ensure that each computer has access to **internet**.
|
||||
|
||||
3) Accounts
|
||||
|
||||
Create a classroom in https://www.microbit.co.uk and pre-populate the classroom with student accounts. **Print the student passwords** and cut out each password.
|
||||
|
||||
4) Print the activity challenges (1 copy per participant):
|
||||
|
||||
* [hour of code](/microbit/js/hourofcode/challenges)
|
||||
|
||||
4) (optional) Raffle tickets and prizes
|
||||
|
||||
Reward students with raffle tickets to keep them engaged. Finishing a tutorial or challenge on paper should equal a raffle ticket. Perform a raffle throughout the hour and give away lots of cheap prizes (candy is always a nice choice).
|
||||
|
||||
5) (optional) Music
|
||||
|
||||
Bring more energy in the room by playing music.
|
||||
|
||||
## Timeline
|
||||
|
||||
* ``00:00`` student sign in using **printed passwords** (see step 3)
|
||||
* ``10:00`` [hour of code tutorial](/microbit/js/hourofcode)
|
||||
* ``40:00`` raffle and demoes
|
||||
* ``50:00`` that's it!
|
||||
|
||||
## Follow up
|
||||
|
||||
After your Hour Of Code™, you will want to provide plenty of material for students to continue learning about coding. Here are some good places to start:
|
||||
|
||||
* [more challenges](/microbit/lessonss) are available for BBC micro:bit
|
||||
* [the Quick Start Guide for Teachers](http://www.slideshare.net/Microsofteduk/bbc-microbit-guide-from-hodder-education) are available within BBC micro:bit
|
||||
|
||||
_The Hour of Code™ is a nationwide initiative by [Computer Science Education Week](http://csedweek.org) and [Code.org](http://code.org) to introduce millions of students to one hour of computer science and computer programming._
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
# If
|
||||
|
||||
Run code based on a condition. #docs #if #then #else #endif #language #elseif
|
||||
|
||||
### @parent js/language
|
||||
|
||||
|
||||
Conditionally run code depending on whether a [Boolean](/microbit/reference/types/boolean) condition is true or false.
|
||||
|
||||
### Block Editor
|
||||
|
||||

|
||||
|
||||
In the Block Editor, click on the dark blue gear icon (see above) to add an *else* or *if* to the current block.
|
||||
|
||||
### Touch Develop
|
||||
|
||||
### ~hide
|
||||
|
||||
```
|
||||
let condition = true
|
||||
```
|
||||
|
||||
### ~
|
||||
|
||||
```
|
||||
if (condition) {
|
||||
// this code runs if `condition` is `true`
|
||||
} else {
|
||||
// this code runs if `condition` is `false`
|
||||
}
|
||||
```
|
||||
|
||||
### Example: adjusting screen brightness
|
||||
|
||||
If the screen [brightness](/microbit/reference/led/brightness) is `< 100`, this code sets the brightness to `255`:
|
||||
|
||||
```
|
||||
if (led.brightness() < 100) {
|
||||
led.setBrightness(255)
|
||||
}
|
||||
```
|
||||
|
||||
You can leave the `then` or `else` blocks empty if they aren't needed.
|
||||
|
||||
### Else if: multiple if statements
|
||||
|
||||
You can chain together if statements by using `else if`. Like this:
|
||||
|
||||
### ~hide
|
||||
|
||||
```
|
||||
let otherCondition = true
|
||||
```
|
||||
|
||||
### ~
|
||||
|
||||
```
|
||||
if (condition) {
|
||||
// this code runs if `condition` is `true`
|
||||
} else if (otherCondition) {
|
||||
// this code runs if `other condition` is `true`
|
||||
}
|
||||
else {
|
||||
// this code runs if neither `condition` or `other condition` are `true`
|
||||
}
|
||||
```
|
||||
|
||||
### Example: compass heading
|
||||
|
||||
The following example gets the [compass heading](/microbit/reference/input/compass-heading) and then uses ``if-then-else`` statements to display a letter on the screen (N for north, E for East, S for South, and W for West).
|
||||
|
||||
```
|
||||
while (true) {
|
||||
let degrees = input.compassHeading()
|
||||
if (degrees < 45) {
|
||||
basic.showString("N", 100)
|
||||
} else if (degrees < 135) {
|
||||
basic.showString("E", 100)
|
||||
}
|
||||
else if (degrees < 225) {
|
||||
basic.showString("S", 100)
|
||||
}
|
||||
else {
|
||||
basic.showString("W", 100)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Drag and drop
|
||||
|
||||
You can move an entire ``if`` block by clicking the ``if`` keyword and dragging and dropping.
|
||||
|
||||
### Lessons
|
||||
|
||||
[love meter](/microbit/lessons/love-meter), [zoomer](/microbit/lessons/zoomer), [offset image](/microbit/lessons/offset-image)
|
||||
|
||||
### See also
|
||||
|
||||
[while loop](/microbit/js/while), [for](/microbit/reference/loops/for), [boolean](/microbit/reference/types/boolean)
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
# Image
|
||||
|
||||
An image for the micro:bit screen. #docs #image #screen #LED
|
||||
|
||||
### @parent js/language
|
||||
|
||||
An *Image* is a matrix of pixels to show on the [LED screen](/microbit/device/screen)
|
||||
|
||||
### Touch Develop editor: plot an image
|
||||
|
||||
To display an image using the [Touch Develop editor](/microbit/js/editor):
|
||||
|
||||
* click `image` , `plot image`, and then `edit`
|
||||
* click the rectangles to create an image
|
||||
* when you're done, click **ok** to return to your code
|
||||
|
||||

|
||||
|
||||
You should see code similar to this:
|
||||
|
||||
```
|
||||
basic.plotImage(`
|
||||
. . . . .
|
||||
. # . # .
|
||||
. . # . .
|
||||
. # . # .
|
||||
. . . . .
|
||||
`)
|
||||
```
|
||||
|
||||
### Creating an image
|
||||
|
||||
To create an image that you can later modify, see the [create image](/microbit/reference/images/create-image) function.
|
||||
|
||||
### Block editor: create and show images
|
||||
|
||||
To create images using the [Block editor](/microbit/blocks/editor):
|
||||
|
||||
1. Click the **Images** category on the left.
|
||||
|
||||
2. Drag and drop the **show image** block into your code.
|
||||
|
||||
3. Drag and drop the **create image** or **create big image** block onto the **show image** block so that they connect.
|
||||
|
||||
4. Make an image on the **create image** block by clicking on the squares.
|
||||
|
||||
### Global image variables
|
||||
|
||||
Images that you create in the [Touch Develop editor](/microbit/js/editor) are [local variables](/microbit/reference/variables/var). To promote a local image variable to a global variable, select the local image variable and click `promote to data`. The *var* keyword changes to the [data](/microbit/js/data) symbol `data->`.
|
||||
|
||||
### Image functions
|
||||
|
||||
* [create image](/microbit/reference/images/create-image): create an image from a series of on/off LED states
|
||||
* [clear](/microbit/reference/basic/clear-screen): turn off all the pixels in an image
|
||||
* [set pixel](/microbit/reference/images/set-pixel): set the state of a pixel in an image
|
||||
* [pixel](/microbit/reference/images/pixel): get the state of a pixel in an image
|
||||
* [plot-image](/microbit/reference/led/plot-image): show a single-frame image on the LED screen
|
||||
* [show animation](/microbit/reference/basic/show-animation): show a series of image frames
|
||||
* [show image](/microbit/reference/images/show-image): show an image on the screen
|
||||
* [scroll image](/microbit/reference/images/scroll-image): scroll an image on the screen
|
||||
* [width](/microbit/functions/width): get the width of an image
|
||||
|
||||
### Lessons
|
||||
|
||||
* [bounce image ](/microbit/lessons/bounce-image)
|
||||
* [offset image](/microbit/lessons/offset-image)
|
||||
|
||||
### See also
|
||||
|
||||
[plot image](/microbit/reference/led/plot-image), [create image](/microbit/reference/images/create-image), [show image](/microbit/reference/images/show-image), [LED screen](/microbit/device/screen)
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
# Touch Develop Lessons
|
||||
|
||||
Overview of Touch Develop lessons for the BBC micro:bit. #docs #contents
|
||||
|
||||
### @short Lessons
|
||||
|
||||
### ~column
|
||||
|
||||
## Maker
|
||||
|
||||
* [The Watch](/microbit/lessons/the-watch), design and create The Watch
|
||||
* [Hack your Headphones](/microbit/lessons/hack-your-headphones), create music on the BBC micro:bit by hacking your headphones
|
||||
* [Banana Keyboard](/microbit/lessons/banana-keyboard), create music with fruits
|
||||
* [Telegraph](/microbit/lessons/telegraph), play the telegraph game between 2 BBC micro:bits
|
||||
* [Ornament Chain](/microbit/lessons/ornament-chain), play the ornament chain game between 2 BBC micro:bits
|
||||
|
||||
## Beginner
|
||||
|
||||
* [Beautiful Image](/microbit/lessons/beautiful-image), show a beautiful image with show LEDs
|
||||
* [Smiley,](/microbit/lessons/smiley) smiley and frowney with show animation
|
||||
* [Lucky 7](/microbit/lessons/lucky-7), show a number on the LED screen with show number
|
||||
* [Answering Machine](/microbit/lessons/answering-machine), show a text message with show string
|
||||
* [Snowflake Fall](/microbit/lessons/snowflake-fall), repeat an animation with forever
|
||||
|
||||
### ~hide
|
||||
|
||||
* [Bounce Image](/microbit/lessons/bounce-image), scroll an image across the screen on shake
|
||||
|
||||
### ~
|
||||
|
||||
* [Magic Logo](/microbit/lessons/magic-logo), show an image on logo up
|
||||
* [Screen Wipe](/microbit/lessons/screen-wipe), turn off the LEDs with clear screen
|
||||
* [Flashing Heart](/microbit/lessons/flashing-heart), display images with a pause
|
||||
* [Blink](/microbit/lessons/blink), turn an LED on and off with plot
|
||||
* [Night Light](/microbit/lessons/night-light), dim the LEDs with set brightness
|
||||
* [Glowing Sword](/microbit/lessons/glowing-sword), make a glowing sword with fade in and fade out
|
||||
|
||||
### ~column
|
||||
|
||||
## Intermediate
|
||||
|
||||
* [Magic 8](/microbit/lessons/magic-8), a fortune teller game with the BBC micro:bit
|
||||
* [Guess the Number](/microbit/lessons/guess-the-number), guess a random number with random
|
||||
* [Rock Paper Scissors](/microbit/lessons/rock-paper-scissors), use image offsets with local variables
|
||||
* [Counter](/microbit/lessons/counter), display a number with a variable
|
||||
* [Love meter](/microbit/lessons/love-meter), create a love meter with on pin pressed
|
||||
* [Zoomer](/microbit/lessons/zoomer), measure the force with acceleration
|
||||
* [Glowing Pendulum](/microbit/lessons/glowing-pendulum), construct a pendulum that glows using acceleration
|
||||
* [Truth or Dare](/microbit/lessons/truth-or-dare), a game that forces each player to reveal a secret or do something funny with if statement
|
||||
* [Spinner](/microbit/lessons/spinner), spin the arrow with multiple if statements
|
||||
* [Die Roll](/microbit/lessons/die-roll), spin with more if statements
|
||||
* [Looper](/microbit/lessons/looper), display a series of numbers with a for loop index
|
||||
* [Strobe Light](/microbit/lessons/strobe-light), develop shapes with a nested for loops
|
||||
* [Digi Yoyo](/microbit/lessons/digi-yoyo), create a counter with a while loop
|
||||
* [Rotation Animation](/microbit/lessons/rotation-animation), control an animation with a boolean variable
|
||||
* [Offset Image](/microbit/lessons/offset-image), shift an image horizontally with image offset
|
||||
* [Compass](/microbit/lessons/compass), displays the direction the BBC micro:bit is pointing
|
||||
|
||||
### ~
|
||||
|
||||
### ~column
|
||||
|
||||
## Advanced
|
||||
|
||||
* [Digital Pet](/microbit/lessons/digital-pet), a display of pet images with sub-functions
|
||||
* [Transformers](/microbit/lessons/transformers), use functions to return values
|
||||
* [Speed Button](/microbit/lessons/speed-button), code a speed game with running time
|
||||
* [Catch the Egg](/microbit/lessons/catch-the-egg-game), catch falling eggs in a basket with an acceleration controller
|
||||
* [Headbands](/microbit/lessons/headbands), create a charades game with a collection of strings that hold the words
|
||||
* [Prank WiFi](/microbit/lessons/prank-wifi), create fake WiFi to trick your friends
|
||||
* [Jailbreak](/microbit/lessons/jailbreak), break out of a counting loop by pressing button "A"
|
||||
* [Flipping Bird](/microbit/lessons/flipping-bird), use modulo with a conditional
|
||||
* [Runaway Pac Man](/microbit/lessons/runaway-pacman), construct the game pac man with the BBC micro:bit
|
||||
* [Line of Fire](/microbit/lessons/line-of-fire), make a game to test hand-eye coordination
|
||||
* [The Hat Game](/microbit/lessons/the-hat-game), make a game to test your focus on the moving ball
|
||||
* [2 Player Pong](/microbit/lessons/2-player-pong), collaborate with a classmate to develop Pong on multiple BBC micro:bits
|
||||
|
||||
### Games
|
||||
|
||||
* [Pong](/microbit/lessons/pong), a light bouncing from left to right
|
||||
* [Meteorite](/microbit/lessons/meteorite), a game where meteorites are coming for you one by one
|
||||
* [Minesweeper](/microbit/lessons/minesweeper), make a game to test your memory for placing a LED mine then finding the hidden LED mine
|
||||
* [Bop it](/microbit/lessons/bop-it), a game where you have to keep up with the commands
|
||||
* [Letter up](/microbit/lessons/letter-up), a guessing game with string operators with string at
|
||||
|
||||
### ~
|
||||
|
||||
### ~hide
|
||||
|
||||
* [Number Psych](/microbit/lessons/number-psych), collaborate with multiple classmates to develop a game on multiple BBC micro:bits and a breadboard
|
||||
|
||||
### ~
|
||||
|
||||
### @section full
|
||||
|
||||
The lessons promote computational thinking and computer science literacy[ read more...](/microbit/lessons/teach)
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
# 2 player pong lesson
|
||||
|
||||
make a game to test your focus on the moving ball #function #on-button-pressed #if #show-animation #mod #random #Boolean #docs
|
||||
|
||||
## Topic
|
||||
|
||||
Functions
|
||||
|
||||
## Quick Links
|
||||
|
||||
* [tutorial](/microbit/lessons/2-player-pong/tutorial)
|
||||
|
||||
## Class
|
||||
|
||||
Year 7
|
||||
|
||||
## Prior learning/place of lesson in scheme of work
|
||||
|
||||
Learn how to create **functions**, `function()` as a unit of code that performs a specific task and returns a result. We will be learning how to create the hat game app using functions, global variables, input on button pressed, if (conditionals), mod, random, Boolean, as well as simple commands such as show animation.
|
||||
|
||||
## What the teacher needs to know/QuickStart Computing Glossary
|
||||
|
||||
* Algorithm: An unambiguous set of rules or a precise step-bystep guide to solve a problem or achieve a particular objective.
|
||||
* Command: An instruction for the computer to execute, written in a particular programming language.
|
||||
* Data: A structured set of numbers, possibly representing digitised text, images, sound or video, which can be processed or transmitted by a computer, also used for numerical (quantitative) information.
|
||||
* Hardware: The physical systems and components of digital devices; see also software.
|
||||
* Input: Data provided to a computer system, such as via a keyboard, mouse, microphone, camera or physical sensors.
|
||||
* Programmable toys: Robots designed for children to use, accepting input, storing short sequences of simple instructions and moving according to this stored program.
|
||||
* Program: A stored set of instructions encoded in a language understood by the computer that does some form of computation, processing input and/or stored data to generate output.
|
||||
* Repetition: Executing a section of computer code a number of times as part of the program.
|
||||
* Script: A computer program typically executed one line at a time through an interpreter, such as the instructions for a Scratch character.
|
||||
* Selection: A programming construct in which one section of code or another is executed depending on whether a particular condition is met.
|
||||
* Sequence: To place program instructions in order, with each executed one after the other.
|
||||
* Simulation: Using a computer to model the state and behaviour of real-world (or imaginary) systems, including physical or social systems; an integral part of most computer games.
|
||||
* Variables: A way in which computer programs can store, retrieve or change data, such as a score, the time left, or the user’s name.
|
||||
|
||||
## Documentation
|
||||
|
||||
* **functions** : [read more...](/microbit/js/function)
|
||||
* **on button pressed** : [read more...](/microbit/reference/input/on-button-pressed)
|
||||
* **for** : [read more...](/microbit/reference/loops/for)
|
||||
* **if** : [read more...](/microbit/reference/logic/if)
|
||||
* **show animation** : [read more...](/microbit/reference/basic/show-animation)
|
||||
|
||||
## Resources
|
||||
|
||||
* Activity: [tutorial](/microbit/lessons/2-player-pong/tutorial)
|
||||
|
||||
## Objectives
|
||||
|
||||
* learn how to create a global variable as a place where you can store data so that you can use it later in your code, accessible across functions and in nested code blocks
|
||||
* learn how to learn how to conditionally run code depending on whether a condition is true or no
|
||||
* learn how to show a series of image frames on the LED screen
|
||||
* learn how to run code when an input button is pressed
|
||||
|
||||
## Links to the National Curriculum Programmes of Study for Computing
|
||||
|
||||
## Progression Pathways / Computational Thinking Framework
|
||||
|
||||
#### Algorithms
|
||||
|
||||
* Designs solutions (algorithms) that use repetition and two-way selection, ie if, then and else.(AL)
|
||||
* Uses diagrams to express solutions.(AB)
|
||||
* Uses logical reasoning to predict outputs, showing an awareness of inputs (AL)
|
||||
* Represents solutions using a structured notation (AL) (AB)
|
||||
|
||||
#### Programming & Development
|
||||
|
||||
* Creates programs that implement algorithms to achieve given goals (AL)
|
||||
* Declares and assigns variables(AB)
|
||||
* Uses post-tested loop e.g.‘until’,and a sequence of selection statements in programs,including an if,then and else statement(AL)
|
||||
* Understands the difference between, and appropriately uses if and if, then and else statements(AL)
|
||||
* Uses a variable and relational operators within a loop to govern termination (AL) (GE)
|
||||
* Uses a range of operators and expressions e.g. Boolean, and applies them in the context of program control. (AL)
|
||||
* Selects the appropriate data types(AL) (AB
|
||||
|
||||
#### Data & Data Representation
|
||||
|
||||
* Understands the difference between data and information(AB)
|
||||
* Performs more complex searches for information e.g. using Boolean and relational operators(AL) (GE) (EV)
|
||||
* Defines data types: real numbers and Boolean (AB)
|
||||
|
||||
#### Hardware & Processing
|
||||
|
||||
* Knows that computers collect data from various input devices, including sensors and application software (AB)
|
||||
|
||||
#### Information Technology
|
||||
|
||||
* Collects, organizes, and presents data and information in digital content (AB)
|
||||
* Makes appropriate improvements to solutions based on feedback received, and can comment on the success of the solution (EV)
|
||||
* Recognises ethical issues surrounding the application of information technology beyond school.
|
||||
|
||||
Computational Thinking Concept: AB = Abstraction; DE = Decomposition; AL = Algorithmic Thinking; EV = Evaluation; GE = Generalisation
|
||||
|
||||
## Activity
|
||||
|
||||
* time: 20 min.
|
||||
* [tutorial](/microbit/lessons/2-player-pong/tutorial)
|
||||
|
||||
## Intended follow on
|
||||
|
||||
Publish script to the classroom.
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
# 2 player pong quiz answers
|
||||
|
||||
a two-player game of Pong using TWO BBC micro:bits! #LED #number #math #acceleration #docs
|
||||
|
||||
## Name
|
||||
|
||||
## Directions
|
||||
|
||||
Use this activity document to guide your work in the [2 player pong tutorial](/microbit/lessons/2-player-pong/tutorial)
|
||||
|
||||
Answer the questions while completing the tutorial. Pay attention to the dialogues!
|
||||
|
||||
## 1. Write the two global variables that record if the player has the ball and if the game is running, and assign these variables to their initial values.
|
||||
|
||||
<br/>
|
||||
|
||||
```
|
||||
hasBall = false
|
||||
gameRunning = false
|
||||
```
|
||||
|
||||
## 2. Write the global variable that keeps track of the game state in which whoever presses button A first will get to start the ball. Assign this variable to its initial value.
|
||||
|
||||
<br/>
|
||||
|
||||
```
|
||||
claimBall = true
|
||||
```
|
||||
|
||||
## 3. Write the code that creates a condition to know when Button A is pressed. Then write the 'If statement' to ensure that 'claim ball' is true. If the claim ball is true, then write the code that sets P0 to 1 to signal to the other device that the player has claimed the ball.
|
||||
|
||||
<br/>
|
||||
|
||||
```
|
||||
input.onButtonPressed("A", () => {
|
||||
if (claimBall) {
|
||||
pins.digitalWritePin("P0", 1)
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
## 4. Write the code to move the paddle right when button B is pressed. Be sure to check if the game is running and if the paddle is not already on the rightmost edge.
|
||||
|
||||
<br/>
|
||||
|
||||
```
|
||||
if (gameRunning) {
|
||||
if (paddleX != 0) {
|
||||
led.unplot(paddleX, 4)
|
||||
paddleX = paddleX - 1
|
||||
led.plot(paddleX, 4)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 5. What are the three pieces of information that we send to the other device when transferring the ball? (Hint: look in your "transfer ball" function, and look for any places that contain "transfer byte").
|
||||
|
||||
<br/>
|
||||
|
||||
The device first transfers a bit of 1 to indicate that the device is going to transfer the data of the ball. After that, the device transfers the x-coordinate of the ball, and then the x-velocity of the ball.
|
||||
|
||||
## 6. Using the function "read velocity", write the code that reads the x-coordinate and the x-velocity of the ball. (Hint: look at the function "read ball".)
|
||||
|
||||
<br/>
|
||||
|
||||
```
|
||||
ballX = micro_bitTransfer.readByte()
|
||||
ballXVelocity = readVelocity()
|
||||
```
|
||||
|
||||
## 7. Write the code that updates 'ball x velocity'. (Hint: look at the "update velocity" function.)
|
||||
|
||||
<br/>
|
||||
|
||||
```
|
||||
if (ballX == 0 || ballX == 4) {
|
||||
ballXVelocity = ballXVelocity * (-1)
|
||||
}
|
||||
```
|
||||
|
||||
## 8. Write the code to move the ball. To move the ball, unplot the ball's original position, update its position variables, and then plot the ball's new position.
|
||||
|
||||
<br/>
|
||||
|
||||
```
|
||||
led.unplot(ballX, 0)
|
||||
ballX = ballX + ballXVelocity
|
||||
ballY = ballY + ballYVelocity
|
||||
led.plot(ballX, ballY)
|
||||
```
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
# 2 player pong quiz
|
||||
|
||||
a two-player game of Pong using TWO BBC micro:bits! #LED #number #math #acceleration #docs
|
||||
|
||||
## Name
|
||||
|
||||
## Directions
|
||||
|
||||
Use this activity document to guide your work in the [2 player pong tutorial](/microbit/lessons/2-player-pong/tutorial)
|
||||
|
||||
Answer the questions while completing the tutorial. Pay attention to the dialogues!
|
||||
|
||||
## 1. Write the two global variables that record if the player has the ball and if the game is running, and assign these variables to their initial values.
|
||||
|
||||
<br/>
|
||||
|
||||
<br/>
|
||||
|
||||
## 2. Write the global variable that keeps track of the game state in which whoever presses button A first will get to start the ball. Assign this variable to its initial value.
|
||||
|
||||
<br/>
|
||||
|
||||
## 3. Write the code that creates a condition to know when Button A is pressed. Then write the 'If statement' to ensure that 'claim ball' is true. If the 'claim ball' is true, then write the code that sets P0 to 1 to signal to the other device that the player has claimed the ball.
|
||||
|
||||
<br/>
|
||||
|
||||
<br/>
|
||||
|
||||
<br/>
|
||||
|
||||
<br/>
|
||||
|
||||
## 4. Write the code to move the paddle right when button B is pressed. Be sure to check if the game is running and if the paddle is not already on the rightmost edge.
|
||||
|
||||
<br/>
|
||||
|
||||
<br/>
|
||||
|
||||
<br/>
|
||||
|
||||
<br/>
|
||||
|
||||
## 5. What are the three pieces of information that we send to the other device when transferring the ball? (Hint: look in your "transfer ball" function, and look for any places that contain "transfer byte").
|
||||
|
||||
<br/>
|
||||
|
||||
<br/>
|
||||
|
||||
## 6. Using the function "read velocity", write the code that reads the x-coordinate and the x-velocity of the ball. (Hint: look at the function "read ball".)
|
||||
|
||||
<br/>
|
||||
|
||||
<br/>
|
||||
|
||||
<br/>
|
||||
|
||||
## 7. Write the code that updates 'ball x velocity'. (Hint: look at the "update velocity" function.)
|
||||
|
||||
<br/>
|
||||
|
||||
<br/>
|
||||
|
||||
## 8. Write the code to move the ball. To move the ball, unplot the ball's original position, update its position variables, and then plot the ball's new position.
|
||||
|
||||
<br/>
|
||||
|
||||
<br/>
|
||||
|
||||
<br/>
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
# zoomer challenges
|
||||
|
||||
The acceleration function. #acceleration #docs #input
|
||||
|
||||
**Challenge 0**
|
||||
|
||||
Great job! You have successfully completed the [zoomer tutorial](https://test.microbit.co.uk/td/lessons/zoomer/challenges) . You have created a script that measures the acceleration on the micro:bit in the "z" direction of a 3D world.
|
||||
|
||||
```
|
||||
basic.forever(() => {
|
||||
let millig = input.acceleration("z")
|
||||
basic.showNumber(millig, 150)
|
||||
basic.pause(100)
|
||||
})
|
||||
```
|
||||
|
||||
**Challenge 1**
|
||||
|
||||
Create a new variable called milliX that holds the acceleration in the "x" direction or the horizontal direction.
|
||||
|
||||
```
|
||||
basic.forever(() => {
|
||||
let millig1 = input.acceleration("z")
|
||||
basic.showNumber(millig1, 150)
|
||||
basic.pause(100)
|
||||
let milliX = input.acceleration("x") // ***
|
||||
})
|
||||
```
|
||||
|
||||
* Run the code to see if it works as expected.
|
||||
|
||||
**Challenge 2**
|
||||
|
||||
If Button `A` is pressed, we want to show the acceleration in the "x" direction by adding an if statement that checks to see if Button `A` is pressed and then calling the show number method passing in milliX as the number.
|
||||
|
||||
```
|
||||
basic.forever(() => {
|
||||
let millig2 = input.acceleration("z")
|
||||
basic.showNumber(millig2, 150)
|
||||
basic.pause(100)
|
||||
let milliX1 = input.acceleration("x")
|
||||
if (input.buttonIsPressed("A")) {
|
||||
basic.showNumber(milliX1, 150) // ***
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
* Run the code to see if it works as expected.
|
||||
|
||||
### Challenge 3
|
||||
|
||||
If Button `B` is pressed, program the micro:bit to display the acceleration in the "y" direction.
|
||||
|
||||
You can do this by storing the acceleration in a variable: `var milliY := input->acceleration("y")`.
|
||||
|
||||
Then add an `if` statement that checks if Button `B` is pressed: `if input-> button is pressed ("B") then`.
|
||||
|
||||
Inside of the `if` statement, add `basic->show number(milliY, 150)`, which will display the acceleration in the "y" direction.
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
# answering machine lesson
|
||||
|
||||
create an answering machine on the BBC micro:bit #docs
|
||||
|
||||
### @video td/videos/answering-machine-0
|
||||
|
||||
## Topic
|
||||
|
||||
Show String
|
||||
|
||||
## Quick Links
|
||||
|
||||
* [activity](/microbit/lessons/answering-machine/activity)
|
||||
* [quiz](/microbit/lessons/answering-machine/quiz)
|
||||
* [quiz answers](/microbit/lessons/answering-machine/quiz-answers)
|
||||
* [challenges](/microbit/lessons/answering-machine/challenges)
|
||||
* [tutorial](/microbit/lessons/answering-machine/tutorial)
|
||||
|
||||
## Class
|
||||
|
||||
Year 7
|
||||
|
||||
## Prior learning / place of lesson in scheme of work
|
||||
|
||||
Learn how to creating a message with a **string**, `basic->show string` to write your message. We will be learning how to create a message using simple commands, such as show string and on button pressed.
|
||||
|
||||
## Documentation
|
||||
|
||||
* **show string** : [read more...](/microbit/reference/basic/show-string)
|
||||
* **on button pressed** : [read more...](/microbit/reference/input/on-button-pressed)
|
||||
|
||||
## Objectives
|
||||
|
||||
* learn how to show a string on the LED screen one character at a time
|
||||
* learn how to use to register an event handler that will execute whenever an input button is pressed
|
||||
|
||||
## Progression Pathways / Computational Thinking Framework
|
||||
|
||||
#### Algorithms
|
||||
|
||||
* Uses diagrams to express solutions.(AB)
|
||||
* Uses logical reasoning to predict outputs, showing an awareness of inputs (AL)
|
||||
* Represents solutions using a structured notation (AL) (AB)
|
||||
|
||||
#### Programming & Development
|
||||
|
||||
* Creates programs that implement algorithms to achieve given goals (AL)
|
||||
|
||||
#### Hardware & Processing
|
||||
|
||||
* Knows that computers collect data from various input devices, including sensors and application software (AB)
|
||||
|
||||
#### Communication Networks
|
||||
|
||||
* Demonstrates responsible use of technologies and online services, and knows a range of ways to report concerns (AL)
|
||||
|
||||
#### Information Technology
|
||||
|
||||
* Collects, organizes, and presents data and information in digital content (AB)
|
||||
* Makes appropriate improvements to solutions based on feedback received, and can comment on the success of the solution (EV)
|
||||
* Uses criteria to evaluate the quality of solutions, can identify improvements making some refinements to the solution, and future solutions (EV)
|
||||
* Evaluates the appropriatness of digital devices, internet services and application software to achieve given goals (EV)
|
||||
* Recognises ethical issues surrounding the application of information technology beyond school.
|
||||
|
||||
Computational Thinking Concept: AB = Abstraction; DE = Decomposition; AL = Algorithmic Thinking; EV = Evaluation; GE = Generalisation
|
||||
|
||||
## Activity
|
||||
|
||||
* time: 20 min.
|
||||
* [activity](/microbit/lessons/answering-machine/activity)
|
||||
* [quiz](/microbit/lessons/answering-machine/quiz)
|
||||
|
||||
## Extended Activity
|
||||
|
||||
* time: 20 min.
|
||||
* [challenges](/microbit/lessons/answering-machine/challenges)
|
||||
|
||||
## Homework
|
||||
|
||||
* Extended Activity: [challenges](/microbit/lessons/answering-machine/challenges)
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
# banana keyboard blocks lesson
|
||||
|
||||
display beautiful images on the BBC micro:bit #var #pause #docs
|
||||
|
||||
## Topic
|
||||
|
||||
Music
|
||||
|
||||
## Quick Links
|
||||
|
||||
* [activity](/microbit/lessons/banana-keyboard/activity)
|
||||
|
||||
## Class
|
||||
|
||||
Year 7
|
||||
|
||||
## Prior learning/place of lesson in scheme of work
|
||||
|
||||
Learn how to convert your BBC micro:bit into a music player using pins P0 and GND, earphones (or speakers), as well as crocodile clips (or spring clips). The connect fruit using pins P1 and GND.
|
||||
|
||||
## Objectives
|
||||
|
||||
* learn how to setup the BBC micro:bit with earphones to play music
|
||||
* learn how to setup the BBC micro:bit with fruit be the musical instrument
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
# beautiful image lesson
|
||||
|
||||
display beautiful images on the BBC micro:bit #var #pause #docs
|
||||
|
||||
### @video td/videos/beautiful-image-0
|
||||
|
||||
## Topic
|
||||
|
||||
Show LEDs
|
||||
|
||||
## Quick Links
|
||||
|
||||
* [activity](/microbit/lessons/beautiful-image/activity)
|
||||
* [quiz](/microbit/lessons/beautiful-image/quiz)
|
||||
* [quiz answers](/microbit/lessons/beautiful-image/quiz-answers)
|
||||
* [challenges](/microbit/lessons/beautiful-image/challenges)
|
||||
* [tutorial](/microbit/lessons/beautiful-image/tutorial)
|
||||
|
||||
## Class
|
||||
|
||||
Year 7
|
||||
|
||||
## Prior learning/place of lesson in scheme of work
|
||||
|
||||
Learn how to **show LEDs**, to show an image on the BBC micro:bit's LED screen. We will be learning how to Show LEDs using simple commands such as Show LEDs and pause.
|
||||
|
||||
## Documentation
|
||||
|
||||
* **show LEDs** : [read more...](/microbit/reference/basic/show-leds)
|
||||
* **pause** : [read more...](/microbit/reference/basic/pause)
|
||||
|
||||
## Objectives
|
||||
|
||||
* learn how to display an image on the micro:bit's LED screen
|
||||
* learn how to pause your code for the specified number of milliseconds
|
||||
|
||||
## Progression Pathways / Computational Thinking Framework
|
||||
|
||||
#### Algorithms
|
||||
|
||||
* Uses diagrams to express solutions.(AB)
|
||||
* Represents solutions using a structured notation (AL) (AB)
|
||||
* Can identify similarities and differences in situations and can use these to solve problems (pattern recognition)(GE)
|
||||
|
||||
#### Programming & Development
|
||||
|
||||
* Creates programs that implement algorithms to achieve given goals (AL)
|
||||
* Declares and assigns variables(AB)
|
||||
* Selects the appropriate data types(AL) (AB
|
||||
|
||||
#### Communication Networks
|
||||
|
||||
* Demonstrates responsible use of technologies and online services, and knows a range of ways to report concerns Understands how search engines rank search results (AL)
|
||||
|
||||
#### Information Technology
|
||||
|
||||
* Collects, organizes, and presents data and information in digital content (AB)
|
||||
* Makes appropriate improvements to solutions based on feedback received, and can comment on the success of the solution (EV)
|
||||
|
||||
Computational Thinking Concept: AB = Abstraction; DE = Decomposition; AL = Algorithmic Thinking; EV = Evaluation; GE = Generalisation
|
||||
|
||||
## Activity
|
||||
|
||||
* time: 20 min.
|
||||
* [tutorial](/microbit/lessons/beautiful-image/tutorial)
|
||||
* [quiz](/microbit/lessons/beautiful-image/quiz)
|
||||
|
||||
## Extended Activity
|
||||
|
||||
* time: 20 min.
|
||||
* [challenges](/microbit/lessons/beautiful-image/challenges)
|
||||
|
||||
## Homework
|
||||
|
||||
* Extended Activity: [challenges](/microbit/lessons/beautiful-image/challenges)
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
# blink lesson
|
||||
|
||||
Learn how to create a blinking LED. #LED #screen #plot #docs #lesson
|
||||
|
||||
### @video td/videos/blink-0
|
||||
|
||||
## Topic
|
||||
|
||||
Plot
|
||||
|
||||
## Quick links
|
||||
|
||||
* [activity](/microbit/lessons/blink/activity)
|
||||
* [quiz](/microbit/lessons/blink/quiz)
|
||||
* [quiz answers](/microbit/lessons/blink/quiz-answers)
|
||||
* [challenges](/microbit/lessons/blink/challenges)
|
||||
* [tutorial](/microbit/lessons/blink/tutorial)
|
||||
|
||||
## Class
|
||||
|
||||
Year 7
|
||||
|
||||
## Prior learning / place of lesson in scheme of work
|
||||
|
||||
Learn how to control a blinking LED. We will be learning how to create a blinking app using forever as well as simple commands, such as plot, unplot and pause.
|
||||
|
||||
## What the teacher needs to know / QuickStart Computing Glossary
|
||||
|
||||
**Program:** A stored set of instructions encoded in a language understood by the computer that does some form of computation, processing input and/or stored data to generate output.
|
||||
|
||||
**Algorithm:** An unambiguous set of rules or a precise step-by-step guide to solve a problem or achieve a particular objective. The guided tutorial follows a algorithm and is a precise step-by-step guide to solve a problem
|
||||
|
||||
**Loop:** A block of code repeated automatically under the program’s control. ** The blink program introduces Forever. The forever loop repeats code in the background forever.
|
||||
|
||||
**Command:** An instruction for the computer to execute, written in a particular programming language.
|
||||
|
||||
## Documentation
|
||||
|
||||
* **plot**: [read more...](/microbit/reference/led/plot)
|
||||
* **unplot**: [read more...](/microbit/reference/led/unplot)
|
||||
* **pause**: [read more...](/microbit/reference/basic/pause)
|
||||
* **forever**: [read more...](/microbit/reference/basic/forever)
|
||||
|
||||
## Resources
|
||||
|
||||
* Activity: [tutorial](/microbit/lessons/blink/tutorial)
|
||||
* Activity: [quiz](/microbit/lessons/blink/quiz)
|
||||
* Extended Activity: [challenges](/microbit/lessons/blink/challenges)
|
||||
|
||||
## Objectives
|
||||
|
||||
* learn how to turn on LED lights on the LED screen
|
||||
* learn how to turn off LED lights on the LED screen
|
||||
* learn how to pause program execution for the specified number of milliseconds
|
||||
|
||||
## Progression Pathways / Computational Thinking Framework
|
||||
|
||||
#### Algorithms
|
||||
|
||||
* Understands that iteration is the repetition of a process such as a loop. (AL)
|
||||
|
||||
#### Programming & Development
|
||||
|
||||
* Creates programs that implement algorithms to achieve given goals. (AL)
|
||||
|
||||
#### Data & Data Representation
|
||||
|
||||
* Understands the difference between data and information. (AB)
|
||||
* Defines data types: real numbers and Boolean. (AB)
|
||||
|
||||
#### Information Technology
|
||||
|
||||
* Collects, organises and presents data and information in digital content. (AB)
|
||||
* Makes appropriate improvements to solutions based on feedback received, and can comment on the success of the solution. (EV)
|
||||
|
||||
Computational Thinking Concept: AB = Abstraction; DE = Decomposition; AL = Algorithmic Thinking; EV = Evaluation; GE = Generalisation
|
||||
|
||||
## Activity
|
||||
|
||||
* time: 20 min.
|
||||
* [tutorial](/microbit/lessons/blink/tutorial)
|
||||
* [quiz](/microbit/lessons/blink/quiz)
|
||||
* [quiz answers](/microbit/lessons/blink/quiz-answers)
|
||||
|
||||
## Extended Activity
|
||||
|
||||
* time: 20 min.
|
||||
* [challenges](/microbit/lessons/blink/challenges)
|
||||
|
||||
## Homework
|
||||
|
||||
* Extended Activity: [challenges](/microbit/lessons/blink/challenges)
|
||||
|
||||
## Intended follow on
|
||||
|
||||
Publish script to the classroom.
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
# blink activity
|
||||
|
||||
Turn an LED on and off with forever
|
||||
|
||||
### ~avatar avatar
|
||||
|
||||
### @video td/videos/blink-0
|
||||
|
||||
Let's learn how to blink an LED.
|
||||
|
||||
### ~
|
||||
|
||||
Have you ever tried to blink a flashlight at night? The concept is fairly simply: turn on the light, wait for a little, turn off the light, wait again, and repeat. That's exactly what we need to code to get a blinking LED.
|
||||
|
||||
Let's start by adding a line of code that turns on the LED at position ``2, 2``.
|
||||
|
||||
```
|
||||
led.plot(2, 2)
|
||||
```
|
||||
|
||||
Run your script to make sure it's correct. Then, let's add code to **pause** `500` milliseconds and turn off the LED.
|
||||
|
||||
```
|
||||
led.plot(2, 2)
|
||||
basic.pause(500)
|
||||
led.unplot(2, 2)
|
||||
```
|
||||
|
||||
We've got the LED blinking once. Let's add another pause and turn on the LED again.
|
||||
|
||||
```
|
||||
led.plot(2, 2)
|
||||
basic.pause(500)
|
||||
led.unplot(2, 2)
|
||||
basic.pause(500)
|
||||
led.plot(2, 2)
|
||||
```
|
||||
|
||||
The current code works but it only blinks once! We are going to use a `basic->forever` loop and move the code inside it to repeat it forever. We've dropped the second `led-plot` line since we don't need it in the loop.
|
||||
|
||||
```
|
||||
basic.forever(() => {
|
||||
led.plot(2, 2)
|
||||
basic.pause(500)
|
||||
led.unplot(2, 2)
|
||||
basic.pause(500)
|
||||
})
|
||||
```
|
||||
|
||||
### ~avatar boothing
|
||||
|
||||
Excellent, you're ready to continue with the [challenges](/microbit/lessons/blink/challenges)!
|
||||
|
||||
### ~
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user