pxt-calliope/docs/lessons/offset-image/activity.md

95 lines
1.9 KiB
Markdown
Raw Normal View History

2016-03-26 00:47:20 +01:00
# offset image challenges
Coding challenges for the offset image tutorial. #docs
## Before we get started
2016-03-31 02:15:42 +02:00
Complete the following exercise. Your code should look like this:
2016-03-26 00:47:20 +01:00
2016-03-31 02:15:42 +02:00
```blocks
2016-03-26 00:47:20 +01:00
offset = 0
basic.forever(() => {
if (offset == -4) {
basic.showString("Push button A", 150)
}
images.createImage(`
. . # . .
. . # . .
. . # . .
. # # # .
. . # . .
`).showImage(offset)
})
input.onButtonPressed(Button.A, () => {
2016-03-26 00:47:20 +01:00
offset = offset + 1
})
```
### Challenge 1
Create a condition for if button `B` is pressed. We want the image to move to the left when button `B` is pressed.
```
offset = 0
basic.forever(() => {
if (offset == -4) {
basic.showString("Push button A", 150)
}
images.createImage(`
. . # . .
. . # . .
. . # . .
. # # # .
. . # . .
`).showImage(offset)
})
input.onButtonPressed(Button.A, () => {
2016-03-26 00:47:20 +01:00
offset = offset + 1
})
input.onButtonPressed(Button.B, () => {
2016-03-26 00:47:20 +01:00
offset = offset - 1 // ***
}) // ***
```
* Run the code to see if it works as expected.
### Challenge 2
2016-05-27 00:24:10 +02:00
2016-03-26 00:47:20 +01:00
Now we want to make sure that the button does not go off the screen to the right. Add a new line that checks to see if offset = 5 after button `A` is pressed.
If `offset = 5` then prompt the user to move the image to the left by displaying the text: "Push button B".
```
offset = 0
basic.forever(() => {
if (offset == -4) {
basic.showString("Push button A", 150)
}
if (offset == 5) {
basic.showString("Press Button B", 150) // ***
}
images.createImage(`
. . # . .
. . # . .
. . # . .
. # # # .
. . # . .
`).showImage(offset)
})
input.onButtonPressed(Button.A, () => {
2016-03-26 00:47:20 +01:00
offset = offset + 1
})
input.onButtonPressed(Button.B, () => {
2016-03-26 00:47:20 +01:00
offset = offset - 1
})
```
* Run the code to see if it works as expected.
2016-05-06 18:28:26 +02:00
### Challenge 3
2016-03-26 00:47:20 +01:00
Now make sure the image does not go off the left side and if it does, prompt the user to push button `A`.