Ejemplo n.º 1
0
        public void MoveSnake(int rowMovement, int blockMovement)
        {
            int snakeHead_RowPosition   = mainSnake.snakeHead.snakeRowPosition;
            int snakeHead_BlockPosition = mainSnake.snakeHead.snakeBlockPosition;

            SnakeNode newSnakeHead = new SnakeNode(snakeHead_RowPosition + (rowMovement), snakeHead_BlockPosition + (blockMovement));

            if (newSnakeHead.snakeRowPosition == mainSnake.snakeHead.next.snakeRowPosition && newSnakeHead.snakeBlockPosition == mainSnake.snakeHead.next.snakeBlockPosition)
            {
                return;
            }

            if (newSnakeHead.snakeRowPosition == 0 || newSnakeHead.snakeRowPosition == MATRIX.GetLength(0) - 1 || newSnakeHead.snakeBlockPosition == 0 || newSnakeHead.snakeBlockPosition == MATRIX.GetLength(1) - 1)
            {
                isGameOver = true;
            }

            newSnakeHead.next = mainSnake.snakeHead;

            mainSnake.snakeHead = newSnakeHead;

            MATRIX[mainSnake.snakeHead.snakeRowPosition, mainSnake.snakeHead.snakeBlockPosition] = SNAKEHEAD;

            MATRIX[mainSnake.snakeHead.next.snakeRowPosition, mainSnake.snakeHead.next.snakeBlockPosition] = SNAKEBODY;

            SnakeNode TEMP_snakePointer      = mainSnake.snakeHead;
            SnakeNode PREV_TEMP_snakePointer = null;

            while (TEMP_snakePointer.next != null)
            {
                PREV_TEMP_snakePointer = TEMP_snakePointer;
                TEMP_snakePointer      = TEMP_snakePointer.next;

                if (mainSnake.snakeHead.snakeRowPosition == TEMP_snakePointer.snakeRowPosition && mainSnake.snakeHead.snakeBlockPosition == TEMP_snakePointer.snakeBlockPosition)
                {
                    isGameOver = true;
                    return;
                }
            }

            if (mainSnake.snakeHead.snakeRowPosition == TEMP_snakePointer.snakeRowPosition && mainSnake.snakeHead.snakeBlockPosition == TEMP_snakePointer.snakeBlockPosition)
            {
                isGameOver = true;
            }

            if (mainSnake.snakeHead.snakeRowPosition == APPLE_COORDINATES[0] && mainSnake.snakeHead.snakeBlockPosition == APPLE_COORDINATES[1])
            {
                GenerateApplePosition();
                score++;
            }
            else
            {
                MATRIX[TEMP_snakePointer.snakeRowPosition, TEMP_snakePointer.snakeBlockPosition] = BOARD_SPACE;

                PREV_TEMP_snakePointer.next = null;
            }
        }
Ejemplo n.º 2
0
        // Creates the snake by creating a new SnakeNode. The snakeHead variable points to this new Node.

        public void CreateSnake(int startingRowPosition, int startingBlockPosition)
        {
            SnakeNode newSnake = new SnakeNode(startingRowPosition, startingBlockPosition);

            snakeHead = newSnake;

            SnakeNode defaultSnakeBody_1 = new SnakeNode(startingRowPosition + 1, startingBlockPosition);
            SnakeNode defaultSnakeBody_2 = new SnakeNode(startingRowPosition + 2, startingBlockPosition);

            snakeHead.next      = defaultSnakeBody_1;
            snakeHead.next.next = defaultSnakeBody_2;
        }
Ejemplo n.º 3
0
        // GenerateSnake() creates the snake object.

        private void GenerateSnake()
        {
            int rowValue   = MATRIX.GetLength(0) / 2;
            int blockValue = MATRIX.GetLength(1) / 2;

            mainSnake.CreateSnake(rowValue, blockValue);

            SnakeNode TEMP_snakePointer = mainSnake.snakeHead;

            MATRIX[TEMP_snakePointer.snakeRowPosition, TEMP_snakePointer.snakeBlockPosition] = SNAKEHEAD;

            while (TEMP_snakePointer.next != null)
            {
                // MATRIX[TEMP_snakePointer.snakeRowPosition, TEMP_snakePointer.snakeBlockPosition] = SNAKEBODY;
                TEMP_snakePointer = TEMP_snakePointer.next;
                MATRIX[TEMP_snakePointer.snakeRowPosition, TEMP_snakePointer.snakeBlockPosition] = SNAKEBODY;
            }

            MATRIX[TEMP_snakePointer.snakeRowPosition, TEMP_snakePointer.snakeBlockPosition] = SNAKEBODY;
        }
Ejemplo n.º 4
0
        public void AddSnakeLength(SnakeNode newSnakeBody)
        {
            newSnakeBody = snakeHead.next;

            snakeHead.next = newSnakeBody;
        }