There are 2 things you must understand:
1. The purpose of OLDX# and OLDZ# are to store where the camera was before it moved. Then you use X# and Z# to say where it is after it moved.
2. If an object is locked onto the screen using the LOCK OBJECT ON command, it cannot have collision.
3.Because of #2, no collision can occur. You need an object to be where the camera is at all times, that way it can test for collision. All you do is make this object (I made it a sphere) follow the camera around, and when it collides, detect for collision and put it back where it was before it moved.
Anyway, after understanding this, here is your code, edited. You can change the number of the camera object by changing the value of CamObj to something else when you see it say "CamObj=6". The object number is currently 6.
SYNC ON : hide mouse
sync rate 30
create bitmap 1,16,16
box 5,5,10,10
get image 1, 0, 0, 16, 16
delete bitmap 1
make matrix 1, 200.0, 200.0, 20, 20
position matrix 1, 0, 0, 0
prepare matrix texture 1, 1, 16, 16
make matrix 2, 200.0, 200.0, 20, 20
position matrix 2, 0, 30, 0
rem the boxes are walls
make object box 3, 200, 30, 5
color object 3, rgb(0, 150, 0)
position object 3, 100, 15, 0
set object collision on 3
make object box 4, 200, 30, 5
color object 4, rgb(50, 100, 67)
position object 4, 100, 15, 200
set object collision on 4
make object box 5, 200, 30, 5
color object 5, rgb(90, 100, 67)
position object 5, 200, 15, 100
yrotate object 5, 90
set object collision on 5
rem Make Gun
Make object cylinder 1,2
XRotate Object 1,90
Fix object pivot 1
Scale object 1,100,100,500
position object 1,5,-7,15
Lock object on 1
set object collision on 1
Rem Make bullet
Make Object Sphere 2,2
Hide Object 2
rem Make Camera Object
CamObj=6
make object sphere CamObj,5
hide object CamObj
DO
oldx#=camera position x()
oldz#=camera position z()
IF LEFTKEY()=1 THEN angle#=angle#-3.0
IF RIGHTKEY()=1 THEN angle#=angle#+3.0
angle#=wrapvalue(angle#)
YROTATE CAMERA angle#
IF UPKEY()=1 THEN MOVE CAMERA 10
IF DOWNKEY()=1 THEN MOVE CAMERA -10
x#=camera position x()
z#=camera position z()
y#=get ground height(1,x#,z#)+10
position camera x#,y#,z#
rem Put the camera object on the camera
position object CamOBj,x#,y#,z#
`If the camera object hits object 3, then go back to where it was
`before it moved.
if object collision(CamObj,3)
position camera Oldx#,y#,Oldz#
endif
SYNC
LOOP