0% found this document useful (0 votes)
220 views4 pages

1-Using The Keyboard To Control Your Sprite

The document describes how to control a sprite in Scratch using keyboard arrow keys. It explains setting the sprite's direction pointer and using "change x by" and "change y by" blocks to move the sprite vertically and horizontally. Multiple copies of the movement script are placed in a forever loop and linked to the different arrow keys to control the sprite's movement. Combining these techniques allows building a script where a cat sprite can walk around the screen when driven by the arrow keys. More efficient code is possible by separating the walking motion from the keyboard control and using the "move 10 steps" block instead of changing x or y.

Uploaded by

decoleman
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
220 views4 pages

1-Using The Keyboard To Control Your Sprite

The document describes how to control a sprite in Scratch using keyboard arrow keys. It explains setting the sprite's direction pointer and using "change x by" and "change y by" blocks to move the sprite vertically and horizontally. Multiple copies of the movement script are placed in a forever loop and linked to the different arrow keys to control the sprite's movement. Combining these techniques allows building a script where a cat sprite can walk around the screen when driven by the arrow keys. More efficient code is possible by separating the walking motion from the keyboard control and using the "move 10 steps" block instead of changing x or y.

Uploaded by

decoleman
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

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
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

Drop the three new copied sets of instructions inside the forever instruction (not inside the existing If
condition instruction)
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.

You might also like