コード例 #1
0
        public void Update()
        {
            previousBlockOccupied = blocksOccupied[0];
            #region InputManagment
            switch (lastPressedKey)
            {
            case ConsoleKey.W:
            case ConsoleKey.UpArrow:
            {
                if (direction != Cords.up)
                {
                    direction = Cords.down;
                }
            }
            break;

            case ConsoleKey.S:
            case ConsoleKey.DownArrow:
            {
                if (direction != Cords.down)
                {
                    direction = Cords.up;
                }
            }
            break;

            case ConsoleKey.A:
            case ConsoleKey.LeftArrow:
            {
                if (direction != Cords.right)
                {
                    direction = Cords.left;
                }
            }
            break;

            case ConsoleKey.D:
            case ConsoleKey.RightArrow:
            {
                if (direction != Cords.left)
                {
                    direction = Cords.right;
                }
            }
            break;
            }
            #endregion

            head += direction;
            for (int i = 0; i < Length - 1; i++)
            {
                blocksOccupied[i] = blocksOccupied[i + 1];
            }
            blocksOccupied[Length - 1] = this.head;
            #region CollitionDetecion
            //collition with the border
            if (head.x < 1 || head.y < 1 || head.x > Program.World.size.x || head.y > Program.World.size.y)
            {
                Loose();
            }
            //collition with the tail
            for (int i = 0; i < Length - 1; i++)//length-1 because blocksOccupied[Length] is the head itself
            {
                if (head == BlocksOccupied[i])
                {
                    Loose();
                }
            }
            //colition with the fruit
            if (head == Program.World.Fruit)
            {
                this.Grow();
                Program.World.CreateFruit();
            }
            #endregion
        }
コード例 #2
0
ファイル: World.cs プロジェクト: karim2989/CsSnake
 public World(int width, int height)
 {
     size = new Cords(width, height);
 }