private void AddObstacles(float minDepth) { //add the right number of obstacles int numberToAdd = (int)((RoadSize / car.Diameter - 1) / 2.0f); //get the min space between obstacles in this section float minSize = ((RoadSize / numberToAdd) - car.Diameter) / 2.0f; for (int i = 0; i < numberToAdd; i++) { //get a random number in the min size range float depth = minDepth - ((float)Utility.Rnd.NextDouble() * minSize); //make sure its in the right range depth -= (i * (car.Diameter * 2)); //pick right or left side of road float location = (Utility.Rnd.Next(50) > 25) ? RoadLocationLeft : RoadLocationRight; //add the obstacle obstacles.Add(new Obstacle(device, location, obstacleHeight, depth)); } }
private void OnFrameUpdate() { if ((isGameOver) || (!hasGameStarted)) { return; } // First, get the elapsed time elapsedTime = Utility.Timer(DirectXTimer.GetElapsedTime); RoadDepth0 += (RoadSpeed * elapsedTime); RoadDepth1 += (RoadSpeed * elapsedTime); // We have two "roads" here a top and bottom, check to see where they are located and if //they exceed their bounds, then swap them. if (RoadDepth0 > 75.0f) { RoadDepth0 = RoadDepth1 - 100.0f; AddObstacles(RoadDepth0); } if (RoadDepth1 > 75.0f) { RoadDepth1 = RoadDepth0 - 100.0f; AddObstacles(RoadDepth1); } // Remove any obstacles that are past the car // Increase the score for each one, and also increase // the road speed to make the game harder. Obstacles removeObstacles = new Obstacles(); foreach (Obstacle o in obstacles) { if (o.Depth > car.Diameter - (Car.Depth * 2)) { // Add this obstacle to our list to remove removeObstacles.Add(o); // Increase roadspeed RoadSpeed += RoadSpeedIncrement; // Make sure the road speed stays below max if (RoadSpeed >= MaximumRoadSpeed) { RoadSpeed = MaximumRoadSpeed; } // Increase the car speed as well car.IncrementSpeed(); // Add the new score score += (int)(RoadSpeed * (RoadSpeed / car.Speed)); } } // Remove the obstacles in the list foreach (Obstacle o in removeObstacles) { obstacles.Remove(o); // May as well dispose it as well o.Dispose(); } removeObstacles.Clear(); // Move our obstacles foreach (Obstacle o in obstacles) { // Update the obstacle, check to see if it hits the car o.Update(elapsedTime, RoadSpeed); if (o.IsHittingCar(car.Location, car.Diameter)) { // If it does hit the car, the game is over. isGameOver = true; gameOverTick = System.Environment.TickCount; // Stop our timer Utility.Timer(DirectXTimer.Stop); // Check to see if we want to add this to our high scores list CheckHighScore(); } } car.Update(elapsedTime); }