Ora

How Do You Move a Player on Scratch?

Published in Scratch Movement Controls 6 mins read

Moving a player, or sprite, on Scratch is fundamentally achieved by using code blocks that change its position on the stage. The most common and versatile method involves using keyboard arrow keys to control movement, allowing players to navigate their character in games and interactive projects.

Keyboard Controls: The Most Common Method

To enable a sprite to move using the keyboard, you'll set up a script that continuously checks which arrow key is being pressed and then adjusts the sprite's horizontal (X) or vertical (Y) position accordingly. This method offers intuitive control for many game types.

Step-by-Step Implementation for Arrow Key Movement

Here's how to create a basic script for keyboard-controlled movement, directly utilizing the core logic for responsive player control:

  1. Start the Script: Begin with the when green flag clicked event block from the Events category. This block ensures your movement script starts whenever the project runs.
  2. Create a Continuous Loop: Drag a forever loop block from the Control category and attach it to the when green flag clicked block. This loop ensures that Scratch constantly checks for key presses.
  3. Check for Up Arrow Press:
    • Inside the forever loop, add an if () then block from the Control category.
    • Inside the if condition, drag a key () pressed? block from the Sensing category.
    • Click the dropdown in key () pressed? and select up arrow.
    • Inside this if block, add a change y by (10) block from the Motion category. Changing 'y' by a positive number moves the sprite upwards.
  4. Check for Down Arrow Press:
    • Add another if () then block below the previous one (still inside the forever loop).
    • Set its condition to key (down arrow) pressed?.
    • Inside this if block, add a change y by (-10) block. Changing 'y' by a negative number moves the sprite downwards.
  5. Check for Right Arrow Press:
    • Add another if () then block.
    • Set its condition to key (right arrow) pressed?.
    • Inside this if block, add a change x by (10) block. Changing 'x' by a positive number moves the sprite to the right.
  6. Check for Left Arrow Press:
    • Add a final if () then block.
    • Set its condition to key (left arrow) pressed?.
    • Inside this if block, add a change x by (-10) block. Changing 'x' by a negative number moves the sprite to the left.

This complete script allows for 4-directional movement: When green flag clicked forever if up arrow pressed change y by 10, if right arrow pressed change x by 10, if left arrow pressed change x by -10, and if down arrow pressed change y by -10.

Example Script Visualization:

when green flag 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
end

For more details on Scratch's interface and basic concepts, you can explore the official Scratch website: scratch.mit.edu.

Other Ways to Move a Player

While arrow keys are popular, Scratch offers several other methods to control sprite movement, catering to different game mechanics and project types:

  • Mouse Control:
    • go to mouse-pointer: Makes the sprite instantly jump to the mouse cursor's position.
    • point towards mouse-pointer: Rotates the sprite to face the mouse cursor, often used with move () steps to follow the mouse.
  • Direct Coordinate Movement:
    • go to x: (0) y: (0): Teleports the sprite to a specific X and Y coordinate on the stage. The center of the stage is (0,0).
    • set x to (0), set y to (0): Sets either the X or Y coordinate directly.
  • Smooth Gliding:
    • glide (1) secs to x: (0) y: (0): Moves the sprite smoothly from its current position to a target X and Y coordinate over a specified duration.
    • glide (1) secs to random position: Moves the sprite smoothly to a random spot on the stage.
  • Relative Movement & Turning:
    • move (10) steps: Moves the sprite forward in the direction it is currently facing.
    • turn right (15) degrees, turn left (15) degrees: Rotates the sprite clockwise or counter-clockwise.
  • Collision-Based Movement:
    • if on edge, bounce: If the sprite touches the edge of the stage, it will automatically turn around.

Understanding X and Y Coordinates

Scratch's stage uses a Cartesian coordinate system to define positions.

  • The X-axis controls horizontal position (left to right).
    • Negative X values are to the left of the center.
    • Positive X values are to the right of the center.
  • The Y-axis controls vertical position (up and down).
    • Negative Y values are below the center.
    • Positive Y values are above the center.

The center of the stage is x: 0, y: 0. The stage typically ranges from x: -240 to x: 240 and y: -180 to y: 180.

Block Effect
change x by 10 Moves the sprite 10 steps to the right.
change x by -10 Moves the sprite 10 steps to the left.
change y by 10 Moves the sprite 10 steps upwards.
change y by -10 Moves the sprite 10 steps downwards.

Essential Movement Blocks in Scratch

The Motion category in Scratch contains all the primary blocks for controlling a sprite's position and orientation.

Block Name Description
move (10) steps Moves the sprite forward by the specified number of steps in its current direction.
turn right (15) degrees Rotates the sprite clockwise by the specified angle.
turn left (15) degrees Rotates the sprite counter-clockwise by the specified angle.
go to random position Teleports the sprite to a random spot on the stage.
go to mouse-pointer Teleports the sprite to the current location of the mouse cursor.
go to x: (0) y: (0) Teleports the sprite to the exact X and Y coordinates specified.
glide (1) secs to random position Moves the sprite smoothly to a random spot over a specified time.
glide (1) secs to x: (0) y: (0) Moves the sprite smoothly to specified X and Y coordinates over a specified time.
point in direction (90) Sets the sprite's direction (0=up, 90=right, 180=down, -90=left).
point towards mouse-pointer Makes the sprite face the mouse cursor.
change x by (10) Adjusts the sprite's horizontal position by adding the value to its current X coordinate.
set x to (0) Sets the sprite's horizontal position to a specific X coordinate.
change y by (10) Adjusts the sprite's vertical position by adding the value to its current Y coordinate.
set y to (0) Sets the sprite's vertical position to a specific Y coordinate.
if on edge, bounce Reverses the sprite's direction if it hits any edge of the stage.

Best Practices for Player Movement

  • Smoothness: For smoother movement, especially in platformers or top-down games, use small change x or change y values (e.g., 5 or 10 steps).
  • Variables for Speed: Instead of hardcoding numbers like change x by 10, create a variable (e.g., speed) and use change x by (speed). This makes it easier to adjust movement speed for different levels or power-ups.
  • Directional Graphics: Ensure your player sprite has costumes that face different directions (up, down, left, right) for a more visually appealing experience. You can switch costumes based on the key pressed.
  • Preventing Rotation: If you don't want your sprite to rotate when moving left and right, set its rotation style to "left-right" using the set rotation style [left-right v] block.