private void OnTriggerEnter(Collider other) { if (other.name == "Player") { //Add onto the score! There could be a score multiplier too. GlobalStats.AddScore(scoreMultiplier * 10, transform.position); //Play the grow sound and up the pitch each time SoundManager.PlaySound(SoundManager.Sounds.GROW, currentPitch); currentPitch += 0.1f; if (currentPitch > 2.0f) { currentPitch = 1.0f; } //Add a snake segment Snake snake = other.GetComponent <Snake>(); snake.AddSegment(); snake.ZoomOutCamera(); snake.IncreaseBoostGuage(); //If a classic snake collectable then notify the food spawner if (ExtraCollectEvent != null) { ExtraCollectEvent(); } //Notify the gate this was collected transform.parent.Find("Gate").GetComponent <SnakeGate>().LowerGate(); Destroy(gameObject); } else if (other.name == "Marker") { Destroy(other.gameObject); //Destroy a food marker if the food spawned from it. } }
static void GameLoop() { //Create game loop while (true) { Time.Update(); Input.Update(); //check if esc was pressed so we can exit the game if (Input.KeyPressed == InputType.ESC) { canvas.WriteMessageBottom("Exiting Game..."); WriteLine(); break; } /* ------------------------------------------ * 4.1 * ------------------------------------------ * - Store the x position of the snake in a variable called prevSnakeX. * - Store the y position of the snake in a variable called prevSnakeY. * - Call the Update method of snake. * * This way we can store the previous position of the snake before * we update it */ int prevSnakeX = snake.X; int prevSnakeY = snake.Y; snake.Update(); /* ------------------------------------------ * 4.2 * ------------------------------------------ * Change the if statement below so that it will only be true * if either the x or y position of the snake has changed since * the last update. * * This will allow us to only make updates to the game * if the snake moved. */ if (snake.X != prevSnakeX || snake.Y != prevSnakeY) { /* ------------------------------------------ * 4.3 * ------------------------------------------ * If the position of the snake is equal to the position of * the pickup (It's been picked up), do the following: * * - Add 100 to the score. * - Use the relevant method of cnavas to update the score * displaying at the top of the screen. * - Increase the speed of the snake with the value of SPEED_INCREASE. * - Use the relevant method to add a segment to the snake. * - Use the relevant method of the pickup to change * its position. */ if (snake.X == pickup.X && snake.Y == pickup.Y) { score += 100; canvas.WriteMessageTop("Score: " + score); snake.Speed += SPEED_INCREASE; snake.AddSegment(); pickup.SetPosition(); } /* ------------------------------------------ * 4.4 * ------------------------------------------ * If the IsSnakeInBounds mehtod returns false, or * the IsHeadTouchingBody method of the snake returns true, * the game is over. Do the following: * * - "Game Over :(" should be written to the bottom of the * screen using the relevant method of the canvas object. * - Call the WriteLine method of the Console class. * - Call the Beep method of the Console class. * - Break out of the game loop. */ if (IsSnakeInBounds() == false || snake.IsHeadTouchingBody()) { canvas.WriteMessageBottom("Game Over :("); Console.WriteLine(); Console.Beep(); break; } //This method call will update the positions of all the snake segments. snake.UpdateSegments(); /* ------------------------------------------ * 4.5 * ------------------------------------------ * We have updated the positions of all the segments of the snake, * but now we need to draw the segements to the canvas. * * REMEMBER: We need to clear the character drawn at the previous * segment position before we draw the new one. * * BONUS: See if you can alternate the colours of the segments * between Magenta and Green. */ for (int i = 0; i < snake.Segments.Count; i++) { if (i % 2 == 0) { canvas.Draw(snake.Segments[i].X, snake.Segments[i].Y, snake.Segments[i].Character, ConsoleColor.Magenta); } else { canvas.Draw(snake.Segments[i].X, snake.Segments[i].Y, snake.Segments[i].Character, ConsoleColor.Green); } } //draw pickup canvas.Draw(pickup.X, pickup.Y, '', ConsoleColor.Red); } } }