Skip to content
Game 0 Unit 11 of 17 1 hr learning time

The Frame Loop

A game is a loop that runs fifty times a second: move things a little, wait for the screen to refresh, repeat. Put your bob inside one and nudge its position each time round, and it stops sitting still and starts to glide. This loop is the heartbeat under every game ever made.

65% of Meet AMOS

Here's the single biggest idea in the whole course. A game isn't a program that runs once and stops — it's a loop that runs over and over, many times a second, and each time round it moves everything on by a tiny step. Round the loop: shift the bob a few pixels, wait for the screen to refresh, round again. Do that fifty times a second and the eye sees not a series of jumps but smooth, gliding motion. Every game you've ever played is this loop, beating away underneath. Now you build your first one.

What you'll see by the end

A near-black screen with the orange-and-yellow bob over on the right-hand side.
A single still can't show motion — but on your screen this bob glides smoothly from the left edge to here on the right. The picture catches it where it arrived.

A still picture can only show one moment, and this is the bob's last one — arrived at the right after sliding across from the left. Run the program and you'll see what the page can't: the block travelling smoothly across, frame by frame, driven by the loop.

The whole program

Hide
Screen Open 0,320,200,16,Lowres
Colour 0,$111
Colour 1,$F40
Colour 2,$FD0
Cls 0
Ink 1
Bar 0,0 To 23,23
Ink 2
Bar 6,6 To 17,17
Get Bob 1,0,0 To 24,24
Cls 0
X=30
Do
   Bob 1,X,100,1
   Wait Vbl
   Add X,3
   If X>270 Then Exit
Loop
Wait Key

The top half is unit 10 exactly — draw a picture, grab it as bob image 1. The new idea is the loop at the bottom.

Set a starting position

X=30

X is a variable holding the bob's horizontal position. It starts at 30 — near the left edge — and the loop's whole job is to keep changing it.

The loop itself

Do
   Bob 1,X,100,1
   Wait Vbl
   Add X,3
   If X>270 Then Exit
Loop

This is a Do … Loop — it repeats the lines between Do and Loop forever, until something tells it to stop. Each time round, in order:

  • Bob 1,X,100,1 draws the bob at the current X, 100 down. First time round that's X=30; next time it'll be 33, then 36…
  • Wait Vbl waits for the screen's next refresh. This is what paces the loop to the Amiga's fifty-frames-a-second and makes the motion smooth instead of a blur — one step per frame, in time with the display.
  • Add X,3 adds 3 to X. (Add X,3 is AMOS's short way of writing X=X+3.) Next time round, the bob draws three pixels further right.
  • If X>270 Then Exit checks whether the bob has reached the right side, and if so Exit breaks out of the loop.

So each pass moves the bob three pixels and waits one frame. Fifty frames a second, three pixels a frame — a smooth glide across the screen.

Move, wait, repeat

That three-line rhythm — change something, wait for the frame, go again — is the shape of every game loop there is. In a real game the "change something" grows to move the player, move the enemies, check for hits, update the score — but the skeleton is exactly this. Learn to see it here, in its simplest form, and you'll recognise it under everything you build later.

Why Wait Vbl matters

Take Wait Vbl out and the loop runs as fast as the Amiga can go — the bob crosses the screen in a blink, and the motion tears and flickers because you're moving it while the screen is mid-draw. Wait Vbl holds the loop to one step per screen refresh, so each frame shows the bob cleanly in its new spot. It's the metronome the whole loop dances to. Fifty beats a second is the Amiga's heartbeat, and your game keeps time with it.

Type it and run it

Type the program in and press F1. The bob glides from left to right and stops. Press a key to return to the editor.

Try this: change the speed

Change Add X,3 to Add X,1 and run it — the bob crawls, one pixel a frame. Change it to Add X,8 and it races. The step size is the speed: bigger step, faster motion, because it covers more ground each frame.

Try this: move down instead

Give the bob a Y that changes instead of X. Set Y=20 before the loop, draw with Bob 1,150,Y,1, use Add Y,3, and test If Y>170. Now it falls down the screen instead of crossing it. The axis you change is the direction it moves.

If it doesn't work

  • The bob crosses instantly, or flickers. The Wait Vbl is missing from inside the loop. Without it the loop runs flat out — put it back, between the Bob and the Add.
  • The bob never moves. Check Add X,3 is inside the loop and that Bob uses X (not a fixed number). If the position never changes, the bob redraws in the same spot forever.
  • The loop never ends. The If X>270 Then Exit is what stops it. Make sure the value it tests for is one X will reach, given your Add step and start.

What you've learnt

The frame loop is the heartbeat of every game. A Do … Loop repeats its body until Exit breaks out, and inside it the rhythm is draw, Wait Vbl, change the position — one small step per screen refresh. Wait Vbl paces the loop to the Amiga's fifty-frames-a-second so motion is smooth, and the size of the step you Add is the speed. Everything that moves in a game moves because it sits inside a loop shaped like this one.

What's next

Your bob moves on its own, the same way every time. Next — Reading the Player — you'll read the joystick inside the loop and let the player decide which way it goes. That's the moment a moving picture becomes a game.