| Currently, you don't do anything with these state variables. You should implement the game-over logic first. You will force the player to hit any key to start the game. After the game is over (you've crashed), there will be a slight pause (approximately one second), and then hitting any key will start the game once again. The first thing you'll need to make sure is that while the game is over, you don't update any other state. So the very first lines in OnFrameUpdate should be // Nothing to update if the game is over if ((isGameOver) || (!hasGameStarted)) return; Now you need to handle the keystrokes to restart the game. At the very end of your OnKeyDown override, you can add this logic: if (isGameOver) { LoadDefaultGameOptions(); } // Always set isGameOver to false when a key is pressed isGameOver = false; hasGameStarted = true; This is the behavior you want. Now, when the game is over, and the player presses a key, a new game is started with the default game options. You can remove the call to LoadDefaultGameOptions from InitializeGraphics now if you want to, since it will be called as soon as you press a key to start a new game. However, you still don't have the code that will cause the slight pause after you crash. You can add that to your OnKeyDown override as well; you should do it directly after checking for Escape: // Ignore keystrokes for a second after the game is over if ((System.Environment.TickCount - gameOverTick) < 1000) { return; } This will ignore any keystrokes (except Escape to quit) for a period of one second after the game is over. Now, you can actually play the game! There's definitely something missing, though. You're maintaining a score, but there is nothing in the game that tells the user anything. You will need to rectify that. There is a Font class in the Direct3D namespace that can be used to draw some text; however, there is also a Font class in the System.Drawing namespace, and these two class names will collide if you attempt to use "Font" without fully qualifying it. Luckily, you can alias with the using statement as follows: using Direct3D = Microsoft.DirectX.Direct3D; Now each font you create can be a different color, but must be a unique size and family. You will want to draw two different types of text for this game, so you will need two different fonts. Add the following variables to the DodgerGame class: // Fonts private Direct3D.Font scoreFont = null; private Direct3D.Font gameFont = null; You will need to initialize these variables, but can't do so until the device has been created. You should do this after the device creation code. This does not need to happen in the OnDeviceReset event since these objects will automatically handle the device reset for you. Just add these lines to the end of the InitializeGraphics method: // Create our fonts scoreFont = new Direct3D.Font(device, new System.Drawing.Font("Arial", 12.0f, FontStyle.Bold)); gameFont = new Direct3D.Font(device, new System.Drawing.Font("Arial", 36.0f, FontStyle.Bold | FontStyle.Italic)); You've created two fonts of different sizes, but of the same family: Arial. Now you need to update the rendering method to draw the text. You will want the text to be drawn last, so after the car's draw method, add the following: if (hasGameStarted) { // Draw our score scoreFont.DrawText(null, string.Format("Current score: {0}", score), new Rectangle(5,5,0,0), DrawTextFormat.NoClip, Color.Yellow); } if (isGameOver) { // If the game is over, notify the player if (hasGameStarted) { gameFont.DrawText(null, "You crashed. The game is over.", new Rectangle(25,45,0,0), DrawTextFormat.NoClip, Color.Red); } if ((System.Environment.TickCount - gameOverTick) >= 1000) { // Only draw this if the game has been over more than one second gameFont.DrawText(null, "Press any key to begin.", new Rectangle(25,100,0,0), DrawTextFormat.NoClip, Color.WhiteSmoke); } } The DrawText method will be discussed in depth in a later chapter, so for now, just know that it does what its name implies. It draws text. As you can see here, once the game has been started the first time, you will always show the current score. Then, if the game is over, and it has been started for the first time already, you notify the player that they have crashed. Finally, if the game has been over for a second or longer, let the player know that pressing a key will start a new game. Wow! Now you've got a full-fledged game in the works. You can play, see your score increase, see your game end when you crash, and start over and play again. What's left? Well, it would be awesome if you could store the highest scores we've achieved so far.
Take a moment to pat yourself on the back. You've just finished your first game. |