Code Ninja (Singapore)

View Original

Parking a Car

If someone asks you to validate their parking, tell them they did a great job.

We get a lot of questions about the use of Directions in Scratch.

So today we’re going to practice Sprite movement and changing Directions on the move.

As an added bonus, we will also be learning how to use the colour picker, which has all sorts of interesting uses.


This project is suitable for Beginners.


Remixing the Template

To start off, let’s pick up this template and remix it to get the background and the car Sprite.

https://scratch.mit.edu/projects/372109471/

Be sure you type the address correctly, or you might not get the right project!

Click the Remix button to create your very own copy of the project.

Once you’ve done this, the Scratch coding window will appear.


STEP 1 - SETTING UP THE CAR

Well, our taxi car is on the Stage. Now to make sure it is the correct

  • Size

  • Costume

  • Position

  • Direction

every time we press the green flag and start the game.  

Add in these code to the car:


STEP 2 - MOVING THE CAR

Now we are going to confront our first challenge today. How does a moving car turn?

Well, if the car is moving forward, and we turn left...we face left. And if we turn right, we face right. Simple right?

What if the car is moving backwards... and we turn left? Hmm...we will face right. And moving backwards + turning right, we will face left!

And of course, if the car is neither moving front or back, we can’t turn at all!

Wow, that is a lot of rules.

Let’s tackle them one by one, starting with simply going forward by pressing the UP key.

Phew! That’s not too hard.

Now we want the car to be turning IF we press the LEFT and RIGHT key, while the car is moving forward. 

We will be doing that by using nested IFs - IFs conditions inside another IF condition.


This allows for multiple conditions to be string together. In this case, you have to press UP and LEFT to turn left.A stationary car won’t just turn left on it own.


Try adding in the codes for going backwards and turning using the above example as a template. 

Don’t look ahead until you tried! 


Ok, I know you tried. Just in case you need help here is the complete code for the car movement.

Notice the differences between the forward and the backwards movement.


STEP 3 - KNOCK KNOCK?!

Your driving won’t be perfect. So what happens if you knock into another car?

Well, not to worry. You’ll just get teleported back to the start and try again.

As you can see, there are all sorts of coloured cars in the parking lot. So let’s start by coding what happens when you run into the yellow car.

If you click on the color, you’ll get a box like this where you can select the colour you want.

And at the bottom of the box is the color picker.

Click on that.

And now click on the colour you want to select.

That’s how you use the color picker to pick the exact colour you want.

Now, we need to code in what happens when you hit that yellow car.


Well, now you add in all the code for all the different coloured cars in the parking lot!


Ok, I’ll show you one more. But you got to do the rest yourself!

Have fun parking your car without going “poof”!


Sign up for for a trial class and experience a new way to learn to code.

Join our mailing list or follow us on Facebook to stay updated with our latest projects.