public Snake(int length) { alive = true; position = new Coordinates(Global.snakeStartingPosition); head = new SnakeHead(Global.defaultSnakeHeadTexture, position); /* Link snake position to head position. */ this.position = head.position; body = new SnakeBody(Global.defaultSnakeBodyTexture, position, length); }
public SnakeBody(Texture bodyTexture, Coordinates position, int length) { isBitten = false; texture = bodyTexture; bodyParts = new List<SnakeBodyPart>(); for (int i = 0; i < length; i++) { bodyParts.Add(new SnakeBodyPart(texture, position)); this.length++; } }
public void SetTo(Coordinates coordinates) { this.X = coordinates.X; this.Y = coordinates.Y; }
public Coordinates(Coordinates coordinates) { this.X = coordinates.X; this.Y = coordinates.Y; }
public SnakeHead(Texture headTexture, Coordinates headPosition) { texture = headTexture; position = new Coordinates(headPosition); previousPosition = new Coordinates(headPosition); targetPosition = new Coordinates(headPosition); }
public SnakeBodyPart(Texture bodyTexture, Coordinates partPosition) { texture = bodyTexture; position = new Coordinates(partPosition); previousPosition = new Coordinates(partPosition); targetPosition = new Coordinates(partPosition); parentPosition = new Coordinates(partPosition); }
public void Update(SnakeHead head) { bodyParts[0].parentPosition.SetTo(head.position); // Move first element if(!bodyParts[0].targetPosition.Equals(bodyParts[0].parentPosition)){ // Save previous position bodyParts[0].previousPosition.SetTo(bodyParts[0].position); // Set position to target position bodyParts[0].position.SetTo(bodyParts[0].targetPosition); // Set new target position bodyParts[0].targetPosition.SetTo(head.position); } // Save the location of the snakes trail trailPosition = new Coordinates(bodyParts.Last().previousPosition); // Rest should follow suit for (int i = 1; i < bodyParts.Count; i++) { // Update each part with its superior part bodyParts[i].Update(bodyParts[i-1]); // Check for self-cannibalism while you are at it, hunny. if (bodyParts[i].position.Equals(head.position)) { isBitten = true; } } }