Guest User

Presser.cs

a guest
Mar 5th, 2017
257
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Presser : MonoBehaviour {
  6.  
  7.     public int score = 0;
  8.     public int scoreNeeded = 10;
  9.  
  10.  
  11.     public float timeGameStarted = 0f;
  12.     public float timeElapsed = 0f;
  13.     public float fastestTime = 100f;
  14.  
  15.     public string playerName = "";
  16.  
  17.     public bool playing = false;
  18.  
  19.     void DisplayStartingMessage()
  20.     {
  21.         Debug.Log ("Press RETURN to begin and SPACE to increase score");
  22.         Debug.Log ("The time to beat is " + fastestTime);
  23.     }
  24.  
  25.     // Use the start function for initialization
  26.     void Start () {
  27.         if (PlayerPrefs.HasKey("fastestTime"))
  28.         {
  29.             fastestTime = PlayerPrefs.GetFloat ("fastestTime");
  30.         }
  31.         DisplayStartingMessage();
  32.     }
  33.    
  34.     // Update function is called once per frame
  35.     void Update () {
  36.         // If we are not playing yet AND we press the spacebar to start playing
  37.         if (!playing && Input.GetKeyDown(KeyCode.Return))
  38.         {
  39.             // Sets the time we started playing to be the current time we hit SPACE first
  40.             timeGameStarted = Time.time;
  41.             playing = true;
  42.         }
  43.         else if (playing)
  44.         {
  45.             // If we hit the spacebar, gain a point
  46.             if (Input.GetKeyDown(KeyCode.Space))
  47.             {
  48.                 score++;
  49.                 Debug.Log ("score:" + score + "/" + scoreNeeded);// remaining:" + (scoreNeeded - score));
  50.             }
  51.             timeElapsed = Time.time - timeGameStarted;
  52.  
  53.             if (score >= scoreNeeded)
  54.             {
  55.  
  56.                 Debug.Log ("You win with time elapsed:" + timeElapsed);
  57.                 // If we got the fastest time, update the fastest time
  58.                 if (fastestTime > timeElapsed) {
  59.                     fastestTime = timeElapsed;
  60.                     PlayerPrefs.SetFloat ("fastestTime", fastestTime);
  61.                     Debug.Log ("You got the fastest time:" + fastestTime);
  62.                 } else {
  63.                     Debug.Log ("You got " + timeElapsed + ". Try again!");
  64.                 }
  65.  
  66.                 playing = false;
  67.                 score = 0;
  68.                 DisplayStartingMessage();
  69.             }
  70.         }
  71.     }
  72. }
RAW Paste Data