.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
List of Tables
Figure 1-17:
Dividing a screen into 64 zones
If all we want to know is whether a certain zone contains an object (disregarding which one), we can use
bytes (instead of arrays) to store the zone information, where each bit will represent a zone on screen; this
is called
zoning
with bits
. We can divide our screen in zones according to the number of bits on each
variable used: 64 (8 × 8) zones with a
byte
, 256 (16 × 16) zones in an
int16
, 1024 (32 × 32) zones in an
int32
, and so on.
Using the zoning with bits method, at each game loop we reset the variables and, for each object, we
process any movement. We then calculate the zone of each object (multiply the current position of the
object by the number of zones on each axis and divide by the width or height of the screen), and set the bit
corresponding to the result at the x-axis variable and at the y-axis variable, accordingly. We have to set a
second bit if the sum of the position and the size of the object (width for x axis, height for y axis) lies in
another zone.
If when checking the variables we see that the bit in both variables is already set, then there's an object in
our zone, so we check all the objects to find out which one it is. Using this method, if we have 15 objects
on the screen, and only one collision, we'll have to do only one check against a given number of objects
(14 in the worst case of this scenario), instead of 15 tests with 14 objects. This method has some
drawbacks:
We don't know which object set the bit, so we have to test all the objects looking for the collision.
Some "ghost objects" are created when crossing the bit set for the x zone by one object with the bit set
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
Figure 1-19:
Using zone arrays, we can keep track of which objects are in each zone. The legend
.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
Guidelines for Developing Successful Games
Index
List of Figures
List of Tables
shows the bit set in each array element, for each object.
.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
How Do I Make Games?
Appendix D
-
Guidelines for Developing Successful Games
Index
List of Figures
List of Tables
Extending the Algorithms to Add a Third Dimension
There are many advanced algorithms for 3-D collisions described on game-related sites all over the
Internet. We'll not stress the many implications on including a z axis in the collision detection algorithms;
instead we'll just add some simple extensions to the preceding algorithms.
Extending the bounding box algorithm is very straightforward, as shown here:
If object1.x <= object2.x + object2.width and _
object1.y <= object2.y + object2.height and _
object1.z <= object2.z + object2.depth) or
(object2.x <= object1.x + object1.width and _
object2.y <= object1.y + object1.height and_
object2.z <= object1.z + object1.depth) then
' => The 3-D boxes are overlapping
Else
' => The 3-D boxes don't collide!!!
end if
As for the proximity algorithms, the extension is just as easy. This code sample depicts a proximity test with
cube-like objects:
Distance = math.max(math.abs(Object1.CenterX - Object2.CenterX), _
math.abs(Object1.CenterY - Object2.CenterY))
Distance = math.max(Distance,
math.abs(Object1.CenterX - Object2.CenterX))
If Distance < Object1.width + Object2.width then
' => The cube objects are overlapping
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
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
-
The main purpose for creating a game proposal is to have clear objectives stated; and everyone
involved in the game creation must agree on every point.
For our project we can summarize the scope in a list of desired features, as shown here:
Our game will be a puzzle game, and it'll be called .Nettrix.
The main objective of the game is to control falling blocks and try to create full horizontal lines, while
not allowing the block pile to reach the top of the game field.
The blocks will be made out of four squares (in every possible arrangement) that fall down in the
game field, until they reach the bottom of the field or a previously fallen block.
When the blocks are falling, the player can move the blocks horizontally and rotate them.
When a block stops falling, we'll check to see if there are continuous horizontal lines of squares in the
game field. Every continuous line must be removed.
The player gets 100 points per removed line, multiplied by the current level. After every couple of
minutes, the blocks must start falling faster, and the level number should be increased.
If the stack of blocks grows until it's touching the top of the game field, the game ends.
This list contains many definitions that are important for any game proposal:
The game genre (puzzle)
The main objective of the game
The actions the player can perform (e.g., to shoot and to get objects)
Details about how the player interacts with the game and vice-versa: keyboard, intuitive interface,
force-feedback joystick, etc.
How the player is rewarded for his or her efforts (points, extra lives, etc.)
How the player gets promoted from one level to the next (in this case, just a time frame)
The criteria for ending the game
Note
In more sophisticated games, there may be other considerations, such as the storyline, the
game flow, details about the level design or level of detail for the maps or textured surfaces, the
difficulty levels for the game, or even details on how the artificial intelligence (AI) of the game
should work.
.NET Game Programming with DirectX 9.0
by Alexandre Santos Lobão and Ellen
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-20
) illustrating the basic structures of the objects
for our game, and then we can add the details and go on refining until we have a complete version. Almost
all of the object-oriented analysis methodologies suggest this cyclic approach, and it's ideal to show how
the game idea evolves from draft to a fully featured project.
Figure 1-20:
The class diagram-first draft
From our game proposal we can see the first two classes:
Block
, which will represent each game piece,
and
Square
, the basic component of the blocks.
Reviewing our game proposal, we can think about some methods (functions) and properties (variables) for
the
Block
class, as described in
Table 1-1
.
Table 1-1:
The Block Class Members
.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
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
TYPE NAME
DESCRIPTION
Method
Down
Makes the block go down on the screen
Method
Right
Draws the square on the screen at its coordinates (
Location
property) and with its size (
Size
property), colored with a specific
color (
ForeColor
property) and filled with
BackColor
Method
Hide
Erases the square from the screen
Property
ForeColor
The square border color
Property
BackColor
The square inside color (fill color)
Property
Location
The x,y position of the square on the screen
Property
Size
The height and width of the square
Comparing the two tables, we can see that there are methods to show and hide the square. Because the
squares will be drawn from the
Block
object, we must have corresponding methods in the
Block
class,