microsoft visual basic game programming with directx phần 2 potx

57 381 0
microsoft visual basic game programming with directx phần 2 potx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

.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 Public Shared Function CheckLines() As Int16 Public Shared Function StopSquare(Square As ClsSquare, _ x As Integer, y As Integer) As Boolean Public Shared Function Redraw() As Boolean End Class The GameField interface shown in the preceding code has its members (properties and methods) defined in the class diagram proposed in the game project, plus the new properties and methods defined in the stubs we created previously. Then again, although it isn't unusual that such changes really happen in a real-life project, it's one of our goals to define a clear and comprehensive project before starting to code. Remember, changing a project is far easier (and cheaper) than changing and adapting code; and that if there are many unpredictable changes to code, the project tends to be more prone to errors and more difficult to maintain. (We refer to this as the "Frankenstein syndrome": the project will be no longer a single and organized piece of code, but many not so well-sewed-on parts.) One interesting point about this class is that every member is declared as shared! In other words, we can access any method or property of the class without creating any objects. This isn't the suggested use of shared properties or methods; we usually create shared class members when we need to create many objects in the class, and have some information-such as a counter for the number of objects created, or properties that, once set, affect all the objects created. The next sections discuss the GameField class methods, starting with the IsEmpty method. The IsEmpty Method The first class method, IsEmpty , must check if a given x,y position of the game array ( arrGameField ) is empty. The next method, CheckLines , has to check each of the lines of the array to see if any one of them is full of squares, and remove any such lines. Since the arrGameField is an array of Square objects, we can check if any position is assigned to a square with a simple test: Public Shared Function IsEmpty(x As Integer, y As Integer) _ As Boolean If arrGameField(x,y) is nothing IsEmpty = True Else IsEmpty = False End if End Function Some extra tests should be done to see if the x or the y position is above (or below) the array boundaries. Although in this game we don't need high-speed calculations, we can use an improved algorithm for collision detection, so that we can see a practical example of using these algorithms. We can improve the performance of the IsEmpty and CheckLines functions using an array of bits to calculate the collisions. Since our game field is 16 squares wide, we can create a new array of integers, where each bit must be set if there's a square associated with it. We still must maintain the arrGameField array, because it will be used to redraw the squares when a line is erased or the entire game field must be redrawn (for example, when the window gets the focus after being belowother window). The array that holds the bits for each line must have the same Height as the arrGameField , and will have just one dimension, since the Width will be given for the bits in each integer (16 bits per element). The array definition is shown in the next code line: .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 Private Shared arrBitGameField(Height) As Integer And the IsEmpty function is as follows: Public Shared Function IsEmpty(x As Integer, y As Integer) _ As Boolean IsEmpty = True ' If the Y or X is beyond the game field, return false If (y < 0 Or y >= Height) Or (x < 0 Or x >= Width) Then IsEmpty = False ' Test the Xth bit of the Yth line of the game field ElseIf arrBitGameField(y) And (2 ^ x) Then IsEmpty = False End If End Function In this sample code, the first if statement checks whether the x and y parameters are inside the game field range. The second if deserves a closer look: What is arrBitGameField(y) And (2 ^ x) supposed to test? In simple words, it just checks the xth bit of the arrBitGameField(y) byte. This piece of code works well because the comparison operators of Visual Basic, since earlier versions, work in a binary way. The AND operator then performs a bit-to-bit comparison, and returns a combination of both operands. If the same bit is set in both operands, this bit will be set in the result; if only one or none of the operators have the bit set, the result won't have the bit set. In Table 1-4 we show the operands' bits for some AND comparisons. Table 1-4: Bits and Results for Some AND Operations NUMBERS BITS 1 AND 2 = 0 01 AND 10 = 0 (false) 3 AND 12 = 0 0011 AND 1100 = 0000 (false) 3 AND 11 = 3 0011 AND 1011 = 0011 (true) In our code, if we want to check, for example, the 7th bit, the first operand must be the array element we want to check, arrBitGameField(Y) , and the second operand must have the bits 00000000 01000000 (16 bits total, with the 7th one checked). If we did our binary homework well, we'd remember that setting the bits one by one results in powers of 2: 1, 2, 4, 8, 16, and so on, for 00001, 00010, 00100, 01000, 10000, etc. The easiest way to calculate powers of 2 is just to shift the bits to the left; but since we have no bit shift operators in Visual Basic, we need to use the power operator (^). Looking again at the second if statement, everything should make sense now: arrBitGameField(y) : The 16 bits of the yth line of the game field. 2 ^ x : Calculates a number with only one bit set-the xth one. arrBitGameField(y) And (2 ^ x) : If the xth bit of the array element is set, then the test will return a nonzero number; any other bit set won't affect the result, since the second operand has only the xth bit set. The CheckLines method will use this same bit array to more easily check if a line is filled with squares, as we'll discuss next. .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 The CheckLines method In the next GameField method, CheckLines , we need to check if a line is totally filled (all bits set) and, if so, erase this line and move down all the lines above it. We don't need to copy the empty lines (all bits reset) one on top of another, but we must return the number of cleared lines. To improve the readability of our code, we'll define some private constants for the class: Private Const bitEmpty As Integer = &H0& '00000000 0000000 Private Const bitFull As Integer = &HFFFF& '11111111 1111111 See the comments in the code and the following explanation to understand the function: Public Shared Function CheckLines() As Integer Dim y As Integer, x As Integer Dim i As Integer CheckLines = 0 y = Height - 1 Do While y >= 0 ' stops the loop when the blank lines are reached If arrBitGameField(y) = bitEmpty Then y = 0 ' If all the bits of the line are set, then increment the ' counter to clear the line and move all above lines down If arrBitGameField(y) = bitFull Then ' Same as: If (arrBitGameField(y) Xor bitFull) = 0 Then CheckLines += 1 ' Move all next lines down For i = y To 0 Step -1 ' if the current line is NOT the first of the game field, ' copy the line above If i > 0 Then ' Copy the bits from the line above arrBitGameField(i) = arrBitGameField(i - 1) ' Copy each of the squares from the line above For x = 0 To Width - 1 ' Copy the square arrGameField(x, i) = arrGameField(x, i - 1) ' update the Location property of the square Try With arrGameField(x, i) .location = New Point(.location.X, _ .location.Y + SquareSize) End With Catch ' Ignore the error if arrGameField(x, y) is Nothing End Try Next Else ' if the current line is the first of the game field ' just clear the line arrBitGameField(i) = bitEmpty For x = 0 To Width - 1 arrGameField(x, i) = Nothing Next .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 If Next Else y -= 1 End If Loop End Function In the preceding code sample, two points need more explanation: the structured error handling and the bit checking logic. NEW IN .NET .NET introduced in Visual Basic structured error handling, an old item on the wish list of programmers. Structured error handling is composed of three blocks- Try , Catch , and Finally - as shown in the code sample that follows. The Catch block receives a variable that is filled with information about the exception, and could be used to do proper error handling. Try ' Statements that could cause an error Catch e As Exception ' Code to be executed only if an error occurred in the Try block Finally ' Code to de executed in any situation, after the Try block End Try In the CheckLines method, we use structured error handling in order to avoid an extra test when copying squares. Since we can copy a square or an empty variable, we should test to see if the Square variable is set before setting the Location property, to avoid errors. Using error handling, the error is just ignored, with no collateral effects. The test we avoid is this one: If arrBitGameField(y - 1) And (2 ^ x) Then ' Same as: If arrGameField(x, y - 1) = Nothing then With arrGameField(x, y) .location = New Point(.location.X,.location.Y + squaresize) end with end if In the CheckLines method we can see the real benefits of creating arrBitGameField for collision detection: We can check if a line is completely filled or empty with only one test, with the use of bitFull and bitEmpty constants we previously created, avoiding the 16 tests we would have had to create for each of the ArrGameField members in a line. The next code listing highlights these tests: If arrBitGameField(y) = bitFull Then ' The line is full If arrBitGameField(y) = bitEmpty Then ' The line is empty The next section discusses the last two methods for the GameField class. The StopSquare and Redraw Methods The last two methods, StopSquare (which sets the arrays when a block stops falling) and Redraw (which redraws the entire game field), have no surprises. The code implementing these methods is shown in the next listing: .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 Public Shared Function StopSquare(Square As ClsSquare, _ x As Integer, y As Integer) As Boolean arrBitGameField(y) = arrBitGameField(y) Or (2 ^ x) arrGameField(x, y) = Square End Function Public Shared Function Redraw() As Boolean Dim x As Integer, y As Integer ' at first, clear the game field picGameField.Invalidate() Application.DoEvents() ' Draws all the squares until reaching the empty lines y = Height - 1 Do While y >= 0 And arrBitGameField(y) <> bitEmpty For x = Width - 1 To 0 Step -1 Try arrGameField(x, y).Show(picGameField) Catch ' there's no square do draw do nothing End Try Next Loop End Function NEW IN .NET In Visual Basic .NET, the graphic routines went through a major transformation, making them much closer to the underlying graphics API , the DLLs for graphics operations. One example is shown in the preceding code: The previous CLS method is now called Invalidate , and can be used to invalidate the whole Image object (and then force it to be redrawn) or receive a specific rectangular structure, which tells us exactly which part of the image must be redrawn. Those who worked with graphics manipulation in the C language will be comfortable with this new notation as it's the same in both languages. Another interesting point is that many functions from the earlier versions of Visual Basic are organized into objects and methods.We can see the math functions compiled as methods into the Math object, or, in the preceding sample, system functions like DoEvents organized as methods of the Application object. This is another useful group of functions that are organized as methods from the System object. The next section shows the code for the final version of the main program, finishing our game code. The Game Engine Now that all the base classes are coded, let's finish the main procedures. In the first drafts for the game engine, we used the form procedures to call methods in our base classes, so we could see if they were working well. Now, the game engine must be coded to implement the features defined in the game proposal, stated earlier in this chapter. Let's remind ourselves of the pseudo-code defined in the game project: Form_Load Creates an object (named currentBlock) of block class Form_KeyPress .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 If Left Arrow was pressed, call Left method of currentBlock If Right Arrow was pressed, call Right method of currentBlock If Up Arrow was pressed, call Rotate method of currentBlock If Down Arrow was pressed, call Down method of currentBlock Timer_Tick If there is no block below currentBlock, and the currentBlock didn't reach the bottom of the screen then Call the Down method of currentBlock Else Stop the block If it's at the top of the screen then The game is over If we filled any horizontal lines then Increase the game score Erase the line Create a new block at the top of the screen Before starting to translate this pseudo-code to actual Visual Basic code, it's important to stress two points: It's not common to use timer objects to control games. The timer object doesn't have the necessary precision or accuracy (we can't trust it entirely when dealing with time frames less than 10 milliseconds). But for games like .Nettrix, the levels of accuracy and precision available with the timer are adequate (remember that we are trying to make the production of this game as simple as possible). In the next chapter , we'll see a GDI+ application that runs at full speed, without using a timer. It's not common in game programming to put the game engine code in a form. Usually we create a GameEngine class that deals with all the game physics and rules (as we'll see in the next chapter ). Looking back at the pseudo-code, we see the following instruction: If it's at the top of the screen then This tests if the block is at the top of the screen. Reviewing our Block class, we see that we have no direct way to retrieve the block Top position, so we would have to test each of the Top positions of the block's composing squares. To solve this, let's make a final adjustment to the Block class, including a new method, as depicted in the next code listing: Public Function Top() As Integer Top = Math.Min(square1.location.Y, _ Math.Min(square2.location.Y, _ Math.Min(square3.location.Y, square4.location.Y))) End Function Now we are ready to finish our program. Based on the preceding pseudo-code and on some minor changes made in the game coding phase, the code for the form will be as follows: Dim CurrentBlock As clsBlock Dim blnRedraw As Boolean Private Sub tmrGameClock_Tick(sender As System.Object, e As System.EventArgs) Handles tmrGameClock.Tick Static stillProcessing As Boolean = False Dim ErasedLines As Integer .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 ' Prevents the code from running if the previous tick ' is still being processed If stillProcessing Then Exit Sub stillProcessing = True ' Controls the block falling If Not CurrentBlock.Down() Then ' Test for game over If CurrentBlock.Top = 0 Then tmrGameClock.Enabled = False CmdStart.Enabled = True MessageBox.Show("GAME OVER", ".Nettrix", _ MessageBoxButtons.OK, MessageBoxIcon.Stop) Exit Sub End If ' increase the score using the number of deleted lines, if any ErasedLines = ClsGameField.CheckLines() lblScoreValue.Text += 100 * ErasedLines ' Clear the game field If ErasedLines > 0 Then PicBackground.Invalidate() Application.DoEvents() ClsGameField.Redraw() End If ' Releases the current block from memory CurrentBlock = Nothing ' Creates the new current block CurrentBlock = New clsBlock(_ New Point(ClsGameField.SquareSize * 6, 0)) CurrentBlock.Show(PicBackground) End If stillProcessing = False End Sub Compare the preceding code listing with the previous pseudo-code to make sure each line of code has been understood. The Load event for the form, and the KeyDown event and the code for the Start button remain unchanged. The final version of .Nettrix has now been coded. When the game is run, it looks like the screen shown in Figure 1-32 . .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 1-32: The final version of .Nettrix We can now play our own homemade clone of Tetris, and are ready to improve it, with the changes discussed 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 Adding the Final Touches After playing the first version of .Nettrix for a few minutes, every player will miss two important features present in almost every Tetris type of game: a feature to show the next block that will appear, and some way to pause the game, for emergency situations (like your boss crossing the office and heading in your direction). Now that we have all base classes already finished, this is easily done. The next sections discuss these and some other features to improve our first game. Coding the Next Block Feature To show the next block, we can create a new pictureBox on the form, to hold the next block image, and adjust the click of the Start button and the timer_tick event. We can use the optional parameter we created on the Block constructor (the New method) to create the new blocks following the block type of the next block. To implement this feature, we'll create a variable to hold the next block in the general section of the form: Dim NextBlock As clsBlock At the end of the cmdStart_click event, we'll add two lines to create the next block: NextBlock = New clsBlock(New Point(20, 10)) NextBlock.Show(PicNextBlock.Handle) And finally we'll adjust the Tick event of the timer, to create a new block every time the current block stops falling, and to force the CurrentBlock type to be the same as the NextBlock type. ' Releases the current block from memory CurrentBlock = Nothing ' Creates the new current block CurrentBlock = New clsBlock(New Point(ClsGameField.SquareSize * 6, 0),_ NextBlock.BlockType) CurrentBlock.Show(PicBackground.Handle) ' Releases the next block from memory NextBlock.Hide(PicNextBlock.Handle) NextBlock = Nothing ' Creates the new next block NextBlock = New clsBlock(New Point(20, 10)) NextBlock.Show(PicNextBlock.Handle) We can now run the game, and see the next block being displayed in the picture box we've just created, as shown in Figure 1-33 . .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 1-33: Showing the next block The next section shows another improvement, the game pause feature. Coding the Game Pause Feature To create a pause function, all we need to do is to stop the timer when a specific key is pressed-usually, the Esc key is used for such features. A simple adjustment in the KeyDown event, including an extra case clause for the Keys.Escape value, will do the trick: Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown Select Case e.KeyCode Case Keys.Right CurrentBlock.Right() Case Keys.Left CurrentBlock.Left() Case Keys.Up CurrentBlock.Rotate() Case Keys.Down CurrentBlock.Down() Case Keys.Escape tmrGameClock.Enabled = Not tmrGameClock.Enabled If tmrGameClock.Enabled Then Me.Text = ".Nettrix" Else Me.Text = ".Nettrix - Press ESC to continue" End If End Select End Sub In the next section we'll discuss an improvement to the graphical part of our game. Coding the Window Redraw [...]... Sprite class 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 Table of Contents NET Game Programming with DirectX 9.0 Foreword... on Visual Basic NET interesting multimedia games using Managed DirectX 9.0 and programming with Visual Basic NET on Everett, the latest Basic concepts about collision detectionStudio version of Microsoft' s Visual and some suggestions on how to implement fast collision algorithms in our games Table of Contents Creation of simple classes and structured error handling in Visual Basic NET NET Game Programming. .. introductory screen on Figure 2- 10 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... ISBN:1590590511 2: Game Programming with Artificial Intelligence and by Alexandre Santos Lobão and Ellen Sprites Hatton © 20 03 (696 pages) Apress 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 the concepts of Studio In this chapter we'll exploreMicrosoft's Visual artificial... of the AI and your game play it While the AI decides what towith Visual Basic NET the Everett, the latest version of Microsoft' s Visual Studio Some examples will make this distinction clearer: Classic pinball games have no AI, only physics Table of Contents NET Game Programming with DirectX 9.0players can't build a new residential block over a river, it's the game In the SimCity game series, when Foreword... with different sizes by NET Game Programming with DirectX 9.0 simply adjusting the SquareSize constant and recompiling the code 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... 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 Table of Contents NET Game Programming with DirectX 9.0 Figure 2- 7: The class diagram—second draft Foreword Preface As for the properties (attributes) and methods, we... ClsGameField.SquareSize + 12 Me.LblNextBlock.Left = ClsGameField.Width * ClsGameField.SquareSize + 12 Me.lblScore.Left = ClsGameField.Width * ClsGameField.SquareSize + 12 Me.lblScoreValue.Left = ClsGameField.Width * ClsGameField.SquareSize + 12 Me.CmdStart.Left = ClsGameField.Width * ClsGameField.SquareSize + 12 We are adjusting neither the font size nor the button sizes, so to work with smaller sizes, some updating of... graphBack = Graphics.FromHwnd(WinHandle) 9.0 and interesting multimedia games using Managed DirectX graphBack.DrawImageUnscaled(BmpSource, Location.X * Scale, _ programming with Visual Basic NET on Everett, the latest version of Microsoft' s Visual Studio Location.Y * Scale) graphBack.Dispose() End Sub Table of Contents NET Game Programming with DirectX 9.0 Sub UnDraw(WinHandle As System.IntPtr) Dim graphBack... Guidelines for Developing Successful Games Index List of Figures List of Tables .NET Game Programming with DirectX 9.0 The Game Proposal by Alexandre Santos Lobão and Ellen ISBN:1590590511 Hatton When creating games, remember that the very first step is to write a clearly defined game proposal This Apress © 20 03 (696 the game creation process can understand and agree with the game ensures that everyone involved . 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

Ngày đăng: 12/08/2014, 20:22

Từ khóa liên quan

Mục lục

  • Adding the Final Touches

  • Summary

  • Chapter 2: .Netterpillars: Artificial Intelligence and Sprites

    • Object-Oriented Programming

    • Artificial Intelligence

    • Sprites and Performance Boosting Tricks

    • The Game Proposal

    • The Game Project

    • The Coding Phase

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan