Step 2: Checking for inequality ( ≠ )

>Click on Level - 2 in the levels bar to switch to the second level.

In level 2 the chicken can fairly easily collect all 5 coins to win the game. Test it out, if you'd like.

>Click Play and try to win on level 2.

Let's make it a bit more difficult by limiting the number of steps the chicken can take. You should be able to win this level in under 30 steps, so we'll make 30 the limit. The step counter is shown in the top-left corner of the game.

>Click Close to return to viewing the level.

Let's program in a limit to the number of steps the chicken can take.

>Right-click on the chicken and select Edit Script.

We'll use another type of comparative operator to check if the steps taken do not equal 30.

We can see that when the player presses arrow keys, a message is broadcasted to the game to tell the chicken to move. We'll add an if-else block to the arrow key event blocks, and we'll only allow the chicken to move if the number of steps does not equal 30, otherwise we'll broadcast "you lose" to make the game end.

>Open Control Flow and drag `if do` into the `When up arrow pressed` block.

>Open Operators and drag `_=_` into the side of the `if do` block.

>Click on = and select ≠ from the drop down menu.

This block is another type of comparative operator called an inequality operator, since it checks if two things are not equal to each other.

>Open Variables and drag `number steps taken` into the left side of the `_≠_` block.

>Open Operators and drag `0` into the right side of the `_≠_` block. Click on 0 and type in 30.

>Drag the existing `broadcast up` block into the `if do` block.

Now the chicken will only move up when the number of steps taken does not equal 30. But what if they do equal 30? We'll use an if-else block to check for that.

>Click on the cog icon on the `if do` block to open the small pop-up window.

>Drag `else` into the `if` block, then click the cog again to close the small pop-up window.

>Open Events and drag `broadcast message` into the else part of the `if-do-else` block.

>Click on "message" and change it to "you lose".

Now we need to make these same additions to the down, left, and right arrow blocks.

>Right-click on the `if-do-else` block and select duplicate.

>Drag the duplicated blocks into the `When down arrow pressed` block.

>Right-click on the `broadcast up` block and delete it.

>Drag the existing `broadcast down` block into the `else` gap.

>Repeat this process for the `When left arrow pressed` block and the `When right arrow pressed` block.

Now click Play and select Level 2 to test it out. When you get to 30 steps then you will lose. But you're still a winner for getting this working! Now you can move on to the next step.

results matching ""

    No results matching ""