Create a Ball that Moves in 4 Directions

This lesson shows how to make a ball object move Up, Down, Left, and Right using the arrow keys in GameMaker.

Be sure to add all these groups
Animation Curves
Extensions
Fonts
Notes
Objects
Particle Systems
Paths
Rooms
Scripts
Sequences
Sounds
Sprites
Tile Sets
Timelines

STEP 1 — Create a Sprite

  1. Right-click SpritesCreate Sprite
  2. Name it: spr_ball
  3. Edit Image → draw a simple circle
  4. Close editor to save

STEP 2 — Create a Ball Object

  1. Right-click ObjectsCreate Object
  2. Name it: obj_ball
  3. Assign the sprite: spr_ball

STEP 3 — Add a Create Event

This sets how fast the ball moves.

speed = 4;

STEP 4 — Add a Step Event

This code checks arrow keys and moves in that direction.

// default: no movement
direction = -1;

// up
if (keyboard_check(vk_up)) {
    direction = 90;
}

// down
if (keyboard_check(vk_down)) {
    direction = 270;
}

// left
if (keyboard_check(vk_left)) {
    direction = 180;
}

// right
if (keyboard_check(vk_right)) {
    direction = 0;
}

STEP 5 — Place in Room & Test

  1. Open Room1
  2. Drag obj_ball into the room
  3. Click Run ▶
Add this Option: Want the ball to stop when no key is pressed? Replace the Step code with this version:
if (keyboard_check(vk_up))        { direction = 90;  speed = 4; }
else if (keyboard_check(vk_down)) { direction = 270; speed = 4; }
else if (keyboard_check(vk_left)) { direction = 180; speed = 4; }
else if (keyboard_check(vk_right)){ direction = 0;   speed = 4; }
else { speed = 0; }

Submission

Submit 3 files:

Save the files into your google drives under your class period and upload to Google Classroom.