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

Author Topic: Noob Troubles on Snake  (Read 1169 times)

0 Members and 1 Guest are viewing this topic.

ascii

  • Newbie
  • *
  • Posts: 8
    • AOL Instant Messenger - 2910+fake+st
    • View Profile
Noob Troubles on Snake
« on: March 30, 2012, 09:33:27 pm »
hey guys, so I've started delving into the world of Game Development and learning SFML.  My first experiences weren't that bad, but now I'm running into some n00by troubles on my first attempt at a game (which is essentially just Snake).  My trouble is with my function AddNode(), which is supposed to add a new node to the snake (as in whenever they eat food).  The node is being added, and rendered properly, but it's new coordinates are off.  So could someone help me out with this?  I thought I was calculating the new coordinates properly, but I guess I'm not. 

So here's the relevant code.  If you want me to post more, I'll be happy too, thanks!
AddNode()
Code: [Select]
#define LASTNODE nodes[nodes.size()-1]
Snake::Node *Snake::AddNode()
{
Node *NewNode;
switch (LASTNODE->GetDirection()) {
case Node::LEFT:
// 10 is the width and height of each node, I'm just currently too lazy to replace it
// with a const
NewNode = new Node(LASTNODE->GetShape().GetPosition().x+10,
   LASTNODE->GetShape().GetPosition().y,
   LASTNODE->GetShape().GetPosition().x+20,
   LASTNODE->GetShape().GetPosition().y+10);
nodes.push_back(NewNode);
NewNode->SetDirection(Node::LEFT);
break;
case Node::RIGHT:
NewNode = new Node(LASTNODE->GetShape().GetPosition().x-10,
   LASTNODE->GetShape().GetPosition().y,
   LASTNODE->GetShape().GetPosition().x-20,
   LASTNODE->GetShape().GetPosition().y-10);
nodes.push_back(NewNode);
NewNode->SetDirection(Node::RIGHT);
break;
case Node::UP:
NewNode = new Node(LASTNODE->GetShape().GetPosition().x,
   LASTNODE->GetShape().GetPosition().y+10,
   LASTNODE->GetShape().GetPosition().x+10,
   LASTNODE->GetShape().GetPosition().y+20);
nodes.push_back(NewNode);
NewNode->SetDirection(Node::UP);
break;
case Node::DOWN:
NewNode = new Node(LASTNODE->GetShape().GetPosition().x,
   LASTNODE->GetShape().GetPosition().y-10,
   LASTNODE->GetShape().GetPosition().x+10,
   LASTNODE->GetShape().GetPosition().y-20);
nodes.push_back(NewNode);
NewNode->SetDirection(Node::DOWN);
break;
}
return NewNode;
}
Node Constructor:
Code: [Select]
Snake::Node::Node(float x1, float y1, float x2, float y2)
: shape(sf::Shape::Rectangle(x1, y1, x2, y2, sf::Color(100, 100, 100)))
{ }
Node Interface (which is nested in the Snake class)
Code: [Select]
class Node {
public:
enum Direction { LEFT, RIGHT, UP, DOWN };

Node(float, float, float, float); // constructor
void Update(sf::RenderWindow &App, sf::Event &Event);

Direction GetDirection() const { return direction; }
sf::Shape &GetShape() const { return shape; }

void SetDirection(Node::Direction direc) { direction = direc; }
private:
sf::Shape shape;
void Move(float ElapsedTime);
// Direction
Direction direction;
void Direction(sf::RenderWindow&, sf::Event&);
};
« Last Edit: March 30, 2012, 09:47:12 pm by ascii »

 

anything