Scratch Part 6: 360 Degree Navigation

Use the arrow keys to allow the sprite to move in any direction.

Think about this.

What direction do you need to turn in?

How many degrees do you want to turn at a time?

How will you make the sprite move forward?

Make a program to implement your ideas.

Task 6: Accelerate and Decelerate

Declare a variable called yspeed. Initialise it to zero at the start of the program.

We know that y increases in value up the screen and decreases in value down the screen. To make a sprite go faster when moving upwards we need to use a variable to control the size of step it takes: bigger steps when accelerating, smaller when decelerating.

Add a Green Flag hat and a Forever if block set to detect an up key press.

Let's say that we initialise yspeed to zero. When we press the up arrow key, as well as setting the direction, the value of y should increase and we will move by the current value of yspeed. We insert the yspeed variable identifier into the number space in the Move command.

Perhaps we would like the sprite to slow down when we stop pressing the up button so that it eventually stops and falls down.

To do this we need to replace the Forever if block with a Forever block. Inside the Forever block place an If Else block. There are now two conditions to detect, pressing the up key or not pressing it. The first part of the block detects an up key press and is the same as the previous version - increment yspeed and move by the current number of steps. The else part of the block deals with the no key press situation, in which case the value of yspeed is reduced by changing it by a negative amount before the sprite is moved.

Make sure the yspeed variable is displayed on the screen. Watch what happens to the value as the program runs. How could you improve it?

Extension

Add a black line to the background. Write code that makes the sprite stop falling when it touches the black line.  To do this you will need an additional if block with a condition set to detect a colour. If the sprite is touching the colour then the value of yspeed will be set to a value.

Add additional controls so the sprite can move left and right.

Add a blue line and make the program stop if the sprite touches the blue line.

Add costumes so that when the sprite is moving upwards there is a spurt of flame from a 'jetpack'. There should be sprites for left- and right- facing situations and for conditions of jet pack on and off.

See here for full code: Jetpack Girl.

Task 7: Race Track Game

Make a background in the form of a racetrack. Add keyboard controls for two car sprites so two people can play. This will mean duplicating the code for the two sprites - use right-click, duplicate and paste or simply drag code onto a sprite. Make sure the cars start at a suitable point on the track (x,y).

Use the previous code ideas to make the cars go faster and slower.

Take appropriate action when a car leaves the road.

You should be able to navigate the sprites around the track but can you count the laps?

What other features can you add to allow laps to be counted?

Make sure you save your work before developing this further.

Collisions

This is a good point to introduce collisions between sprites. In a race game it is hard to tell which sprite crashed into another so you have to treat them  both equally. Use a new Green Flag control and a forever if block to detect a collision and take some action such as putting the cars back at the start.

The condition you need to detect is whether sprites are touching - use the touching ? test in Sensing.

Documentation

Summarise the actions of the code you have written. Include a section on each block of code and the algorithm it contains. Include at least one flow chart in your account.