Thanks for the reply.
ah yes, i see it up there, and glad to see the solution was the same as mine:) (I'll be honest, i did only read the first 4 pages of the thread
About the snippets of code. well I do hope you get the chance to edit it in, or at least mention that setters/getters are left out, so that people will know that before hand... adding them will make the book more "beginner friendly" but then again, your target audience doe not seem to be the absolute beginner
one thing i was wondering.
in chapter 3, you have this piece of code:
void Snake::Extend() {
if (m_snakeBody.empty()) { return; }
SnakeSegment& tail_head = m_snakeBody[m_snakeBody.size() - 1];
if (m_snakeBody.size() < 1) {
SnakeSegment& tail_bone = m_snakeBody[m_snakeBody.size() - 2];
if (tail_head.position.x == tail_bone.position.x) {
if (tail_head.position.y == tail_bone.position.y) {
m_snakeBody.push_back(SnakeSegment(tail_head.position.x, tail_head.position.y + 1));
}
else {
m_snakeBody.push_back(SnakeSegment(tail_head.position.x, tail_head.position.y - 1));
}
}
else if (tail_head.position.y == tail_bone.position.y) {
if (tail_head.position.x > tail_bone.position.x) {
m_snakeBody.push_back(SnakeSegment(tail_head.position.x + 1, tail_head.position.y));
}
else {
m_snakeBody.push_back(SnakeSegment(tail_head.position.x - 1, tail_head.position.y));
}
}
}
else {
if (m_dir == Direction::Up) {
m_snakeBody.push_back(SnakeSegment(tail_head.position.x, tail_head.position.y + 1));
}
else if (m_dir == Direction::Down) {
m_snakeBody.push_back(SnakeSegment(tail_head.position.x, tail_head.position.y - 1));
}
else if (m_dir == Direction::Left) {
m_snakeBody.push_back(SnakeSegment(tail_head.position.x+1, tail_head.position.y));
}
else if (m_dir == Direction::Right) {
m_snakeBody.push_back(SnakeSegment(tail_head.position.x-1, tail_head.position.y));
}
}
}
While its a nice little algorithm, and nice way of explaining how to find direction from two "objects" i do not see the point of it.
As the game is made i cant see there ever being a time where you are only 1 segment long (reset makes us 3 long) and since we move in tiles, i dont see why you place the new segment behind the last one.
I'd just do something like this:
void Snake::Extend() {
int i = m_snakeBody.size();
m_snakeBody.push_back(SnakeSegment(m_snakeBody[i ].x,m_snakeBody[i ].y));
}
This will place the new segment on top of the last one, allowing it to "pop out" whenever the snake moves next.
of course, the other way teaches us more, but seems a bit overkill for its functionality...
or am i lost here?