Game Development of .Nettrix: GDI+ and Collision Detection - The Game Engine
(Page 10 of 22 )
Using the C# events jargon, you can think about coding three main events to implement the behaviors described at the game proposal:
- When the form loads, you can create the first block.
- At the form KeyPress event, you can handle the keyboard input from the user.
- With a timer you can call the Down method at each clock tick, producing the desired falling effect for the blocks. As you’ll see later, using a timer isn’t a recommended practice when creating games that need to run at full speed, but that’s not the case here.
Writing pseudo-code is helpful for validating the class diagram, checking whether you use every method and property, and determining whether you can achieve the results stated in the game proposal with those class members. The pseudo-code for your game is shown in the following code sample:
Form_Load Creates an object (named currentBlock) of block class
You’ll use the currentBlock object in all other events, so it must have the same scope as the form.
Form_KeyPress
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
In the previous pseudo-code, you use the up arrow key to rotate the block and the down arrow key to force the block to go down faster, while the right arrow key and left arrow key move the block in the horizontal direction.
The game engine core will be the timer event. Reviewing the game proposal, you probably see what you must do here: Make the block fall, stop it according to the game rules, check to see if there are any full horizontal lines, and check for the game being over. Possible pseudo-code to do this is shown in the following sample:
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
Analyzing this code, you may see some features your current class diagram doesn’t take into account. For instance, how can you check if there is no block below the current block? How can you erase the horizontal line you just managed to fill? We’ll discuss these points in the next section.
The Class Diagram: Final Version In order to check the previous block positions to see if there are any blocks below the current block or if there are any filled lines, you must have a way to store and check each of the squares of the block, independently of the original blocks (remember, when you erase a line, you can erase just a square or two from a given block). You can do this by creating a new class representing the game field, which will store the information for all squares and have some methods that allow line erasing, among other features. With a quick brainstorm, you can add this class to your model, which will evolve into the diagram shown in Figure 1-22.

Figure 1-22. The final class diagram
Table 1-3 lists the methods and properties of the new class, along with a short description for each one.
Table 1-3. The Game Field Class Members
| TYPE | NAME | DESCRIPTION |
|---|
| Properties | Width and Height | Represents the width and height of the game field, measured in squares. |
| | |
| Property | SquareSize | Indicates the size of each square, so you can translate pixels to squares. |
| | |
| Property | ArrGameField | Constitutes an array to store all the squares from all the blocks that stopped falling. |
| | |
| Method | CheckLines | Checks if there are any complete horizontal lines, erasing them if so, and returns the number of erased lines so the main program can increase the player’s score. |
| Method | IsEmpty | Checks if the square at a particular location (a given x and y) is empty, therefore telling you when a block is in motion. |
| Method | Redraw | Forces the full redraw of the game field. This will be used when a line has been erased or when another window has overlapped yours. |
In a real project, you would possibly go beyond this point, refining all methods to include their interfaces (received parameters and return values) and specifying the data types for the properties, which would probably lead to another revision of your class diagram. But we’ve given you the basic idea here, and that’s the main point.
This chapter is from Beginning .NET Game Programming in C#, by David Weller, et al., (Apress, 2004, ISBN: 1590593197). Check it out at your favorite bookstore today.
Buy this book now. |
Next: The Coding Phase >>
More .NET Articles
More By Apress Publishing