Description:
Snake Cave is like the classic Snake, where you control a snake that grows longer with every piece of “food” you eat (in this game it’s called candy), but with a random generated border. The goal of this border is to make the game more dynamic and challenging. Every playthrough is different and maybe even easier/more difficult. The player can also choose between three difficulties in the options menu: Easy, Normal and Hard. Easy has a big field, slower snake and more candies. Hard has a small field, fast snake and a small amount of candies.
The game is made in Unity3D with C#.
Started: September 2017
Finished: Oktober 2017
Links:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
public class LevelGenerator : MonoBehaviour { public GameObject tile; public List<Vector3> createdTiles; public int tileAmount; public int tileSize; // Chances of the stated directions public float chanceUp; public float chanceRight; public float chanceDown; // Generating the border at the start of the game public float drawTime = 0.1f; public bool isDrawn = false; private int iterator = 0; private Vector3 startPosition; private bool stopGenerator = false; // Min and max values of the created tiles. public float minX = 9999f; public float maxX = 0f; public float minY = 9999f; public float maxY = 0f; // Min and max values of the screen (used for corners). private float minScreenX; private float maxScreenX; private float minScreenY; private float maxScreenY; private void Awake() { SetScreenValues(); transform.position = startPosition; StartCoroutine(GenerateLevel()); } private void SetScreenValues() { // easy if (PlayerPrefsManager.GetDifficulty() == 0f) { startPosition = new Vector3(-39, 25, -5); minScreenX = -40f; maxScreenX = 40f; minScreenY = -25f; maxScreenY = 25f; } // normal else if (PlayerPrefsManager.GetDifficulty() == 1f) { startPosition = new Vector3(-29, 20, -5); minScreenX = -30f; maxScreenX = 30f; minScreenY = -20f; maxScreenY = 20f; } // hard else { startPosition = new Vector3(-24, 14, -5); minScreenX = -25f; maxScreenX = 25f; minScreenY = -15f; maxScreenY = 15f; } } private IEnumerator GenerateLevel() { do { float direction = UnityEngine.Random.Range(0f, 1f); CreateTile(); CallMoveGenerator(direction); iterator++; yield return new WaitForSeconds(drawTime); } while (iterator < tileAmount); if (iterator >= tileAmount) { isDrawn = true; } yield return 0; } private void CallMoveGenerator(float randomDirection) { CreateCorner(); if (randomDirection < chanceUp) { MoveGenerator(0); } else if (randomDirection < chanceRight) { MoveGenerator(1); } else if (randomDirection < chanceDown) { MoveGenerator(2); } else { MoveGenerator(3); } } private void MoveGenerator(int direction) { switch (direction) { case 0: // up transform.position = new Vector3(transform.position.x, transform.position.y + tileSize, 0); break; case 1: // right transform.position = new Vector3(transform.position.x + tileSize, transform.position.y, 0); break; case 2: // down transform.position = new Vector3(transform.position.x, transform.position.y - tileSize, 0); break; case 3: // left transform.position = new Vector3(transform.position.x - tileSize, transform.position.y, 0); break; } } private void CreateTile() { // If there already is a tile on this position, add one iteration. if (createdTiles.Contains(transform.position)) { tileAmount++; } else { GameObject tileObject = Instantiate(tile, transform.position, transform.rotation) as GameObject; createdTiles.Add(tileObject.transform.position); } } private void CreateCorner() { if (transform.position.x > maxScreenX) // top right corner { chanceUp = 0f; chanceRight = 0.2f; chanceDown = 0.8f; } if (transform.position.y < minScreenY) // bottom right corner { chanceUp = 0.2f; chanceRight = 0; chanceDown = 0.4f; } if (transform.position.x < minScreenX) // bottom left corner { chanceUp = 0.6f; chanceRight = 0.85f; chanceDown = 0; stopGenerator = true; } if (stopGenerator && transform.position.y >= maxScreenY) // top left corner/end { StopAllCoroutines(); SetValues(); isDrawn = true; } } // Set values for min and max tiles. Candies spawn between these. private void SetValues() { for (int i = 0; i < createdTiles.Count - 1; i++) { if (createdTiles[i].x < minX) { minX = createdTiles[i].x; } else if (createdTiles[i].x > maxX) { maxX = createdTiles[i].x; } else if (createdTiles[i].y < minY) { minY = createdTiles[i].y; } else if (createdTiles[i].y > maxY) { maxY = createdTiles[i].y; } } } } |
Screenshots: