Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.76 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Linq;
  5. using UnityEngine.UI;
  6. using System;
  7. using System.Text;
  8.  
  9. public class Snake : MonoBehaviour {
  10.  
  11.     private List<Transform> tail = new List<Transform>();
  12.     private bool ate = false;
  13.     public GameObject tailPrefab;
  14.     public Text scoreText;
  15.     public GameObject gameOverPanel;
  16.     public Button startButton;
  17.     public float speed = 5;
  18.     public Text speedtext;
  19.     private bool gameOver;
  20.     private int score;
  21.     private Vector3 rot;
  22.     private string username = "Ceeyay";
  23.     public Text highscoresUserText;
  24.     public Text highscoresScoreText;
  25.  
  26.     private Vector3 left = new Vector3(0, 0, 90);
  27.     private Vector3 right = new Vector3(0, 0, -90);
  28.     private Vector3 up = new Vector3(0, 0, 0);
  29.     private Vector3 down = new Vector3(0, 0, 180);
  30.  
  31.     // Use this for initialization
  32.     void Start() {
  33.         InvokeRepeating("Move", 0.0f, (float)(1 / speed));
  34.         DisplayMenu();
  35.         gameOver = true;
  36.     }
  37.     void Restart() {
  38.         foreach (Transform t in tail) {
  39.             Destroy(t.gameObject);
  40.         }
  41.         tail.Clear();
  42.         ate = false;
  43.         CancelInvoke();
  44.         InvokeRepeating("Move", 0.0f, (float)(1 / speed));
  45.         transform.position = new Vector2(0, 0);
  46.         UpdateScore();
  47.         gameOver = false;
  48.         gameOverPanel.SetActive(false);
  49.         Text startButtonText = startButton.GetComponentInChildren<Text>();
  50.         if (startButtonText.text.Equals("Play")) {
  51.             startButtonText.text = "Restart";
  52.         }
  53.     }
  54.  
  55.     void GameOver() {
  56.         gameOver = true;
  57.         gameOverPanel.SetActive(true);
  58.         HighScoreManager.SaveHighscore(score, username);
  59.         HighScoreManager.ShowHighscores();
  60.         SpawnFood.DestroyAllFood();
  61.         highscoresUserText.text = username.ToString();
  62.         highscoresScoreText.text = score.ToString();
  63.     }
  64.  
  65.     void OnTriggerEnter2D(Collider2D coll) {
  66.         //On collision with food
  67.         if (coll.name.StartsWith("FoodPrefab")) {
  68.             //get longer in next Move call
  69.             ate = true;
  70.             SpawnFood.Destroy(coll.gameObject);
  71.             UpdateScore(1);
  72.         }
  73.         //On collision with border
  74.         if (coll.name.StartsWith("Border")) {
  75.             GameOver();
  76.         }
  77.         //On collision with tail
  78.         if (coll.name.StartsWith("Tail")) {
  79.             if (!(coll.transform.position.x == tail[0].position.x && coll.transform.position.y == tail[0].position.y))
  80.                 GameOver();
  81.         }
  82.     }
  83.  
  84.     // Update is called once per frame
  85.     void Update() {
  86.         if (!gameOver) {
  87.             CheckGameInput();
  88.         }
  89.     }
  90.  
  91.     void CheckGameInput() {
  92.         //Move in a new direction?
  93.         if (Input.GetKey(KeyCode.RightArrow)) {
  94.             if (rot != left || tail.Count == 0) {
  95.                 rot = right;
  96.             }
  97.         } else if (Input.GetKey(KeyCode.DownArrow)) {
  98.             if (rot != up || tail.Count == 0) {
  99.                 rot = down;
  100.             }
  101.         } else if (Input.GetKey(KeyCode.LeftArrow)) {
  102.             if (rot != right || tail.Count == 0) {
  103.                 rot = left;
  104.             }
  105.         } else if (Input.GetKey(KeyCode.UpArrow)) {
  106.             if (rot != down || tail.Count == 0) {
  107.                 rot = up;
  108.             }
  109.  
  110.         }
  111.         transform.rotation = Quaternion.Euler(rot);
  112.     }
  113.  
  114.  
  115.     void Move() {
  116.         if (!gameOver) {
  117.  
  118.             //Save current position (Gap will be here)
  119.             Vector2 previousPos = transform.position;
  120.  
  121.             //Move forward
  122.             transform.Translate(Vector2.up);
  123.  
  124.             if (ate) {
  125.  
  126.                 //Load Prefab into the world
  127.                 GameObject newTail = Instantiate(tailPrefab, previousPos, Quaternion.identity);
  128.  
  129.                 //Keep track of it in our tail list
  130.                 tail.Insert(0, newTail.transform);
  131.  
  132.                 //Reset the flag
  133.                 ate = false;
  134.             } else if (tail.Count > 0) { //Do we have a tail?
  135.  
  136.                 //move Last tail element to where the head was
  137.                 tail.Last().position = previousPos;
  138.  
  139.                 //Add to front of list, remove from back
  140.                 tail.Insert(0, tail.Last());
  141.                 tail.RemoveAt(tail.Count - 1);
  142.             }
  143.         }
  144.     }
  145.  
  146.     void UpdateScore(int score = 0) {
  147.         if (score == 0) {
  148.             this.score = 0;
  149.         } else {
  150.             this.score += score;
  151.         }
  152.         scoreText.text = this.score.ToString();
  153.     }
  154.  
  155.     public void SetSpeed(int speedChange) {
  156.         if (speed + speedChange >= 1) {
  157.             speed += speedChange;
  158.         }
  159.         speedtext.text = this.speed.ToString();
  160.     }
  161.  
  162.     public void StartOver() {
  163.         Restart();
  164.     }
  165.  
  166.  
  167.     public void DisplayMenu() {
  168.         gameOverPanel.SetActive(true);
  169.     }
  170.  
  171.     private void FormatHighscores() {
  172.         List<string> hs = HighScoreManager.GetHighscores();
  173.  
  174.         StringBuilder username = new StringBuilder();
  175.         StringBuilder score = new StringBuilder();
  176.  
  177.         for (int i = 0; i > hs.Count; i++) {
  178.             if (i % 2 == 0 || i == 0) {
  179.                 if (i != 0) {
  180.                     username.Append(Environment.NewLine);
  181.                     username.Append(i);
  182.                     username.Append(": ");
  183.                 } else {
  184.                     username.Append("1: ");
  185.                 }
  186.                 username.Append(hs[i]);
  187.             } else {
  188.                 if (i != 1) {
  189.                     score.Append(Environment.NewLine);
  190.                 }
  191.                 score.Append(((int)i / 2) + 1);
  192.                 score.Append(": ");
  193.                 score.Append(hs[i]);
  194.             }
  195.         }
  196.     }  
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement