.NET Game Programming with DirectX 9.0 by Alexandre Santos Lobão and Ellen Hatton ISBN:1590590511 Apress © 2003 (696 pages) The authors of this text show how easy it can be to produce interesting multimedia games using Managed DirectX 9.0 and programming with Visual Basic .NET on Everett, the latest version of Microsoft's Visual Studio. Table of Contents .NET Game Programming with DirectX 9.0 Foreword Preface Introduction Chapter 1 - .Nettrix: GDI+ and Collision Detection Chapter 2 - .Netterpillars: Artificial Intelligence and Sprites Chapter 3 - Managed DirectX First Steps: Direct3D Basics and DirectX vs. GDI+ Chapter 4 - River Pla.Net: Tiled Game Fields, Scrolling, and DirectAudio Chapter 5 - River Pla.Net II: DirectInput and Writing Text to Screen Chapter 6 - Magic KindergarteN.: Adventure Games, ADO.NET, and DirectShow Chapter 7 - Magic KindergarteN. II: Animation Techniques and Speech API Chapter 8 - .Netterpillars II: Multiplayer Games and Directplay Chapter 9 - D-iNfEcT: Multithreading, Nonrectangular Windows, and Access to Nonmanaged Code Bonus Chapter Porting .Nettrix to Pocket PC Appendix A - The State of PC Gaming Appendix B - Motivations in Games Appendix C - How Do I Make Games? Appendix D - Guidelines for Developing Successful Games Index List of Figures List of Tables NEW IN .NET In Visual Basic .NET, we don't have a parameter of the form Show method anymore to say if it should be shown modally (the form is shown and execution continues after the form is closed) or modeless (the form is shown and the code continues executing); instead we have a ShowDialog method that should be used when we wish to show a form modally. In the preceding sample code, the WinConfig.Dispose() line is only executed after the form is closed by the user. Coding for the Introduction Screen Now is a good time to create an intro screen for our game. Our suggestion is shown in the Figure 2-17 , but feel free to use your artistic talent to improve it. Figure 2-17: The .Netterpillars splash screen The Main procedure must be changed to reflect the workflow diagram created in the project phase: Sub main() Dim WinSplash As frmSplash Dim WinGameField As frmGameField ' Create the game engine object objGameEngine = New clsGameEngine(WinGameField.PicGameField.Handle) WinSplash = New frmSplash() Do While WinSplash.ShowDialog() = DialogResult.OK WinGameField = New frmGameField() WinGameField.Show() Application.DoEvents() ' Shows the window immediately objGameEngine.BackgroundImage=WinGameField.PicGameField.Image.Clone Do While Not objGameEngine.GameOver objGameEngine.Render() Application.DoEvents() Loop MsgBox("Game Over ", MsgBoxStyle.Exclamation, ".Netterpillars") WinGameField.Dispose() Loop objGameEngine = Nothing WinSplash.Dispose() .NET Game Programming with DirectX 9.0 by Alexandre Santos Lobão and Ellen Hatton ISBN:1590590511 Apress © 2003 (696 pages) The authors of this text show how easy it can be to produce interesting multimedia games using Managed DirectX 9.0 and programming with Visual Basic .NET on Everett, the latest version of Microsoft's Visual Studio. Table of Contents .NET Game Programming with DirectX 9.0 Foreword Preface Introduction Chapter 1 - .Nettrix: GDI+ and Collision Detection Chapter 2 - .Netterpillars: Artificial Intelligence and Sprites Chapter 3 - Managed DirectX First Steps: Direct3D Basics and DirectX vs. GDI+ Chapter 4 - River Pla.Net: Tiled Game Fields, Scrolling, and DirectAudio Chapter 5 - River Pla.Net II: DirectInput and Writing Text to Screen Chapter 6 - Magic KindergarteN.: Adventure Games, ADO.NET, and DirectShow Chapter 7 - Magic KindergarteN. II: Animation Techniques and Speech API Chapter 8 - .Netterpillars II: Multiplayer Games and Directplay Chapter 9 - D-iNfEcT: Multithreading, Nonrectangular Windows, and Access to Nonmanaged Code Bonus Chapter Porting .Nettrix to Pocket PC Appendix A - The State of PC Gaming Appendix B - Motivations in Games Appendix C - How Do I Make Games? Appendix D - Guidelines for Developing Successful Games Index List of Figures List of Tables End Sub That's it. We can now play with different field sizes, number of mushrooms, and netterpillars. But after playing a couple of times, we'll soon discover that when we run our game a second time without making any configuration changes, our properties don't get reset; so, among other things, we'll start with the last quantity of mushrooms (that is, without the ones that were eaten). And worst of all: If the game field screen is being created for each game, our handle (passed to the objGameEngine New method) becomes invalid. Since we can't simply move the objGameEngine creation to inside the loop (we'll need it in the configuration screen, and if we re-create the object, the previous configuration will be lost), a solution is to create a new method to reset the game variables, which can be called just after the program Game Over loop. We can call this method CreateGameField , and move all the code from the New procedure to it, including the parameter that receives the window handle. We have shown these details to clarify a point: A game project, as any other project, will have problems en route. The better the project, the less unexpected the behavior in the coding phase. Nevertheless, there's no way to guarantee immediate success. Don't be ashamed to go back and correct everything if you think that it'll make your game faster, more stable, or easier to update with new features. Another detail that requires extra care is the code for setting the game field size: When we resize the game field, the game field window must be resized accordingly. We must do that in the Load event of the frmGameField window: Sub frmGameField_Load(sender As System.Object, e As System.EventArgs)_ Handles MyBase.Load PicGameField.Location = New Point(0, 0) PicGameField.Size = New Size(objGameEngine.Width * clsSprite.IMAGE_SIZE, _ objGameEngine.Height * clsSprite.IMAGE_SIZE) Me.ClientSize = PicGameField.Size End Sub With this last little adjustment, our code will work. But we don't have code for the game over yet. We'll show that next. Coding for Game Over Looking back at our game proposal, we stated that "the game is over when all the players die (computer or human ones), or when the last mushroom is eaten." Since we have a property stating whether a player is dead or not, and a property that stores the number of mushrooms (that is already reduced every time a mushroom is eaten), all we need to do is include the code in the Render procedure to test the preceding conditions and set the GameOver property to True if one of the requirements is met. Sub Render() Dim i As Integer ' Move the Netterpillars MoveNetterpillars() ' If all Netterpillars die - GameOver GameOver = True For i = 0 To NetterpillarNumber - 1 If Not objNetterpillars(i).IsDead Then GameOver = False End If .NET Game Programming with DirectX 9.0 by Alexandre Santos Lobão and Ellen Hatton ISBN:1590590511 Apress © 2003 (696 pages) The authors of this text show how easy it can be to produce interesting multimedia games using Managed DirectX 9.0 and programming with Visual Basic .NET on Everett, the latest version of Microsoft's Visual Studio. Table of Contents .NET Game Programming with DirectX 9.0 Foreword Preface Introduction Chapter 1 - .Nettrix: GDI+ and Collision Detection Chapter 2 - .Netterpillars: Artificial Intelligence and Sprites Chapter 3 - Managed DirectX First Steps: Direct3D Basics and DirectX vs. GDI+ Chapter 4 - River Pla.Net: Tiled Game Fields, Scrolling, and DirectAudio Chapter 5 - River Pla.Net II: DirectInput and Writing Text to Screen Chapter 6 - Magic KindergarteN.: Adventure Games, ADO.NET, and DirectShow Chapter 7 - Magic KindergarteN. II: Animation Techniques and Speech API Chapter 8 - .Netterpillars II: Multiplayer Games and Directplay Chapter 9 - D-iNfEcT: Multithreading, Nonrectangular Windows, and Access to Nonmanaged Code Bonus Chapter Porting .Nettrix to Pocket PC Appendix A - The State of PC Gaming Appendix B - Motivations in Games Appendix C - How Do I Make Games? Appendix D - Guidelines for Developing Successful Games Index List of Figures List of Tables Next ' If all mushrooms got eaten - Game Over If MushroomNumber = 0 Then GameOver = True End If End Sub We mustn't forget to remove the code for forcing the game to finish when the Esc key is pressed, on the keyboard event handler for the frmGameField , unless we need this behavior in our finished game. Although the code for the "game over" works fine, it can be improved if we include a screen with game statistics—such as the netterpillar's size—so players can have clearer information about how well they played. Such a screen is added in the "Adding the Final Touches" section; for now, let's alter our code to include a real computer-controlled competitor. Final Version: Coding the Netterpillars AI To finish our game, we need to code the NetterpillarAI class and make the final adjustments in the Main procedure, as shown in the next sections. The Netterpillar AI Class As we decided in the game proposal and in the game project, we only need to use a simple form of artificial intelligence. Just avoid walls and eat mushrooms if they are near, that's all. Public Class clsAINetterpillar Inherits clsGameEngine Private RandomPercent As Integer = 5 Function ChooseNetterpillarDirection(CurrentLocation As Point, _ CurrentDirection As clsSprite.enDirection) As clsSprite.enDirection Function RandomDirection(CurrentLocation As Point, _ ChoosenDirection As clsSprite.enDirection) As clsSprite.enDirection End Class Let's review what the game objects are: Protected Enum enGameObjects Mushroom = 0 Empty = 1 Branch = 2 Netterpillar = 3 End Enum Not by accident, when we defined this enumeration, did we put the game objects in ascending order of collision preference. When we check the objects around us, the lowest value is the preferred one: A mushroom is better than empty space, and both are preferable to a collision resulting in death. We can use this to our advantage, to ease the choice of the best object by checking the lowest value (with the min function) from the positions around the current position of the netterpillar's head: BestObject = Math.Min(Math.Min(Math.Min(_ arrGameField(CurrentLocation.X + 1, CurrentLocation.Y), _ .NET Game Programming with DirectX 9.0 by Alexandre Santos Lobão and Ellen Hatton ISBN:1590590511 Apress © 2003 (696 pages) The authors of this text show how easy it can be to produce interesting multimedia games using Managed DirectX 9.0 and programming with Visual Basic .NET on Everett, the latest version of Microsoft's Visual Studio. Table of Contents .NET Game Programming with DirectX 9.0 Foreword Preface Introduction Chapter 1 - .Nettrix: GDI+ and Collision Detection Chapter 2 - .Netterpillars: Artificial Intelligence and Sprites Chapter 3 - Managed DirectX First Steps: Direct3D Basics and DirectX vs. GDI+ Chapter 4 - River Pla.Net: Tiled Game Fields, Scrolling, and DirectAudio Chapter 5 - River Pla.Net II: DirectInput and Writing Text to Screen Chapter 6 - Magic KindergarteN.: Adventure Games, ADO.NET, and DirectShow Chapter 7 - Magic KindergarteN. II: Animation Techniques and Speech API Chapter 8 - .Netterpillars II: Multiplayer Games and Directplay Chapter 9 - D-iNfEcT: Multithreading, Nonrectangular Windows, and Access to Nonmanaged Code Bonus Chapter Porting .Nettrix to Pocket PC Appendix A - The State of PC Gaming Appendix B - Motivations in Games Appendix C - How Do I Make Games? Appendix D - Guidelines for Developing Successful Games Index List of Figures List of Tables arrGameField(CurrentLocation.X - 1, CurrentLocation.Y)), _ arrGameField(CurrentLocation.X, CurrentLocation.Y + 1)), _ arrGameField(CurrentLocation.X, CurrentLocation.Y - 1)) Once the best object has been chosen, we can check it against the next object in the current direction; and if they are the same (there can be two or more optimal objects), we choose to stay in the current direction, to make the netterpillar's movement less erratic. One last step is to add some random behavior to make the movement less predictable and less prone to getting stuck in an infinite loop; for example, the netterpillar could move in circles around the game field forever if there's no aleatory component. In our tests, anything greater than 10 percent randomness can lead to erratic behavior (remember, we choose a new direction many times a second); a value between 0 and 5 generates good results. Function ChooseNetterpillarDirection(CurrentLocation As Point, _ CurrentDirection As clsSprite.enDirection) As clsSprite.enDirection Dim BestObject As enGameObjects Dim NextObject As enGameObjects Select Case CurrentDirection Case clsSprite.enDirection.East NextObject = arrGameField(CurrentLocation.X + 1, CurrentLocation.Y) Case clsSprite.enDirection.West NextObject = arrGameField(CurrentLocation.X - 1, CurrentLocation.Y) Case clsSprite.enDirection.South NextObject = arrGameField(CurrentLocation.X, CurrentLocation.Y + 1) Case clsSprite.enDirection.North NextObject = arrGameField(CurrentLocation.X, CurrentLocation.Y - 1) End Select ' Pick the lowest value - Mushroom or empty BestObject = Math.Min(Math.Min(Math.Min(_ arrGameField(CurrentLocation.X + 1, CurrentLocation.Y), _ arrGameField(CurrentLocation.X - 1, CurrentLocation.Y)), _ arrGameField(CurrentLocation.X, CurrentLocation.Y + 1)), _ arrGameField(CurrentLocation.X, CurrentLocation.Y - 1)) ' If the current direction is equal the best direction, ' stay in current direction If NextObject = BestObject Then ChooseNetterpillarDirection = CurrentDirection Else ' Select the direction of the best object Select Case BestObject Case arrGameField(CurrentLocation.X + 1, CurrentLocation.Y) ChooseNetterpillarDirection = clsSprite.enDirection.East Case arrGameField(CurrentLocation.X - 1, CurrentLocation.Y) ChooseNetterpillarDirection = clsSprite.enDirection.West Case arrGameField(CurrentLocation.X, CurrentLocation.Y + 1) ChooseNetterpillarDirection = clsSprite.enDirection.South Case arrGameField(CurrentLocation.X, CurrentLocation.Y - 1) ChooseNetterpillarDirection = clsSprite.enDirection.North End Select End If .NET Game Programming with DirectX 9.0 by Alexandre Santos Lobão and Ellen Hatton ISBN:1590590511 Apress © 2003 (696 pages) The authors of this text show how easy it can be to produce interesting multimedia games using Managed DirectX 9.0 and programming with Visual Basic .NET on Everett, the latest version of Microsoft's Visual Studio. Table of Contents .NET Game Programming with DirectX 9.0 Foreword Preface Introduction Chapter 1 - .Nettrix: GDI+ and Collision Detection Chapter 2 - .Netterpillars: Artificial Intelligence and Sprites Chapter 3 - Managed DirectX First Steps: Direct3D Basics and DirectX vs. GDI+ Chapter 4 - River Pla.Net: Tiled Game Fields, Scrolling, and DirectAudio Chapter 5 - River Pla.Net II: DirectInput and Writing Text to Screen Chapter 6 - Magic KindergarteN.: Adventure Games, ADO.NET, and DirectShow Chapter 7 - Magic KindergarteN. II: Animation Techniques and Speech API Chapter 8 - .Netterpillars II: Multiplayer Games and Directplay Chapter 9 - D-iNfEcT: Multithreading, Nonrectangular Windows, and Access to Nonmanaged Code Bonus Chapter Porting .Nettrix to Pocket PC Appendix A - The State of PC Gaming Appendix B - Motivations in Games Appendix C - How Do I Make Games? Appendix D - Guidelines for Developing Successful Games Index List of Figures List of Tables ChooseNetterpillarDirection = RandomDirection(CurrentLocation, _ ChooseNetterpillarDirection) End Function To code the RandomDirection method, called in the last line of the preceding code, we'll simply pick a random number from 0 to 100, and if it's less than the RandomPercent property, choose a new movement direction for the netterpillar. The next code sample presents the full code for this method: Function RandomDirection(CurrentLocation As Point, ChoosenDirection _ As clsSprite.enDirection) As clsSprite.enDirection Dim x As Integer = Rnd(1) * 100 RandomDirection = ChoosenDirection If x < RandomPercent Then Select Case ChoosenDirection Case clsSprite.enDirection.East ' Try the other directions If arrGameField(CurrentLocation.X, CurrentLocation.Y + 1) <= _ enGameObjects.Empty Then RandomDirection = clsSprite.enDirection.South ElseIf arrGameField(CurrentLocation.X, CurrentLocation.Y - 1) <= _ enGameObjects.Empty Then RandomDirection = clsSprite.enDirection.North ElseIf arrGameField(CurrentLocation.X - 1, CurrentLocation.Y) <= _ enGameObjects.Empty Then RandomDirection = clsSprite.enDirection.West End If Case clsSprite.enDirection.West ' Try the other directions If arrGameField(CurrentLocation.X, CurrentLocation.Y + 1) <= _ enGameObjects.Empty Then RandomDirection = clsSprite.enDirection.South ElseIf arrGameField(CurrentLocation.X, CurrentLocation.Y - 1) <= _ enGameObjects.Empty Then RandomDirection = clsSprite.enDirection.North ElseIf arrGameField(CurrentLocation.X + 1, CurrentLocation.Y) <= _ enGameObjects.Empty Then RandomDirection = clsSprite.enDirection.East End If Case clsSprite.enDirection.North ' Try the other directions If arrGameField(CurrentLocation.X, CurrentLocation.Y + 1) <=_ enGameObjects.Empty Then RandomDirection = clsSprite.enDirection.South ElseIf arrGameField(CurrentLocation.X + 1, CurrentLocation.Y) <= _ enGameObjects.Empty Then RandomDirection = clsSprite.enDirection.East ElseIf arrGameField(CurrentLocation.X - 1, CurrentLocation.Y) <=_ enGameObjects.Empty Then RandomDirection = clsSprite.enDirection.West End If Case clsSprite.enDirection.South ' Try the other directions If arrGameField(CurrentLocation.X, CurrentLocation.Y - 1) <= _ .NET Game Programming with DirectX 9.0 by Alexandre Santos Lobão and Ellen Hatton ISBN:1590590511 Apress © 2003 (696 pages) The authors of this text show how easy it can be to produce interesting multimedia games using Managed DirectX 9.0 and programming with Visual Basic .NET on Everett, the latest version of Microsoft's Visual Studio. Table of Contents .NET Game Programming with DirectX 9.0 Foreword Preface Introduction Chapter 1 - .Nettrix: GDI+ and Collision Detection Chapter 2 - .Netterpillars: Artificial Intelligence and Sprites Chapter 3 - Managed DirectX First Steps: Direct3D Basics and DirectX vs. GDI+ Chapter 4 - River Pla.Net: Tiled Game Fields, Scrolling, and DirectAudio Chapter 5 - River Pla.Net II: DirectInput and Writing Text to Screen Chapter 6 - Magic KindergarteN.: Adventure Games, ADO.NET, and DirectShow Chapter 7 - Magic KindergarteN. II: Animation Techniques and Speech API Chapter 8 - .Netterpillars II: Multiplayer Games and Directplay Chapter 9 - D-iNfEcT: Multithreading, Nonrectangular Windows, and Access to Nonmanaged Code Bonus Chapter Porting .Nettrix to Pocket PC Appendix A - The State of PC Gaming Appendix B - Motivations in Games Appendix C - How Do I Make Games? Appendix D - Guidelines for Developing Successful Games Index List of Figures List of Tables enGameObjects.Empty Then RandomDirection = clsSprite.enDirection.North ElseIf arrGameField(CurrentLocation.X + 1, CurrentLocation.Y) <= _ enGameObjects.Empty Then RandomDirection = clsSprite.enDirection.East ElseIf arrGameField(CurrentLocation.X - 1, CurrentLocation.Y) <= _ enGameObjects.Empty Then RandomDirection = clsSprite.enDirection.West End If End Select End If End Function Since the code in the clsGameEngine is intended to take care of the game's physics (for example, it moves the netterpillars, regardless of whether one is changing direction), we'll have to put the code for moving the netterpillars based on the AI outside the game engine object; our Main procedure is the best option. Another valid approach would be to include the AI code inside the Netterpillar object—it's just a matter of choice: a small number of bigger classes or many smaller ones. The Main Program: Final Version In order to call the AI code, we create a new procedure, which will be called from the game main loop. The procedure, shown in the following code, just loops through the Netterpillars objects and, if they aren't dead and if they are computer controlled, sets the current direction to the result of the ChooseNetterpillarDirection method. Private objAINetterpillar As New clsAINetterpillar() Sub MoveComputerCharacters() Dim i As Integer ' Move the Netterpillars For i = 0 To objGameEngine.NetterpillarNumber - 1 If Not objGameEngine.objNetterpillars(i).IsDead Then ' A.I. for the computer-controled Netterpillars If objGameEngine.objNetterpillars(i).IsComputer Then objGameEngine.objNetterpillars(i).Direction = _ objAINetterpillar.ChooseNetterpillarDirection(_ objGameEngine.objNetterpillars(i).Location, _ objGameEngine.objNetterpillars(i).Direction) End If End If Next End Sub The main program loop should include one more section to call the MoveComputerCharacters procedure. Do While Not objGameEngine.GameOver MoveComputerCharacters() objGameEngine.Render() Application.DoEvents() Loop This finishes our coding phase; some code to add polish to the final product is suggested in the next section . .NET Game Programming with DirectX 9.0 by Alexandre Santos Lobão and Ellen Hatton ISBN:1590590511 Apress © 2003 (696 pages) The authors of this text show how easy it can be to produce interesting multimedia games using Managed DirectX 9.0 and programming with Visual Basic .NET on Everett, the latest version of Microsoft's Visual Studio. Table of Contents .NET Game Programming with DirectX 9.0 Foreword Preface Introduction Chapter 1 - .Nettrix: GDI+ and Collision Detection Chapter 2 - .Netterpillars: Artificial Intelligence and Sprites Chapter 3 - Managed DirectX First Steps: Direct3D Basics and DirectX vs. GDI+ Chapter 4 - River Pla.Net: Tiled Game Fields, Scrolling, and DirectAudio Chapter 5 - River Pla.Net II: DirectInput and Writing Text to Screen Chapter 6 - Magic KindergarteN.: Adventure Games, ADO.NET, and DirectShow Chapter 7 - Magic KindergarteN. II: Animation Techniques and Speech API Chapter 8 - .Netterpillars II: Multiplayer Games and Directplay Chapter 9 - D-iNfEcT: Multithreading, Nonrectangular Windows, and Access to Nonmanaged Code Bonus Chapter Porting .Nettrix to Pocket PC Appendix A - The State of PC Gaming Appendix B - Motivations in Games Appendix C - How Do I Make Games? Appendix D - Guidelines for Developing Successful Games Index List of Figures List of Tables .NET Game Programming with DirectX 9.0 by Alexandre Santos Lobão and Ellen Hatton ISBN:1590590511 Apress © 2003 (696 pages) The authors of this text show how easy it can be to produce interesting multimedia games using Managed DirectX 9.0 and programming with Visual Basic .NET on Everett, the latest version of Microsoft's Visual Studio. Table of Contents .NET Game Programming with DirectX 9.0 Foreword Preface Introduction Chapter 1 - .Nettrix: GDI+ and Collision Detection Chapter 2 - .Netterpillars: Artificial Intelligence and Sprites Chapter 3 - Managed DirectX First Steps: Direct3D Basics and DirectX vs. GDI+ Chapter 4 - River Pla.Net: Tiled Game Fields, Scrolling, and DirectAudio Chapter 5 - River Pla.Net II: DirectInput and Writing Text to Screen Chapter 6 - Magic KindergarteN.: Adventure Games, ADO.NET, and DirectShow Chapter 7 - Magic KindergarteN. II: Animation Techniques and Speech API Chapter 8 - .Netterpillars II: Multiplayer Games and Directplay Chapter 9 - D-iNfEcT: Multithreading, Nonrectangular Windows, and Access to Nonmanaged Code Bonus Chapter Porting .Nettrix to Pocket PC Appendix A - The State of PC Gaming Appendix B - Motivations in Games Appendix C - How Do I Make Games? Appendix D - Guidelines for Developing Successful Games Index List of Figures List of Tables Adding the Final Touches In this section we add some extra features to our game. These final touches, although simple, are important and need to be considered. Coding the Pause Game Feature As in the .Nettrix game, we could insert code to pause (and restart) the game when the Esc key is pressed. This basic improvement is shown here: Private Sub frmGameField_KeyDown(sender As Object, _ e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown . . . Case Keys.Escape objGameEngine.Paused = Not objGameEngine.Paused If objGameEngine.Paused Then Me.Text = ".Netterpillars - Press ESC to continue" Else Me.Text = ".Netterpillars" End If End Select End Sub Improving the Game Over Screen Our game over routine also needs an improvement. A good game programmer should not forget that a good game ending is far more important that a nice intro screen. Players must be rewarded for all their efforts in completing the game; it's very frustrating for players to spend days and days finishing a game and not getting anything in return to give them a feeling of accomplishment. In our game, the "Game Over" message box is one of these frustrations. Although a high scores table would be better, let's at least give players some feedback about the results of the game and how well they played. We can do this by creating a new Game Over window, where we can display some game statistics, as shown in Figure 2-18 . .NET Game Programming with DirectX 9.0 by Alexandre Santos Lobão and Ellen Hatton ISBN:1590590511 Apress © 2003 (696 pages) The authors of this text show how easy it can be to produce interesting multimedia games using Managed DirectX 9.0 and programming with Visual Basic .NET on Everett, the latest version of Microsoft's Visual Studio. Table of Contents .NET Game Programming with DirectX 9.0 Foreword Preface Introduction Chapter 1 - .Nettrix: GDI+ and Collision Detection Chapter 2 - .Netterpillars: Artificial Intelligence and Sprites Chapter 3 - Managed DirectX First Steps: Direct3D Basics and DirectX vs. GDI+ Chapter 4 - River Pla.Net: Tiled Game Fields, Scrolling, and DirectAudio Chapter 5 - River Pla.Net II: DirectInput and Writing Text to Screen Chapter 6 - Magic KindergarteN.: Adventure Games, ADO.NET, and DirectShow Chapter 7 - Magic KindergarteN. II: Animation Techniques and Speech API Chapter 8 - .Netterpillars II: Multiplayer Games and Directplay Chapter 9 - D-iNfEcT: Multithreading, Nonrectangular Windows, and Access to Nonmanaged Code Bonus Chapter Porting .Nettrix to Pocket PC Appendix A - The State of PC Gaming Appendix B - Motivations in Games Appendix C - How Do I Make Games? Appendix D - Guidelines for Developing Successful Games Index List of Figures List of Tables Figure 2-18: A Game Over screen This screen can access the objGameEngine , which is a public variable, and gather information about players and how long their netterpillars were when the game finished. To load the label with the statistics, we must access each of the objNetterpillars objects, checking the IsComputer property and the NetterBodyLength property. We'll need to avoid unset objects (remember, the player could be playing with any number of opponents, from 0 to 3). The IIF commands in the next code sample (which must be placed on the Load event of the window) aren't new to .NET, although they aren't commonly used because sometimes they can lead to more complex code. The IIF command tests the first parameter (an expression) and, if true, returns the second parameter; otherwise it returns the last parameter. LblPlayer1Length.Text= _ objGameEngine.objNetterpillars(0).NetterBodyLength LblPlayer1Is.Text= _ IIf(objGameEngine.objNetterpillars(0).IsComputer, "Computer", "Human") If Not objGameEngine.objNetterpillars(1) Is Nothing Then LblPlayer2Length.Text = _ objGameEngine.objNetterpillars(1).NetterBodyLength LblPlayer2Is.Text = _ IIf(objGameEngine.objNetterpillars(1).IsComputer, "Computer", "Human") Else LblPlayer2Length.Text = "-" LblPlayer2Is.Text = "-" End If If Not objGameEngine.objNetterpillars(2) Is Nothing Then LblPlayer3Length.Text = _ objGameEngine.objNetterpillars(2).NetterBodyLength LblPlayer3Is.Text = _ IIf(objGameEngine.objNetterpillars(2).IsComputer, "Computer", "Human") Else LblPlayer3Length.Text = "-" LblPlayer3Is.Text = "-" End If If Not objGameEngine.objNetterpillars(3) Is Nothing Then LblPlayer4Length.Text=_ objGameEngine.objNetterpillars(3).NetterBodyLength LblPlayer4Is.Text= _ IIf(objGameEngine.objNetterpillars(3).IsComputer, "Computer", "Human") Else LblPlayer4Length.Text = "-" LblPlayer4Is.Text = "-" End If In final version of the main program, we must replace the "Game Over" message box by a call to the ShowDialog method of the game over form. Coding for the Garbage Collection A technical enhancement is to improve the speed of the garbage collection calling the Collect method of the System.GC object, in the end of the Render method, as shown: .NET Game Programming with DirectX 9.0 by Alexandre Santos Lobão and Ellen Hatton ISBN:1590590511 Apress © 2003 (696 pages) The authors of this text show how easy it can be to produce interesting multimedia games using Managed DirectX 9.0 and programming with Visual Basic .NET on Everett, the latest version of Microsoft's Visual Studio. Table of Contents .NET Game Programming with DirectX 9.0 Foreword Preface Introduction Chapter 1 - .Nettrix: GDI+ and Collision Detection Chapter 2 - .Netterpillars: Artificial Intelligence and Sprites Chapter 3 - Managed DirectX First Steps: Direct3D Basics and DirectX vs. GDI+ Chapter 4 - River Pla.Net: Tiled Game Fields, Scrolling, and DirectAudio Chapter 5 - River Pla.Net II: DirectInput and Writing Text to Screen Chapter 6 - Magic KindergarteN.: Adventure Games, ADO.NET, and DirectShow Chapter 7 - Magic KindergarteN. II: Animation Techniques and Speech API Chapter 8 - .Netterpillars II: Multiplayer Games and Directplay Chapter 9 - D-iNfEcT: Multithreading, Nonrectangular Windows, and Access to Nonmanaged Code Bonus Chapter Porting .Nettrix to Pocket PC Appendix A - The State of PC Gaming Appendix B - Motivations in Games Appendix C - How Do I Make Games? Appendix D - Guidelines for Developing Successful Games Index List of Figures List of Tables Sub Render() System.GC.Collect() End Sub NEW IN .NET The .NET Framework provides an advanced garbage collector that frees the memory from all objects left behind by the program. The garbage collection takes place in idle system time, but we can force it to run by calling the Collect method, which is good practice if we are dealing with lots of memory allocations and reallocations—which we do, for example, with the Graphics object in each Draw method in the game objects. Further Improvements We saved the best for last: What about creating new intelligent characters for our game, maybe some opposition—like a spider who eats the netterpillars? In the code for this chapter on the samples CD-ROM, you will find an almost fully working spider character. We already did all the dirty work: the Spider and AISpider class interfaces, the call to the moving functions at the MoveComputerCharacters routine and at the Render and Redraw method of the objGameEngine —almost everything is there. The code for ChooseDirection method of the AISpider class is empty, so our spiders aren't going anywhere. This gives you the opportunity to create the AI from scratch, without worrying about the details. Will the spider attack the netterpillars heads and kill them? Or will they just eat part of their tails? Or, maybe make new mushrooms grow? Start making your proposal for the second version of the game, and enjoy! [...]... Successful Games Index List of Figures List of Tables Chapter NETManaged DirectX First Steps: Direct3D 3: Game Programming with DirectX 9.0 ISBN:1590590511 by Alexandre Santos Lobão and Ellen Hatton Basics and DirectX vs GDI+ Apress © 20 03 (696 pages) The authors of this text show how easy it can be to produce Highlights interesting multimedia games using Managed DirectX 9.0 and programming with Visual Basic. .. using Managed analysis programming with Visual Basic NET on Everett, the latest Basic concepts about artificial intelligence, and ideas about how to implement it to solve different version of Microsoft' s Visual Studio challenges when programming games Table of Contents The difference between game AI and game physics NET Game Programming with DirectX 9.0 How Foreword to create a basic objects library... games using Managed DirectX 9.0 and programming with Visual Basic NET on Everett, the latest version of Microsoft' s Visual Studio Table of Contents NET Game Programming with DirectX 9.0 Foreword Preface Introduction Chapter 1 - Nettrix: GDI+ and Collision Detection Chapter 2 - Netterpillars: Artificial Intelligence and Sprites Chapter 3 - Managed DirectX First Steps: Direct3D Basics and DirectX vs GDI+... Figure 3- 3 show a graphical representation of the perspective projection NET Game Programming with DirectX 9.0 by Alexandre Santos Lobão and Ellen Hatton Apress © 20 03 (696 pages) ISBN:1590590511 The authors of this text show how easy it can be to produce interesting multimedia games using Managed DirectX 9.0 and programming with Visual Basic NET on Everett, the latest version of Microsoft' s Visual. .. matrix Figure 3- 7 presents the same operation applied to the first vertex .NET Game Programming with DirectX 9.0 by Alexandre Santos Lobão and Ellen Hatton Apress © 20 03 (696 pages) ISBN:1590590511 The authors of this text show how easy it can be to produce interesting multimedia games using Managed DirectX 9.0 and programming with Visual Basic NET on Everett, the latest version of Microsoft' s Visual Studio... effects Figure 3- 9 presents a set of vertices rendered as a point list .NET Game Programming with DirectX 9.0 by Alexandre Santos Lobão and Ellen Hatton Apress © 20 03 (696 pages) ISBN:1590590511 The authors of this text show how easy it can be to produce interesting multimedia games using Managed DirectX 9.0 and programming with Visual Basic NET on Everett, the latest version of Microsoft' s Visual Studio... triangles Figure 3- 12 showsISBN:1590590511 triangle list primitive type to the use of the Hatton render vertices Apress © 20 03 (696 pages) The authors of this text show how easy it can be to produce interesting multimedia games using Managed DirectX 9.0 and programming with Visual Basic NET on Everett, the latest version of Microsoft' s Visual Studio Table of Contents NET Game Programming with DirectX 9.0... object interesting multimedia games using Managed DirectX 9.0 and according to the values set to all vertices In Figure 3- 16 we see three vertexes with valid tu and tv values, programming with Visual Basic NET on Everett, the latest the texture loaded, and the Microsoft' s Visual by the device version of result rendered Studio Table of Contents NET Game Programming with DirectX 9.0 Foreword Preface Introduction... make the20 03 (696 pages)automatically, shaking around The matrix transformations control Apress © figure move window is shown on Figure 3- 19 The authors of this text show how easy it can be to produce interesting multimedia games using Managed DirectX 9.0 and programming with Visual Basic NET on Everett, the latest version of Microsoft' s Visual Studio Table of Contents NET Game Programming with DirectX. .. multimedia games in your computer Since this interface is installed with DirectX 9.0, have the managed DirectX interface installed using Managed DirectX 9.0 and programming with Visual latest version from the Microsoft you'll need to install it by downloading theBasic NET on Everett, the latest site version of Microsoft' s Visual Studio First Step: Coding the Main Window Table of Contents NET Game Programming . multimedia games using Managed DirectX 9.0 and programming with Visual Basic .NET on Everett, the latest version of Microsoft& apos;s Visual Studio. Table of Contents .NET Game Programming with DirectX. multimedia games using Managed DirectX 9.0 and programming with Visual Basic .NET on Everett, the latest version of Microsoft& apos;s Visual Studio. Table of Contents .NET Game Programming with DirectX. multimedia games using Managed DirectX 9.0 and programming with Visual Basic .NET on Everett, the latest version of Microsoft& apos;s Visual Studio. Table of Contents .NET Game Programming with DirectX