Step 2 - Efficient Looping

This time, let’s tell our chicken in this level how to reach the coin with the least amount of instructions possible. That’s called ‘being efficient’.

> Click on Level 2 in the levels bar.

image alt text

> Think about how many movements the chicken will need to make to get to the coin.

image alt text

It looks like the chicken will need to move up twice, and left twice.

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

We can use two "repeat" blocks to achieve our efficient instructions.

> Open Events and drag "When the level starts" into the work space.

> Open Control Flow and drag two "repeat 10 times" blocks into the "When the level starts" block, one over the other.

image alt text

> Open Events and drag two "Broadcast message" blocks into the work space. Place one "broadcast" block in each “repeat” block.

> The chicken first needs to move up two times, so change the first "repeat 10 times" to “repeat 2 times”. Also change “broadcast message” to “broadcast up”.

> The chicken then needs to move left two times, so change the second "repeat 10 times" block to “repeat 2 times”, and change the second “broadcast message” block to “broadcast left”.

image alt text

> Click Play to see if our code works!

Did the chicken reach the coin? Great! Let’s continue to the next step.

If not, have a look back over your Script and see if you can spot the bugs.

results matching ""

    No results matching ""