Bumping Into Things
Catch, hit, pick up, crash — every one of those is two things touching. AMOS can tell you the moment one bob overlaps another with a single test. Drop it into your frame loop and your game gains consequences: the point where moving pictures turn into something you can win or lose.
Here's the last piece. You can put things on screen, move them, and steer them — but nothing happens when they meet. The ship flies through the asteroid; the net misses the ball. A game needs consequence, and consequence starts with one question: are these two things touching? Catch a coin, take a hit, land on a platform, score a goal — every one is two objects overlapping. AMOS answers that question with a single test, and once you have the answer, the game is yours to react to.
What you'll see by the end
Two bobs, side by side and overlapping, with a message that says so. The program didn't just assume they touch because of where you put them — it asked, with a collision test, and printed the result of the answer. Move them apart and the other message would show instead.
The whole program
Hide
Screen Open 0,320,200,16,Lowres
Colour 0,$111
Colour 1,$F40
Colour 2,$2DF
Colour 3,$FFF
Cls 0
Ink 1
Bar 0,0 To 23,23
Get Bob 1,0,0 To 24,24
Cls 0
Ink 2
Bar 0,0 To 23,23
Get Bob 2,0,0 To 24,24
Cls 0
Bob 1,140,80,1
Bob 2,152,80,2
Wait Vbl
Pen 3
Paper 0
If Bob Col(1,2 To 2)
Locate 8,16
Print "The bobs are touching!"
Else
Locate 8,16
Print "The bobs are apart."
End If
Wait Key
Most of this is setup you know: make two bob images (an orange one and a cyan one), and place them on screen close enough to overlap. The new idea is the test.
Asking "are they touching?"
If Bob Col(1,2 To 2)
Locate 8,16
Print "The bobs are touching!"
Else
Locate 8,16
Print "The bobs are apart."
End If
Bob Col is the collision test, and like the joystick functions it gives a yes-or-no answer you drop straight into an If. Read Bob Col(1,2 To 2) as: does bob 1 collide with any bob in the range 2 to 2 — that is, with bob 2? Because the two bobs overlap, the answer is yes, so the If runs its first branch and prints the touching message. Pull them apart and the answer turns to no, and the Else branch runs instead.
The test isn't fooled by the bobs just being near each other — it checks whether their actual shapes overlap, pixel against pixel. Two bobs can sit close without Bob Col reporting a hit, right up until their pictures genuinely touch.
Why Wait Vbl comes first
Bob 1,140,80,1
Bob 2,152,80,2
Wait Vbl
The collision test reads where the bobs sit on screen, and bobs settle into place on the next screen refresh. The Wait Vbl lets that happen before you ask — test too early, before the bobs have landed, and you can get the wrong answer. Place, wait a frame, then test.
Where this goes in a game
In a real game the test lives inside the frame loop, asked every frame, right after everything moves:
' read input, move bobs ...
If Bob Col(1,2 To 2) Then Dec LIVES : Bell
Read the player, move everything, then ask did anything hit? — and react: lose a life, add a point, play a sound, end the game. That single question, asked every frame, is what turns your moving bobs into something with stakes. Every score you've ever racked up and every life you've ever lost came down to a collision test like this one, firing fifty times a second.
Type it and run it
Type the program in and press F1. The two overlapping bobs appear with the touching message. Press a key to return to the editor.
Try this: pull them apart
Change Bob 2,152,80,2 to Bob 2,250,80,2 and run it. The bobs are now well separated, the collision test comes back false, and the Else branch prints The bobs are apart. — proof the message follows the test, not your guess.
Try this: just touching
Set bob 2 back close, then nudge its position a pixel at a time — Bob 2,164,80,2 (just clear) versus Bob 2,160,80,2 (just touching). Somewhere in between, the message flips. You're feeling out the exact edge where two shapes meet — the same edge a player feels when a jump just clears a gap.
If it doesn't work
- It always says "apart", even when they overlap. Check the
Wait Vblis there before the test, so the bobs have settled. Check the range too —Bob Col(1,2 To 2)tests bob 1 against bob 2. - AMOS complains about
Bob Col. It's two words, and it needs the bob number plus thestart To endrange in the brackets. - Both bobs show but no message. The message uses
PenandPaperfor a colour that shows on the background — make sure the pen colour isn't the same as the screen behind it.
What you've learnt
Collision is where movement becomes a game. Bob Col(n,first To last) asks whether bob n overlaps any bob in a range — a yes-or-no answer, ready for an If — and it checks real shapes, not just nearness. Test after a Wait Vbl so the bobs have settled. Dropped into the frame loop and asked every frame, that one test drives every catch, hit and pickup there is. That's the movement phase complete: you can place, move, steer, animate, and now detect — everything a game's action is built from.
What's next
You've got the makings of a game. The last short phase ties off the practical bits: AMOS Makes a Sound — giving your hits and pickups a noise — then saving your work, and a look back at just how much AMOS has been doing for you.