Getting started with

Teachnet logo
A Teachnet Project by Trevor Blunn working at the Cedars Upper School Leighton Buzzard Bedfordshire

Using the keyboard to control your sprite
(using if condition instruction)

It is possible to control your sprite using the mouse keys. There is a small pointer next to the Sprite symbol at the top of the screen which can be set to any angle thereby determining the natural direction of movement of the sprite. In this exercise we want the Sprite to move exactly up and down vertically or side to side horizontally so we will use the change x by and change y by instructions to order to ensure the correct movement of the Sprite

Download this project

Now copy the main part of the script then edit the sub script for each movement of the mouse determined by each of the respective four arrow keys

Catwalkimage

Drop the three new copied sets of instructions inside the forever instruction (not inside the existing If condition instruction)

Catwalkimage

Click the green flag button.

It should now be possible to drive your cat sprite around the screen using the arrow keys

Cat walking with keyboard control
It should be possible to make a script combining what you have already learned that allows the cat sprite to walk around the screen driven by the arrow keys. Note that the script has been shown in two parts for the sake of displaying here. It is possible to set all of this up in one script

Is this the most efficient code?
In the national curriculum students are expected to identify the most efficient code possible for their work. The script shown above has repeated sections. It is more efficient in fact to create two sets of code, one for the walking motion and one for the keyboard control. We also discovered that we could use the move 10 steps feature rather than change x or y by 10 after the point in direction command. This gives the facility to create a pacman game where you can bounce off objects. If touching whatever (say, a certain colour) then move -10 steps.