Advertisement
Guest User

Untitled

a guest
Oct 17th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using UnityEngine;
  4.  
  5. public class Snake : MonoBehaviour
  6. {
  7. // Panel for gameover
  8. public RectTransform GameOver_m;
  9.  
  10. //text game over
  11. public Text eGameSmore;
  12.  
  13. // Snake ate the food
  14. bool ate = false;
  15.  
  16. // Tail Prefab
  17. public GameObject tailPrefab;
  18.  
  19. // Current Movement Direction
  20. Vector2 dir = Vector2.right;
  21. private List<Transform> tail = new List<Transform>();
  22.  
  23. // initialization
  24. void Start()
  25. {
  26. // Move the Snake every second
  27. InvokeRepeating("Move", 0.1f, 0.1f);
  28. }
  29.  
  30. // Update is called once per Frame
  31. void Update()
  32. {
  33. // Move in a new Direction?
  34. if (Input.GetKey(KeyCode.RightArrow))
  35. dir = Vector2.right;
  36. else if (Input.GetKey(KeyCode.DownArrow))
  37. dir = -Vector2.up; // -up means down -- who made these rules up....
  38. else if (Input.GetKey(KeyCode.LeftArrow))
  39. dir = -Vector2.right; // -right means left -- who made these rules up....
  40. else if (Input.GetKey(KeyCode.UpArrow))
  41. dir = Vector2.up;
  42. }
  43.  
  44. void Move()
  45. {
  46. // Save current position (gap will be here)
  47. Vector2 v = transform.position;
  48.  
  49. // Move head into new direction (now there is a gap)
  50. transform.Translate(dir);
  51.  
  52. // Ate something? Then insert new Element into gap
  53. if (ate)
  54. {
  55. // Load Prefab into the world
  56. GameObject g = (GameObject) Instantiate(tailPrefab,
  57. v,
  58. Quaternion.identity);
  59.  
  60. // Keep track of it in our tail list
  61. tail.Insert(0, g.transform);
  62.  
  63. // Reset the flag
  64. ate = false;
  65. }
  66. //Does have a Tail?
  67. else if (tail.Count > 0)
  68. {
  69. // Move last Tail Element to where the Head was
  70. tail.Last().position = v;
  71.  
  72. // Add to front of list, remove from the back
  73. tail.Insert(0, tail.Last());
  74. tail.RemoveAt(tail.Count - 1);
  75. }
  76.  
  77. }
  78.  
  79. void OnTriggerEnter2D(Collider2D coll)
  80. {
  81. // Food?
  82. if (coll.name.StartsWith("FoodPrefab"))
  83. {
  84. // Get longer in next Move call
  85. ate = true;
  86.  
  87. // Remove the Food
  88. Destroy(coll.gameObject);
  89. }
  90. // Collided with Tail or Border
  91. else Destroy(gameObject);
  92. {
  93. // 'You lose' - Game over.
  94. else private void OnEnable()
  95. {
  96. StartCoroutine(Co_ReloadScene(3f));
  97. }
  98. private IEnumerator Co_ReloadScene(float delay)
  99. {
  100. yield return new WaitForSeconds(delay);
  101. //load scene code here. Game Over
  102. int scene = SceneManager.GetActiveScene().buildIndex;
  103. SceneManager.LoadScene(scene, LoadSceneMode.Single);
  104. }
  105.  
  106.  
  107. }
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement