Migrate docs from the other repo
This commit is contained in:
129
docs/reference/js/lessons/digital-pet/challenges.md
Normal file
129
docs/reference/js/lessons/digital-pet/challenges.md
Normal file
@ -0,0 +1,129 @@
|
||||
# digital pet challenges
|
||||
|
||||
Coding challenges for the digital pet tutorial.
|
||||
|
||||
## Before we get started
|
||||
|
||||
Complete the following guided tutorial:
|
||||
|
||||
* [tutorial](/microbit/lessons/digital-pet/tutorial)
|
||||
|
||||
At the end of the tutorial, click `keep editing`. Your code should look like this:
|
||||
|
||||
```
|
||||
basic.forever(() => {
|
||||
if (input.buttonIsPressed("A")) {
|
||||
setSleep()
|
||||
basic.pause(5000)
|
||||
} else {
|
||||
setAwake()
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### Challenge 1
|
||||
|
||||
Now let's feed the pet! Add an **ELSE IF** statement that checks if button `B` is pressed. Click on the **ELSE** and type **IF** next to it to get the **ELSE IF**.
|
||||
|
||||
```
|
||||
basic.forever(() => {
|
||||
if (input.buttonIsPressed("A")) {
|
||||
setSleep()
|
||||
basic.pause(5000)
|
||||
} else if (input.buttonIsPressed("B")) {
|
||||
|
||||
}
|
||||
else {
|
||||
setAwake()
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### Challenge 2
|
||||
|
||||
### @video td/videos/digital-pet-2
|
||||
|
||||
Now we want to show your eating pet! Let's create a function called `set eat` that will do create an image. Store that image in a variable and then show it.
|
||||
|
||||
```
|
||||
export function setEat() {
|
||||
let img = images.createImage(`
|
||||
. # . # .
|
||||
. . # . .
|
||||
. . # . .
|
||||
. # . # .
|
||||
. . # . .
|
||||
`)
|
||||
img.showImage(0)
|
||||
}
|
||||
```
|
||||
|
||||
Once you create the function `set eat`, call it in the **ELSE IF** statement that checks if button `B` is pressed.
|
||||
|
||||
```
|
||||
basic.forever(() => {
|
||||
if (input.buttonIsPressed("A")) {
|
||||
setSleep()
|
||||
basic.pause(5000)
|
||||
} else if (input.buttonIsPressed("B")) {
|
||||
setEat()
|
||||
}
|
||||
else {
|
||||
setAwake()
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### Challenge 3
|
||||
|
||||
### @video td/videos/digital-pet-3
|
||||
|
||||
Have your pet tell you when it is going to sleep! Do this inside of the **IF** statement that checks if button `A` is pressed before you call the function `set sleep`.
|
||||
|
||||
```
|
||||
basic.forever(() => {
|
||||
if (input.buttonIsPressed("A")) {
|
||||
basic.showString("I am going to sleep.", 150) // ***
|
||||
setSleep()
|
||||
basic.pause(5000)
|
||||
} else if (input.buttonIsPressed("B")) {
|
||||
setEat()
|
||||
}
|
||||
else {
|
||||
setAwake()
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### Challenge 4
|
||||
|
||||
### @video td/videos/digital-pet-4
|
||||
|
||||
Now, how about we keep track of how many times our pet eats? Add a global variable called `feed` that keeps track of how many times you feed your pet. If button `B` is pressed, increment `feed` by one. Add a condition `on shake` to check your total.
|
||||
|
||||
```
|
||||
feed = 0 // ***
|
||||
basic.forever(() => {
|
||||
if (input.buttonIsPressed("A")) {
|
||||
basic.showString("I am going to sleep.", 150)
|
||||
setSleep()
|
||||
basic.pause(5000)
|
||||
} else if (input.buttonIsPressed("B")) {
|
||||
feed = feed + 1 // ***
|
||||
setEat()
|
||||
}
|
||||
else {
|
||||
setAwake()
|
||||
}
|
||||
})
|
||||
input.onGesture(Gesture.Shake, () => {
|
||||
basic.showNumber(feed, 150) // ***
|
||||
}) // ***
|
||||
```
|
||||
|
||||
### Challenge 5
|
||||
|
||||
Program your pet to say that it is hungry after 60 seconds.
|
||||
|
||||
**Hint**: use `input->running time`
|
||||
|
60
docs/reference/js/lessons/digital-pet/quiz-answers.md
Normal file
60
docs/reference/js/lessons/digital-pet/quiz-answers.md
Normal file
@ -0,0 +1,60 @@
|
||||
# digital pet quiz answers
|
||||
|
||||
A display of pet images for the BBC micro:bit
|
||||
|
||||
## Name
|
||||
|
||||
## Directions
|
||||
|
||||
Use this activity document to guide your work in the [digital pet tutorial](/microbit/lessons/digital-pet/tutorial)
|
||||
|
||||
Answer the questions while completing the tutorial. Pay attention to the dialogues!
|
||||
|
||||
## 1. What is a 'function'?
|
||||
|
||||
<br/>
|
||||
|
||||
A function is a unit of code that performs a specific task and may return a result.
|
||||
|
||||
## 2. Write the steps to create the function called set awake()
|
||||
|
||||
<br/>
|
||||
|
||||
Click on "+ add new" and then "function". Click on the function name (by default it is "set awake"), and rename it to "set awake()".
|
||||
|
||||
## 3. Write the code inside the function "set awake()" that shows an image of the pet awake
|
||||
|
||||
<br/>
|
||||
|
||||
```
|
||||
let img = images.createImage(`
|
||||
. # . # .
|
||||
. . # . .
|
||||
. . . . .
|
||||
. # # # .
|
||||
. . . . .
|
||||
`)
|
||||
img.showImage(0)
|
||||
```
|
||||
|
||||
## 4. Write the steps to create the function called set sleep, function set sleep()
|
||||
|
||||
<br/>
|
||||
|
||||
Click on "+ add new" and then "function". Click on the function name (by default it is "set sleep"), and rename it to "set sleep()".
|
||||
|
||||
## 5. Write the code inside the function "set sleep()" that shows an image of the pet asleep
|
||||
|
||||
<br/>
|
||||
|
||||
```
|
||||
img = images.createImage(`
|
||||
# # . # #
|
||||
. . # . .
|
||||
. . . . .
|
||||
. # # # .
|
||||
. . . . .
|
||||
`)
|
||||
img.showImage(0)
|
||||
```
|
||||
|
32
docs/reference/js/lessons/digital-pet/quiz.md
Normal file
32
docs/reference/js/lessons/digital-pet/quiz.md
Normal file
@ -0,0 +1,32 @@
|
||||
# digital pet quiz
|
||||
|
||||
A display of pet images for the BBC micro:bit
|
||||
|
||||
## Name
|
||||
|
||||
## Directions
|
||||
|
||||
Use this activity document to guide your work in the [digital pet tutorial](/microbit/lessons/digital-pet/tutorial)
|
||||
|
||||
Answer the questions while completing the tutorial. Pay attention to the dialogues!
|
||||
|
||||
## 1. What is a 'function'?
|
||||
|
||||
<br/>
|
||||
|
||||
## 2. Write the steps to create the function called set awake()
|
||||
|
||||
<br/>
|
||||
|
||||
## 3. Write the code inside the function "set awake()" that shows an image of the pet awake
|
||||
|
||||
<br/>
|
||||
|
||||
## 4. Write the steps to create the function called set sleep, function set sleep()
|
||||
|
||||
<br/>
|
||||
|
||||
## 5. Write the code inside the function "set sleep()" that shows an image of the pet asleep
|
||||
|
||||
<br/>
|
||||
|
Reference in New Issue
Block a user