This commit is contained in:
Michael Elliot Braun 2016-03-31 18:27:20 -07:00
parent 3c9c30e489
commit 3a12314332
1 changed files with 41 additions and 1 deletions

View File

@ -159,8 +159,48 @@ while (true) {
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.
![](/static/mb/blocks/lessons/hero-8.jpg)
```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 (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 );
}
if (input.acceleration(Dimension.X) > 200) {
hero.change(LedSpriteProperty.X, 1);
}
else if (input.acceleration(Dimension.X) < -200 ) {
hero.change(LedSpriteProperty.X, -1 );
}
if (input.acceleration(Dimension.Y) > 200) {
hero.change(LedSpriteProperty.Y, 1);
}
else if (input.acceleration(Dimension.Y) > -200 ) {
hero.change(LedSpriteProperty.Y, -1 );
}
if (hero.isTouching(food)) {
game.addScore(1);
food.set(LedSpriteProperty.X, Math.random(5));
food.set(LedSpriteProperty.Y, Math.random(5));
}
}
```
**Do not disconnect the blocks from the conditional statements. We are focusing on this section of the code and are not showing the entire code**