This topic has not yet been rated - Rate this topic

How To: Add Levels to the XNA Framework Application

Windows Phone

March 22, 2012

This topic demonstrates how to add levels to the XNA Framework application that is being implemented in this series. So far, the application consists of a square graphic object moving around the screen. In the preceding section, we added the ability to detect when the user taps the square. We record these hits, or collisions, as the user’s score. To demonstrate a trial, multiple levels will be added to the game. The trial experience, described in a subsequent topic, can then limit the number of levels the user can play based on the license of the game, trial or full. The level implementation, described in this topic, defines a speed at each level for the square moving around the screen. The square that bounces around the screen will move faster for each level.

Before continuing with this step, you should complete the following topics.

  1. How to: Create an XNA Framework Application

  2. How To: Add Collision Detection to the XNA Framework Application

Note Note:

The steps in the following procedure are for Visual Studio 2010 Express for Windows Phone. You may see some minor variations in menu commands or window layouts when you are using the add-in for Visual Studio 2010 Professional or Visual Studio 2010 Ultimate.

To demonstrate a trial experience, we will add levels to the game. When the player completes a level, an attempt will be made to go to the next level of the game. In this topic, the game changes per level by increasing the speed of the graphic object as it bounces around the screen.

To add levels to the game

  1. At the top of the Game1 class, add the following variables. We store a speed for each level in a List<Vector2>. The current level that the user is playing is stored in the currentLevel variable.

    
            // Each level in the game changes the speed of the sprite moving across the screen.
            // Store the speed per level in a List<Vector2>
            List<Vector2> levels;
    
            // The current level that the user is playing
            int currentLevel;
    
    
  2. In the Initialize method, replace the line that sets the spriteSpeed with the following lines of code. This code initializes the game with three game levels, each defining a Vector2 object, which represents the speed of the graphic object as it moves around the screen. The currentLevel is initialized to 1 and the spriteSpeed is now initialized to the first level’s speed, retrieved from the list of levels defined here.

    
                // Initialize the list of level speeds.
                levels = new List<Vector2>();
                levels.Add(new Vector2(50.0f, 50.0f));
                levels.Add(new Vector2(100.0f, 100.0f));
                levels.Add(new Vector2(200.0f, 200.0f));
    
                // Start the game at level one.
                currentLevel = 1;
    
                // Set the initial speed of the sprite.
                spriteSpeed = levels[currentLevel - 1];
    
    
  3. In the Game1 class, add the following methods. GoToNextLevel will check the currentLevel against the maximum number of levels, that is, levels.Count. If the user has not reached the maximum level, the currentLevel is incremented, the speed of the graphic object is set to the speed at this new level, and the counters for the game are reset. In this example, there is no end-of-game experience. If the user has reached the maximum number of levels, that level is replayed.

    
            private void GoToNextLevel()
            {
                // Are all levels completed
                if (currentLevel == levels.Count)
                {
                    // game over
                    ResetGameCounters();
                }
                else
                {
                    currentLevel++;
                    spriteSpeed = levels[currentLevel - 1];
                    ResetGameCounters();
                }
    
            }
    
            private void ResetGameCounters()
            {
                collisionCount = 0;
                spritePosition.X = 0;
                spritePosition.Y = 0;
            }
    
    
  4. Build the solution by selecting the Build | Build Solution menu command. The project should build without any errors. You can open the Error List window, if it is not already open, by selecting the View | Error List menu command. If there are errors, review the steps above, correct any errors, and then build the solution again.

  5. On the standard toolbar, set the deployment target of the application to Windows Phone Emulator .

    Target on Standard Toolbar selecting emulator

  6. Run the application by selecting the Debug | Start Debugging menu command. This will open the emulator window and launch the application. You will see a graphic bounce around the screen.

  7. If the emulator times out into the lock screen, you can unlock it by clicking at the bottom of the screen and swiping upward.

  8. You can set debug breakpoints in the code by placing the cursor on the desired line of code and selecting the Debug | Toggle Breakpoint menu command.

  9. To stop debugging, select the Debug | Stop Debugging menu command.

The next topic in this series will demonstrate how to display the current score and current level on the screen as the user plays the game. At this point, we have a simple game and the ability to track the user’s score. We also have built three levels into the game. Adding a scoreboard will enable us to see the current score and the current level at all times.

How To: Display a Scoreboard for the XNA Framework Application

Did you find this helpful?
(1500 characters remaining)