your tecnique with "bouncing back" with the X/Y pos, andyUK is good though it'll look quite bad when you'll go in and out of the wall repetetively.
it wont do that if you get the events in the right order (i think) i use that for my game with a 3 pixel movement and it doesnt bounce back and forth. 40 is different though.