/// <summary> /// Call this to start the game. /// </summary> private void StartGame() { // Start a new game. Snake = new List <snakeSegment>(); gameMatrix.Children.Clear(); currentGameState = GameState.InGame; ScoreTxT.Text = "Score: 0"; gameScore = 0; // Create the snakes body. Random r = new Random(); for (/*Declaration*/ int startX = r.Next(10, 35), startY = r.Next(10, 35), startDir = r.Next(0, 4), y = startY, x = startX, i = 0; /*Query*/ i < 8; /*Increment*/ y = (startDir == 0) ? y + 1 : (startDir == 1) ? y - 1 : startY, x = (startDir == 2) ? x + 1 : (startDir == 3) ? x - 1 : startX, i++) { Snake.Add(new snakeSegment(x, y, (MovementDirection)startDir)); } // Draw snake. foreach (snakeSegment seg in Snake) { gameMatrix.Children.Add(seg.Pixle); } // Create the apple. Apple = new gameApple(Snake); // Draw apple. gameMatrix.Children.Add(Apple.Pixle); // Start game. if (!gameTick.IsBusy) { gameTick.RunWorkerAsync(); } }
/// <summary> /// This will draw the new snake position. /// </summary> private void GameTick_MoveSnake(object sender, ProgressChangedEventArgs e) { // Note: X & Y Go on a grid from 0 to 44 // Move snake forward one place in the correct Direction. If that direction is a wall; Die. if (Snake[0].Direction == MovementDirection.Up && Snake[0].Y > minGrid) { Snake.RemoveAt(Snake.Count - 1); Snake.Insert(0, new snakeSegment(Snake[0].X, Snake[0].Y - 1, Snake[0].Direction)); } else if (Snake[0].Direction == MovementDirection.Up && Snake[0].Y == minGrid) { currentGameState = GameState.GameOver; } else if (Snake[0].Direction == MovementDirection.Down && Snake[0].Y < maxGrid) { Snake.RemoveAt(Snake.Count - 1); Snake.Insert(0, new snakeSegment(Snake[0].X, Snake[0].Y + 1, Snake[0].Direction)); } else if (Snake[0].Direction == MovementDirection.Down && Snake[0].Y == maxGrid) { currentGameState = GameState.GameOver; } else if (Snake[0].Direction == MovementDirection.Left && Snake[0].X > minGrid) { Snake.RemoveAt(Snake.Count - 1); Snake.Insert(0, new snakeSegment(Snake[0].X - 1, Snake[0].Y, Snake[0].Direction)); } else if (Snake[0].Direction == MovementDirection.Left && Snake[0].X == minGrid) { currentGameState = GameState.GameOver; } else if (Snake[0].Direction == MovementDirection.Right && Snake[0].X < maxGrid) { Snake.RemoveAt(Snake.Count - 1); Snake.Insert(0, new snakeSegment(Snake[0].X + 1, Snake[0].Y, Snake[0].Direction)); } else if (Snake[0].Direction == MovementDirection.Right && Snake[0].X == maxGrid) { currentGameState = GameState.GameOver; } // For every segement except for the head. for (int i = 1; i < Snake.Count; i++) { // Handles the snake body intersecting its self. if (Snake[i].X == Snake[0].X && Snake[i].Y == Snake[0].Y) { currentGameState = GameState.GameOver; break; } // Make sure the snake keeps its direction. else { Snake[i].Direction = Snake[i - 1].Direction; } } // Eat the apple. if (Snake[0].X == Apple.X && Snake[0].Y == Apple.Y) { if (Snake[Snake.Count - 1].Direction == MovementDirection.Up) { Snake.Add(new snakeSegment(Snake[Snake.Count - 1].X, Snake[Snake.Count - 1].Y + 1, Snake[Snake.Count - 1].Direction)); } else if (Snake[Snake.Count - 1].Direction == MovementDirection.Down) { Snake.Add(new snakeSegment(Snake[Snake.Count - 1].X, Snake[Snake.Count - 1].Y - 1, Snake[Snake.Count - 1].Direction)); } else if (Snake[Snake.Count - 1].Direction == MovementDirection.Left) { Snake.Add(new snakeSegment(Snake[Snake.Count - 1].X + 1, Snake[Snake.Count - 1].Y, Snake[Snake.Count - 1].Direction)); } else if (Snake[Snake.Count - 1].Direction == MovementDirection.Right) { Snake.Add(new snakeSegment(Snake[Snake.Count - 1].X - 1, Snake[Snake.Count - 1].Y, Snake[Snake.Count - 1].Direction)); } Apple = new gameApple(Snake); gameScore++; ScoreTxT.Text = "Score: " + gameScore; } if (currentGameState == GameState.InGame) { // Clear the matrix. gameMatrix.Children.Clear(); // Draw the snake. foreach (snakeSegment seg in Snake) { gameMatrix.Children.Add(seg.Pixle); } // Draw the apple. gameMatrix.Children.Add(Apple.Pixle); } }