Scratch Part 3: Motion

To move a sprite use the Motion area.

Task 1: Control a Sprite’s Movement

Practise with the default sprite.

To write the most basic of programs in Scratch you need to use commands from a range of code areas.

Take a Green Flag hat block from the Control section and add commands  to set the sprite to the centre of the screen (x=0,y=0) and scale it to 50% (from the Looks area). This sets up the elements of a Scratch program such as sprite size and position - technically known as 'initialisation'.

(Note that a 'hat' block is one that you can't put anything above: it is a starter only. The green flag hat runs its code when the green flag above the graphics/game area is clicked.)

Now add key press controls for left, right, up and down so that the sprite turns and moves when the arrow keys are pressed.

You will need a forever if block under the green flag hat with a condition to detect a particular key press. This will watch out for the condition specified until the program ends. The 'key pressed' condition can be found in the 'Sensing' section.

Inside the Forever if block you add a 'Change x by' or 'Change y by' command from the  Motion section (x is across and y is up-down on the screen).

If 'Change x by 5' moves pixels right how do you move 5 pixels left? If 'Change y by 5' moves pixels up how do you move 5 pixels down?

What controls the speed of movement? What is the equivalent on the screen to one step of movement?

Add a command to bounce at the edge using a new green flag hat, a forever block and the 'If on edge bounce' command.

Note that a forever block inside a green flag hat block will do whatever is inside it for the duration of the program. If you leave out 'forever' or 'forever if' your code will only run once and you will get no results.

Once you have a block of code to move in one direction you can duplicate it and amend it for the other directions. Right click the block and choose Duplicate then amend it.

Program blocks run in parallel fashion so you can write as many green hat blocks and have them run simultaneously rather than in sequence. This means you can put a <if on edge bounce> command inside a separate green flag hat and it will perform this action continuously - as long as you set it to run more than once!

Add a Second Sprite

Add a second sprite by choosing it from the folders: click on the middle Create sprite button and choose a new sprite.

Make this sprite start on the left side of the screen (x=-240) at a random position and move towards the right side. When it reaches the right side it should disappear and start again on the left.

Use Pick random from the green Operators area to get a value in the range -150 to +150 so the sprite starts at a different point each time.

Note that the Scratch graphics/game area is 480 pixels wide by 360 tall. The centre coordinate is [0,0] and the visible limits are: left x: -240; right x: 240; top y: 180; bottom y: -180. You can use points outside this area (useful in scrolling games).

Use Hide and Show from the Looks section to control the visibility of the sprite.

Use an if block to detect whether the sprite has reached the right hand side. Add a < or > condition to detect the position of the sprite. Note that Scratch tracks the x and y positions of every sprite and these are available in 'x position' and 'y position' under Motion.

Solution (quick peek if you need it).

Add a Third Sprite

Now add a third sprite and make this one move in the opposite direction, from right to left (or up-down or down-up if you prefer). Hint: you will have to invert everything in your code for the first sprite.

The Rotate controls: there are three tiny icons in a column under the menu. These set the amount of rotation to none, left-right only and full rotation. Experiment with these to get the effect you want.

Save your finished work so you can use it when developing a better version later. Note that when you save your work you must click on 'Computer' and choose your N drive. Simply saving it will not preserve it for the following week.

Ready to Make 'Pong' now? Instructions at the bottom of this page.

Algorithms

You have implemented your first algorithm. An algorithm is a formula, rather like a recipe, that describes how to solve a problem. Algorithms are found in maths and especially in computer science.

An algorithm to display the even numbers up to 100 would be:

  1. start at 0;
  2. repeat
  3.  add 2
  4.  until 100
  5.  end

Some algorithms are already implemented in Scratch, for example if on edge bounce. You have implemented the same algorithm four times to control the sprite with key presses:

  1. repeat
  2.  if the specified key is pressed
  3.  do begin
  4.   point in the direction that matches the key press
  5.   move 1 step
  6.  end
  7. until the program stops

Task 2: Constant Motion

Add a sprite to a project and make it move across the screen. When it reaches the edge it should reappear on the other side. In this way you could create targets for another sprite to hit and score points.

Steps:

Possible solution.

Repeat this code for any number of sprites.

Extension 1

Try reversing the direction of the sprites.

Extension 2

Make a simple game where the main sprite has to dodge the ones moving across the stage. Use the touching ? condition in Sensing to detect a collision between sprites (as usual the program will have check for this condition continually, not just once.)

Develop this by using backgrounds and sprites from a scenario of your choice e.g. The Simpsons. You know what Homer will say if he bumps into something!

Extension 3

Try using the pick random from m to n command from Operators.

Possible solution.

Further development: have a third sprite moving from right to left; add a variable to keep a total score; add a point if one of the sprites collides with the main sprite; deduct a point if the other sprite collides; change the background and sprites into a scenario of your own choice.

Programming Concepts

This simple program introduces all three features of what is called structured programming:

sequence - one instruction after another, as in hide; goto; show

repetition - repeating something over and over, in this case forever with no finishing condition apart from the program stops

control - the if statement checks to see if the sprite has disappeared on the right of the screen

You will meet these three things time and time again in programming: at the most basic level it's all there is!

Flow Chart

Algorithms are often represented as a flow chart. The basic symbols include:

Note that as this program loops forever the code never reaches the end box, which is not really needed.

Documentation

Start a Word document. Add a heading 'Scratch Programs' then save it.

Write a summary of your work under these headings:

Further Games

Link

Pong

Make a one-player game of Pong

Follow the method of problem solving studied earlier.

  1. Understand: Give a full description of the game of Pong. (Ask what, why, how... look for causes, brainstorm, break the problem into parts, draw a diagram, collect information.)
  2. Make a plan: List the items that make up a game of Pong; list the problems that you have to solve to make your code work; work backwards.
  3. Implement the plan: if it doesn't work then review your plan.
  4. Reflect and look back so you have information for future reference

Specification, definition and description of the problem are really important.

Basic techniques required:

Extension: a two player game: classic Pong.

Target Game

In this game you move the mouse over a moving target and click to 'kill' it - if you are quick enough! When a kill is made the score is increased. The game should run for a fixed period.

Extension: add a top scores table.

Flying Objects Game

In this game you control an object that moves up and down in the centre of the screen while objects fly towards it from left and right. Some of the objects could reduce your 'lives' while others could increase your score.

Extension: add objects that fly in from different angles.