This is what I used when I was messing around with a pong thing:
PHP Code:
this.velocity={random(-1,1),random(-1,1)};
x+=this.velocity[0];
y+=this.velocity[1];
if (onwall(x+this.velocity[0],y+.5)) this.velocity[0]=this.velocity[0]*-1; //Assuming the center is half a tile in
if (onwall(x+.5,y+this.velocity[1])) this.velocity[1]=this.velocity[1]*-1;
Of course, this script itself isn't going to work, but you can see where I'm going with it.