tutorial source code from http://www.retrogamecode.blogspot.com