LP2-5XNAGameClass

=XNA Game Class =

=5. XNA Game Class=



5.1. In the second project I refined all of the games main variables. Instead of having two collections for the rocks and two collections for the elements, I combined all of the code into individual rock and element classes. I had to create a separate Animation object for the ship movement because I could not combine it into the Ship class. Then there is the Quadrant class and two Boolean variables that track the game state and the main screen state.



5.2. The remaining class members record the keyboard and controller states. Then I set the text variables that I write to the screen. The Game1 constructor initializes the main objects in the game, ship lasers, rocks, and elements in addition to the quadrant class. I also have code to play the game in full screen mode.



5.3. The Initialize method has no custom code.



5.4. In the LoadContent method there are a lot of things going on. The first thing that is done is to load a background image. Next I define the viewing rectangle. Then I establish the center of the screen and initial starting location for the ship. Once I get these values I then create the ship object called objJackHammer. The next statements setup the structures for the elements and rocks. In this project the code is reduced to a few method calls to create the objects instead of having all of the code imbedded here. The last thing I do is load the screen font.



5.5. I don’t unload any content at this time. In the finished game I will probably null out all of my collection objects.



5.6. I start the Update function by adding a condition for the escape key. This adds a quick exit for testing purposes. I also look for the “P” and “F5” keys. The “P” key is used to pause the game. A feature I wanted in my last project. I thought there would be a XNA pause method to stop everything, however I found that there was no such function. I created my own functionality for it. When you press the “P” key it flips the game state variable. The update method looks at game state and if it is active it will continue to run all of the other update methods. It checks to see if the ship object is still active by checking the hit count. It then calls the ships move and rotate methods. If the ship is in a moving state it calls the animation object so the ship looks like it is moving.



5.7. After the ship movements are complete the rest of the objects are updated. The last operation performed is to check if the lasers are fired



5.8. The last method of the Game Class is the Draw method. I draw the background and then look to see if the main screen state is active. If it is I draw the message about hitting F5 and than wait for the player to hit the button. If the main screen is not active, I draw the lasers, rocks, and then the element objects by passing the sprite batch variable to each method. I then make sure that the ship is still active. If it is and it’s moving I draw the animation, if it is not moving I just draw the ship image to the screen.



5.9. After the ship is drawn, I draw all of the text for the game and the remaining ships graphic. If the ship is not active I draw the game over text.



Project Home Next Section