Here's a quick solution visualized using Rubygame:

 http://www.pastie.org/1018079

I'm sorry about the lack of comments (the code should
be mostly self-explanatory, if not just ask.)

-- 
Lars Haugseth

[1] http://rubygame.org/