XNA GameStudio 2.0
Creating A Beginning & End
We have sucessfully programmed the main game loop for this game. Kicking off, scoring goals and keeping track of the score have all been implemented in our project. At the moment, people would have to play the game and stop when they have had enough. It makes sense for us to think about a more natural end to each game. For example, the first player to reach 10 goals might be the winner of the game.
The first thing to do is to make a field to store the number of goals that are required for a win in our game. Place the following field declaration in the list of fields that we already have in the main Game class.
int numWins = 1;
Now find the code in the Update() method where we increased the players scores following a goal. Below the line score2 += 1;, add the following,
if (score2 == numWins)
gamestate = 3;
The code required for the other player's score is very similar,
if (score1 == numWins)
gamestate = 3;
Notice that we have added another number to the gamestate field to represent the Game Over state - the period of time between the end of one game and the start of the next.
When the game is over, we need to display a message on screen that tells the players. We do this in the Draw() method of the main game class.
if (gamestate == 3)
spriteBatch.DrawString(gamefont, "GAME OVER - PRESS ENTER", new Vector2(200.0f, 250.0f), Color.Black);
Finally, we need to make it possible for the players to start a new game. We do this in the HandleInput() method.
if (gamestate == 3 && kbState.IsKeyDown(Keys.Enter))
score1 = score2 = 0;
gamestate = 1;
Here we reset both scores to zero and change the gamestate back to 1 - waiting for player1 to kick off.
When you test the work that you have done from this web page, you may wish to set the number of wins required for a victory to a lower number - that way you won't have to wait too long to see if it works. Change it back to a decent number afterwards, natch.