Quick Links |
| Home |
| Part I: Game Design |
| Part II: Designing the Form |
| Part III: Starting the Code |
Tutorial #1: Creating Traffic Jam Part IV: Coding the Puzzle |
|||||||||||||
Before actually designing the part where the user gets to move pieces around, we must first create the structures for the puzzle. We will do this by creating a new class for the puzzle which will do everything itself. This class will store the information for the puzzle, save a puzzle, load a puzzle, check and track movement in a puzzle, and draw the puzzle. The puzzle class will be pretty self-sustaining. To create this class, right click on your project in the Solution Explorer window on the right hand side of the screen. Go to New->Class. A window will open asking for the name of the class, type "Puzzle" into the box and press OK. Visual Basic will present you with a new code window. We will be entering stuff here, but we will do it a piece at a time. Start with...
This enumeration is to track how difficult the puzzle is. It will be unused during this lesson, but will be useful in futire lessons after we have added code to load and save puzzles and need to allow the user to choose a puzzle. What you will slowly come to realize is that this class is far from complete, but we will add as we go along. Next, we get the following:
These are two more enumerations whose purpose is probbly clear. In Traffic Jam, there are two types of vehicles, cars and trucks. A car will occupy two spaces of the board, and a truck will occupy three. The second enumeration tells which direction the vehicle runs. Next up is a structure which will store the data for each vehicle.
This structure should be pretty self-explanatory. It stores and X and Y position, which will represent the vehicle's position on the board. The rows and columns are numbered from 0 to 5, as you will see soon. The VType variable stores which kind of a vehicle it is and refers to the above enumeration.The Dir variable stores a direction from the above enumeration. This structure will change over the course of the development of this game for reasons that will be explained shortly. Next up, some variable declarations:
Here, we have a constant that declares the number of vehicles and some variables to store data concerning the vehicles. Notice the use of a collection to store the vehicles. A collection will allow the number of vehicles to grow and shrink without wasting memory. At a later time, this will change, but for now, it works well for us. Notice also that the red car is given its own variable. This makes it easier for us to track and will make checking for the victory conditions easier. The board array will store which vehicle is at a given position. Vehicles 1 to 15 represent the other vehicles while 16 represents the red car. This will be used in the next tutorial to check for collisions as we move the cars. Next, a couple of routines:
These two pieces of code are somewhat self-explanatory, so we will move on.
That is a lengthy piece of code that allows us to add a piece to the board. This will allow us to add any piece except for the red car. Notice that it will also allow any number of cars and trucks, when, in the final version, we will only be allowed to have four trucks. (Enjoy the extras for now, or just modify the code when we are finished to allow for more!) I have commented this code while writing it, so hopefully it explains what is happening. Basically, it checks to see which direction the vehicle goes and what type it is. Then, it checks to make sure that there is no collision with another vehicle. If there is, it is not allowed to be added. If not, then the vehicle is added to the board and the collection.
This code does the same thing, but only for the red car. Notice that there is no check for type or direction as these are automatic.
Finally, some meat to the functions. This one draws the cars and trucks onto a graphics item that is passed in. Notice that, for now, the program merely draws filled rectangles to represent the cars. (Welcome to programmer art!) This will, hopefully, be changed by the time we finish the program. I will not spend a lot of time on this function as it will change drastically once we get the real art going. The difference in art is also the main reason why I have said that several things will change.
This function merely tells Draw what color to make each rectangle. For now, I only need the first seven and the red car. Feel free to add more colors if you want to, but once we get to real art, this will go away. Okay, that's it for the Puzzle class part of the code. Now, we have to add it to our form. First, at the top of the code, just under "Class frmMain" add:
This will declare and initialize a puzzle for the form. We will keep reusing this puzzle throughout the game. Then, you need to add the following code somewhere within the module:
All that this function does is to add some cars and the red car to the puzzle. If you run now, though, you will still see nothing. We need to tell the form to have the puzzle draw itself, so add the following below all of the "DrawLine"s in the paint event.
This will tell the form to draw the cars after it draws all of the lines. Now, run the program and you should see...
Each of those rectangles represents a different vehicle. The red one is the target and must be moved until it is at the right edge of the board.
Well, that is all for this portion. We have actually accomplished quite a bit. We have developed the internal means of storing our puzzle, provided a means by which we can add the vehicles to the puzzle, and written code to draw the puzzle. Next time, let's write code to move things around and check if it is solved! See you then...
|
|||||||||||||
| Part III: Starting the Code | Part V: Moving the Pieces | ||||||||||||
| Last edited on 3/4/2008. | |||||||||||||