Advertisement
wingman007

SnakeOOPFinal2b_Snake

Oct 28th, 2014
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.90 KB | None | 0 0
  1. using SnakeOOP2b.Controller;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace SnakeOOP2b
  9. {
  10.     class Snake
  11.     {
  12.         public Queue<Point> body = new Queue<Point>();
  13.  
  14.         public Snake(int snakeLenght)
  15.         {
  16.             for (int i = 0; i < snakeLenght; i++)
  17.             {
  18.                 body.Enqueue(new Point(i,0));
  19.             }
  20.         }
  21.  
  22.         public void Move(Direction direction)
  23.         {
  24.             Point newHead = CreateNewHead(direction);
  25.             body.Enqueue(newHead);
  26.             body.Dequeue();
  27.         }
  28.  
  29.         public void Eat(Direction direction)
  30.         {
  31.             Point newHead = CreateNewHead(direction);
  32.             body.Enqueue(newHead);
  33.         }
  34.  
  35.         private Point CreateNewHead(Direction direction)
  36.         {
  37.             Point newHead = new Point(0,0), head = body.Last();
  38.             switch (direction)
  39.             {
  40.                 case Direction.Right:
  41.                     if (head.x == Console.WindowWidth - 1) newHead = new Point(0, head.y);
  42.                     else newHead = new Point(head.x + 1, head.y);
  43.                     break;
  44.                 case Direction.Left:
  45.                     if (head.x == 0) newHead = new Point(Console.WindowWidth - 1, head.y);
  46.                     else newHead = new Point(head.x - 1, head.y);
  47.                     break;
  48.                 case Direction.Up:
  49.                     if (head.y == 0) newHead = new Point(head.x, Console.WindowHeight - 1);
  50.                     else newHead = new Point(head.x, head.y - 1);
  51.                     break;
  52.                 case Direction.Down:
  53.                     if (head.y == Console.WindowHeight - 1) newHead = new Point(head.x, 0);
  54.                     else newHead = new Point(head.x, head.y + 1);
  55.                     break;
  56.             }
  57.             return newHead;
  58.         }
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement