SFML community forums
Help => General => Topic started by: Helios101 on August 12, 2014, 11:28:53 am
-
.
-
The genius thing about Snake is, that you don't have to move all the blocks every time. It really doesn't matter whether block 4 remains block 4 or if it will be block 5 later on.
All you essentially need to care about when moving the snake is, removing the tail (last block), moving the head and adding a new element behind the head. All the in-between parts never need to move. ;)
To change direction, you simply move the head in the given direction and add a new block where the head was before, it's very simple. But it's best to use a std::deque so you can work on both ends.
-
Exactly. Every segment except for the first and the last remain the same when you update the snake. Essentially, the update step then becomes:
- Pop last segment (tail) from deque
- Push new segment (head) to front
And you should iterate with iterators and not indices, because it's more generic and faster (http://en.sfml-dev.org/forums/index.php?topic=10084) (for std::deque).