Example #1
0
        void UpdateView()
        {
            char[,] fieldTemp = new char[field.GetLength(0), field.GetLength(1)];
            for (int x = 0; x < field.GetLength(0); x++)
            {
                for (int y = 0; y < field.GetLength(1); y++)
                {
                    if (field[x, y] == '\0')
                    {
                        fieldTemp[x, y] = '.';
                    }
                    else
                    {
                        fieldTemp[x, y] = field[x, y];
                    }
                }
            }
            for (int i = 0; i < snake.Length; i++)
            {
                SnakePiece snakeElement = snake[i];
                if (snakeElement == null)
                {
                    continue;
                }
                fieldTemp[snakeElement.X, snakeElement.Y] = snakeChar;
            }


            Console.Clear();
            string viewString = "";

            for (int x = 0; x < field.GetLength(0); x++)
            {
                for (int y = 0; y < field.GetLength(1); y++)
                {
                    viewString += fieldTemp[x, y];
                }
                viewString += '\n';
            }

            Console.Write(viewString);
        }
Example #2
0
 public Game(int fieldWidth, int fieldHeight)
 {
     field = new char[fieldWidth, fieldHeight];
     for (int x = 0; x < fieldHeight; x++)
     {
         field[0, x] = wallChar;
         field[fieldWidth - 1, x] = wallChar;
     }
     for (int y = 0; y < fieldWidth; y++)
     {
         field[y, 0] = wallChar;
         field[y, fieldHeight - 1] = wallChar;
     }
     snake            = new SnakePiece[fieldHeight * fieldWidth];
     snake[0]         = new SnakePiece(fieldWidth / 2, fieldHeight / 2);
     snake[1]         = new SnakePiece(fieldWidth / 2, fieldHeight / 2 + 1);
     head             = snake[1];
     tail             = snake[0];
     tail.NextElement = head;
     GenerateFood();
 }
Example #3
0
        void MoveSnake()
        {
            SnakePiece headOld  = head;
            int        oldTailX = tail.X;
            int        oldTailY = tail.Y;

            head = tail;
            tail = head.NextElement;
            headOld.NextElement = head;
            head.X = headOld.X;
            head.Y = headOld.Y;
            switch (currentDirection)
            {
            case Direction.Up: head.X--; break;

            case Direction.Down: head.X++; break;

            case Direction.Left: head.Y--; break;

            case Direction.Right: head.Y++; break;
            }
            if (field[head.X, head.Y] == foodChar)
            {
                field[head.X, head.Y] = '\0';
                SnakePiece newTail = new SnakePiece(oldTailX, oldTailY);
                newTail.NextElement = tail;
                tail = newTail;
                for (int i = 0; i < snake.Length; i++)
                {
                    if (snake[i] == null)
                    {
                        snake[i] = newTail;
                        break;
                    }
                }
                GenerateFood();
            }
        }