From e0da743cb4e0f41d899a0a22fef4a3060118b625 Mon Sep 17 00:00:00 2001 From: Michael Elliot Braun Date: Thu, 31 Mar 2016 18:16:03 -0700 Subject: [PATCH] updated hero --- docs/lessons/catch-the-egg-game/activity.md | 6 +- docs/lessons/headbands/activity.md | 4 +- docs/lessons/hero/activity.md | 81 ++++++++++++++++++--- 3 files changed, 76 insertions(+), 15 deletions(-) diff --git a/docs/lessons/catch-the-egg-game/activity.md b/docs/lessons/catch-the-egg-game/activity.md index ddf37fe6..61d29db4 100644 --- a/docs/lessons/catch-the-egg-game/activity.md +++ b/docs/lessons/catch-the-egg-game/activity.md @@ -56,9 +56,9 @@ basic.forever(() => { } if (eggY1 == 4) { if (basketX1 == eggX1) { - game.addScore(1) // *** + game.addScore(1) } else { - game.removeLife(1) // *** + game.removeLife(1) } } basic.pause(300) @@ -142,7 +142,7 @@ basic.forever(() => { game.removeLife(1) } } - basic.pause(fallingPause1) // *** + basic.pause(fallingPause1) }) ``` diff --git a/docs/lessons/headbands/activity.md b/docs/lessons/headbands/activity.md index c4e6bfda..a8c467bf 100644 --- a/docs/lessons/headbands/activity.md +++ b/docs/lessons/headbands/activity.md @@ -1,6 +1,4 @@ -# headbands challenges - -These challenges will teach you how to create a fun charades game to play with your friends. #docs +# headbands activity ## Before we get started diff --git a/docs/lessons/hero/activity.md b/docs/lessons/hero/activity.md index 7b001a52..7f81263f 100644 --- a/docs/lessons/hero/activity.md +++ b/docs/lessons/hero/activity.md @@ -39,36 +39,99 @@ ghost.change(LedSpriteProperty.Blink, 100); We want to identify the food so the player moves towards the food. We need to set the `brightness` of the `variable` food to 8. The brightness of the LED screen is expressed as a number between 0 and 255. -![](/static/mb/blocks/lessons/hero-3.png) - - -We want to include a block from the Loops drawer called `While`. Then set the `While` loop to `true`. This code will be important for repeating code of the game logic of the game. The game will continue to run using `While` loop while the Boolean condition is true. Finally, include a `pause` of 400 milliseconds before the logic of the game begins. - - ```blocks let hero = game.createSprite(2, 2); let food = game.createSprite(4, 4); let ghost = game.createSprite(0, 0); ghost.change(LedSpriteProperty.Blink, 100); +food = led.brightness() == 8; + +``` + +We want to include a block from the Loops drawer called `While`. Then set the `While` loop to `true`. This code will be important for repeating code of the game logic of the game. The game will continue to run using `While` loop while the Boolean condition is true. Finally, include a `pause` of 400 milliseconds before the logic of the game begins. + + +```blocks + let ghost = 0; + let food = 0; + let hero = 0; +hero = game.createSprite(2, 2); +food = game.createSprite(4, 4); +ghost = game.createSprite(0, 0); +ghost.change(LedSpriteProperty.Blink, 100); +food = led.brightness() == 8; while (true) { - basic.pause(400) + basic.pause(400); } + + ``` Let's create a function that will take care of keep the ghost pursuing the hero. We will need to a conditional statement that checks the position of the ghost and hero. The first condition will check if the horizontal coordinates of the ghost is less than the horizontal coordinates of the hero. We create a function from the Game drawer that will check the coordinates of the hero and the ghost. Finally, change the x-direction of the ghost by 1. Then create another function that will take care of keep the ghost pursuing the hero. We will need to a conditional statement that checks the position of the ghost and hero. The second condition will check if the horizontal coordinates of the ghost is greater than the x-direction of hero. We create a function from the Game drawer that will check the x-direction of hero and ghost. Finally, change the x-direction of the ghost by -1. -![](/static/mb/blocks/lessons/hero-5.png) +```blocks +let hero = game.createSprite(2, 2); +let food = game.createSprite(4, 4); +let ghost = game.createSprite(0, 0); +ghost.change(LedSpriteProperty.Blink, 100); +food = led.brightness() == 8; +while (true) { + basic.pause(400); + if (ghost.get(LedSpriteProperty.X) < hero.get(LedSpriteProperty.X)) { + ghost.change(LedSpriteProperty.X, 1); + } + else if (ghost.get(LedSpriteProperty.X) > hero.get(LedSpriteProperty.X)) { + ghost.change(LedSpriteProperty.X, -1 ); + } + else if (false) { + } + else if (false) { + } + basic.pause(20); +} + + +``` Let's create the third function and forth function that continues the same logic in the y-direction of pacman and ghost. We create a function from the Game drawer that will check the y-direction of pacman and ghost. Finally, change the y-direction of the ghost to continue following pacman. -![](/static/mb/blocks/lessons/hero-6.png) +```blocks + + let ghost = 0; + let food = 0; + let hero = 0; +hero = game.createSprite(2, 2); +food = game.createSprite(4, 4); +ghost = game.createSprite(0, 0); +ghost.change(LedSpriteProperty.Blink, 100); +food = led.brightness() == 8; +while (true) { + basic.pause(400); + if (ghost.get(LedSpriteProperty.X) < hero.get(LedSpriteProperty.X)) { + ghost.change(LedSpriteProperty.X, 1); + } + else if (ghost.get(LedSpriteProperty.X) < hero.get(LedSpriteProperty.X)) { + ghost.change(LedSpriteProperty.X, -1 ); + } + else if (ghost.get(LedSpriteProperty.Y) < hero.get(LedSpriteProperty.Y)) { + ghost.change(LedSpriteProperty.Y, 1); + } + else if (ghost.get(LedSpriteProperty.Y) > hero.get(LedSpriteProperty.Y)) { + ghost.change(LedSpriteProperty.Y, -1 ); + } + basic.pause(20); +} + + +``` Let's enable pacman to move in the x-direction and move in the y-direction with acceleration using the micor:bit sensor ![](/static/mb/blocks/lessons/hero-7.png) + **Do not disconnect the blocks for the conditional statements. We are focusing on this section of the code and are not showing the entire code** Let's setup the logic for the food. If hero is `touching` "food", increase the score of the game by 1 and `set` ``x`` -direction of food randomly randomly from 0 to 4 and `set` ``y``-direction of food randomly from 0 to 4.