Time for action - viewing the StarField in action

  1. Add the following declaration to the Game1 class:
    StarField starField;
    
  2. In the declarations area of the Game1 class, temporarily modify the declaration for gameState from GameStates.TitleScreen to GameStates.Playing:
    GameStates gameState = GameStates.Playing;
    
  3. Update the LoadContent() method of the Game1 class to initialize the starField object. Be sure to place this code after the spriteSheet texture is loaded.
    starField = new StarField(
    this.Window.ClientBounds.Width,
    this.Window.ClientBounds.Height,
    200,
    new Vector2(0, 30f),
    spriteSheet,
    new Rectangle(0, 450, 2, 2));
    
  4. In the Upate() method, add the following line to the GameStates.Playing section of the switch statement you created earlier:
    starField.Update(gameTime);
    
  5. In the Draw() method, change the background color from Color.CornflowerBlue to Color.Black.
  6. Still in the Draw() method, add the following line to the if block containing GameStates.Playing:
    starField.Draw(spriteBatch);
    
  7. Run the game by hitting F5 and observe the star field. Exit the game by pressing Alt + F4 on the keyboard.

What just happened?

Using our StarField class is simply a matter of creating an instance of the class and then calling its Update() and Draw() methods during the game's corresponding methods. When the starField object is constructed, we pass in the size of the screen, and specify that 200 stars will be created. Each star is assigned a velocity of (0, 30f), meaning that in one second the star will move 30 pixels downward on the screen. With a screen height of 600 pixels, each star will take 20 seconds to travel from the top of the screen to the bottom.

The spriteSheet texture is specified, and the rectangle specifies an area on the textures that contains white pixels as we indicated above. The resulting sprite contains a single frame that is 2 by 2 pixels, located at (0, 450) on the sprite sheet.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.54.6