public static SnakePart[] Grow(SnakePart[] myOldSnake, Vector2 direction, Vector2 foodPosition, float myScale) { SnakePart[] myNewSnake = new SnakePart[myOldSnake.Length + 1]; SnakePart newSnakePart = new SnakePart(); newSnakePart.Spawn(lastTailPosition, myScale, Color.blue); myNewSnake[myNewSnake.Length - 1] = newSnakePart; for (int i = 0; i < myOldSnake.Length; i++) { myNewSnake[i] = myOldSnake[i]; } snakeSize = myNewSnake.Length; return(myNewSnake); }
private void initializeGame() { myScale = Screen.width / SettingsHandler.horizontalRows; mainBackgroundWidth = Screen.width - myScale * 2; // Initializing main background height as a % of the screen height. for (int i = 0; mainBackgroundHeight < (heightPercentage * Screen.height); i++) { mainBackgroundHeight = myScale * i; } mainBackgroundHeight -= myScale * 2; //Debug.Log("Width: " + mainBackgroundWidth + " Height: " + mainBackgroundHeight); // Initializing backgrounds. StaticBackground myStaticBackground = new StaticBackground(); myStaticBackground.Spawn(new Vector2(mainBackgroundWidth, mainBackgroundHeight), new Vector2(0, 0), Color.black, "MainStaticBackground"); //StaticBackground myScoreBackground = new StaticBackground(); //myStaticBackground.Spawn(new Vector2((float)(Screen.width * scoreBoardWidthFactor)-myScale, Screen.height - mainBackgroundHeight -myScale), new Vector2((float)(((-Screen.width * (1 - scoreBoardWidthFactor)) /2) - (myScale / 2)), ((Screen.height-mainBackgroundHeight)/2)+(mainBackgroundHeight/2)+(myScale/2)), Color.black, "ScoreBackground"); //StaticBackground myPauseBackground = new StaticBackground(); //myStaticBackground.Spawn(new Vector2(Screen.height - mainBackgroundHeight - myScale, Screen.height - mainBackgroundHeight - myScale), new Vector2((float)(Screen.width * scoreBoardWidthFactor) / 2, ((Screen.height - mainBackgroundHeight) / 2) + (mainBackgroundHeight / 2) + (myScale / 2)), Color.yellow, "PauseBackground"); StaticBackground myScoreBackground = new StaticBackground(); myStaticBackground.Spawn(new Vector2(Screen.width - (Screen.height - mainBackgroundHeight), Screen.height - mainBackgroundHeight - myScale * 3), new Vector2(((Screen.width - (Screen.height - mainBackgroundHeight)) / 2) - (Screen.width / 2) + myScale, ((Screen.height - mainBackgroundHeight) / 2) + (mainBackgroundHeight / 2) + (myScale / 2) - myScale), Color.black, "ScoreBackground"); StaticBackground myPauseBackground = new StaticBackground(); myStaticBackground.Spawn(new Vector2(Screen.height - mainBackgroundHeight - myScale * 3, Screen.height - mainBackgroundHeight - myScale * 3), new Vector2((float)(((Screen.width) / 2) - (Screen.height - mainBackgroundHeight - myScale) / 2), ((Screen.height - mainBackgroundHeight) / 2) + (mainBackgroundHeight / 2) + (myScale / 2) - myScale), Color.black, "PauseBackground"); GameObject.Find("PauseBackground").AddComponent <PauseButtonScript>(); GameObject.Find("PauseBackground").AddComponent <BoxCollider2D>(); // initializing my snake array position for (int i = 0; i < mySnakePartArray.Length; i++) { SnakePart tempSnakePart = new SnakePart(); Vector2 tempVector = new Vector2(((-mainBackgroundWidth / 2) + myScale / 2) - (i * myScale) + 4 * myScale, (-mainBackgroundHeight / 2) + (myScale / 2) + 2 * myScale); if (i == 0) { tempSnakePart.Spawn(tempVector, myScale, Color.cyan); } else { tempSnakePart.Spawn(tempVector, myScale, Color.blue); } mySnakePartArray[i] = tempSnakePart; //Debug.Log("MySnakePosition:" + mySnakePartArray[i].getPosition()); } snakeDirection.x = myScale; snakeDirection.y = 0; // Initializing food position. myFood.SpawnRandomSpot(mainBackgroundWidth, mainBackgroundHeight, myScale, mySnakePartArray); }