How to Move Sprites with the Arrow Keys

Материал из Поле цифровой дидактики
Описание Как управлять спрайтами при помощи клавиш клавиатуры - в блочных языках реализуется через связку нажатия определённой клавиши и поворотом в
Область знаний Информатика
Область использования (ISTE) Дизайнер Сообществ
Возрастная категория 7


Поясняющее видео
Близкие рецепту понятия
Среды и средства для приготовления рецепта: Scratch, Snap!

This tutorial explains how one can make a game or interactive animation that moves sprites with the arrow keys.

X-Y Method

This is the easiest way to do this. First, go to the sprite you want to make move. Then, add these scripts:

when [up arrow v] key pressed
change y by (10)
when [down arrow v] key pressed
change y by (-10)
when [right arrow v] key pressed
change x by (10)
when [left arrow v] key pressed
change x by (-10)

The sprite will move when the arrow keys are pressed however it will point the same direction.

Steps Method

Using these scripts, the sprite will turn around while it moves. This is not recommended in a sprite that must turn for other reasons.

when [up arrow v] key pressed
point in direction (0)
move (10) steps
when [down arrow v] key pressed
point in direction (180)
move (10) steps
when [right arrow v] key pressed
point in direction (90)
move (10) steps
when [left arrow v] key pressed
point in direction (-90)
move (10) steps

Now your sprite will turn when it moves.

Loop Method

Both methods above can also be written as such below (note in this example the X-Y method is used but the steps method will work with this code too if changing direction is desired):

when gf clicked
forever
if <key (up arrow v) pressed?> then
change y by (10)
end
if <key (down arrow v) pressed?> then
change y by (-10)
end
if <key (right arrow v) pressed?> then
change x by (10)
end
if <key (left arrow v) pressed?> then
change x by (-10)
end