The objective of the game will be to hit a moving target with a projectile fired from a shooter!
Requirements:
Visual C++ 2008 express
The "Dark GDK" add-in for VC++ 08
_____________________________
First Steps: Create the sprites
You will need to create the sprites that will be used in the program, I am going to use sprites without animations for this tutorial.
First I will design the background of my game, the background image can be anything from a simple colored canvas to a 3D world!
Ok, somehow I managed to come up with this beautiful creation!!! (not really!)



Number of downloads: 23053
Next, the target

Number of downloads: 12344
the shooter

Number of downloads: 13079
and last but not least, the projectile!

Number of downloads: 10612
_____________________________________
Now that we have our sprites ready, we are ready to start on the coding part of our game!
1. Start VC++ 08
2. Click "File->New->Project"
3. Under wizards, select "Dark GDK - GAME"
4. Set the name & storage location
5. Click Ok.
1. Delete all the code in "main.cpp".
2. Insert the following starting code into the main.cpp file:
#include "DarkGDK.h" //Required Header File void DarkGDK ( void )//The Main Funtion for the Dark GDK { // in this application a backdrop is loaded and then several // sprites are loaded, the sprites are targets! dbSyncOn ( ); //Turn on Sync dbSyncRate ( 100 ); // Set Sync Rate for screen: // The lower it is, the faster the game runs but the visual effects will be bad. // The higher the setting - the opposite will occour! dbDisableEscapeKey ( ); //disable the "escape" key dbRandomize ( dbTimer ( ) ); //use the clock to obtian random numbers more effieciently dbLoadImage ( "backdrop.bmp", 1 ); // this will load our background onto the game dbSprite ( 1, 0, 0, 1 ); // sprite used for the background dbSetImageColorKey ( 255, 0, 255 );// Set the transparency color of the sprites, in this case, it will be bright pink while ( LoopGDK () ) { if ( dbEscapeKey ( ) ) // if the ecape key is pressed, the program will exit break; dbSync ( );//update the screen contents } // close the program // // delete all the sprites for ( int i = 1; i < 30; i++ ) dbDeleteSprite ( i ); // delete the backdrop image dbDeleteImage ( 1 ); return;//return back to windows }
Now, we have a simple game going! No I mean like REALLY Simple!
So let's add in the code that loads and displays the target and the shooter:
between dbSetImageColorKey ( 255, 0, 255 ); & while ( LoopGDK () ) Insert this code:
int i = 1, T = 2, S = 3, P = 4; dbLoadImage("target.bmp", T);//load the target dbSprite ( T, 250, 0, T );//Display the target
Now the target will be displayed at the top of the screen!
Insert the shooter:
before the while ( LoopGDK () ) line, Insert this code:
i++; dbLoadImage("shooter.bmp", S);//Load the shooter dbSprite ( S, 200, 460, S );//Display the Shooter
This will load the shooter onto the screen when the game is started!
After that, we are ready to create the part of the game that controls the animation and movement of the target!
Let's add the variables to our project:
beforewhile ( LoopGDK() )
add this:
//begin the animation loop// dbLoadImage("PROJ.bmp", P);//load the Projectile sprite int D = 0; int R = 1; int M = 200; int AC = 50; int BC = 4;
this code will load the projectile sprite and prepare the variables
To make the target move back and forth, add this code after the LoopGDK() statement:
//START the animation of the target// int DIF = 5;//The difficulty of the game, *Make sure this number is either 2, 5, 10, or 50!; if(R == 1) { dbSprite(T,D, 1,T); D=D+DIF; if(D==650) { R = 0; } } else if(R == 0) { dbSprite(T,D,1,T); D=D-DIF; if(D == 0) { R = 1; } } //END target animation//
Now, if you run the game, the target will bounce back and forth at the top of the screen!
We want the user to be able to move the shooter back and forth at the bottom of the screen. After //END target animation// add:
//START user input controls// if( dbRightKey() && !(M > 625)) { dbSprite(S, M, 460, S); M = M + 2; } if( dbLeftKey() && !(M <= 0)) { dbSprite(S, M, 460, S); M = M - 2; }
Then, after that, add the control that will enable the user to "Fire" the projectile:
if( dbSpaceKey()) { if(AC == 50) { dbSprite(P, M, 425, P); AC = 0; } } if(AC < 50)//If the projectile is active, { dbMoveSprite(P, 10);//then make it move AC++; } //END User input controls//
This will make the program fire the projectile when the [SPACE] bar is pressed. The seconde part (
if(AC < 50)//If the projectile is active, { dbMoveSprite(P, 10);//then make it move AC++; }) will make the projectile move up untill it hits the top of the screen
Now that that's done, The game is almost done, if you run it, you should be able to move the shooter and fire the projectile. Also, the target will move from back to forth.
Finally, we will add the part of the game where, if the projectile hits the target, the game will complete and exit:
After //END User input controls//
Add this code:
//START Target hit/missed controls// if(AC < 50) { if((dbSpriteY(P) >= 0 && dbSpriteY(P) <= 10)) { if((dbSpriteX(P) <= (dbSpriteX(T) + 20) && (dbSpriteX(P) >= (dbSpriteX(T) - 20)))) { dbDeleteSprite(T); MessageBox(NULL, "You Won!, Click OK to exit.", "Congrats!", MB_OK); break; } } } //END Target hit/missed controls//
Now, the game should be fully functional! You should be able to fire at the target and, if you hit it, the game will display a message box, and exit!
We are now pretty much done, there are just a few things that can be edited: The first thing is the difficulty level, do you see in the code int DIF = 5;//The difficulty of the game, *Make sure this number is either 1, 2, 5, 10, or 50!;
This code controls how fast the target moves, by default, it is set to 5, the higher the number, the harder it is to hit the target. *Remember: If you change the difficulty, it MUST be set to either: 1, 2, 5, 10, or 50, or the game will not work properly!
The second thing is the screen sync rate: dbSyncRate ( 100 ); , I have it set to 100, if this number is lower, the actions of the game will be slower, if higher, the actions of the game will work faster, but the game will require more system resources. In this game, there is not much to worry about, since it is not that complex, but in a very complex 3D game, higher sync rates will greaty affect the system resources required.
And that's about it, enjoy your new game!
-AJ32
