Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System.Linq;
- using UnityEngine.UI;
- using System;
- public class Snake : MonoBehaviour {
- private List<Transform> tail = new List <Transform>();
- private bool ate = false;
- public GameObject tailPrefab;
- public Text scoreText;
- public GameObject gameOverPanel;
- public Button startButton;
- public float speed = 5;
- public Text highscorePanel;
- private bool gameOver;
- private int score;
- private Vector3 rot;
- // Use this for initialization
- void Start ()
- {
- highscorePanel.text = PlayerPrefs.GetInt("Highscore").ToString();
- InvokeRepeating ("Move", 0.0f, (float)(1 / speed));
- DisplayMenu ();
- gameOver = true;
- }
- void Restart() {
- foreach (Transform t in tail) {
- Destroy (t.gameObject);
- }
- tail.Clear();
- ate = false;
- CancelInvoke();
- InvokeRepeating ("Move", 0.0f, (float)(1 / speed));
- transform.position = new Vector2(0,0);
- UpdateScore ();
- gameOver = false;
- gameOverPanel.SetActive(false);
- Text startButtonText = startButton.GetComponentInChildren<Text>();
- if(startButtonText.text.Equals("Play")){
- startButtonText.text = "Restart";
- }
- }
- void GameOver()
- {
- gameOver = true;
- gameOverPanel.SetActive (true);
- HighScoreManager.SaveHighscore(score, name);
- HighScoreManager.ShowHighscores();
- if (score > PlayerPrefs.GetInt("Highscore", 0)) {
- PlayerPrefs.SetInt("Highscore", score);
- highscorePanel.text = score.ToString();
- }
- }
- void OnTriggerEnter2D(Collider2D coll)
- {
- //On collision with food
- if (coll.name.StartsWith ("FoodPrefab")) {
- //get longer in next Move call
- ate = true;
- //Remove the food
- Destroy (coll.gameObject);
- UpdateScore (1);
- }
- //On collision with border
- if (coll.name.StartsWith ("Border")) {
- GameOver ();
- }
- //On collision with tail
- if (coll.name.StartsWith ("Tail")) {
- if(!(coll.transform.position.x == tail[0].position.x && coll.transform.position.y == tail[0].position.y))
- GameOver ();
- }
- }
- // Update is called once per frame
- void Update () {
- if(!gameOver){
- CheckGameInput ();
- }
- }
- void CheckGameInput(){
- //Move in a new direction?
- if (Input.GetKey (KeyCode.RightArrow)) {
- rot = new Vector3 (0, 0, -90);
- } else if (Input.GetKey (KeyCode.DownArrow)) {
- rot = new Vector3 (0, 0, 180);
- } else if (Input.GetKey (KeyCode.LeftArrow)) {
- rot = new Vector3 (0, 0, 90);
- } else if (Input.GetKey (KeyCode.UpArrow)) {
- rot = new Vector3 (0, 0, 0);
- }
- transform.rotation = Quaternion.Euler(rot);
- }
- void Move() {
- if (!gameOver) {
- //Save current position (Gap will be here)
- Vector2 previousPos = transform.position;
- //Move forward
- transform.Translate(Vector2.up);
- if (ate) {
- //Load Prefab into the world
- GameObject newTail = Instantiate(tailPrefab, previousPos, Quaternion.identity);
- //Keep track of it in our tail list
- tail.Insert(0, newTail.transform);
- //Reset the flag
- ate = false;
- } else if (tail.Count > 0) { //Do we have a tail?
- //move Last tail element to where the head was
- tail.Last().position = previousPos;
- //Add to front of list, remove from back
- tail.Insert(0, tail.Last());
- tail.RemoveAt(tail.Count - 1);
- }
- }
- }
- void UpdateScore (int score = 0){
- if (score == 0) {
- this.score = 0;
- } else {
- this.score += score;
- }
- scoreText.text = this.score.ToString ();
- }
- public void SetSpeed(int speedChange){
- if(speed + speedChange >= 1){
- speed += speedChange;
- }
- }
- public void StartOver (){
- Restart ();
- }
- public void DisplayMenu() {
- gameOverPanel.SetActive(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement