I coded up snake for practice. </a>
2018 Update – I’ve since taken the game down since it’s not really worth maintaining. Keeping the post up just for the archive.
Speed. I was worried that constantly redrawing the snake between every frame would slow down the browser and possibly cause bugs if the redraws took more time than the scheduled step size (2oo ms) between every movement. However, representing the snake as an array allowed me to take a shortcut. Instead of redrawing the whole snake every time, I could just move the last piece of the snake to the the new position. This worked out especially well for food, as I could just convert a food block to a snake class and it would join the rest.
I haven’t tested this for the larger lengths of the snake but performance at the levels I attained weren’t impacted.
Every programming project is a wonderful exercise in problem solving– I’m glad I was able to relax and work out the kinks of this!