Advertisement
Guest User

Untitled

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