Movement
Описание | Как управлять движением агентов на экране |
---|---|
Область знаний | Информатика, Game design, Геометрия |
Область использования (ISTE) | |
Возрастная категория | 10
|
Поясняющее видео | |
Близкие рецепту понятия | |
Среды и средства для приготовления рецепта: | Scratch, Snap!, NetLogo |
Movement is the action of changing an object's position. It is used to show action. Scratch provides various blocks that can be used to create movement, including the Change X by (), Change Y by (), and Move () Steps blocks.
Following the Mouse
This script can be used to make a sprite follow the mouse-pointer.
when gf clicked // starts the script forever // makes it so the movement will keep going point towards (mouse-pointer v) // aims for the mouse move (10) steps // moves
Arrow Keys Movement
This script gives more control over the movement, and it uses the arrow keys rather than the mouse. However, this script only allows for movement horizontally and vertically.
when green flag clicked // Starts the script. forever // lets the script repeat if <key (up arrow v) pressed?> then // if key is pressed repeat until <not <key (up arrow v) pressed?>> // repeats until key is not pressed change y by (10) // moves in wanted direction end end if <key (down arrow v) pressed?> then repeat until <not <key (down arrow v) pressed?>> change y by (-10) end end if <key (right arrow v) pressed?> then repeat until <not <key (right arrow v) pressed?>> change x by (10) end end if <key (left arrow v) pressed?> then repeat until <not <key (left arrow v) pressed?>> change x by (-10)
This script is similar to the previous, but it allows diagonal movement as well. It works since the forever loop detects all keys and is not looping each individual key press.
when green flag clicked // starts the script forever // lets the script repeat if <key (up arrow v) pressed?> then // if key is pressed change y by (10) // moves in wanted direction 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 end
Velocity Movement
This script creates a smoother form of movement. It works by gradually increasing or decreasing the amount that the sprite moves each loop through the use of a variable.
when gf clicked // starts the script forever // loops the movement script if <key (right arrow v) pressed> then // detects an input change [x velocity v] by (1) // changes the velocity variable end if <key (left arrow v) pressed> then change [x velocity v] by (-1) end set [x velocity v] to ((x velocity) * (0.9)) // slowly decreases velocity change x by (x velocity) // makes the sprite move based on the velocity variable