コード例 #1
0
ファイル: Snake.cs プロジェクト: Ket/PG3300-Innlevering1
 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);
 }
コード例 #2
0
ファイル: Snake.cs プロジェクト: Ket/PG3300-Innlevering1
 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++;
     }
 }
コード例 #3
0
 public void SetTo(Coordinates coordinates)
 {
     this.X = coordinates.X;
     this.Y = coordinates.Y;
 }
コード例 #4
0
 public Coordinates(Coordinates coordinates)
 {
     this.X = coordinates.X;
     this.Y = coordinates.Y;
 }
コード例 #5
0
ファイル: Snake.cs プロジェクト: Ket/PG3300-Innlevering1
 public SnakeHead(Texture headTexture, Coordinates headPosition)
 {
     texture = headTexture;
     position = new Coordinates(headPosition);
     previousPosition = new Coordinates(headPosition);
     targetPosition = new Coordinates(headPosition);
 }
コード例 #6
0
ファイル: Snake.cs プロジェクト: Ket/PG3300-Innlevering1
 public SnakeBodyPart(Texture bodyTexture, Coordinates partPosition)
 {
     texture = bodyTexture;
     position = new Coordinates(partPosition);
     previousPosition = new Coordinates(partPosition);
     targetPosition = new Coordinates(partPosition);
     parentPosition = new Coordinates(partPosition);
 }
コード例 #7
0
ファイル: Snake.cs プロジェクト: Ket/PG3300-Innlevering1
        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;
                }

            }
        }