public void updateGame() { //Update snake snakeHead.update(); if (appleCollidesWithSnakeHead()) { //When the snake head collides with an apple while (appleCollidesWithSnakeHead() || appleCollidesWithBodyOfSnake() || appleIsToCloseToSnakeHead()) { apple = new Apple(foregroundColor); growSnake = true; } } //Snake grows if player just hit an apple if (growSnake) { biteSoundEffect.Play(); lengthenSnake(); growSnake = false; ++playerScore; } //If a cover collides with the last body segment, stop drawing it. BodySegment lastBodySegment = bodySegments[bodySegments.Count() - 1]; for (int i = 0; i < covers.Count; ++i) { if (covers[i].collides(lastBodySegment.x, lastBodySegment.y)) { covers.RemoveAt(i); } } //Update all the body segments for (int i = 0; i < bodySegments.Count; ++i) { bodySegments[i].update(); } }
public void resetGame(Color newColor) { foregroundColor = newColor; snakeHead = new Head(foregroundColor); apple = new Apple(foregroundColor); bodySegments = new List <BodySegment>(); covers = new List <Cover>(); playerScore = 0; //Stop apple from spawning on snake or right in front of snake while (appleCollidesWithSnakeHead() || appleCollidesWithBodyOfSnake() || appleIsToCloseToSnakeHead()) { apple = new Apple(foregroundColor); } //Initialize snake to have six body segments. bodySegments.Add(new BodySegment(snakeHead.x - snakeHead.l, snakeHead.y, snakeHead.l, foregroundColor, direction.R)); bodySegments.Add(new BodySegment(snakeHead.x - (2 * snakeHead.l), snakeHead.y, snakeHead.l, foregroundColor, direction.R)); bodySegments.Add(new BodySegment(snakeHead.x - (3 * snakeHead.l), snakeHead.y, snakeHead.l, foregroundColor, direction.R)); bodySegments.Add(new BodySegment(snakeHead.x - (4 * snakeHead.l), snakeHead.y, snakeHead.l, foregroundColor, direction.R)); bodySegments.Add(new BodySegment(snakeHead.x - (5 * snakeHead.l), snakeHead.y, snakeHead.l, foregroundColor, direction.R)); bodySegments.Add(new BodySegment(snakeHead.x - (6 * snakeHead.l), snakeHead.y, snakeHead.l, foregroundColor, direction.R)); }
public Snake(Color foregroundColor, Color backgroundColor) { snakeHead = new Head(foregroundColor); apple = new Apple(foregroundColor); bodySegments = new List <BodySegment>(); covers = new List <Cover>(); this.foregroundColor = foregroundColor; this.backgroundColor = backgroundColor; growSnake = false; playerScore = 0; //Stop apple from spawning on snake or right in front of snake while (appleCollidesWithSnakeHead() || appleCollidesWithBodyOfSnake() || appleIsToCloseToSnakeHead()) { apple = new Apple(foregroundColor); } //Set up bite sound effect //Source: https://www.youtube.com/watch?v=aiNg8ChsyUg biteSoundEffect = new MediaPlayer(); biteSoundEffect.Source = MediaSource.CreateFromUri(new Uri("ms-appx:///Assets/chomp_sound_effect.wav")); //Set up yipee sound effect that never plays //Source: https://themushroomkingdom.net/media/sm64/wav yahooSoundEffect = new MediaPlayer(); yahooSoundEffect.Source = MediaSource.CreateFromUri(new Uri("ms-appx:///Assets/sm64_mario_yahoo.wav")); //Initialize snake to have six body segments. bodySegments.Add(new BodySegment(snakeHead.x - snakeHead.l, snakeHead.y, snakeHead.l, foregroundColor, direction.R)); bodySegments.Add(new BodySegment(snakeHead.x - (2 * snakeHead.l), snakeHead.y, snakeHead.l, foregroundColor, direction.R)); bodySegments.Add(new BodySegment(snakeHead.x - (3 * snakeHead.l), snakeHead.y, snakeHead.l, foregroundColor, direction.R)); bodySegments.Add(new BodySegment(snakeHead.x - (4 * snakeHead.l), snakeHead.y, snakeHead.l, foregroundColor, direction.R)); bodySegments.Add(new BodySegment(snakeHead.x - (5 * snakeHead.l), snakeHead.y, snakeHead.l, foregroundColor, direction.R)); bodySegments.Add(new BodySegment(snakeHead.x - (6 * snakeHead.l), snakeHead.y, snakeHead.l, foregroundColor, direction.R)); }