Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: .  (Read 1579 times)

0 Members and 1 Guest are viewing this topic.


  • Newbie
  • *
  • Posts: 15
    • View Profile
« on: August 12, 2014, 11:28:53 am »
« Last Edit: May 03, 2018, 05:17:47 am by Helios101 »


  • SFML Team
  • Hero Member
  • *****
  • Posts: 10191
    • View Profile
    • development blog
    • Email
Re: Giving the snake the bending effect
« Reply #1 on: August 12, 2014, 11:54:34 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Nightly Builds: https://www.nightlybuilds.ch/
Dev Blog: https://dev.my-gate.net/
Thor: http://www.bromeon.ch/libraries/thor/


  • SFML Team
  • Hero Member
  • *****
  • Posts: 6275
  • Thor Developer
    • View Profile
    • Bromeon
Re: Giving the snake the bending effect
« Reply #2 on: August 13, 2014, 08:34:40 am »
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 (for std::deque).
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: