My Final GoHome game

Well, I tried to spice it up without going too all-out as I’m really enjoying this course and look forward to learning more.

To make it a little different, I gave some context in the beginning to tell the player that they’ve just woken up, cold and weak. They don’t know what happened, everything is a blur. They can hear their dog barking in the distance. Follow the bark.

If the player doesn’t make it home in 20 steps or less, they will collapse and get the game over text.

Script:

Blockquote using UnityEngine;

public class GoHomeGame : MonoBehaviour {

public Vector2 PlayerLocation;
public Vector2 homeLocation;
bool gameOver;
public int steps = 20;

// Use this for initialization
void Start()
{
    homeLocation = new Vector2(7,6);
    steps = 20;
    print("*You slowly begin to open your eyes*");
    print("''Cold.. Weak.. I must be close to home. I can hear my dog barking at me.. I have to follow his voice before I collapse again! ''");
    print("You're exhausted. You have about 20 steps left in you before you collapse. You have to make it home in time.. or else.");
    bool gameOver = false; //0 means havent won, 1 means you won and can't play
   
}

void Average(float x, float y)
{
    float avg1 = (x + y) / 2;
    print("avg: " + avg1);
}


// Update is called once per frame
void Update() {

    UpdateMovement(KeyCode.LeftArrow, new Vector2(-1, 0));
    UpdateMovement(KeyCode.RightArrow, new Vector2(1, 0));
    UpdateMovement(KeyCode.UpArrow, new Vector2(0, 1));
    UpdateMovement(KeyCode.DownArrow, new Vector2(0, -1));


}

private void UpdateMovement(KeyCode kc, Vector2 movementVector)
{

    if (Input.GetKeyDown(kc) && !gameOver && (steps>0)) 
    {
        PlayerLocation = PlayerLocation + movementVector;
        PrintDistanceToHome();
        steps = steps - 1;
    }
    if (Input.GetKeyDown(kc) && steps==0) {
        print("You have collapsed. Game Over.");
    }
}

private void PrintDistanceToHome()
{
    Vector2 pathToHome = homeLocation - PlayerLocation;
    print("You hear your dog barking in the distance. She sounds like she's about " + pathToHome.magnitude + "m away.");
    if (homeLocation == PlayerLocation)
    {
        print("You made it!");
        gameOver = true;
    }
}

}

EDIT: I apologize for the poor formatting - not entirely sure how to copy/paste code without uploading. I’ll look back on this post and cringe one day.

Privacy & Terms