K's code is really the best way to do it, so I suppose I'll have to try and explain how INTERSECT OBJECT works
The basics are that you specify a start position and an end position, and it will slowly trace along the line between these points until it either gets to the end, or hits the object that you have specified.
If it gets to the end point without detecting a collision it will return a value of 0.
If it instead hits the object you're looking for, it will return the distance between your start point and where it collided with the object.
The way that K's code works is to use several INTERSECT OBJECT commands in a vertical direction for the floor, and horizonally for wall detection. When the distance to a wall becomes small enough, his code will no longer let you move in that direction.