Pages: 82
Rating : ⭐⭐⭐⭐⭐
Price: $10.99
Page 1 Preview
refer chapter river pla tiled game fields

Refer chapter river pla tiled game fields

.NET Game Programming

Technical Reviewer: David Jung
Editorial Directors: Dan Appleman, Gary Cornell, Simon Hayes, Karen Watterson, John Zukowski Managing Editor: Grace Wong
Project Manager: Sofia Marchant
Copy Editor: Ami Knox
Production Manager: Kari Brooks
Compositor: Diana Van Winkle, Van Winkle Design Group
Artist and Cover Designer: Kurt Krames
Indexer: Lynn Armstrong
Manufacturing Manager: Tom Debolski
Distributed to the book trade in the United States by Springer-Verlag New York, Inc., 175 Fifth Avenue, New York, NY, 10010 and outside the United States by Springer-Verlag GmbH & Co. KG, Tiergartenstr. 17, 69112 Heidelberg, Germany.

In the United States, phone 1-800-SPRINGER, email, or visit


IN THIS CHAPTER we’ll apply the
concepts learned in the pre-
vious chapter about Direct3D
to implement DirectX gaming
classes (such as GameEngine
and Sprite), so we’ll easily be
able to create high-speed
graphics games. We’ll also
introduce basic DirectAudio
concepts that will allow us to
include sound effects and
background music in our

We’ll also examine the
concept of tiled game fields


Chapter 4

All scrolling games are either vertical scrollers, horizontal scrollers, or full scrollers, meaning that the background on these games scroll in a vertical direction, in a horizontal direction, or in any direction. We’ll discuss some variations of these movements in this section.

The most common choice is to implement vertical “up-down” scrollers (as does the sample game for this chapter), where the background moves from the top to the bottom of the screen, and horizontal “right-left” scrollers, where the back-ground moves from right to left. We don’t see many scrolling games using the opposite direction schemes because using these directions makes our games seem more natural to players.


River Pla.Net: Tiled Game Fields, Scrolling, and DirectAudio

Some games use a similar solution: A player walks freely in a restricted area, and when he or she gets near any border, the background starts to move until the player starts walking back toward the center of the screen.

Some other games use a combination of automatic scrolling with player-controlled scrolling; the player controls scrolling right or left, but is always moving from the top to the bottom of the screen.


Chapter 4

Tile-Based Games

A tile is just a small piece of a graphic with a certain property that reveals its status for the game (a background, an enemy, an obstacle, a ladder, etc.). Creating a tiled game field is simply a matter of putting the tiles together in a logical fashion. We can do this by creating a level-map file with a level designer or even with a text editor; our game, when running, translates the tile codes in the file to graphical tiles on screen.


Figure 4-2. A basic set of tiles, comprising two terrain types

To create border tiles, we must separate the tiles into groups that will have con-nections with each other, and then create the borders for the tiles in each group. We must do this because usually some tiles won’t need to have borders with some of the others—for example, the tiles that will create internal parts of a building don’t need to have any special border with the outside tiles.

Within every group, create the border tiles between each type of terrain. There are basically three types of borders we can create, as shown in Figure 4-3:

Chapter 4

Half-to-half tiles: With this kind of tile, each type of terrain occupies half of the tile; the transition between terrain types can be on the vertical, hori-zontal, or diagonal axis.

Include Extra Transition Tiles

For those transitions that will be presented most of the time to the player, include some different tiles for each transition and for the basic set, which will be used sparingly to break down the feeling of patterns of repetition. For example, when creating tiles between water and land, include some rocks, a bay, or a larger beach, so you can use them eventually to give more variation to the game visual.

River Pla.Net: Tiled Game Fields, Scrolling, and DirectAudio

To create a better set of tiles, test if the transitions for each tile are seamless in every direction (when we rotate the tiles). An improved game engine can use the same tiles with various rotations to achieve better results. An easy way to do this is to create some tiles with only borders (and a flat color at the middle), and use them as “masks” over other tiles, employing any graphical editor to hide the tran-sitions between the base tiles and the masks. Ensuring that the border pixels are always the same will allow smooth transitions.

Creating New Game Classes

Looking at the similarities amongst the test programs we did in Chapter 3, we can choose some parts of the code to create DirectX versions for the two basic game classes we created in Chapter 2: a GameEngine class, which will be responsible for initializing, terminating, and managing the device operations, and a Sprite class, which will create some vertices and load the images as textures (transparent or otherwise) from image files.

We’ll also extend our game class library by creating a GameMusic class according to the basic concepts we’ll examine when studying the DirectAudio interface.

The GameEngine Class

Looking again at the samples in Chapter 3, we can see a repetitive pattern in every Direct3D application. This gives us some clues about possible methods to include in our new GameEngine class:

1. Initialize the various Direct3D objects.

River Pla.Net: Tiled Game Fields, Scrolling, and DirectAudio

Dim MyGameEngine as clsGameEngine

The suggested interface for the GameEngine class is shown in Figure 4-6; when creating new games, we can improve the class as needed.

Table 4-1. Interface Members of the DirectX GameEngine Class

The Device object, used by all graphical operations.
Property BackgroundColor


The width of the game field.

Property ScreenWinHandle The window handle used by all drawing functions.


If true, the game is paused. This flag and the

Constant FVF_CustomVertex The constant that will define which flexible vertex
Constants IMAGE_PATH and

files are stored.




This method will dispose any objects created in the



River Pla.Net: Tiled Game Fields, Scrolling, and DirectAudio

' defines the default background color as black
Public BackgroundColor As Color = Color.FromArgb(255, 0, 0, 0) ' Images path, to be used by the child classes
Protected Const IMAGE_PATH As String = "Images"

Public Structure CUSTOMVERTEX
Public X As Single
Public Y As Single
Public Z As Single
Public tu As Single
Public tv As Single
End Structure


Chapter 4

Now is a good time to look at the scope keywords used before variable and method declarations, and used extensively in the GameEngine class:

• Private: Visible only inside the class

• Overrideable: This keyword indicates that a class member can be overridden by derived classes. In the preceding sample code, the Render procedure must be an overrideable function, since the code for it will be supplied by the derived classes, although it will be called by the Run method in the base class.

• Overrides: This keyword indicates that the class member is overriding a cor-responding member of the base class. For example, to code a working Finalize event for any Visual Basic .NET class, we need to override the base class event Finalize.

Coding the Class Methods

There are no new concepts in these methods, so we can simply copy the code from one of the samples in the previous chapter and organize it as methods of the GameEngine class. As previously explained, we have an Initialize method to do the initialization (as we saw in Chapter 3) for a full-screen application using an orthogonal view. The Finalize method disposes of the objects created, and the Run method has the rendering loop, used in all programs in Chapter 3, that calls the empty Render method for each loop interaction. The Render method will be coded in the derived class, which will include specific features for each game.

Public Function Initialize(Owner as Windows.Forms.Control) As Boolean Dim WinHandle As IntPtr = Owner.handle
Dim objDirect3Dpp As PresentParameters
Initialize = True

DispMode = Manager.Adapters(_
Manager.Adapters.Default.Adapter).CurrentDisplayMode DispMode.Width = 640
DispMode.Height = 480
' Define the presentation parameters
objDirect3Dpp = New PresentParameters()
objDirect3Dpp.BackBufferFormat = DispMode.Format
objDirect3Dpp.BackBufferWidth = DispMode.Width
objDirect3Dpp.BackBufferHeight = DispMode.Height
objDirect3Dpp.SwapEffect = SwapEffect.Discard

' Set the Projection Matrix to use a orthogonal view
objDirect3DDevice.Transform.Projection = Matrix.OrthoOffCenterLH(0,_ DispMode.Width, 0, DispMode.Height, 0.0F, 0.0F)
Catch de As DirectXException
MessageBox.Show("Could not initialize Direct3D. Error: " & _
de.ErrorString, "3D Initialization.", MessageBoxButtons.OK, _ MessageBoxIcon.Error)
Initialize = False
End Try

' Dispose the used objects
DispMode = Nothing
objDirect3Dpp = Nothing
End Function

In the next section we’ll see the upgraded code for the second game class of our library: the Sprite class.

The Sprite Class

Because a sprite is drawn over a polygon, we’ll need a property to store the vertex buffer and a helper function to create the flexible vertices. Because a sprite is a 2-D image, there’s no need to store z values for the transformations.

The complete interface for a Direct3D sprite is shown in Figure 4-7.

Table 4-2. Interface Members for the DirectX Sprite Class


SizeX and SizeY

The size of the sprite, in the x and y axes.


transparent; such a color is used only when loading

the textures.



The default size for a square sprite.


IMAGE_SIZE. Useful for creating tiled game fields.

Properties SpeedX and SpeedY

The speed (translation increment per frame) of the

Properties TranslationX and
ScaleX and ScaleY

The scale to be applied to the sprite in each axis.


RotationX and



The vertex buffer with the vertices of the sprite.


creates the vertices used to draw the image on the



buffer used by the sprite.

Method CreateFlexVertex

Helper method used when creating the sprite vertex

You are viewing 1/3rd of the document.Purchase the document to get full access instantly

Immediately available after payment
Both online and downloadable
No strings attached
How It Works
Login account
Login Your Account
Place in cart
Add to Cart
send in the money
Make payment
Document download
Download File

Uploaded by : Laura Salazar

PageId: ELIE327F01