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.

whenclickedstartsthescriptforevermakesitsothemovementwillkeepgoingpointtowardsmouse-pointeraimsforthemousemove10stepsmoves

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.

whenclickedStartsthescript.foreverletsthescriptrepeatifkeyuparrowpressed?thenifkeyispressedrepeatuntilnotkeyuparrowpressed?repeatsuntilkeyisnotpressedchangeyby10movesinwanteddirectionendendifkeydownarrowpressed?thenrepeatuntilnotkeydownarrowpressed?changeyby-10endendifkeyrightarrowpressed?thenrepeatuntilnotkeyrightarrowpressed?changexby10endendifkeyleftarrowpressed?thenrepeatuntilnotkeyleftarrowpressed?changexby-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.

whenclickedstartsthescriptforeverletsthescriptrepeatifkeyuparrowpressed?thenifkeyispressedchangeyby10movesinwanteddirectionendifkeydownarrowpressed?thenchangeyby-10endifkeyrightarrowpressed?thenchangexby10endifkeyleftarrowpressed?thenchangexby-10endend

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.

whenclickedstartsthescriptforeverloopsthemovementscriptifkeyrightarrowpressedthendetectsaninputchangexvelocityby1changesthevelocityvariableendifkeyleftarrowpressedthenchangexvelocityby-1endsetxvelocitytoxvelocity*0.9slowlydecreasesvelocitychangexbyxvelocitymakesthespritemovebasedonthevelocityvariable

See Also