Showing posts with label DarkGDK. Show all posts
Showing posts with label DarkGDK. Show all posts

Animating 3d models in DarkGDK using dbAppendObject

Been watching 3d Animation tutorials lately but I’ve never really found a tutorial or detailed example for that matter that actually teaches how to animate 3d models in DarkGDK in a less unclear way. And so I’m sharing this example in hope that it will be used by some computer enthusiast out there like myself as a starting point in creating their very own 3d computer game.

I have used the Babe model in this example. Babe model is a Dark Matter Model that is accesible by default when you install DarkGDK and is located at C:\Program Files\The Game Creators\Dark GDK\Media\Dark Matter Models\People. The controls and actions of our model in this example are illustrated in the following table.

ControlsActions
W,A,S,DMove.x
TabDie.x
QImpact.x
No key PressedIdle.x


Enough said, here’s the code:


// Dark GDK - The Game Creators - www.thegamecreators.com
// the wizard has created a very simple project that uses Dark GDK
// it contains the basic code for a GDK application
// whenever using Dark GDK you must ensure you include the header file
#include "DarkGDK.h"
// the main entry point for the application is this function
void DarkGDK ( void )
{
// turn on sync rate and set maximum rate to 30 fps
 dbSyncOn();
 dbSyncRate(30);

 //loads our 3d model
 //we'll be borrowing the Babe DirectX Darkmatter model
 //it's included in the DarkGdk package located in
 //C:\Program Files\The Game Creators\Dark GDK\Media\Dark Matter Models\People\Babe
 //Just copy the contents of the Babe folder to your game project folder
 dbLoadObject("H-Babe-Idle.x",2);
 //Idle.x has 25 frames
 //to find how how many frames there is in a .x animation
 //Click Start>All Programs>The Game Creators>DarkGDK>Documentation>Information>Type "Frame" 
 //in the Textbox and the command used to determine the number of frames will reveal itself to you.
 //appends the number of frames of the Move.x animation to the loaded model
 //Move.x has 25 frames
 dbAppendObject("H-Babe-Move.x",2,26);
 //Attack.x has 24 frames
 //adds the number of frames of Attack.x to the loaded model
 dbAppendObject("H-Babe-Attack1.x",2,51);
 //Die.x has 50 frames
 //adds the number of frames of Die.x to the loaded model
 dbAppendObject("H-Babe-Die.x",2,76);
 //Impact.x has 10 frames
 //adds the number of frames of Impact.x to the loaded model
 dbAppendObject("H-Babe-Impact.x",2,126);
 //positions the object in the -y axis cause its a little bit top aligned by default when loaded
 dbPositionObject(2,0,-1,0);


 while (LoopGDK())
 {
 //display descriptive text
 dbSetTextSize(14);
 dbText(10,0,"WASD-Movement");
 dbText(10,10,"Spacebar-Attack.x");
 dbText(10,20,"Tab-Die.x");
 dbText(10,30,"Q-Impact.x");
 dbText(10,40,"No key pressed-Idle.x");
  

 
   //if the W or A or S or D or Spacebar or Tab or Q is pressed
   if ( dbKeyState(17) || dbKeyState(31) ||dbKeyState(32) || dbKeyState(30)||dbKeyState(57) || dbKeyState(15)|| dbKeyState(16))
   {
   //stop the presently looping animation
  dbStopObject(2);
   //retrieves the object present angle
   int ObjAngleY=dbObjectAngleY(2);
   //determines if the object is presently looping
   int looping=dbObjectLooping(2);
      //if the key pressed is W
   if(dbKeyState(17)) 
   { 
   //move the object on the -Z axis
     dbMoveObject(2,-0.05f);
   //is the object presently not playing?
   if (looping==0 )
   {
   //if yes then execute move.x
      //move Move.x has 25 frames
   //the number of frames from the starting frame to the ending frame must be at least 25 frames

    dbLoopObject ( 2,26, 51 );
   };
   }
   //if the key pressed is S
   if (dbKeyState(31))
   {
   //move the object on the Z axis
    dbMoveObject(2,0.05f);
   //is the object currently not playing?
   if (looping==0 )
   {
      //if yes then execute move.x
      //move directx animation has 25 frames
   //starting frame up to the end frame must be at least 25 frames

    dbLoopObject ( 2,26, 51 );

   };
   }
   //if the A key is pressed

   if (dbKeyState(30))
   {
  //rotate the object along the Y axis
  //for instance, if the objects current angle is 275-5
  //The object will then be rotated 270 degrees which is exactly south
  //from the user's POV
  //the dbWrapValue is to ensure that the object rotation will not exceed 360 degrees

    dbYRotateObject(2,dbWrapValue(ObjAngleY-5.0f));
   }

  //if the D key is pressed
   if (dbKeyState(32))
   {
 
     //rotate the object along the Y axis
  //for instance, if the objects current angle is 85+5
  //The object will then be rotated 90 degrees which is exactly north
  //from the user's POV
  //the dbWrapValue is to ensure that the object rotation will not exceed 360 degrees
    dbYRotateObject(2,dbWrapValue(ObjAngleY+5.0f));
   }
     //if the space key is pressed
   if(dbKeyState(57))
   {
  //is the object currently not playing?
   if (looping==0 )
   {
   //if yes then execute Attack.x
   //Attack.x has 24 frames so from the starting to ending frames
   //must be at least 24 frames
    dbLoopObject ( 2,51,77 );


   };
   }
   //if the Tab key is pressed
   if(dbKeyState(15))
   {
   //is the object currently not playing?
   if (looping==0 )
   {
   //if yes then execute Die.x
   //Die.x has 50 frames so from the starting to ending frames
   //must be at least 50 frames
    dbLoopObject ( 2,78,127 );


   };
   }
       //if the Q key is pressed
  
   if(dbKeyState(16))
   {
  //is the object currently not playing?
   if (looping==0 )
   {
  //if yes then execute Impact.x
     //Impact.x has 10 frames so from the starting to ending frames
     //must be at least 10 frames
    dbLoopObject ( 2,127,137 );


   };
   }


   }
  else
  //if no key is pressed
  //execute Idle.x
  //Idle.x has 25 frames so from the starting to the ending frames miust be at least 25 frames
  {
   dbLoopObject(2,1,25);
  }
// update the screen
  dbSync();
 } 
//return to windows
return;
}




Another way of animating models is by using the dbShowObject and dbHideObject commands. I'll post an example of that here when I have time. If you want more information on animating darkGDK models or creating games per se, you can visit DarkGDK's official site www.thegamecreators.com

Creating a Hit Points (HP) Indicator in DarkGDK

HP indicators are normally used in Adventure games to show the character’s present HP level. There are several ways to create an HP level indicator in DarkGDK but the easiest way (I guess) is to create a sprite sheet with three or more columns and with different HP numbers per column. For instance, three HP’s on the first column, two on the second and one on the last. If a character dies, display the appropriate column relative to the character’s HP level. This method can also be used to specify the remaining number of bullets left or the character’s current energy level. If you are confused, follow these steps:

1. Start Microsoft Visual 2008 C++ Express Edition.

2. Click File>New Project>Select the Wizards Project type>Select Dark GDK-2d Game from the Visual Studio installed templates.

3. Click View>Solution Explorer. Double-click main.cpp. The following should then appear:
// Dark GDK - The Game Creators - www.thegamecreators.com

// the wizard has created a very simple 2D project that uses Dark GDK
// it can be used as a starting point in making your own 2D games

// whenever using Dark GDK you must ensure you include the header file
#include "DarkGDK.h"

// the main entry point for the application is this function
void DarkGDK ( void )
{
// in this application a backdrop is loaded and then several
// animated sprites are displayed on screen

// when starting a Dark GDK program it is useful to set global
// application properties, we begin by turning the sync rate on,
// this means we control when the screen is updated, we also set
// the maximum rate to 60 which means the maximum frame rate will
// be set at 60 frames per second
dbSyncOn   ( );
dbSyncRate ( 60 );

// a call is made to this function so we can stop the GDK from
// responding to the escape key, we can then add in some code in our
// main loop so we can control what happens when the escape key is pressed
dbDisableEscapeKey ( );

// now we will set the random seed value to the timer, this will
// help us to get more random values each time we run the program
dbRandomize ( dbTimer ( ) );

// we are going to display a backdrop for the scene, to do this
// we load our image and give it an ID number of 1, this particular
// image is of a sky at night with stars
dbLoadImage ( "backdrop.bmp", 1 );

// the next step is to create a sprite that uses this image, this
// is achieved by calling dbSprite and passing in a value of 1 for the
// sprites ID, 0 for the X coordinate, 0 for the Y coordinates and a
// value of 1 for the image
dbSprite ( 1, 0, 0, 1 );

// next we will load in some animated sprites, before doing this
// we need to adjust the image color key, by using this function we
// can make a specific color be transparent, in our case we want this
// to be bright pink
dbSetImageColorKey ( 255, 0, 255 );

// in this loop we're going to create some animated sprites, the image
// we load contains frames of animation for an asteroid
for ( int i = 2; i < 30; i++ )
 {
  // create an animated sprite and give it the ID number from the
  // variable i, next is the filename, now we come to how many frames
  // across and down, in our case this is 4, finally we come to the image
  // ID that the sprite will use, again we use i
  dbCreateAnimatedSprite ( i, "sprite.bmp", 4, 4, i );

  // position our sprite at a random location
  dbSprite ( i, dbRnd ( 640 ), -dbRnd ( 1500 ), i );
 }

 // now we come to our main loop, we call LoopGDK so some internal
 // work can be carried out by the GDK
 while ( LoopGDK ( ) )
 {
  // run a loop through all our sprites
  for ( int i = 2; i < 30; i++ )
  {
   // move the sprite down and play its animation
   // moving from frame 1 to 16 with a delay of 60 ms
   dbMoveSprite ( i, -2 );
   dbPlaySprite ( i, 1, 16, 60 );

   // check the position of the sprite, if it has gone off scren
   // then reposition it back to the top
   if ( dbSpriteY ( i ) > 500 )
dbSprite ( i, dbRnd ( 640 ), -dbRnd ( 1500 ), i );
}

// here we check if the escape key has been pressed, when it has
// we will break out of the loop
if ( dbEscapeKey ( ) )
break;

// here we make a call to update the contents of the screen
dbSync ( );
}

// when the user presses escape the code will break out to this location
// and we can free up any previously allocated resources

// delete all the sprites
for ( int i = 1; i < 30; i++ )
  dbDeleteSprite ( i );

 // delete the backdrop image
 dbDeleteImage ( 1 );

 // and now everything is ready to return back to Windows
 return;
}
4. Since we intend to make our sample application from the scratch, delete all the pre-made codes, except the following:
// Dark GDK - The Game Creators - www.thegamecreators.com
#include "DarkGDK.h"

// the main entry point for the application is this function
void DarkGDK ( void )
{
 
 dbSyncOn   ( );
 dbSyncRate ( 60 );

 
 while ( LoopGDK ( ) )
 {
  

  // here we make a call to update the contents of the screen
  dbSync ( );
 }

}
5. Make a spritesheet using gimp or photoshop. In this example we will be using the following image: 6. Later on we will be dividing this image into three columns. If you noticed if we divide this image into three divisions each divisions we will have different number of HP’s. 7. Copy this image from whatever location you have saved it. In this example, I have saved it on my desktop so all I need to do is go to the desktop. Select the file “hplevel.bmp”. Then press CTRL + C to copy it.
8. Switch to Micorsoft Visual C++ 2008 DarkGDK to view our game again then click the Open File icon from the formatting toolbar. 9. This causes the Open File dialog box to appear. Click on the empty pane>Press Ctrl + V to paste our “hplevel.bmp” file. 10. The next thing that you’ll need to do is to load this image into the computer’s memory, and divide it into three columns. We can do so by using the dbCreateAnimatedSprite command. Click the cancel button from the open dialog box to close it. This causes our code window to appear. Enter the ff. after the dbSyncRate(60); line.
//divides the image into 3 columns
dbCreateAnimatedSprite(1,"hplevel.bmp",3,1,1);
11. If you run your application now, it will just display a blue screen. To display your animated sprite,use the dbSprite command. Enter the following code after the dbCreateAnimatedSprite line.
//displays our animated sprite
dbSprite(1,0,0,1);
12. Press Ctrl+ F5 to test our application again. If you noticed the first column of our animated sprite is shown together with a white background. To clear the white background, use the dbSetImageColorKey command. Enter the following before the dbCreateAnimated line:
//clears the white background
dbSetImageColorKey(255,255,255);
13. Your code should now look like this:
// Dark GDK - The Game Creators - www.thegamecreators.com
#include "DarkGDK.h"

// the main entry point for the application is this function
void DarkGDK ( void )
{

dbSyncOn   ( );
dbSyncRate ( 60 );
//clears the white background
dbSetImageColorKey(dbRgb(255,255,255));
//divides the image into 3 columns
dbCreateAnimatedSprite(1,"hplevel.bmp",3,1,1);
//displays our animated sprite
dbSprite(1,0,0,1);



while ( LoopGDK ( ) )
{


// here we make a call to update the contents of the screen
dbSync ( );
}

}
14. The next thing that we want to do is to subtract the number of HP’s everytime the space key is pressed. Of course you can replace this with more advanced algorithms such as when your player dies or when a shot is fired, etc. But for the sake of simplicity, lets just go with the space key(hehehe). 15. Declare an integer variable named intspritesheetcols just below #include "DarkGDK.h" line and assign a a default value 1. 16. Enter the following after the while ( LoopGDK ( ) ){
//you can replace dbSpaceKey with  "if a player dies" or "if a gun is fired" algorithms
if (dbSpaceKey())
{
//increment the value of intspritesheetcols by 1
//if you recall we've initially assigned a starting value 1 to
//our intspritesheetcols variable. If you press the
//the space key for the first time it will be incremented by 1
intspritesheetcols=intspritesheetcols + 1;
//displays the spritesheet division relative to the specified
//intspritesheetcols value. The first time you press the space key
//intspritescols will have a value to causing the second column
//of our spritesheet to be displayed
dbSetSpriteFrame(1,intspritesheetcols);
//displays our sprite
dbSprite(1,0,0,1);
}
//if the number of columns exceeds 3
//you can display your game over screen here but in this case let's
//just exit the game
if(intspritesheetcols>3)
{
return;
}
17. Your whole code should now look like this:
// Dark GDK - The Game Creators - www.thegamecreators.com
#include "DarkGDK.h"
int intspritesheetcols=1;
// the main entry point for the application is this function
void DarkGDK ( void )
{

dbSyncOn   ( );
dbSyncRate(5);
//clears the white background
dbSetImageColorKey(255,255,255);
//divides the image into 3 columns
dbCreateAnimatedSprite(1,"hplevel.bmp",3,1,1);
//displays our animated sprite
dbSprite(1,0,0,1);

while ( LoopGDK ( ) )
{
//you can replace dbSpaceKey with  "if a player dies" or "if a gun is fired" algorithms
if (dbSpaceKey())
{
//increment the value of intspritesheetcols by 1
//if you recall we've initially assigned a starting value 1 to
//our intspritesheetcols variable. If you press the
//the space key for the first time it will be incremented by 1
intspritesheetcols=intspritesheetcols + 1;
//displays the spritesheet division relative to the specified
//intspritesheetcols value. The first time you press the space key
//intspritescols will have a value to causing the second column
//of our spritesheet to be displayed
dbSetSpriteFrame(1,intspritesheetcols);
//displays our sprite
dbSprite(1,0,0,1);
}
//if the number of columns exceeds 3
//you can display your game over screen here but in this case let's
//just exit the game
if(intspritesheetcols>3)
{
return;
}
// here we make a call to update the contents of the screen
dbSync ( );
}

}
18. Press CTRL + F5 to run your application. You should now see an output similar to the following:
19. That’s all.

Creating a menu system in Dark GDK

There are several ways on how to create a menu system in Dark GDK but since I hate to complicate simple things, today we will be learning the simplest. The idea is to assign a sprite as a customized mouse pointer and hide that sprite when your game is loaded using dbHideSprite so that only the mouse pointer is visible and not your customized sprite pointer. The next problem would be to test which menu item or button was clicked, to be able to do that, simply use dbSpriteHit or dbSpriteCollission to check whether your hidden sprite acting as your mouse pointer collided with your button sprite. It’s that simple. If you are confused, follow these steps:

1. Before the actual programming, draw the buttons and customized mouse pointer that will be needed in your game using your favorite image creation software such as Paint, GIMP, or PhotoShop. It does not matter how your customized mouse pointer looks like because it will not show up on your form, just make sure that you buttons and your customized pointer are of the same height and width.



2. Start Microsoft Visual C++. Click File>New>Project>Select Wizards>Dark GDK-Game>Type “Using Menu” in the name textbox no quotes.

3. Minimize the MSVC++ window then go to My Documents>Visual Studio 2008>Projects>Using Menu>Using Menu> Then paste the images that you have created in step 1.



4. Maximize Visual C++>View>Solution Explorer>Double-Click Main.cpp. The following should then come into view:
// Dark GDK - The Game Creators - www.thegamecreators.com

// the wizard has created a very simple project that uses Dark GDK
// it contains the basic code for a GDK application

// whenever using Dark GDK you must ensure you include the header file
#include "DarkGDK.h"

// the main entry point for the application is this function
void DarkGDK ( void )
{
 // turn on sync rate and set maximum rate to 60 fps
 dbSyncOn   ( );
 dbSyncRate ( 60 );

 // our main loop
 while ( LoopGDK ( ) )
 {
  // update the screen
  dbSync ( );
 }

 // return back to windows
 return;
}

5. Locate the line void DarkGDK ( void ) then type the following after the line dbSyncRate ( 60 );
//loads our images
dbLoadImage("hibutton.jpg",1);
dbLoadImage("exitbutton.jpg",2);
dbLoadImage("customizedpointer.jpg",3);
//displays our images
dbSprite(1,400,420,1);
dbSprite(2,500,420,2);

6.If you try to run this,only the "Say Hi" and "Exit" buttons are visible.That's because we haven't display our sprite pointer yet.To view it, locate the line while ( LoopGDK ( ) ) and type the following aster the open curly bracket:

//assigns the currentX and CurrentY position
//of our mouse an X and Y coordinate of
//our customized pointer
//we need to place it inside the
//while loop so that it is always
//updated depending on the current mouse coordinate
dbSprite(3,dbMouseX(),dbMouseY(),3);
//hides the sprite that acts as our pointer
dbHideSprite(3);

7. The next thing that we need to do is to enable our button to react to mouse click. To be able to do that locate the line dbHideSprite(3); and type the ff. below it:

//if our hidden sprite collided with the “Say Hi” button 
//and the mouse button is clicked then
if (dbSpriteCollision(3,1)==1 && dbMouseClick()==1)
{
//do something…you can add your game sequence action codes here
//but in this case we will be displaying a “Hi!” text
dbSetTextFont("Garamond");
dbSetTextSize(70);
dbText(0,0,"Hi!");      
}
//if our hidden sprite collided with the “Exit” button 
//and the mouse button is clicked then
if (dbSpriteCollision(3,2)==1 && dbMouseClick()==1)
{
//quit the form
return; 
}

8. Let us have a quick look of our codes once again:

// Dark GDK - The Game Creators - www.thegamecreators.com

// the wizard has created a very simple project that uses Dark GDK
// it contains the basic code for a GDK application

// whenever using Dark GDK you must ensure you include the header file
#include "DarkGDK.h"

// the main entry point for the application is this function
void DarkGDK ( void )
{
 // turn on sync rate and set maximum rate to 60 fps
 dbSyncOn   ( );
 dbSyncRate ( 60 );
 //loads our images
 dbLoadImage("hibutton.jpg",1);
 dbLoadImage("exitbutton.jpg",2);
 dbLoadImage("customizedpointer.jpg",3);
 //displays our images
 dbSprite(1,400,420,1);
 dbSprite(2,500,420,2);



 // our main loop
 while ( LoopGDK ( ) )
 {
 
  dbSprite(3,dbMouseX(),dbMouseY(),3);
  dbHideSprite(3);
  if (dbSpriteCollision(3,1)==1 && dbMouseClick()==1)
  {
   dbSetTextFont("Garamond");
   dbSetTextSize(70);
   dbText(0,0,"Hi!"); 
  }
  if (dbSpriteCollision(3,2)==1 && dbMouseClick()==1)
  {
  return; 
  }


  // update the screen
  dbSync ( );
 }

 // return back to windows
 return;
}

9. Press F5 to test your game. Try clicking the “Say Hi” and "Exit" button.



10. And that would be all. One of the first menu system tutorial in Dark GDK. My buttons looks disgusting because I just rushed it, I didn’t have time to make one in Blender. Hope you learn something from it, till next time.

A simple 2D pong game in darkGDK

In case you are wondering, Pong is a video game that simulates ping pong. It is a game where you maneuver a bat or paddle by pressing a control or joystick and the goal of the game is to return the ball to the opposing side just like the real ping pong. If you totally don't have any idea how pong works, I suggest googling it first before moving onto the next section. Being accustomed with the game and how it works greatly aids in trasforming it into codes.

Apart from familiarizing the game, we also need to learn few darkGDK game operations and the underlying commands behind them. These operations and commands are enumerated below:

1. Displaying an Image

Before you can display an image in darkGDK, you'll need to load it first into the computer memory. To be able to do that, use the dbLoadImage command which has the following syntax:
dbLoadImage("Imagefilename",imageloadid);
Where;
1. Imagefilename is an existent image file saved inside your game project folder. darkGDK supports .bmp, .jpg, and .png image file types.

2. imageloadid is a unique image numeric id that can be used later on to refer to a loaded image.

Example:
dbLoadImage("Galaxian.jpg",1);
Once an image is loaded in the computer memory, you can then display it by using dbPasteImage or dbSprite. dbSprite has the following syntax:
dbSprite(spriteid, x-coordinate, y-coordinate, imageloadid);
Where;
1. spriteid is a numerical id that can be use to manipulate the displayed image. spriteid can be identical to the imageloadid.

2. x-coordinate and y-coordinate are the x and y coordinate of the upper-left corner of your sprite or image. If you are unfamiliar of x and y coordinate, think of the screen as the fourth quadrant of the Cartesian coordinate system. The upper left corner is the origin which has the coordinate of x=0,y=0. The value of x coordinate increases as you move right. The value of the y coordinate increases as you move down.

3. imageloadid is the numeric id of a loaded image that you wanted to display.

Example:
dbSprite(1,0,01);
2. Moving Sprites

Sprites and images can be moved or animated by using dbMoveSprite or by simply incrementing or decrementing the x and y coordinate of an image. If you are familiar with java, c++, or visual basic, you probably know what I am talking about.

3. Detecting Collisions

darkGDK comes with powerful command that can be used to detect sprite or object collision. To detect collision, simply use dbSpriteCollission or dbSpriteHit. dbSpriteHit has the following syntax:
dbSpriteHit(Spriteid1, Spriteid2);
Where;
1. Spriteid1 and Spriteid2 are the numeric spriteids of the images that you wanted to test for collision.

Now that you have learned few basic commands and operations in darkGDK,I guess we can proceed into making our pong game. The following steps demonstrates how:

1. Click Start>All Programs>Microsoft Visual C++ Express Edition.

2. Click on File>New>Project. In the Project Types tree control, select on Wizards.

3. Select Dark GDK-Game from the available Game project options.

4. Type “Pong” no quotes in the Project Name textbox then Click Ok.

5. Click on View>Solution Explorer. Locate Main.cpp in the Solution Explorer Window then double-click it.

6. The following should then appear:

// Dark GDK - The Game Creators - www.thegamecreators.com   
// the wizard has created a very simple project that uses Dark GDK   
// it contains the basic code for a GDK application     
// whenever using Dark GDK you must ensure you include the header file   
#include "DarkGDK.h"    
// the main entry point for the application is this function   
void DarkGDK ( void )   
{   
// turn on sync rate and set maximum rate to 60 fps   
dbSyncOn   ( );   
dbSyncRate ( 60 );     
// our main loop   
while ( LoopGDK ( ) )   
{   
// update the screen   
dbSync ( );   
}     
// return back to windows   
return;   
}   

7. Locate the line #include "DarkGDK.h" and declare the following variables below it:
//handles the ball velocity, the larger the value 
//the faster the ball will move
int ballxvelocity=5,ballyvelocity=5;
//handles the updated x and y coordinate of the ball
int ballxcoord,ballycoord;
//handles the updated x and y coordinate 
//of player1's paddle
int bat1xcoord,bat1ycoord;
//handles the updated x and y coordinate 
//of player2's paddle
int bat2xcoord,bat2ycoord;

8. Locate the line dbSyncOn and dbSynRate(60) then type the following below it:
//sets the window title to pong
dbSetWindowTitle("Pong");
//sets the window size to 600x400 that is
//x coordinates runs from 0-600
//y coordinates runs from 0-400
dbSetWindowSize(600,400);
//loads an image located in your game project folder
//and gives it an id of 1
dbLoadImage("ball.png",1);
//loads an image located in your game project folder
//and gives it an id of 2
dbLoadImage("bat1.jpg",2);
//loads an image located in your game project folder
//and gives it an id of 3
dbLoadImage("bat2.jpg",3);

//displays the ball at the center of the screen
dbSprite(1,300,200,1);
//player1 paddle at the left side of the screen
dbSprite(2,50,150,2); 
//player2 paddle at the right side of the screen 
dbSprite(3,500,150,3);


9. Locate the line while ( LoopGDK ( ) ) and type the following after the open curly bracket:
//move the ball
//adds the value of ballxvelocity 
//and the current x coordinate of the ball
//and assign it as a new ball x coord value
ballxcoord=dbSpriteX(1) + ballxvelocity;
//do the same with y
ballycoord=dbSpriteY(1) + ballyvelocity;
//displays the ball in the updated x and y coord positions
dbSprite(1,ballxcoord,ballycoord,1);
//if the ball collides with the 
//top or bottom of the screen then
if (ballycoord <= 0 || ballycoord >= 400) 
{
//Multiply ballyvelocity by negative 1
//The result will  later on be added to the
//current y coordinate of the ball
//causing the ball to change position
//depending on the value of ballyvelocity and
//the current coord of the ball
ballyvelocity=ballyvelocity*-1;
}
//if the ball collides with the left side 
//or right side of the screen then
if (ballxcoord <= 0 || ballxcoord >= 600)
{
//Multiply ballxvelocity by negative 1
//The result will later on be added to the
//current x coordinate of the ball
//causing the ball to change position
//depending on the value of ballxvelocity and
//the current x coord of the ball
ballxvelocity=ballxvelocity*-1;
}

//check bat collision
//if the ball collides with player 1's paddle
if (dbSpriteHit(1,2)==1)
{
//if at this point you still don't 
//understand what this do
//I suggest recalling the rules in multiplying
//and adding negative numbers
ballxvelocity=ballxvelocity*-1;
}
//if the ball collides with player 2's paddle
if (dbSpriteHit(1,3)==1)
{
//reverse the direction of the ball
ballxvelocity=ballxvelocity*-1;
}

//move the player 1 bat
//if the enter key is pressed
if (dbReturnKey()==1)
{
//decrement the paddle y coord value
//causing the paddle to move up
bat1ycoord=dbSpriteY(2)-5;
//display the paddle in the
//updated position
dbSprite(2,bat1xcoord,bat1ycoord,2);
}
//if the down arrow is pressed
if (dbShiftKey()==1)
{  
//increment the paddle y coord value
//causing the paddle to move down
bat1ycoord=dbSpriteY(2)+5;
//display the paddle in its
//updated position



//move player 2 bat
//if the up arrow is pressed
if (dbUpKey()==1)
{
//decrement the paddle y coord value
//causing the paddle to move up
bat2ycoord=dbSpriteY(3)-5;
dbSprite(3,bat2xcoord,bat2ycoord,3);
}
//if the shift key is pressed
if (dbDownKey()==1)
{
//increment the paddle y coord value
//causing the paddle to move down
bat2ycoord=dbSpriteY(3)+5;
dbSprite(3,bat2xcoord,bat2ycoord,3);
}
10. Locate the line dbSync ( ); and type the following after the open curly bracket:
//Deletes the loaded image
//and sprites when the game is closed
//this will free up the used memory
for ( int i = 1; i < 30; i++ )
dbDeleteSprite ( i );
dbDeleteImage ( 1 );
11. Let's have a glance of our codes again and this time comments omitted:
// Dark GDK - The Game Creators - www.thegamecreators.com

// the wizard has created a very simple project that uses Dark GDK
// it contains the basic code for a GDK application

// whenever using Dark GDK you must ensure you include the header file
#include "DarkGDK.h"
int ballxvelocity=5,ballyvelocity=5;
int ballxcoord,ballycoord;
int bat1xcoord,bat1ycoord;
int bat2xcoord,bat2ycoord;
// the main entry point for the application is this function
void DarkGDK ( void )
{
// turn on sync rate and set maximum rate to 60 fps
dbSyncOn   ( );
dbSyncRate ( 60 );
dbSetWindowTitle("Pong");
dbSetWindowSize(600,400);
dbLoadImage("ball.png",1);
dbLoadImage("bat1.jpg",2);
dbLoadImage("bat2.jpg",3);

 
dbSprite(1,300,200,1);   
dbSprite(2,50,150,2);  
dbSprite(3,500,150,3);  



// our main loop
while ( LoopGDK ( ) )
{
//move the ball
ballxcoord=dbSpriteX(1) + ballxvelocity;
ballycoord=dbSpriteY(1) + ballyvelocity;
dbSprite(1,ballxcoord,ballycoord,1);
if (ballycoord <= 0 || ballycoord >= 400) 
{
ballyvelocity=ballyvelocity*-1;
}
if (ballxcoord <= 0 || ballxcoord >= 600)
{
ballxvelocity=ballxvelocity*-1;
}

//check bat collission
if (dbSpriteHit(1,2)==1)
{

ballxvelocity=ballxvelocity*-1;
}
if (dbSpriteHit(1,3)==1)
{

ballxvelocity=ballxvelocity*-1;
}

//moves player 1 bat
if (dbReturnKey()==1)
{
bat1ycoord=dbSpriteY(2)-5;
dbSprite(2,bat1xcoord,bat1ycoord,2);
}
if (dbShiftKey()==1)
{
bat1ycoord=dbSpriteY(2)+5;
dbSprite(2,bat1xcoord,bat1ycoord,2);
}


//moves player 2 bat
if (dbUpKey()==1)
{
bat2ycoord=dbSpriteY(3)-5;
dbSprite(3,bat2xcoord,bat2ycoord,3);
}
if (dbDownKey()==1)
{
bat2ycoord=dbSpriteY(3)+5;
dbSprite(3,bat2xcoord,bat2ycoord,3);
}


// update the screen
dbSync ( );
}
for ( int i = 1; i < 30; i++ )
{
dbDeleteSprite ( i );
dbDeleteImage ( 1 );
}
// return back to windows
return;
}

12. Press F5 to test the game. You can press the Enter key and shift key to move player1's bat and the up and down arrow to move player2's bat.

13. And that's it. A very simple pong game using few lines of codes. At the point of writing I guess this was the very first pong game tutorial written using darkGDK. I will add attract mode animation, menus, scores, and AI enemy to this when I have time but for now, though it's incomplete, you can use it as starting point. Hope you'll learn something from it and enjoy the game.

A simple 2D Color Slot Machine in Dark GDK

I’m sure every one of us is familiar with slot machine or fruit machine game. Today we will be making a slight version of that game, and we are going to name it as “Color Slot Machine”. There are several elements missing in this game such as the game title screen and the game overview screen so I’ll just explain to you the mechanics of the game. Before the game starts you have an initial account of 100000. To play the game, click on the Spin button. After clicking on the Spin button the computer will generate random colors and your score depends upon the generated color. The scoring system is presented below:

ColorWin
Red-Green-Blue+2000
Red-Red-Red+1000
Green-Green-Green+1000
Blue-Blue-Blue+1000
Others-1000
The following steps demonstrate how to create this game:

1.Prepare the images that will be needed in your game. You can use Ms-Paint, Photoshop or GIMP. The following images are required for this game:



2. After preparing the images, Start Visual C++>File>New>Projects>Wizards>Select Dark GDK-Game>Type “Color Machine” in the name textbox (no quotes).

3. Assuming that you have saved the project in the default directory, minimize Microsoft Visual C++ then go to My Documents>Visual Studio 2008>Projects>Color Machine>Color Machine>Then paste the images that you have made in Step 1.



4. Maximize Visual C++>View>Solution Explorer>Double-click Main.cpp. The following should then come into view:

// Dark GDK - The Game Creators - www.thegamecreators.com

// the wizard has created a very simple project that uses Dark GDK
// it contains the basic code for a GDK application

// whenever using Dark GDK you must ensure you include the header file
#include "DarkGDK.h"

// the main entry point for the application is this function
void DarkGDK ( void )
{
 // turn on sync rate and set maximum rate to 60 fps
 dbSyncOn   ( );
 dbSyncRate ( 60 );

 // our main loop
 while ( LoopGDK ( ) )
 {
  // update the screen
  dbSync ( );
 }

 // return back to windows
 return;
}

5. Locate the line #include "DarkGDK.h" then declare the following variables below it:
//handles the spriteid of the left box
int intred;
//handles the spriteid of the middle box
int intgreen;
//handles the spriteid of the right box
int intblue;
//handles our loop
int intcounter;
//determines if the spin button has been clicked
int intclicked;
//displays our status text
char* chrmessage;
//handles the balance
int intbalance=100000;

6. Locate the line dbSynRate(60); then replace the 60 with 10 so that dark GDK will try to play 10 frames per second and our loop will not play funny , then type the following after it:
//sets the window title
dbSetWindowTitle("Color Slot Machine");
//sets the window size
dbSetWindowSize(400,400);
//loads our images
dbLoadImage("red.png",1);
dbLoadImage("green.png",2);
dbLoadImage("blue.png",3);
dbLoadImage("backdrop.png",4);
dbLoadImage("Spin.png",5);
dbLoadImage("pointer.png",6);
//displays our images
dbSprite(4,0,0,4);
//sets our backdrop alpha transparency
//so that our text will be visible
dbSetSpriteAlpha(4,150);
dbSprite(5,240,380,5);
dbSprite(1,130,100,1);
dbSprite(2,260,100,2);
dbSprite(3,390,100,3);

7. Locate the Line while ( LoopGDK ( ) ) then type the following after the open curly brace:
//sets text size to 30 points
dbSetTextSize(30);
//displays the balance
//I still don’t know how to concanate text
dbText(0,0,"Balance:");
dbText(150,0,dbStr(intbalance));
  //displays our status text
  dbText(250,250,chrmessage);
  //pessimistic programming
  //assume that the button has not yet been clicked
  intclicked=0;
  //hides our customized mouse pointer
  //so that only the mouse pointer is visible
  dbHideSprite(6);
  //Enables dbRnd to generate an unpredictable
  //random number generation
  dbRandomize(dbTimer());
  dbSetSprite(6,1,100);
  //enables our sprite to follow the mouse
  dbSprite(6,dbMouseX(),dbMouseY(),6);
  //if our hidden sprite collided with the mouse
  //and the button has been clicked
  if(dbSpriteHit(6,5)==1 && dbMouseClick()==1)
  {
  //assigns 1 to inclicked variable
  //resets the value of chrmessage
  intclicked=1;
  chrmessage="";
  //generate a random color 10 times
  for(intcounter=1;intcounter<=10;intcounter++)
  {
   intred=1 + dbRnd(2);
   intblue=1 + dbRnd(2);
   intgreen=dbRnd(2)+1;
   dbSprite(1,130,100,intred);
   dbSprite(2,260,100,intgreen);
   dbSprite(3,390,100,intblue);
   dbSync();
  }
  }
  
  //check if the button has been clicked
  //if it was clicked then

  if (intclicked==1)
  {
   //check for color matches
   if (intred==1 && intblue==1 && intgreen==1)
   {
   chrmessage="You win 1000!";
   intbalance=intbalance+1000;
  
   }
   else if (intred==2 && intblue==2 && intgreen==2)
   {
   chrmessage="You win 1000!";
   intbalance=intbalance+1000;
   }
   else if (intred==3 && intblue==3 && intgreen==3)
   {
   chrmessage="You win 1000!";
   intbalance=intbalance+1000;
   }
   else if (intred==1 && intblue==2 && intgreen==3)
   {
   chrmessage="You win 2000!";
   intbalance=intbalance+1000;
   }
   //if no color matches then
   else
   {
   chrmessage="You Lose!";
   intbalance=intbalance-1000;
   }
  }
  //if the balance is less than or equal to zero
  if (intbalance<=0)
  {
  dbCLS();
  dbSetTextSize(32);
  dbText(180,250,"Insufficient balance");
  dbText(250,300,"GAME OVER");
  dbSync();
  dbWait(4000);
  return;
 }
8. Let’s have a look of our code once again and this time, crappy comments ommitted.
// Dark GDK - The Game Creators - www.thegamecreators.com

// the wizard has created a very simple project that uses Dark GDK
// it contains the basic code for a GDK application

// whenever using Dark GDK you must ensure you include the header file
#include "DarkGDK.h"
int intred;
int intblue;
int intgreen;
int intcounter;
int intclicked;
char* chrmessage;
int intbalance=100000;
// the main entry point for the application is this function
void DarkGDK ( void )
{

 // turn on sync rate and set maximum rate to 60 fps
 dbSyncOn   ( );
 dbSyncRate ( 20 );
 dbSetWindowTitle("Color Slot Machine");
 dbSetWindowSize(400,400);
 dbSetGamma(500,255,500);
 dbLoadImage("red.png",1);
 dbLoadImage("green.png",2);
 dbLoadImage("blue.png",3);
 dbLoadImage("backdrop.png",4);
 dbLoadImage("Spin.png",5);
 dbLoadImage("pointer.png",6);
 dbSprite(4,0,0,4);
 dbSetSpriteAlpha(4,150);
 dbSprite(5,240,380,5);
 dbSprite(1,130,100,1);
 dbSprite(2,260,100,2);
 dbSprite(3,390,100,3);

 // our main loop
 while ( LoopGDK ( ) )
 {

  dbSetTextSize(30);
  dbText(0,0,"Balance:");
  dbText(150,0,dbStr(intbalance));
  dbText(250,250,chrmessage);
  intclicked=0;
  dbHideSprite(6);
  dbRandomize(dbTimer());
  dbSetSprite(6,1,100);
  dbSprite(6,dbMouseX(),dbMouseY(),6);
  if(dbSpriteHit(6,5)==1 && dbMouseClick()==1)
  {
  intclicked=1;
  chrmessage="";
   for(intcounter=1;intcounter<=10;intcounter++)
   {
   intred=1 + dbRnd(2);
   intblue=1 + dbRnd(2);
   intgreen=dbRnd(2)+1;
   dbSprite(1,130,100,intred);
   dbSprite(2,260,100,intgreen);
   dbSprite(3,390,100,intblue);
   dbSync();
   }
  }

  if (intclicked==1)
  {
   if (intred==1 && intblue==1 && intgreen==1)
   {
   chrmessage="You win 1000!";
   intbalance=intbalance+1000;
  
   }
   else if (intred==2 && intblue==2 && intgreen==2)
   {
   chrmessage="You win 1000!";
   intbalance=intbalance+1000;
   }
   else if (intred==3 && intblue==3 && intgreen==3)
   {
   chrmessage="You win 1000!";
   intbalance=intbalance+1000;
   }
   else if (intred==1 && intblue==2 && intgreen==3)
   {
   chrmessage="You win 2000!";
   intbalance=intbalance+1000;
   }
   else
   {
   chrmessage="You Lose!";
   intbalance=intbalance-1000;
   }
  }
  
  if (intbalance<=0)
  {
  dbCLS();
  dbSetTextSize(32);
  dbText(180,250,"Insufficient balance");
  dbText(250,300,"GAME OVER");
  dbSync();
  dbWait(4000);
  return;
  }

  // update the screen
  
  dbSync ( );
 }

 // return back to windows
 return;
}
9. Press F5 to test our game. Click the Spin button and see what happens.

Creating a plain object in DarkGDK

A plain is a mesh object that can be used as ground in your game scene. To create a plain object, use the dbMakeObjectPlain function.

Syntax:

dbMakeObjectPlain(object id,plain width,plain height);

Example:
dbMakeObjectPlain(1,100,100);

By default an object is drawn in gray. You can add colors to your plain object by Using the dbColorObject function which has the following syntax:

dbColorObject(object id, color);

Colors can be defined by using the dbRGB function which has the following syntax:

dbRGB(red,green,blue);

Wherein red, green, and blue are numbers that ranges from 0-255.

The following codes demonstrates dbMakeObjectPlain and dbColorObject at work:

// Dark GDK - The Game Creators - www.thegamecreators.com

// the wizard has created a very simple project that uses Dark GDK
// it contains the basic code for a GDK application

// whenever using Dark GDK you must ensure you include the header file
#include "DarkGDK.h"

// the main entry point for the application is this function
void DarkGDK ( void )
{
// turn on sync rate and set maximum rate to 60 fps
dbSyncOn   ( );
dbSyncRate ( 60 );
//Creates a 400 X 200 plain object with an object id of 1 and 
dbMakeObjectPlain(1,400,200);
//Applies a red color to our plain
dbColorObject(1,dbRGB(255,0,0));
//Positions the camera on the side of our object
dbPositionCamera(-1,-600,-100);
// our main loop
while ( LoopGDK ( ) )
{
// update the screen
dbSync ( );
}
//deletes all objects
for ( int i = 1; i < 50; i++ )
dbDeleteObject ( i );
// return back to windows
return;
}