Scratch exercise 108

Park a car challenge - repeat loop animations 2

The car park is almost full. Can you park your car in each of the remaining four spaces?


1. Create a new game and then download and save this stage background image (e.g. right-click it) ..


2. Select the stage, and then select the "Backgrounds" tab. Use the Import button to import the background image you have just downloaded and saved ..

You can delete the white background, we only need the car park.


3. Add these scripts to the stage ..


4. Download and save this sprite of a car (e.g. right-click it) ..


5. Use the "Choose new sprite from file" button under the stage to load the "Car-red" sprite, then delete the cat sprite.


6. Make sure the "Car-red" sprite is selected and then add these scripts ..

Press "B" on your keyboard and you will see the "Car-red" sprite shrink and position itself read to enter the car park.


7. Use File>Save as to save your game. Name it "Car parking".


8. It is useful to be able to see the exact position of our "Car-red" sprite as we create our scripts.

With the "Car-red" sprite still selected, click on the Motion scripts and turn on "x position" and "y position" for the car ..

Now drag their displays on the stage and position them out of the way near the bottom like this ..


9. Let's create the first animation. We are going to make the "Car-red" sprite drive to and park in space number 1. Add this script to the "Car-red" sprite ..

Press "B" to start your game and then "1" to test the animation. Notice how "Car-red" sprite slows down as it gets near to the space. This is because we are using two repeat script blocks. In the first the car is moving at the speed of 4 steps, and in the second it slows to 1 step.


10. Press "E" to end the game. Now create a second animation to park the car in space number 2. This one is more complex ..

Test this animation. The "Car-red" sprite should drive forwards, turn and then reverse into space 2.

Notice also how we are using "repeat until" script blocks and some 'more than' (>) and 'less than' (<) operators. These work better than "=" operators because the "Car-red" sprite sometimes over-shoots. Also, when the car turns we have to keep in moving forward, this time at a speed of 2 steps.


11. Use File>Save to save your game again.


12. Challenge 1: Using what you have learnt, create two more animations for spaces 3 and 4. Use only repeat loops and 3 speeds, slow (1 step), medium (2 steps) and fast (4 steps).


13. Challenge 2 (advanced): Complete these steps ..

a) Download and save this sprite of a yellow car (e.g. right-click it) ..


b) Add "Car-yellow.sprite" to your game, then duplicate it twice so you have three car-yellow sprites.

c) Create scripts that position the car-yellow sprites randomly in 3 of the 4 empty parking spaces (they don't need to drive into the spaces). Each time you play the game the remaining empty space should be randomly selected.

d) Create scripts that automatically select the remaining empty space and choose the correct animation to park "Car-red". Use only repeat loops and 3 speeds, slow (1 step), medium (2 steps) and fast (4 steps).