batch replace onButtonPressed(Button...
This commit is contained in:
parent
1f7e0b0f79
commit
850c313c5d
@ -52,7 +52,7 @@ The first job of the scheduler is to allow multiple *subprograms* to be queued u
|
|||||||
|
|
||||||
```
|
```
|
||||||
export function countButtonPresses() {
|
export function countButtonPresses() {
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
count = count + 1
|
count = count + 1
|
||||||
})
|
})
|
||||||
basic.forever(() => {
|
basic.forever(() => {
|
||||||
@ -65,7 +65,7 @@ export function countButtonPresses() {
|
|||||||
The program above contains three statements that execute in order from top to bottom. The first statement
|
The program above contains three statements that execute in order from top to bottom. The first statement
|
||||||
|
|
||||||
```
|
```
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
count = count + 1
|
count = count + 1
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
@ -132,14 +132,14 @@ As a result, you can easily add a new capability to the micro:bit by just adding
|
|||||||
|
|
||||||
```
|
```
|
||||||
export function countButtonPressesWithReset() {
|
export function countButtonPressesWithReset() {
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
count = count + 1
|
count = count + 1
|
||||||
})
|
})
|
||||||
basic.forever(() => {
|
basic.forever(() => {
|
||||||
basic.showNumber(count, 150)
|
basic.showNumber(count, 150)
|
||||||
})
|
})
|
||||||
count = 0
|
count = 0
|
||||||
input.onButtonPressed("B", () => {
|
input.onButtonPressed(Button.B, () => {
|
||||||
count = 0
|
count = 0
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,7 @@ We create a **variable**, `count` to keep track of the current count. The number
|
|||||||
|
|
||||||
```
|
```
|
||||||
let count_ = 0
|
let count_ = 0
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
count_ = count_ + 1
|
count_ = count_ + 1
|
||||||
basic.showNumber(count, 150)
|
basic.showNumber(count, 150)
|
||||||
})
|
})
|
||||||
@ -40,7 +40,7 @@ We are only pressing on button pressed once. So the number to display on the mic
|
|||||||
|
|
||||||
```
|
```
|
||||||
count_ = 0
|
count_ = 0
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
count_ = count_ + 1
|
count_ = count_ + 1
|
||||||
basic.showNumber(count_, 100)
|
basic.showNumber(count_, 100)
|
||||||
})
|
})
|
||||||
|
@ -28,7 +28,7 @@ let count = 0
|
|||||||
|
|
||||||
```
|
```
|
||||||
let count_ = 0
|
let count_ = 0
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
count_ = count_ + 1
|
count_ = count_ + 1
|
||||||
basic.showNumber(count_, 100)
|
basic.showNumber(count_, 100)
|
||||||
})
|
})
|
||||||
@ -42,7 +42,7 @@ input.onButtonPressed("A", () => {
|
|||||||
|
|
||||||
```
|
```
|
||||||
count_ = 0
|
count_ = 0
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
count_ = count_ + 1
|
count_ = count_ + 1
|
||||||
basic.showNumber(count_, 100)
|
basic.showNumber(count_, 100)
|
||||||
})
|
})
|
||||||
|
@ -13,7 +13,7 @@ Answers may vary. Generally, on button pressed run code when an input button is
|
|||||||
Write the line of code that creates a condition when the BBC micro:bit button A is pressed.
|
Write the line of code that creates a condition when the BBC micro:bit button A is pressed.
|
||||||
|
|
||||||
```
|
```
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -37,7 +37,7 @@ let num = 0
|
|||||||
basic.forever(() => {
|
basic.forever(() => {
|
||||||
basic.showNumber(num, 150)
|
basic.showNumber(num, 150)
|
||||||
})
|
})
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
num = num + 1
|
num = num + 1
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
@ -101,13 +101,13 @@ for (let i3 = 0; i3 < 5; i3++) {
|
|||||||
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:
|
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", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
for (let i4 = 0; i4 < 5; i4++) {
|
for (let i4 = 0; i4 < 5; i4++) {
|
||||||
led.plot(i4, i4)
|
led.plot(i4, i4)
|
||||||
basic.pause(1000)
|
basic.pause(1000)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
input.onButtonPressed("B", () => {
|
input.onButtonPressed(Button.B, () => {
|
||||||
for (let i5 = 0; i5 < 5; i5++) {
|
for (let i5 = 0; i5 < 5; i5++) {
|
||||||
led.plot(4 - i5, i5)
|
led.plot(4 - i5, i5)
|
||||||
basic.pause(1000)
|
basic.pause(1000)
|
||||||
|
@ -19,7 +19,7 @@ control.inBackground(() => {
|
|||||||
basic.pause(100)
|
basic.pause(100)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
num++;
|
num++;
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
@ -31,7 +31,7 @@ let num = 0
|
|||||||
basic.forever(() => {
|
basic.forever(() => {
|
||||||
basic.showNumber(num, 150)
|
basic.showNumber(num, 150)
|
||||||
})
|
})
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
num++;
|
num++;
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
@ -44,7 +44,7 @@ If you have multiple processes that each show something on the LED screen, you m
|
|||||||
basic.forever(() => {
|
basic.forever(() => {
|
||||||
basic.showNumber(6789, 150)
|
basic.showNumber(6789, 150)
|
||||||
})
|
})
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
basic.showNumber(2, 150)
|
basic.showNumber(2, 150)
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
@ -15,7 +15,7 @@ The code below shows a simple game where the user gets to press the button ``A``
|
|||||||
The code below shows a simple game where the user gets to press the button ``A`` as much times as possible in 10 seconds.
|
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", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
game.addScore(1)
|
game.addScore(1)
|
||||||
})
|
})
|
||||||
game.startCountdown(10000)
|
game.startCountdown(10000)
|
||||||
|
@ -27,7 +27,7 @@ let img = images.createImage(`
|
|||||||
. . . . .
|
. . . . .
|
||||||
`)
|
`)
|
||||||
img.showImage(0)
|
img.showImage(0)
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
img.clear()
|
img.clear()
|
||||||
img.showImage(0)
|
img.showImage(0)
|
||||||
})
|
})
|
||||||
|
@ -13,7 +13,7 @@ The game library supports simple single-player time-based games. The player has
|
|||||||
The code below shows a simple game where the user gets to press the button ``A`` as much times as possible in 10 seconds.
|
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", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
game.addScore(1)
|
game.addScore(1)
|
||||||
})
|
})
|
||||||
game.startCountdown(10000)
|
game.startCountdown(10000)
|
||||||
|
@ -15,7 +15,7 @@ The code below shows a simple game where the user gets to press the button ``A``
|
|||||||
The code below shows a simple game where the user gets to press the button ``A`` as much times as possible in 10 seconds.
|
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", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
game.addScore(1)
|
game.addScore(1)
|
||||||
})
|
})
|
||||||
game.startCountdown(10000)
|
game.startCountdown(10000)
|
||||||
|
@ -13,7 +13,7 @@ The game library supports simple single-player time-based games. The general goa
|
|||||||
The code below shows a simple game where the user gets to press the button ``A`` as much times as possible in 10 seconds.
|
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", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
game.addScore(1)
|
game.addScore(1)
|
||||||
})
|
})
|
||||||
game.startCountdown(10000)
|
game.startCountdown(10000)
|
||||||
|
@ -98,10 +98,10 @@ This program will record the current acceleration measured on `x` when you press
|
|||||||
|
|
||||||
```
|
```
|
||||||
let accelerations = (<number[]>[])
|
let accelerations = (<number[]>[])
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
accelerations.push(input.acceleration("x"))
|
accelerations.push(input.acceleration("x"))
|
||||||
})
|
})
|
||||||
input.onButtonPressed("B", () => {
|
input.onButtonPressed(Button.B, () => {
|
||||||
for (let i1 = 0; i1 < accelerations.length; i1++) {
|
for (let i1 = 0; i1 < accelerations.length; i1++) {
|
||||||
basic.showString(accelerations[i1].toString(), 150)
|
basic.showString(accelerations[i1].toString(), 150)
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ An event handler is code that is associated with a particular event, such as "bu
|
|||||||
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:
|
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", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
basic.showString("hello", 150)
|
basic.showString("hello", 150)
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
@ -21,7 +21,7 @@ After this code executes, then whenever button A is pressed in the future, the s
|
|||||||
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:
|
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", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -32,10 +32,10 @@ The above code associated an event handler that does nothing with the event of a
|
|||||||
The above example also illustrates that there is only one event handler for each event. What is the result of the following code?
|
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", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
basic.showString("hello", 150)
|
basic.showString("hello", 150)
|
||||||
})
|
})
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
basic.showString("goodbye", 150)
|
basic.showString("goodbye", 150)
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
@ -43,7 +43,7 @@ input.onButtonPressed("A", () => {
|
|||||||
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:
|
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", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
basic.showString("hello", 150)
|
basic.showString("hello", 150)
|
||||||
basic.showString("goodbye", 150)
|
basic.showString("goodbye", 150)
|
||||||
})
|
})
|
||||||
|
@ -9,7 +9,7 @@ The game library supports simple single-player time-based games. The player has
|
|||||||
The code below shows a simple game where the user gets to press the button ``A`` as much times as possible in 10 seconds.
|
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", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
game.addScore(1)
|
game.addScore(1)
|
||||||
})
|
})
|
||||||
game.startCountdown(10000)
|
game.startCountdown(10000)
|
||||||
|
@ -15,7 +15,7 @@ The code below shows a simple game where the user gets to press the button ``A``
|
|||||||
The code below shows a simple game where the user gets to press the button ``A`` as much times as possible in 10 seconds.
|
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", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
game.addScore(1)
|
game.addScore(1)
|
||||||
})
|
})
|
||||||
game.startCountdown(10000)
|
game.startCountdown(10000)
|
||||||
|
@ -15,7 +15,7 @@ The code below shows a simple game where the user gets to press the button ``A``
|
|||||||
The code below shows a simple game where the user gets to press the button ``A`` as much times as possible in 10 seconds.
|
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", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
game.addScore(1)
|
game.addScore(1)
|
||||||
})
|
})
|
||||||
game.startCountdown(10000)
|
game.startCountdown(10000)
|
||||||
|
@ -15,7 +15,7 @@ The code below shows a simple game where the user gets to press the button ``A``
|
|||||||
The code below shows a simple game where the user gets to press the button ``A`` as much times as possible in 10 seconds.
|
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", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
game.addScore(1)
|
game.addScore(1)
|
||||||
})
|
})
|
||||||
game.startCountdown(10000)
|
game.startCountdown(10000)
|
||||||
|
@ -13,7 +13,7 @@ The game library supports simple single-player time-based games. The general goa
|
|||||||
The code below shows a simple game where the user gets to press the button ``A`` as much times as possible in 10 seconds.
|
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", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
game.addScore(1)
|
game.addScore(1)
|
||||||
})
|
})
|
||||||
game.startCountdown(10000)
|
game.startCountdown(10000)
|
||||||
|
@ -32,7 +32,7 @@ claimBall = true
|
|||||||
<br/>
|
<br/>
|
||||||
|
|
||||||
```
|
```
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
if (claimBall) {
|
if (claimBall) {
|
||||||
pins.digitalWritePin("P0", 1)
|
pins.digitalWritePin("P0", 1)
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ Welcome! This [guided tutorial](https://live.microbit.co.uk/td/lessons/speed-but
|
|||||||
```
|
```
|
||||||
counter = 0
|
counter = 0
|
||||||
fastPress = false
|
fastPress = false
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
counter = counter + 1
|
counter = counter + 1
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
@ -21,7 +21,7 @@ We need to know when the user has hit button `A` 15 times. The user wins when he
|
|||||||
```
|
```
|
||||||
counter = 0
|
counter = 0
|
||||||
fastPress = false
|
fastPress = false
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
counter = counter + 1
|
counter = counter + 1
|
||||||
if (counter == 15 && input.runningTime() < 3500) {
|
if (counter == 15 && input.runningTime() < 3500) {
|
||||||
}
|
}
|
||||||
@ -33,7 +33,7 @@ Next, if the user has won, let's set our boolean to true. This indicates that he
|
|||||||
```
|
```
|
||||||
counter = 0
|
counter = 0
|
||||||
fastPress = false
|
fastPress = false
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
counter = counter + 1
|
counter = counter + 1
|
||||||
if (counter == 15 && input.runningTime() < 3500) {
|
if (counter == 15 && input.runningTime() < 3500) {
|
||||||
fastPress = true // ***
|
fastPress = true // ***
|
||||||
@ -48,7 +48,7 @@ We want to set `fastPress` to false if the user was too slow. To do so, we need
|
|||||||
```
|
```
|
||||||
counter = 0
|
counter = 0
|
||||||
fastPress = false
|
fastPress = false
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
counter = counter + 1
|
counter = counter + 1
|
||||||
if (counter == 15 && input.runningTime() < 3500) {
|
if (counter == 15 && input.runningTime() < 3500) {
|
||||||
fastPress = true
|
fastPress = true
|
||||||
@ -66,7 +66,7 @@ Now let's display if the user won or lost. To do so, we need to check the status
|
|||||||
```
|
```
|
||||||
counter = 0
|
counter = 0
|
||||||
fastPress = false
|
fastPress = false
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
counter = counter + 1
|
counter = counter + 1
|
||||||
if (counter == 15 && input.runningTime() < 3500) {
|
if (counter == 15 && input.runningTime() < 3500) {
|
||||||
fastPress = true
|
fastPress = true
|
||||||
|
@ -12,7 +12,7 @@ At the end of the tutorial, click `keep editing`. Your code should look like thi
|
|||||||
|
|
||||||
```
|
```
|
||||||
newAction() // ***
|
newAction() // ***
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
if (action == 0) {
|
if (action == 0) {
|
||||||
game.addScore(1) // ***
|
game.addScore(1) // ***
|
||||||
newAction() // ***
|
newAction() // ***
|
||||||
@ -30,7 +30,7 @@ input.onGesture(Gesture.Shake, () => {
|
|||||||
newAction() // ***
|
newAction() // ***
|
||||||
}
|
}
|
||||||
}) // ***
|
}) // ***
|
||||||
input.onButtonPressed("B", () => {
|
input.onButtonPressed(Button.B, () => {
|
||||||
basic.showNumber(game.score(), 150) // ***
|
basic.showNumber(game.score(), 150) // ***
|
||||||
basic.pause(2000) // ***
|
basic.pause(2000) // ***
|
||||||
newAction() // ***
|
newAction() // ***
|
||||||
@ -68,12 +68,12 @@ Now let's implement `PRESS PIN 0` in the main. Create a condition of `input->on
|
|||||||
|
|
||||||
```
|
```
|
||||||
// **. . .**
|
// **. . .**
|
||||||
input.onButtonPressed("B", () => {
|
input.onButtonPressed(Button.B, () => {
|
||||||
basic.showNumber(game.score(), 150) // ***
|
basic.showNumber(game.score(), 150) // ***
|
||||||
basic.pause(2000) // ***
|
basic.pause(2000) // ***
|
||||||
newAction() // ***
|
newAction() // ***
|
||||||
}) // ***
|
}) // ***
|
||||||
input.onPinPressed("P0", () => {
|
input.onPinPressed(TouchPin.P0, () => {
|
||||||
if (action == 3) {
|
if (action == 3) {
|
||||||
game.addScore(1) // ***
|
game.addScore(1) // ***
|
||||||
newAction() // ***
|
newAction() // ***
|
||||||
|
@ -33,7 +33,7 @@ if (action == 0) {
|
|||||||
<br />
|
<br />
|
||||||
|
|
||||||
```
|
```
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
if (action == 0) {
|
if (action == 0) {
|
||||||
game.addScore(1)
|
game.addScore(1)
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ Welcome! This [guided tutorial](/microbit/lessons/break/tutorial) will assist yo
|
|||||||
```
|
```
|
||||||
count = 0
|
count = 0
|
||||||
shouldBreak = false
|
shouldBreak = false
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
shouldBreak = true
|
shouldBreak = true
|
||||||
})
|
})
|
||||||
while (true) {
|
while (true) {
|
||||||
@ -49,7 +49,7 @@ while (true) {
|
|||||||
basic.showNumber(count, 150)
|
basic.showNumber(count, 150)
|
||||||
basic.pause(1000)
|
basic.pause(1000)
|
||||||
}
|
}
|
||||||
input.onButtonPressed("B", () => {
|
input.onButtonPressed(Button.B, () => {
|
||||||
}) // ***
|
}) // ***
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -57,7 +57,7 @@ Next, set `shouldBreak` back to false to indicate we want to run the `while` loo
|
|||||||
|
|
||||||
```
|
```
|
||||||
// **. . .**
|
// **. . .**
|
||||||
input.onButtonPressed("B", () => {
|
input.onButtonPressed(Button.B, () => {
|
||||||
shouldBreak = false // ***
|
shouldBreak = false // ***
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
@ -66,7 +66,7 @@ And now copy the code from the previous while loop into the condition of `input-
|
|||||||
|
|
||||||
```
|
```
|
||||||
// **. . .**
|
// **. . .**
|
||||||
input.onButtonPressed("B", () => {
|
input.onButtonPressed(Button.B, () => {
|
||||||
shouldBreak = false
|
shouldBreak = false
|
||||||
while (true) {
|
while (true) {
|
||||||
if (shouldBreak) {
|
if (shouldBreak) {
|
||||||
|
@ -9,14 +9,14 @@ Howdy! This [guided tutorial](/microbit/rxqgzy) will help you complete this acti
|
|||||||
In this guide, you will learn how to use buttons and show text on the screen. Let's start by adding to respond **when the left button is pressed**.
|
In this guide, you will learn how to use buttons and show text on the screen. Let's start by adding to respond **when the left button is pressed**.
|
||||||
|
|
||||||
```
|
```
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
All the code inside `input->on button pressed` runs when the button is pressed. Let's add the code to show some text.
|
All the code inside `input->on button pressed` runs when the button is pressed. Let's add the code to show some text.
|
||||||
|
|
||||||
```
|
```
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
basic.showString("hello", 150)
|
basic.showString("hello", 150)
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
@ -26,10 +26,10 @@ input.onButtonPressed("A", () => {
|
|||||||
Let's add an event handler for Button `B`.
|
Let's add an event handler for Button `B`.
|
||||||
|
|
||||||
```
|
```
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
basic.showString("hello", 150)
|
basic.showString("hello", 150)
|
||||||
})
|
})
|
||||||
input.onButtonPressed("B", () => {
|
input.onButtonPressed(Button.B, () => {
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -38,10 +38,10 @@ input.onButtonPressed("B", () => {
|
|||||||
Display `bye` when the `B` button is pressed.
|
Display `bye` when the `B` button is pressed.
|
||||||
|
|
||||||
```
|
```
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
basic.showString("hello", 150)
|
basic.showString("hello", 150)
|
||||||
})
|
})
|
||||||
input.onButtonPressed("B", () => {
|
input.onButtonPressed(Button.B, () => {
|
||||||
basic.showString("bye", 150)
|
basic.showString("bye", 150)
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
@ -18,7 +18,7 @@ basic.showAnimation(`
|
|||||||
. . . . . # # # # # # # # # # # # # # #
|
. . . . . # # # # # # # # # # # # # # #
|
||||||
. . . . . . . . . . . . . . . # # # # #
|
. . . . . . . . . . . . . . . # # # # #
|
||||||
`, 400)
|
`, 400)
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
basic.clearScreen()
|
basic.clearScreen()
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
@ -37,10 +37,10 @@ basic.showAnimation(`
|
|||||||
. . . . . # # # # # # # # # # # # # # #
|
. . . . . # # # # # # # # # # # # # # #
|
||||||
. . . . . . . . . . . . . . . # # # # #
|
. . . . . . . . . . . . . . . # # # # #
|
||||||
`, 400)
|
`, 400)
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
basic.clearScreen()
|
basic.clearScreen()
|
||||||
})
|
})
|
||||||
input.onButtonPressed("B", () => {
|
input.onButtonPressed(Button.B, () => {
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -56,10 +56,10 @@ basic.showAnimation(`
|
|||||||
. . . . . # # # # # # # # # # # # # # #
|
. . . . . # # # # # # # # # # # # # # #
|
||||||
. . . . . . . . . . . . . . . # # # # #
|
. . . . . . . . . . . . . . . # # # # #
|
||||||
`, 400)
|
`, 400)
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
basic.clearScreen()
|
basic.clearScreen()
|
||||||
})
|
})
|
||||||
input.onButtonPressed("B", () => {
|
input.onButtonPressed(Button.B, () => {
|
||||||
basic.showAnimation(`
|
basic.showAnimation(`
|
||||||
# # # # # # # # # # . . . . . . . . . .
|
# # # # # # # # # # . . . . . . . . . .
|
||||||
# # # # # # # # # # . . . . . . . . . .
|
# # # # # # # # # # . . . . . . . . . .
|
||||||
|
@ -8,7 +8,7 @@ Welcome! This [guided tutorial](/microbit/lessons/comparison/tutorial) will assi
|
|||||||
|
|
||||||
```
|
```
|
||||||
counter = 0
|
counter = 0
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
counter = counter + 1
|
counter = counter + 1
|
||||||
if (counter == 10) {
|
if (counter == 10) {
|
||||||
counter = 1
|
counter = 1
|
||||||
@ -23,7 +23,7 @@ Now let's do something special when the micro:bit reaches the number `5`. Instea
|
|||||||
|
|
||||||
```
|
```
|
||||||
counter = 0
|
counter = 0
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
counter = counter + 1
|
counter = counter + 1
|
||||||
if (counter == 10) {
|
if (counter == 10) {
|
||||||
counter = 1
|
counter = 1
|
||||||
@ -40,7 +40,7 @@ Let's continue our plan of displaying `HALF WAY!` when `counter = 5`. To do so,
|
|||||||
|
|
||||||
```
|
```
|
||||||
counter = 0
|
counter = 0
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
counter = counter + 1
|
counter = counter + 1
|
||||||
if (counter == 10) {
|
if (counter == 10) {
|
||||||
counter = 1
|
counter = 1
|
||||||
@ -60,7 +60,7 @@ You may notice a problem right now. When `counter = 5`, the micro:bit will show
|
|||||||
|
|
||||||
```
|
```
|
||||||
counter = 0
|
counter = 0
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
counter = counter + 1
|
counter = counter + 1
|
||||||
if (counter == 10) {
|
if (counter == 10) {
|
||||||
counter = 1
|
counter = 1
|
||||||
|
@ -22,7 +22,7 @@ The code under ``on button pressed("A")`` will run each time the user presses A.
|
|||||||
|
|
||||||
```
|
```
|
||||||
let count_ = 0
|
let count_ = 0
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
count = count + 1
|
count = count + 1
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
@ -31,7 +31,7 @@ Since the count has changed, it's time to refresh the screen display. Let's add
|
|||||||
|
|
||||||
```
|
```
|
||||||
let count_1 = 0
|
let count_1 = 0
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
count = count + 1
|
count = count + 1
|
||||||
basic.showNumber(count, 150)
|
basic.showNumber(count, 150)
|
||||||
})
|
})
|
||||||
|
@ -8,7 +8,7 @@ Complete the following [guided tutorial](/microbit/lessons/counter/activity) At
|
|||||||
|
|
||||||
```
|
```
|
||||||
let count = 0
|
let count = 0
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
count = count + 1
|
count = count + 1
|
||||||
basic.showNumber(count, 150)
|
basic.showNumber(count, 150)
|
||||||
})
|
})
|
||||||
@ -22,11 +22,11 @@ Let's add the code to `count` when `B` is pressed. Add an event handler with `in
|
|||||||
|
|
||||||
```
|
```
|
||||||
let count1 = 0
|
let count1 = 0
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
count1 = count1 + 1
|
count1 = count1 + 1
|
||||||
basic.showNumber(count1, 150)
|
basic.showNumber(count1, 150)
|
||||||
})
|
})
|
||||||
input.onButtonPressed("B", () => {
|
input.onButtonPressed(Button.B, () => {
|
||||||
count1 = count1 - 1 // ***
|
count1 = count1 - 1 // ***
|
||||||
basic.showNumber(count1, 150) // ***
|
basic.showNumber(count1, 150) // ***
|
||||||
}) // ***
|
}) // ***
|
||||||
|
@ -15,14 +15,14 @@ To create a new script, go to the [Create Code](/microbit/create-code) page and
|
|||||||
Add an event handler when button `A` is pressed.
|
Add an event handler when button `A` is pressed.
|
||||||
|
|
||||||
```
|
```
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
Create a local variable of type number `x` and set it to a random number using `math->random`. `math->random(10)` generates a random number between `0` and `10` **excluded**.
|
Create a local variable of type number `x` and set it to a random number using `math->random`. `math->random(10)` generates a random number between `0` and `10` **excluded**.
|
||||||
|
|
||||||
```
|
```
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
let x = Math.random(10)
|
let x = Math.random(10)
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
@ -30,7 +30,7 @@ input.onButtonPressed("A", () => {
|
|||||||
Show the random number on the screen.
|
Show the random number on the screen.
|
||||||
|
|
||||||
```
|
```
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
let x1 = Math.random(10)
|
let x1 = Math.random(10)
|
||||||
basic.showNumber(x1, 150)
|
basic.showNumber(x1, 150)
|
||||||
})
|
})
|
||||||
|
@ -11,7 +11,7 @@ Complete the following guided tutorial:
|
|||||||
At the end of the tutorial, click `keep editing`. Your code should look like this:
|
At the end of the tutorial, click `keep editing`. Your code should look like this:
|
||||||
|
|
||||||
```
|
```
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
let x = Math.random(10)
|
let x = Math.random(10)
|
||||||
basic.showNumber(x, 150)
|
basic.showNumber(x, 150)
|
||||||
})
|
})
|
||||||
@ -24,11 +24,11 @@ input.onButtonPressed("A", () => {
|
|||||||
When button `B` is pressed, we want to clear the screen. This will make it so users can play your game over and over again! Add an event handler to handle this case.
|
When button `B` is pressed, we want to clear the screen. This will make it so users can play your game over and over again! Add an event handler to handle this case.
|
||||||
|
|
||||||
```
|
```
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
let x1 = Math.random(10)
|
let x1 = Math.random(10)
|
||||||
basic.showNumber(x1, 150)
|
basic.showNumber(x1, 150)
|
||||||
})
|
})
|
||||||
input.onButtonPressed("B", () => {
|
input.onButtonPressed(Button.B, () => {
|
||||||
basic.clearScreen() // ***
|
basic.clearScreen() // ***
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
@ -13,7 +13,7 @@ At the end of the tutorial, click `keep editing`. Your code should look like thi
|
|||||||
```
|
```
|
||||||
count = 0
|
count = 0
|
||||||
shouldBreak = false
|
shouldBreak = false
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
shouldBreak = true
|
shouldBreak = true
|
||||||
})
|
})
|
||||||
while (true) {
|
while (true) {
|
||||||
@ -60,7 +60,7 @@ while (true) {
|
|||||||
basic.showNumber(count, 150)
|
basic.showNumber(count, 150)
|
||||||
basic.pause(1000)
|
basic.pause(1000)
|
||||||
}
|
}
|
||||||
input.onButtonPressed("B", () => {
|
input.onButtonPressed(Button.B, () => {
|
||||||
}) // ***
|
}) // ***
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -68,7 +68,7 @@ Next, set `should break` back to false to indicate we want to run the `while` lo
|
|||||||
|
|
||||||
```
|
```
|
||||||
// **. . .**
|
// **. . .**
|
||||||
input.onButtonPressed("B", () => {
|
input.onButtonPressed(Button.B, () => {
|
||||||
shouldBreak = false // ***
|
shouldBreak = false // ***
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
@ -77,7 +77,7 @@ And now copy the code from the previous while loop into the condition of `input-
|
|||||||
|
|
||||||
```
|
```
|
||||||
// **. . .**
|
// **. . .**
|
||||||
input.onButtonPressed("B", () => {
|
input.onButtonPressed(Button.B, () => {
|
||||||
shouldBreak = false
|
shouldBreak = false
|
||||||
while (true) {
|
while (true) {
|
||||||
if (shouldBreak) {
|
if (shouldBreak) {
|
||||||
|
@ -22,7 +22,7 @@ shouldBreak = false
|
|||||||
Write the line of code to stop incrementing `count` when the button is pressed. (Hint: This will set `should break` to true).
|
Write the line of code to stop incrementing `count` when the button is pressed. (Hint: This will set `should break` to true).
|
||||||
|
|
||||||
```
|
```
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
shouldBreak = true
|
shouldBreak = true
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
@ -128,7 +128,7 @@ for (let j = 0; j < 10; j++) {
|
|||||||
<br/>
|
<br/>
|
||||||
|
|
||||||
```
|
```
|
||||||
input.onButtonPressed("B", () => {
|
input.onButtonPressed(Button.B, () => {
|
||||||
basic.showString("WINS", 150)
|
basic.showString("WINS", 150)
|
||||||
basic.showNumber(wins, 150)
|
basic.showNumber(wins, 150)
|
||||||
basic.pause(500)
|
basic.pause(500)
|
||||||
|
@ -15,14 +15,14 @@ To create a new script, go to the [Create Code](/microbit/create-code) page and
|
|||||||
Begin by registering an event with `input->on pin pressed(PO)` to know when someone is holding pin ``P0`` and pin ``Gnd``.
|
Begin by registering an event with `input->on pin pressed(PO)` to know when someone is holding pin ``P0`` and pin ``Gnd``.
|
||||||
|
|
||||||
```
|
```
|
||||||
input.onPinPressed("P0", () => {
|
input.onPinPressed(TouchPin.P0, () => {
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
We are going to create a meter that displays a random number from 0 to 10. We use ``11`` as `math->random(n)` returns a number between ``0`` and ``n-1``.
|
We are going to create a meter that displays a random number from 0 to 10. We use ``11`` as `math->random(n)` returns a number between ``0`` and ``n-1``.
|
||||||
|
|
||||||
```
|
```
|
||||||
input.onPinPressed("P0", () => {
|
input.onPinPressed(TouchPin.P0, () => {
|
||||||
let x = Math.random(11)
|
let x = Math.random(11)
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
@ -30,7 +30,7 @@ input.onPinPressed("P0", () => {
|
|||||||
Finally, let's show that number on the micro:bit.
|
Finally, let's show that number on the micro:bit.
|
||||||
|
|
||||||
```
|
```
|
||||||
input.onPinPressed("P0", () => {
|
input.onPinPressed(TouchPin.P0, () => {
|
||||||
let x_ = Math.random(11)
|
let x_ = Math.random(11)
|
||||||
basic.showNumber(x_, 150)
|
basic.showNumber(x_, 150)
|
||||||
})
|
})
|
||||||
|
@ -11,7 +11,7 @@ Complete the following guided tutorial:
|
|||||||
At the end of the tutorial, click `keep editing`. Your code should look like this:
|
At the end of the tutorial, click `keep editing`. Your code should look like this:
|
||||||
|
|
||||||
```
|
```
|
||||||
input.onPinPressed("P0", () => {
|
input.onPinPressed(TouchPin.P0, () => {
|
||||||
let x = Math.random(11)
|
let x = Math.random(11)
|
||||||
basic.showNumber(x, 150)
|
basic.showNumber(x, 150)
|
||||||
})
|
})
|
||||||
@ -22,7 +22,7 @@ input.onPinPressed("P0", () => {
|
|||||||
Add a pause of 3000 milliseconds (3 seconds) after showing the number so that the number won't immediately disappear in the next challenge.
|
Add a pause of 3000 milliseconds (3 seconds) after showing the number so that the number won't immediately disappear in the next challenge.
|
||||||
|
|
||||||
```
|
```
|
||||||
input.onPinPressed("P0", () => {
|
input.onPinPressed(TouchPin.P0, () => {
|
||||||
let x1 = Math.random(11)
|
let x1 = Math.random(11)
|
||||||
basic.showNumber(x1, 150)
|
basic.showNumber(x1, 150)
|
||||||
basic.pause(3000) // ***
|
basic.pause(3000) // ***
|
||||||
@ -34,7 +34,7 @@ input.onPinPressed("P0", () => {
|
|||||||
If the rating **x** is between ``0`` and ``3`` (strictly less than ``4``), display the text "HORRIBLE!".
|
If the rating **x** is between ``0`` and ``3`` (strictly less than ``4``), display the text "HORRIBLE!".
|
||||||
|
|
||||||
```
|
```
|
||||||
input.onPinPressed("P0", () => {
|
input.onPinPressed(TouchPin.P0, () => {
|
||||||
let x2 = Math.random(11)
|
let x2 = Math.random(11)
|
||||||
basic.showNumber(x2, 150)
|
basic.showNumber(x2, 150)
|
||||||
basic.pause(3000)
|
basic.pause(3000)
|
||||||
@ -51,7 +51,7 @@ input.onPinPressed("P0", () => {
|
|||||||
If the rating is between 4 and 7, display the text "MEDIOCRE!" **else** display the text "MATCHED!"
|
If the rating is between 4 and 7, display the text "MEDIOCRE!" **else** display the text "MATCHED!"
|
||||||
|
|
||||||
```
|
```
|
||||||
input.onPinPressed("P0", () => {
|
input.onPinPressed(TouchPin.P0, () => {
|
||||||
let x3 = Math.random(11)
|
let x3 = Math.random(11)
|
||||||
basic.showNumber(x3, 150)
|
basic.showNumber(x3, 150)
|
||||||
basic.pause(3000)
|
basic.pause(3000)
|
||||||
|
@ -13,7 +13,7 @@ It's a method that runs code when the user holds the GND pin with a finger of on
|
|||||||
## 2. Create a condition for on pin pressed ("P1").
|
## 2. Create a condition for on pin pressed ("P1").
|
||||||
|
|
||||||
```
|
```
|
||||||
input.onPinPressed("P1", () => {
|
input.onPinPressed(TouchPin.P1, () => {
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -32,7 +32,7 @@ Add a new event handler for `input->on button pressed(A)` and add the code to se
|
|||||||
```
|
```
|
||||||
led.setBrightness(255)
|
led.setBrightness(255)
|
||||||
led.plotAll()
|
led.plotAll()
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
led.setBrightness(64) // ***
|
led.setBrightness(64) // ***
|
||||||
}) // ***
|
}) // ***
|
||||||
```
|
```
|
||||||
|
@ -13,7 +13,7 @@ At the end of the tutorial, click `keep editing`. Your code should look like thi
|
|||||||
```
|
```
|
||||||
led.setBrightness(255)
|
led.setBrightness(255)
|
||||||
led.plotAll()
|
led.plotAll()
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
led.setBrightness(64)
|
led.setBrightness(64)
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
@ -27,10 +27,10 @@ What if we want to turn off all the LEDs? Let's do this by setting the brightnes
|
|||||||
```
|
```
|
||||||
led.setBrightness(255)
|
led.setBrightness(255)
|
||||||
led.plotAll()
|
led.plotAll()
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
led.setBrightness(64)
|
led.setBrightness(64)
|
||||||
})
|
})
|
||||||
input.onButtonPressed("B", () => {
|
input.onButtonPressed(Button.B, () => {
|
||||||
led.setBrightness(0) // ***
|
led.setBrightness(0) // ***
|
||||||
}) // ***
|
}) // ***
|
||||||
```
|
```
|
||||||
|
@ -126,7 +126,7 @@ for (let i4 = 0; i4 < 4; i4++) {
|
|||||||
<br />
|
<br />
|
||||||
|
|
||||||
```
|
```
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
if (gameMode == 0 && playerNumber == 0) {
|
if (gameMode == 0 && playerNumber == 0) {
|
||||||
micro_bitTransfer.transferByte(255)
|
micro_bitTransfer.transferByte(255)
|
||||||
}
|
}
|
||||||
@ -148,7 +148,7 @@ for (let k1 = 0; k1 < 3; k1++) {
|
|||||||
<br />
|
<br />
|
||||||
|
|
||||||
```
|
```
|
||||||
input.onButtonPressed("B", () => {
|
input.onButtonPressed(Button.B, () => {
|
||||||
if (gameMode == 2) {
|
if (gameMode == 2) {
|
||||||
gameMode = 1
|
gameMode = 1
|
||||||
basic.plotImage(`
|
basic.plotImage(`
|
||||||
|
@ -24,7 +24,7 @@ basic.forever(() => {
|
|||||||
. . # . .
|
. . # . .
|
||||||
`).showImage(offset)
|
`).showImage(offset)
|
||||||
})
|
})
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
offset = offset + 1
|
offset = offset + 1
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
@ -47,10 +47,10 @@ basic.forever(() => {
|
|||||||
. . # . .
|
. . # . .
|
||||||
`).showImage(offset)
|
`).showImage(offset)
|
||||||
})
|
})
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
offset = offset + 1
|
offset = offset + 1
|
||||||
})
|
})
|
||||||
input.onButtonPressed("B", () => {
|
input.onButtonPressed(Button.B, () => {
|
||||||
offset = offset - 1 // ***
|
offset = offset - 1 // ***
|
||||||
}) // ***
|
}) // ***
|
||||||
```
|
```
|
||||||
@ -82,10 +82,10 @@ basic.forever(() => {
|
|||||||
. . # . .
|
. . # . .
|
||||||
`).showImage(offset)
|
`).showImage(offset)
|
||||||
})
|
})
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
offset = offset + 1
|
offset = offset + 1
|
||||||
})
|
})
|
||||||
input.onButtonPressed("B", () => {
|
input.onButtonPressed(Button.B, () => {
|
||||||
offset = offset - 1
|
offset = offset - 1
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
@ -41,7 +41,7 @@ Write the two lines of code that cause the `variable` offset to increase by one
|
|||||||
<br/>
|
<br/>
|
||||||
|
|
||||||
```
|
```
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
offset = offset + 1
|
offset = offset + 1
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
@ -26,10 +26,10 @@ ball.setDirection(-45)
|
|||||||
The user will control the paddle by pressing ``A`` to go up and ``B`` to go down. Let's add ``on button pressed`` event handlers to do that.
|
The user will control the paddle by pressing ``A`` to go up and ``B`` to go down. Let's add ``on button pressed`` event handlers to do that.
|
||||||
|
|
||||||
```
|
```
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
paddle.changeYBy(-1)
|
paddle.changeYBy(-1)
|
||||||
})
|
})
|
||||||
input.onButtonPressed("B", () => {
|
input.onButtonPressed(Button.B, () => {
|
||||||
paddle.changeYBy(1)
|
paddle.changeYBy(1)
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
@ -42,7 +42,7 @@ led.plot(ballX, ballY)
|
|||||||
<br/>
|
<br/>
|
||||||
|
|
||||||
```
|
```
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
if (paddleNotUp()) {
|
if (paddleNotUp()) {
|
||||||
led.unplot(0, paddleY)
|
led.unplot(0, paddleY)
|
||||||
paddleY = paddleY - 1
|
paddleY = paddleY - 1
|
||||||
@ -56,7 +56,7 @@ input.onButtonPressed("A", () => {
|
|||||||
<br/>
|
<br/>
|
||||||
|
|
||||||
```
|
```
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
if (paddleNotDown()) {
|
if (paddleNotDown()) {
|
||||||
led.unplot(0, paddleY)
|
led.unplot(0, paddleY)
|
||||||
paddleY = paddleY + 1
|
paddleY = paddleY + 1
|
||||||
|
@ -8,7 +8,7 @@ Welcome! This [guided tutorial](/microbit/lessons/return/tutorial) will help you
|
|||||||
|
|
||||||
```
|
```
|
||||||
let original1 = 5
|
let original1 = 5
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
let doubled = doubleIt_(5)
|
let doubled = doubleIt_(5)
|
||||||
basic.showNumber(doubled, 150) // ***
|
basic.showNumber(doubled, 150) // ***
|
||||||
})
|
})
|
||||||
@ -32,11 +32,11 @@ Add a condition to know when button `B` is pressed. We will use this condition i
|
|||||||
|
|
||||||
```
|
```
|
||||||
let original = 5
|
let original = 5
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
let one = doubleIt_(original)
|
let one = doubleIt_(original)
|
||||||
basic.showNumber(one, 150)
|
basic.showNumber(one, 150)
|
||||||
})
|
})
|
||||||
input.onButtonPressed("B", () => {
|
input.onButtonPressed(Button.B, () => {
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -50,7 +50,7 @@ input.onGesture(Gesture.Shake, () => {
|
|||||||
let offset1 = 5 * Math.random(3)
|
let offset1 = 5 * Math.random(3)
|
||||||
img1.showImage(offset1)
|
img1.showImage(offset1)
|
||||||
})
|
})
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
}) // ***
|
}) // ***
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -71,7 +71,7 @@ input.onGesture(Gesture.Shake, () => {
|
|||||||
let offset2 = 5 * Math.random(3)
|
let offset2 = 5 * Math.random(3)
|
||||||
img2.showImage(offset2)
|
img2.showImage(offset2)
|
||||||
})
|
})
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
wins = wins + 1 // ***
|
wins = wins + 1 // ***
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
@ -93,7 +93,7 @@ input.onGesture(Gesture.Shake, () => {
|
|||||||
let offset3 = 5 * Math.random(3)
|
let offset3 = 5 * Math.random(3)
|
||||||
img3.showImage(offset3)
|
img3.showImage(offset3)
|
||||||
})
|
})
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
wins = wins + 1
|
wins = wins + 1
|
||||||
basic.showString("WINS:", 150) // ***
|
basic.showString("WINS:", 150) // ***
|
||||||
basic.showNumber(wins, 150) // ***
|
basic.showNumber(wins, 150) // ***
|
||||||
@ -129,12 +129,12 @@ input.onGesture(Gesture.Shake, () => {
|
|||||||
let offset4 = 5 * Math.random(3)
|
let offset4 = 5 * Math.random(3)
|
||||||
img4.showImage(offset4)
|
img4.showImage(offset4)
|
||||||
})
|
})
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
wins = wins + 1
|
wins = wins + 1
|
||||||
basic.showString("WINS:", 150)
|
basic.showString("WINS:", 150)
|
||||||
basic.showNumber(wins, 150)
|
basic.showNumber(wins, 150)
|
||||||
})
|
})
|
||||||
input.onButtonPressed("B", () => {
|
input.onButtonPressed(Button.B, () => {
|
||||||
}) // ***
|
}) // ***
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -155,12 +155,12 @@ input.onGesture(Gesture.Shake, () => {
|
|||||||
let offset5 = 5 * Math.random(3)
|
let offset5 = 5 * Math.random(3)
|
||||||
img5.showImage(offset5)
|
img5.showImage(offset5)
|
||||||
})
|
})
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
wins = wins + 1
|
wins = wins + 1
|
||||||
basic.showString("WINS:", 150)
|
basic.showString("WINS:", 150)
|
||||||
basic.showNumber(wins, 150)
|
basic.showNumber(wins, 150)
|
||||||
})
|
})
|
||||||
input.onButtonPressed("B", () => {
|
input.onButtonPressed(Button.B, () => {
|
||||||
losses = losses + 1 // ***
|
losses = losses + 1 // ***
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
@ -182,12 +182,12 @@ input.onGesture(Gesture.Shake, () => {
|
|||||||
let offset6 = 5 * Math.random(3)
|
let offset6 = 5 * Math.random(3)
|
||||||
img6.showImage(offset6)
|
img6.showImage(offset6)
|
||||||
})
|
})
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
wins = wins + 1
|
wins = wins + 1
|
||||||
basic.showString("WINS:", 150)
|
basic.showString("WINS:", 150)
|
||||||
basic.showNumber(wins, 150)
|
basic.showNumber(wins, 150)
|
||||||
})
|
})
|
||||||
input.onButtonPressed("B", () => {
|
input.onButtonPressed(Button.B, () => {
|
||||||
losses = losses + 1
|
losses = losses + 1
|
||||||
basic.showString("WINS", 150) // ***
|
basic.showString("WINS", 150) // ***
|
||||||
basic.showNumber(wins, 150) // ***
|
basic.showNumber(wins, 150) // ***
|
||||||
@ -213,14 +213,14 @@ input.onGesture(Gesture.Shake, () => {
|
|||||||
let offset7 = 5 * Math.random(3)
|
let offset7 = 5 * Math.random(3)
|
||||||
img7.showImage(offset7)
|
img7.showImage(offset7)
|
||||||
})
|
})
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
wins = wins + 1
|
wins = wins + 1
|
||||||
basic.showString("WINS:", 150)
|
basic.showString("WINS:", 150)
|
||||||
basic.showNumber(wins, 150)
|
basic.showNumber(wins, 150)
|
||||||
basic.showString("LOSSES:", 150) // ***
|
basic.showString("LOSSES:", 150) // ***
|
||||||
basic.showNumber(losses, 150) // ***
|
basic.showNumber(losses, 150) // ***
|
||||||
})
|
})
|
||||||
input.onButtonPressed("B", () => {
|
input.onButtonPressed(Button.B, () => {
|
||||||
losses = losses + 1
|
losses = losses + 1
|
||||||
basic.showString("WINS", 150)
|
basic.showString("WINS", 150)
|
||||||
basic.showNumber(wins, 150)
|
basic.showNumber(wins, 150)
|
||||||
|
@ -50,7 +50,7 @@ input.onGesture(Gesture.Shake, () => {
|
|||||||
let offset1 = 5 * Math.random(3)
|
let offset1 = 5 * Math.random(3)
|
||||||
img1.showImage(offset1)
|
img1.showImage(offset1)
|
||||||
})
|
})
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
}) // ***
|
}) // ***
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -71,7 +71,7 @@ input.onGesture(Gesture.Shake, () => {
|
|||||||
let offset2 = 5 * Math.random(3)
|
let offset2 = 5 * Math.random(3)
|
||||||
img2.showImage(offset2)
|
img2.showImage(offset2)
|
||||||
})
|
})
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
wins = wins + 1 // ***
|
wins = wins + 1 // ***
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
@ -93,7 +93,7 @@ input.onGesture(Gesture.Shake, () => {
|
|||||||
let offset3 = 5 * Math.random(3)
|
let offset3 = 5 * Math.random(3)
|
||||||
img3.showImage(offset3)
|
img3.showImage(offset3)
|
||||||
})
|
})
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
wins = wins + 1
|
wins = wins + 1
|
||||||
basic.showString("WINS:", 150) // ***
|
basic.showString("WINS:", 150) // ***
|
||||||
basic.showNumber(wins, 150) // ***
|
basic.showNumber(wins, 150) // ***
|
||||||
@ -129,12 +129,12 @@ input.onGesture(Gesture.Shake, () => {
|
|||||||
let offset4 = 5 * Math.random(3)
|
let offset4 = 5 * Math.random(3)
|
||||||
img4.showImage(offset4)
|
img4.showImage(offset4)
|
||||||
})
|
})
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
wins = wins + 1
|
wins = wins + 1
|
||||||
basic.showString("WINS:", 150)
|
basic.showString("WINS:", 150)
|
||||||
basic.showNumber(wins, 150)
|
basic.showNumber(wins, 150)
|
||||||
})
|
})
|
||||||
input.onButtonPressed("B", () => {
|
input.onButtonPressed(Button.B, () => {
|
||||||
}) // ***
|
}) // ***
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -155,12 +155,12 @@ input.onGesture(Gesture.Shake, () => {
|
|||||||
let offset5 = 5 * Math.random(3)
|
let offset5 = 5 * Math.random(3)
|
||||||
img5.showImage(offset5)
|
img5.showImage(offset5)
|
||||||
})
|
})
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
wins = wins + 1
|
wins = wins + 1
|
||||||
basic.showString("WINS:", 150)
|
basic.showString("WINS:", 150)
|
||||||
basic.showNumber(wins, 150)
|
basic.showNumber(wins, 150)
|
||||||
})
|
})
|
||||||
input.onButtonPressed("B", () => {
|
input.onButtonPressed(Button.B, () => {
|
||||||
losses = losses + 1 // ***
|
losses = losses + 1 // ***
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
@ -182,12 +182,12 @@ input.onGesture(Gesture.Shake, () => {
|
|||||||
let offset6 = 5 * Math.random(3)
|
let offset6 = 5 * Math.random(3)
|
||||||
img6.showImage(offset6)
|
img6.showImage(offset6)
|
||||||
})
|
})
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
wins = wins + 1
|
wins = wins + 1
|
||||||
basic.showString("WINS:", 150)
|
basic.showString("WINS:", 150)
|
||||||
basic.showNumber(wins, 150)
|
basic.showNumber(wins, 150)
|
||||||
})
|
})
|
||||||
input.onButtonPressed("B", () => {
|
input.onButtonPressed(Button.B, () => {
|
||||||
losses = losses + 1
|
losses = losses + 1
|
||||||
basic.showString("WINS", 150) // ***
|
basic.showString("WINS", 150) // ***
|
||||||
basic.showNumber(wins, 150) // ***
|
basic.showNumber(wins, 150) // ***
|
||||||
@ -213,14 +213,14 @@ input.onGesture(Gesture.Shake, () => {
|
|||||||
let offset7 = 5 * Math.random(3)
|
let offset7 = 5 * Math.random(3)
|
||||||
img7.showImage(offset7)
|
img7.showImage(offset7)
|
||||||
})
|
})
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
wins = wins + 1
|
wins = wins + 1
|
||||||
basic.showString("WINS:", 150)
|
basic.showString("WINS:", 150)
|
||||||
basic.showNumber(wins, 150)
|
basic.showNumber(wins, 150)
|
||||||
basic.showString("LOSSES:", 150) // ***
|
basic.showString("LOSSES:", 150) // ***
|
||||||
basic.showNumber(losses, 150) // ***
|
basic.showNumber(losses, 150) // ***
|
||||||
})
|
})
|
||||||
input.onButtonPressed("B", () => {
|
input.onButtonPressed(Button.B, () => {
|
||||||
losses = losses + 1
|
losses = losses + 1
|
||||||
basic.showString("WINS", 150)
|
basic.showString("WINS", 150)
|
||||||
basic.showNumber(wins, 150)
|
basic.showNumber(wins, 150)
|
||||||
|
@ -34,7 +34,7 @@ input.onGesture(Gesture.Shake, () => {
|
|||||||
`)
|
`)
|
||||||
img1.plotFrame(Math.random(3))
|
img1.plotFrame(Math.random(3))
|
||||||
})
|
})
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
game.addScore(1) // ***
|
game.addScore(1) // ***
|
||||||
}) // ***
|
}) // ***
|
||||||
```
|
```
|
||||||
@ -54,7 +54,7 @@ input.onGesture(Gesture.Shake, () => {
|
|||||||
`)
|
`)
|
||||||
img2.plotFrame(Math.random(3))
|
img2.plotFrame(Math.random(3))
|
||||||
})
|
})
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
game.addScore(1)
|
game.addScore(1)
|
||||||
basic.showString("WINS: ", 150) // ***
|
basic.showString("WINS: ", 150) // ***
|
||||||
basic.showNumber(game.score(), 150) // ***
|
basic.showNumber(game.score(), 150) // ***
|
||||||
|
@ -27,7 +27,7 @@ Now let's add to this by creating a condition for on button pressed `A` before t
|
|||||||
|
|
||||||
```
|
```
|
||||||
rotating = true
|
rotating = true
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
}) // ***
|
}) // ***
|
||||||
while (rotating) {
|
while (rotating) {
|
||||||
basic.showAnimation(`
|
basic.showAnimation(`
|
||||||
@ -48,7 +48,7 @@ Now that we have the on button pressed condition, let's make the animation stop
|
|||||||
|
|
||||||
```
|
```
|
||||||
rotating = true
|
rotating = true
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
rotating = false // ***
|
rotating = false // ***
|
||||||
}) // ***
|
}) // ***
|
||||||
while (rotating) {
|
while (rotating) {
|
||||||
|
@ -30,7 +30,7 @@ Now let's add to this by creating a condition for on button pressed `A` before t
|
|||||||
|
|
||||||
```
|
```
|
||||||
rotating = true
|
rotating = true
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
}) // ***
|
}) // ***
|
||||||
while (rotating) {
|
while (rotating) {
|
||||||
basic.showAnimation(`
|
basic.showAnimation(`
|
||||||
@ -52,7 +52,7 @@ Now that we have the on button pressed condition, let's make the animation stop
|
|||||||
|
|
||||||
```
|
```
|
||||||
rotating = true
|
rotating = true
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
rotating = false // ***
|
rotating = false // ***
|
||||||
}) // ***
|
}) // ***
|
||||||
while (rotating) {
|
while (rotating) {
|
||||||
|
@ -88,7 +88,7 @@ levelTime = 0
|
|||||||
<br/>
|
<br/>
|
||||||
|
|
||||||
```
|
```
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
let temp = math.abs(person.dirX) * (-1)
|
let temp = math.abs(person.dirX) * (-1)
|
||||||
// {stcode}
|
// {stcode}
|
||||||
// MACRO: stcode
|
// MACRO: stcode
|
||||||
@ -104,7 +104,7 @@ input.onButtonPressed("A", () => {
|
|||||||
<br/>
|
<br/>
|
||||||
|
|
||||||
```
|
```
|
||||||
input.onButtonPressed("B", () => {
|
input.onButtonPressed(Button.B, () => {
|
||||||
let temp1 = math.abs(person.dirX)
|
let temp1 = math.abs(person.dirX)
|
||||||
// {stcode}
|
// {stcode}
|
||||||
// MACRO: stcode
|
// MACRO: stcode
|
||||||
|
@ -32,7 +32,7 @@ basic.showAnimation(`
|
|||||||
. . . . . # # # # # # # # # # # # # # #
|
. . . . . # # # # # # # # # # # # # # #
|
||||||
. . . . . . . . . . . . . . . # # # # #
|
. . . . . . . . . . . . . . . # # # # #
|
||||||
`, 400)
|
`, 400)
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
basic.clearScreen() // ***
|
basic.clearScreen() // ***
|
||||||
}) // ***
|
}) // ***
|
||||||
```
|
```
|
||||||
|
@ -14,7 +14,7 @@ basic.showAnimation(`
|
|||||||
. . . . . # # # # # # # # # # # # # # #
|
. . . . . # # # # # # # # # # # # # # #
|
||||||
. . . . . . . . . . . . . . . # # # # #
|
. . . . . . . . . . . . . . . # # # # #
|
||||||
`, 400)
|
`, 400)
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
basic.clearScreen()
|
basic.clearScreen()
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
@ -31,10 +31,10 @@ basic.showAnimation(`
|
|||||||
. . . . . # # # # # # # # # # # # # # #
|
. . . . . # # # # # # # # # # # # # # #
|
||||||
. . . . . . . . . . . . . . . # # # # #
|
. . . . . . . . . . . . . . . # # # # #
|
||||||
`, 400)
|
`, 400)
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
basic.clearScreen()
|
basic.clearScreen()
|
||||||
})
|
})
|
||||||
input.onButtonPressed("B", () => {
|
input.onButtonPressed(Button.B, () => {
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -52,10 +52,10 @@ basic.showAnimation(`
|
|||||||
. . . . . # # # # # # # # # # # # # # #
|
. . . . . # # # # # # # # # # # # # # #
|
||||||
. . . . . . . . . . . . . . . # # # # #
|
. . . . . . . . . . . . . . . # # # # #
|
||||||
`, 400)
|
`, 400)
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
basic.clearScreen()
|
basic.clearScreen()
|
||||||
})
|
})
|
||||||
input.onButtonPressed("B", () => {
|
input.onButtonPressed(Button.B, () => {
|
||||||
basic.showAnimation(`
|
basic.showAnimation(`
|
||||||
# # # # # # # # # # . . . . . . . . . .
|
# # # # # # # # # # . . . . . . . . . .
|
||||||
# # # # # # # # # # . . . . . . . . . .
|
# # # # # # # # # # . . . . . . . . . .
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
# set brightness challenges
|
# set brightness challenges
|
||||||
|
|
||||||
These challenges will allow you to change the brightness of the micro:bit. #docs
|
These challenges will allow you to change the brightness of the micro:bit. docs
|
||||||
|
|
||||||
**Challenge 0**
|
**Challenge 0**
|
||||||
|
|
||||||
@ -11,7 +11,7 @@ These challenges will allow you to change the brightness of the micro:bit. #docs
|
|||||||
```
|
```
|
||||||
led.setBrightness(255)
|
led.setBrightness(255)
|
||||||
led.plotAll()
|
led.plotAll()
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
led.setBrightness(64)
|
led.setBrightness(64)
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
@ -25,10 +25,10 @@ What if we want to turn off all the LEDs? Let's do this by setting the brightnes
|
|||||||
```
|
```
|
||||||
led.setBrightness(255)
|
led.setBrightness(255)
|
||||||
led.plotAll()
|
led.plotAll()
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
led.setBrightness(64)
|
led.setBrightness(64)
|
||||||
})
|
})
|
||||||
input.onButtonPressed("B", () => {
|
input.onButtonPressed(Button.B, () => {
|
||||||
}) // ***
|
}) // ***
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -39,10 +39,10 @@ Inside of the condition `input->on button pressed("B")`, add `led->set brightnes
|
|||||||
```
|
```
|
||||||
led.setBrightness(255)
|
led.setBrightness(255)
|
||||||
led.plotAll()
|
led.plotAll()
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
led.setBrightness(64)
|
led.setBrightness(64)
|
||||||
})
|
})
|
||||||
input.onButtonPressed("B", () => {
|
input.onButtonPressed(Button.B, () => {
|
||||||
led.setBrightness(0) // ***
|
led.setBrightness(0) // ***
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
@ -30,7 +30,7 @@ basic.showAnimation(`
|
|||||||
# . . . # . . . . .
|
# . . . # . . . . .
|
||||||
. # # # . . . . . .
|
. # # # . . . . . .
|
||||||
`, 400)
|
`, 400)
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
}) // ***
|
}) // ***
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -48,7 +48,7 @@ basic.showAnimation(`
|
|||||||
# . . . # . . . . .
|
# . . . # . . . . .
|
||||||
. # # # . . . . . .
|
. # # # . . . . . .
|
||||||
`, 400)
|
`, 400)
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
basic.showLeds(`
|
basic.showLeds(`
|
||||||
. # . # .
|
. # . # .
|
||||||
. # . # .
|
. # . # .
|
||||||
|
@ -13,7 +13,7 @@ At the end of the tutorial, click `keep editing`. Your code should look like thi
|
|||||||
```
|
```
|
||||||
let counter = 0
|
let counter = 0
|
||||||
let fastPress = false
|
let fastPress = false
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
counter = counter + 1
|
counter = counter + 1
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
@ -25,7 +25,7 @@ We need to know when the user has hit button `A` 15 times. The user wins when he
|
|||||||
```
|
```
|
||||||
let counter1 = 0
|
let counter1 = 0
|
||||||
let fastPress1 = false
|
let fastPress1 = false
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
counter1 = counter1 + 1
|
counter1 = counter1 + 1
|
||||||
if (counter1 == 15 && input.runningTime() < 5000) {
|
if (counter1 == 15 && input.runningTime() < 5000) {
|
||||||
}
|
}
|
||||||
@ -37,7 +37,7 @@ Next, if the user has won, let's set our boolean to true. This indicates that he
|
|||||||
```
|
```
|
||||||
let counter2 = 0
|
let counter2 = 0
|
||||||
let fastPress2 = false
|
let fastPress2 = false
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
counter2 = counter2 + 1
|
counter2 = counter2 + 1
|
||||||
if (counter2 == 15 && input.runningTime() < 5000) {
|
if (counter2 == 15 && input.runningTime() < 5000) {
|
||||||
fastPress2 = true // ***
|
fastPress2 = true // ***
|
||||||
@ -52,7 +52,7 @@ We want to set `fastPress` to false if the user was too slow. To do so, we need
|
|||||||
```
|
```
|
||||||
let counter3 = 0
|
let counter3 = 0
|
||||||
let fastPress3 = false
|
let fastPress3 = false
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
counter3 = counter3 + 1
|
counter3 = counter3 + 1
|
||||||
if (counter3 == 15 && input.runningTime() < 5000) {
|
if (counter3 == 15 && input.runningTime() < 5000) {
|
||||||
fastPress3 = true
|
fastPress3 = true
|
||||||
@ -72,7 +72,7 @@ Now let's display if the user won or lost. To do so, we need to check the status
|
|||||||
```
|
```
|
||||||
let counter4 = 0
|
let counter4 = 0
|
||||||
let fastPress4 = false
|
let fastPress4 = false
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
counter4 = counter4 + 1
|
counter4 = counter4 + 1
|
||||||
if (counter4 == 15 && input.runningTime() < 5000) {
|
if (counter4 == 15 && input.runningTime() < 5000) {
|
||||||
fastPress4 = true
|
fastPress4 = true
|
||||||
|
@ -22,7 +22,7 @@ let count = 0
|
|||||||
|
|
||||||
```
|
```
|
||||||
let count_ = 0
|
let count_ = 0
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
count_ = count_ + 1
|
count_ = count_ + 1
|
||||||
basic.showNumber(count_, 100)
|
basic.showNumber(count_, 100)
|
||||||
})
|
})
|
||||||
@ -38,7 +38,7 @@ After two button presses, **count** will be equal to 2.
|
|||||||
|
|
||||||
```
|
```
|
||||||
count_ = 0
|
count_ = 0
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
count_ = count_ + 1
|
count_ = count_ + 1
|
||||||
basic.showNumber(count_, 100)
|
basic.showNumber(count_, 100)
|
||||||
})
|
})
|
||||||
|
@ -24,7 +24,7 @@ let count = 0
|
|||||||
|
|
||||||
```
|
```
|
||||||
let count_ = 0
|
let count_ = 0
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
count_ = count_ + 1
|
count_ = count_ + 1
|
||||||
basic.showNumber(count_, 100)
|
basic.showNumber(count_, 100)
|
||||||
})
|
})
|
||||||
@ -38,7 +38,7 @@ input.onButtonPressed("A", () => {
|
|||||||
|
|
||||||
```
|
```
|
||||||
count_ = 0
|
count_ = 0
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
count_ = count_ + 1
|
count_ = count_ + 1
|
||||||
basic.showNumber(count_, 100)
|
basic.showNumber(count_, 100)
|
||||||
})
|
})
|
||||||
|
@ -13,10 +13,10 @@ At the end of the tutorial, click `keep editing`. Your code should look like thi
|
|||||||
```
|
```
|
||||||
initializeGame()
|
initializeGame()
|
||||||
playLevel()
|
playLevel()
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
selectHat()
|
selectHat()
|
||||||
})
|
})
|
||||||
input.onButtonPressed("B", () => {
|
input.onButtonPressed(Button.B, () => {
|
||||||
chooseHat()
|
chooseHat()
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
@ -46,7 +46,7 @@ Write the code that displays the next letter of the string in "cup select" when
|
|||||||
<br/>
|
<br/>
|
||||||
|
|
||||||
```
|
```
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
index = index + 1
|
index = index + 1
|
||||||
if (index > 2) {
|
if (index > 2) {
|
||||||
index = 0
|
index = 0
|
||||||
|
@ -12,7 +12,7 @@ At the end of the tutorial, click `keep editing`. Your code should look like thi
|
|||||||
|
|
||||||
```
|
```
|
||||||
let inital = 5
|
let inital = 5
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
let doubled1 = double(initial)
|
let doubled1 = double(initial)
|
||||||
basic.showNumber(doubled1, 150) // ***
|
basic.showNumber(doubled1, 150) // ***
|
||||||
})
|
})
|
||||||
@ -40,11 +40,11 @@ Add a condition for when button `B` is pressed. We will use this condition in th
|
|||||||
|
|
||||||
```
|
```
|
||||||
initial = 5
|
initial = 5
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
let doubled = double(initial)
|
let doubled = double(initial)
|
||||||
basic.showNumber(doubled, 150)
|
basic.showNumber(doubled, 150)
|
||||||
})
|
})
|
||||||
input.onButtonPressed("B", () => {
|
input.onButtonPressed(Button.B, () => {
|
||||||
}) // ***
|
}) // ***
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -21,7 +21,7 @@ let x = 5
|
|||||||
<br/>
|
<br/>
|
||||||
|
|
||||||
```
|
```
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -28,7 +28,7 @@ basic.showLeds(`
|
|||||||
. . # . .
|
. . # . .
|
||||||
. . # . .
|
. . # . .
|
||||||
`, 400)
|
`, 400)
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -42,7 +42,7 @@ basic.showLeds(`
|
|||||||
. . # . .
|
. . # . .
|
||||||
. . # . .
|
. . # . .
|
||||||
`, 400)
|
`, 400)
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
let random = Math.random(2)
|
let random = Math.random(2)
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
@ -57,7 +57,7 @@ basic.showLeds(`
|
|||||||
. . # . .
|
. . # . .
|
||||||
. . # . .
|
. . # . .
|
||||||
`, 400)
|
`, 400)
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
let random1 = Math.random(2)
|
let random1 = Math.random(2)
|
||||||
if (random1 == 0) {
|
if (random1 == 0) {
|
||||||
basic.showString("TRUTH", 150)
|
basic.showString("TRUTH", 150)
|
||||||
@ -79,7 +79,7 @@ basic.showLeds(`
|
|||||||
. . # . .
|
. . # . .
|
||||||
. . # . .
|
. . # . .
|
||||||
`, 400)
|
`, 400)
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
let random2 = Math.random(2)
|
let random2 = Math.random(2)
|
||||||
if (random2 == 0) {
|
if (random2 == 0) {
|
||||||
basic.showString("TRUTH", 150)
|
basic.showString("TRUTH", 150)
|
||||||
|
@ -18,7 +18,7 @@ basic.plotImage(`
|
|||||||
. . # . .
|
. . # . .
|
||||||
. . # . .
|
. . # . .
|
||||||
`)
|
`)
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
let random = Math.random(2)
|
let random = Math.random(2)
|
||||||
if (random == 0) {
|
if (random == 0) {
|
||||||
basic.showString("TRUTH", 150)
|
basic.showString("TRUTH", 150)
|
||||||
@ -47,7 +47,7 @@ basic.plotImage(`
|
|||||||
. . # . .
|
. . # . .
|
||||||
. . # . .
|
. . # . .
|
||||||
`)
|
`)
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
let random1 = Math.random(3) // ***
|
let random1 = Math.random(3) // ***
|
||||||
if (random1 == 0) {
|
if (random1 == 0) {
|
||||||
basic.showString("TRUTH", 150)
|
basic.showString("TRUTH", 150)
|
||||||
@ -76,7 +76,7 @@ basic.plotImage(`
|
|||||||
. . # . .
|
. . # . .
|
||||||
. . # . .
|
. . # . .
|
||||||
`)
|
`)
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
let random2 = Math.random(3)
|
let random2 = Math.random(3)
|
||||||
if (random2 == 0) {
|
if (random2 == 0) {
|
||||||
basic.showString("TRUTH", 150) // ***
|
basic.showString("TRUTH", 150) // ***
|
||||||
|
@ -10,14 +10,14 @@ This [guided tutorial](/microbit/lessons/typing-game/tutorial) will teach you ho
|
|||||||
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||||
index = 0
|
index = 0
|
||||||
name = ""
|
name = ""
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
led.showString(alphabet.substr(index, 1), 0)
|
led.showString(alphabet.substr(index, 1), 0)
|
||||||
index = index + 1
|
index = index + 1
|
||||||
})
|
})
|
||||||
if (index > 25) {
|
if (index > 25) {
|
||||||
index = 0
|
index = 0
|
||||||
}
|
}
|
||||||
input.onButtonPressed("B", () => {
|
input.onButtonPressed(Button.B, () => {
|
||||||
name = name.concat(alphabet.substr(index - 1, 1))
|
name = name.concat(alphabet.substr(index - 1, 1))
|
||||||
})
|
})
|
||||||
input.onGesture(Gesture.Shake, () => {
|
input.onGesture(Gesture.Shake, () => {
|
||||||
@ -33,14 +33,14 @@ After you have shown the string in the condition `on shake`, make the name varia
|
|||||||
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||||
index = 0
|
index = 0
|
||||||
name = ""
|
name = ""
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
led.showString(alphabet.substr(index, 1), 0)
|
led.showString(alphabet.substr(index, 1), 0)
|
||||||
index = index + 1
|
index = index + 1
|
||||||
})
|
})
|
||||||
if (index > 25) {
|
if (index > 25) {
|
||||||
index = 0
|
index = 0
|
||||||
}
|
}
|
||||||
input.onButtonPressed("B", () => {
|
input.onButtonPressed(Button.B, () => {
|
||||||
name = name.concat(alphabet.substr(index - 1, 1))
|
name = name.concat(alphabet.substr(index - 1, 1))
|
||||||
})
|
})
|
||||||
input.onGesture(Gesture.Shake, () => {
|
input.onGesture(Gesture.Shake, () => {
|
||||||
@ -57,14 +57,14 @@ After you have cleared the name variable to hold nothing, make `index := 0` so t
|
|||||||
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||||
index = 0
|
index = 0
|
||||||
name = ""
|
name = ""
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
led.showString(alphabet.substr(index, 1), 0)
|
led.showString(alphabet.substr(index, 1), 0)
|
||||||
index = index + 1
|
index = index + 1
|
||||||
})
|
})
|
||||||
if (index > 25) {
|
if (index > 25) {
|
||||||
index = 0
|
index = 0
|
||||||
}
|
}
|
||||||
input.onButtonPressed("B", () => {
|
input.onButtonPressed(Button.B, () => {
|
||||||
name = name.concat(alphabet.substr(index - 1, 1))
|
name = name.concat(alphabet.substr(index - 1, 1))
|
||||||
})
|
})
|
||||||
input.onGesture(Gesture.Shake, () => {
|
input.onGesture(Gesture.Shake, () => {
|
||||||
|
@ -18,7 +18,7 @@ Now we need to reply after someone asks Micro a yes or no question. We want to
|
|||||||
|
|
||||||
```
|
```
|
||||||
basic.showString("ASK ME A QUESTION", 150)
|
basic.showString("ASK ME A QUESTION", 150)
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
basic.showString("YES", 150) // ***
|
basic.showString("YES", 150) // ***
|
||||||
}) // ***
|
}) // ***
|
||||||
```
|
```
|
||||||
@ -33,10 +33,10 @@ What if Micro's answer to the question is no? Let's have `NO` be displayed when
|
|||||||
|
|
||||||
```
|
```
|
||||||
basic.showString("ASK ME A QUESTION", 150)
|
basic.showString("ASK ME A QUESTION", 150)
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
basic.showString("YES", 150)
|
basic.showString("YES", 150)
|
||||||
})
|
})
|
||||||
input.onButtonPressed("B", () => {
|
input.onButtonPressed(Button.B, () => {
|
||||||
basic.showString("NO", 150) // ***
|
basic.showString("NO", 150) // ***
|
||||||
}) // ***
|
}) // ***
|
||||||
```
|
```
|
||||||
|
@ -53,7 +53,7 @@ The first job of the scheduler is to allow multiple *subprograms* to be queued u
|
|||||||
```
|
```
|
||||||
export function countButtonPresses() {
|
export function countButtonPresses() {
|
||||||
let count = 0
|
let count = 0
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
count = count + 1
|
count = count + 1
|
||||||
})
|
})
|
||||||
basic.forever(() => {
|
basic.forever(() => {
|
||||||
@ -71,7 +71,7 @@ let count1 = 0
|
|||||||
initializesthe variable `count`. The second statement
|
initializesthe variable `count`. The second statement
|
||||||
|
|
||||||
```
|
```
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
count1 = count1 + 1
|
count1 = count1 + 1
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
@ -135,13 +135,13 @@ As a result, you can easily add a new capability to the micro:bit by just adding
|
|||||||
```
|
```
|
||||||
export function countButtonPressesWithReset() {
|
export function countButtonPressesWithReset() {
|
||||||
let count = 0
|
let count = 0
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
count = count + 1
|
count = count + 1
|
||||||
})
|
})
|
||||||
basic.forever(() => {
|
basic.forever(() => {
|
||||||
basic.showNumber(count, 150)
|
basic.showNumber(count, 150)
|
||||||
})
|
})
|
||||||
input.onButtonPressed("B", () => {
|
input.onButtonPressed(Button.B, () => {
|
||||||
count = 0
|
count = 0
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,7 @@ The code below shows a simple script that sends a line when the BBC micro:bit st
|
|||||||
|
|
||||||
```
|
```
|
||||||
serial.writeLine("started...")
|
serial.writeLine("started...")
|
||||||
input.onButtonPressed("A", () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
serial.writeLine("A pressed")
|
serial.writeLine("A pressed")
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
Loading…
Reference in New Issue
Block a user