Copying and Pasting cs Code

In cs, like in almost any computer programming language, reading data from a file can be tricky. You add extra lines of code to tell the computer what to do. Sometimes you can copy and paste these lines from other peoples’ code.

For example, you can follow the pattern in this listing:

   protected override void AttemptMove (int xDir, int yDir)
   {
    //Every time player moves, subtract from food points total.
    food--;

    //Update food text display to reflect current score.
    foodText.text = "Food: " + food;

    //Call the AttemptMove method of the base class, passing in the component T (in this case Wall) and x and y direction to move.
    base.AttemptMove (xDir, yDir);

    //Hit allows us to reference the result of the Linecast done in Move.
    RaycastHit2D hit;

    //If Move returns true, meaning Player was able to move into an empty space.
    if (Move (xDir, yDir, out hit))
    {
     //Call RandomizeSfx of SoundManager to play the move sound, passing in two audio clips to choose from.
     SoundManager.instance.RandomizeSfx (moveSound1, moveSound2);
    }

    //Since the player has moved and lost food points, check if the game has ended.
    CheckIfGameOver ();

    //Set the playersTurn boolean of GameManager to false now that players turn is over.
    GameManager.instance.playersTurn = false;
   }