Sunday, September 29, 2013

A snake is born

Today I went into the algorithm part of the snake game. Exactly how do we manage the snake segments as they grow in number, and change direction?

A buddy of mine at work mentioned that a linked list made most sense. I didn't probe him on it, but that did intuitively make sense because the snake does in fact look like a bunch of linked nodes. This appears to work pretty well:
  1. Each node carries its own position in the gameboard.
  2. When advancing the snake, work backwards from the last node and make its position the next node. This is what causes the snake to appear to follow its head.
  3. Update the first node to the new position.
Do that, then voila! you get this:


Super cool!

No comments:

Post a Comment