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;
- using System.Text;
- 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 speedtext;
- private bool gameOver;
- private int score;
- private Vector3 rot;
- private string username = "Ceeyay";
- public Text highscoresUserText;
- public Text highscoresScoreText;
- private Vector3 left = new Vector3(0, 0, 90);
- private Vector3 right = new Vector3(0, 0, -90);
- private Vector3 up = new Vector3(0, 0, 0);
- private Vector3 down = new Vector3(0, 0, 180);
- // Use this for initialization
- void Start() {
- 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, username);
- HighScoreManager.ShowHighscores();
- SpawnFood.DestroyAllFood();
- highscoresUserText.text = username.ToString();
- highscoresScoreText.text = score.ToString();
- }
- void OnTriggerEnter2D(Collider2D coll) {
- //On collision with food
- if (coll.name.StartsWith("FoodPrefab")) {
- //get longer in next Move call
- ate = true;
- SpawnFood.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)) {
- if (rot != left || tail.Count == 0) {
- rot = right;
- }
- } else if (Input.GetKey(KeyCode.DownArrow)) {
- if (rot != up || tail.Count == 0) {
- rot = down;
- }
- } else if (Input.GetKey(KeyCode.LeftArrow)) {
- if (rot != right || tail.Count == 0) {
- rot = left;
- }
- } else if (Input.GetKey(KeyCode.UpArrow)) {
- if (rot != down || tail.Count == 0) {
- rot = up;
- }
- }
- 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;
- }
- speedtext.text = this.speed.ToString();
- }
- public void StartOver() {
- Restart();
- }
- public void DisplayMenu() {
- gameOverPanel.SetActive(true);
- }
- private void FormatHighscores() {
- List<string> hs = HighScoreManager.GetHighscores();
- StringBuilder username = new StringBuilder();
- StringBuilder score = new StringBuilder();
- for (int i = 0; i > hs.Count; i++) {
- if (i % 2 == 0 || i == 0) {
- if (i != 0) {
- username.Append(Environment.NewLine);
- username.Append(i);
- username.Append(": ");
- } else {
- username.Append("1: ");
- }
- username.Append(hs[i]);
- } else {
- if (i != 1) {
- score.Append(Environment.NewLine);
- }
- score.Append(((int)i / 2) + 1);
- score.Append(": ");
- score.Append(hs[i]);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement