Some updates to 'catch the egg' (#1588)

This commit is contained in:
Galen Nickel 2018-11-05 09:07:50 -08:00 committed by Peli de Halleux
parent 54695601fa
commit ec45f388df
3 changed files with 40 additions and 38 deletions

View File

@ -9,7 +9,7 @@ Variables
## Quick Links
* [activity](/lessons/catch-the-egg-game/activity)
* [tutorial](/lessons/catch-the-egg-game/tutorial)
* [challenge](/lessons/catch-the-egg-game/challenge)
* [quiz](/lessons/catch-the-egg-game/quiz)
* [quiz answers](/lessons/catch-the-egg-game/quiz-answers)
@ -29,7 +29,7 @@ basic.pause(300);
input.acceleration(Dimension.X);
Math.min(0,0);
Math.max(0,1);
Math.randomRange(0, 5);
Math.randomRange(0, 4);
game.addScore(1);
game.score();
game.removeLife(1);

View File

@ -8,10 +8,10 @@ Your starting code should look like this:
let basketX = 2
let eggX = 2
let eggY = 0
basic.forever(() => {
basic.forever(function() {
led.unplot(basketX, 4)
led.unplot(eggX, eggY)
eggY = eggY + 1
eggY += 1
led.plot(eggX, eggY)
basic.pause(300)
let accX = input.acceleration(Dimension.X)
@ -19,30 +19,30 @@ basic.forever(() => {
led.plot(basketX, 4)
if (eggY > 4) {
eggY = -1
eggX = Math.randomRange(0, 5)
eggX = Math.randomRange(0, 4)
}
basic.pause(300)
})
```
## ~avatar avatar impressed
## Challenge 1
### ~avatar avatar impressed
Let's use an **IF** statement to detect if the egg and the basket are lined up.
Now that we know when an egg is caught, we can keep track of the score! We need to use the `add score` function built into the game library to add `1` point for every egg that is caught. However, let's not forget to `remove life` if an egg falls off the display before it's caught!
## ~
### ~
```blocks
let basketX1 = 2
let eggX1 = 2
let eggY1 = 0
basic.forever(() => {
basic.forever(function() {
led.unplot(basketX1, 4)
led.unplot(eggX1, eggY1)
eggY1 = eggY1 + 1
eggY1 += 1
led.plot(eggX1, eggY1)
basic.pause(300)
let accX = input.acceleration(Dimension.X)
@ -50,7 +50,7 @@ basic.forever(() => {
led.plot(basketX1, 4)
if (eggY1 > 4) {
eggY1 = -1
eggX1 = Math.randomRange(0, 5)
eggX1 = Math.randomRange(0, 4)
}
if (eggY1 == 4) {
if (basketX1 == eggX1) {
@ -65,23 +65,23 @@ basic.forever(() => {
* Press the `run` button to test out your game.
## ~avatar avatar encourage
## Challenge 2
### ~avatar avatar encourage
Catching eggs gets easier with practice so let's make the eggs fall faster every 5 catches. We can do this by tracking how long the egg pauses in each position while falling with a global variable called **falling pause**. Let's create this variable and set it to `300` initially. Don't forget to also create a condition that will be true every 5 catches.
## ~
### ~
```blocks
let basketX2 = 2
let eggX2 = 2
let eggY2 = 0
let fallingPause = 300 // ***
basic.forever(() => {
let fallingPause = 300
basic.forever(function() {
led.unplot(basketX2, 4)
led.unplot(eggX2, eggY2)
eggY2 = eggY2 + 1
eggY2 += 1
led.plot(eggX2, eggY2)
basic.pause(300)
let accX2 = input.acceleration(Dimension.X)
@ -89,7 +89,7 @@ basic.forever(() => {
led.plot(basketX2, 4)
if (eggY2 > 4) {
eggY2 = -1
eggX2 = Math.randomRange(0, 5)
eggX2 = Math.randomRange(0, 4)
}
if (eggY2 == 4) {
if (basketX2 == eggX2) {
@ -104,21 +104,23 @@ basic.forever(() => {
})
```
## ~avatar avatar surprised
## Challenge 3
### ~avatar avatar surprised
Let's make the egg fall faster by decreasing the amount of time it pauses in each position by decreasing **falling pause** by `25` every 5 catches. Now, instead of pausing for 300 milliseconds we can pause for the value of **falling pause**.
### ~
```blocks
let basketX3 = 2
let eggX3 = 2
let eggY3 = 0
let fallingPause1 = 300
basic.forever(() => {
basic.forever(function() {
led.unplot(basketX3, 4)
led.unplot(eggX3, eggY3)
eggY3 = eggY3 + 1
eggY3 += 1
led.plot(eggX3, eggY3)
basic.pause(300)
let accX3 = input.acceleration(Dimension.X)
@ -126,13 +128,13 @@ basic.forever(() => {
led.plot(basketX3, 4)
if (eggY3 > 4) {
eggY3 = -1
eggX3 = Math.randomRange(0, 5)
eggX3 = Math.randomRange(0, 4)
}
if (eggY3 == 4) {
if (basketX3 == eggX3) {
game.addScore(1)
if (game.score()% 5 == 0) {
fallingPause1 = fallingPause1 - 25 // ***
fallingPause1 = fallingPause1 - 25
}
} else {
game.removeLife(1)
@ -140,7 +142,6 @@ basic.forever(() => {
}
basic.pause(fallingPause1)
})
```
Fantastic! Your game is now ready to show off.

View File

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