// Start is called before the first frame update void Awake() { // Check if there are multiple instances of the game manager if (instance == null) { instance = this; } else if (instance != null) { Destroy(gameObject); } DontDestroyOnLoad(gameObject); boardScript = GetComponent <BoardManager>(); InitGame(); // Get the Player and AI snake playerSnakeScript = playerSnake.GetComponent <PlayerSnake>(); enemySnakeScript = enemySnake.GetComponent <EnemySnake>(); // Get the UI elements playerScoreTxt = GameObject.Find("PlayerScore").GetComponent <Text>(); aiScoreTxt = GameObject.Find("AIScore").GetComponent <Text>(); gameOverTxt = GameObject.Find("GameOverText").GetComponent <Text>(); endScoreTxt = GameObject.Find("EndScoreText").GetComponent <Text>(); btnPlayAgain = GameObject.Find("Button_PlayAgain").GetComponent <Button>(); btnExit = GameObject.Find("Button_Exit").GetComponent <Button>(); // Hides the end game screen while the game is in progress instance.HideEndGameScreen(); }
//Main game logic goes below: public void StartGame() { //Initializes the player snake InitializeSnake(); //Initialie the enemy snake InitializeEnemySnake(); //Initializes the obstacles InitializeObstacles(); //Initializes the moving obstacles InitilizeMovingObstacles(); ////Initializes the food InitilizeFood(); //Check of timer is null timer?.Stop(); randomMovingObjectsChecker = random.Next(BaseConstants.MinMovingObstaclesPrescaller, BaseConstants.MaxMovingObstaclesPrescaller); //Create the timer and add the tick event delegate timer = new DispatcherTimer(); timer.Interval = TimeSpan.FromMilliseconds(50); int ticksCounter = 0; //Do this on every tick of the timer; timer.Tick += delegate { //At every tick move the snake and the enemy snake Snake.Move(); EnemySnake.Move(); //Change the direction of the enemy snake randomly if (ticksCounter == randomMovingObjectsChecker) { MovingObstaclesChangeDirection(); EnemySnakeChangeDirection(); ticksCounter = 0; } ////Handle moving food movement foreach (var movingFood in this.MovingFoods) { var shouldChangeDirection = random.Next() % 3 == 0; if (shouldChangeDirection) { Directions newDirection = (Directions)random.Next(0, 4); movingFood.ChangeDirection(newDirection); } } //Increase counter ticks ticksCounter++; //Move obstacles which are able to move this.MoveObstacles(); this.MoveFoods(); ////Check for eaten food or obstacle CheckFoodEaten(); CheckFoodEatenSnakeObstakle(); //Check if snake is dead var isDead = IsSnakeDead(); if (isDead) { GameOver(); } //Notify for property changed this.OnPropertyChanged("Snake"); this.OnPropertyChanged("EnemySnake"); this.OnPropertyChanged("Obstacles"); }; //Start the timer timer.Start(); }