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:

  1. // Dark GDK - The Game Creators - www.thegamecreators.com  
  2.   
  3. // the wizard has created a very simple project that uses Dark GDK  
  4. // it contains the basic code for a GDK application  
  5.   
  6. // whenever using Dark GDK you must ensure you include the header file  
  7. #include "DarkGDK.h"  
  8.   
  9. // the main entry point for the application is this function  
  10. void DarkGDK ( void )  
  11. {  
  12.  // turn on sync rate and set maximum rate to 60 fps  
  13.  dbSyncOn   ( );  
  14.  dbSyncRate ( 60 );  
  15.   
  16.  // our main loop  
  17.  while ( LoopGDK ( ) )  
  18.  {  
  19.   // update the screen  
  20.   dbSync ( );  
  21.  }  
  22.   
  23.  // return back to windows  
  24.  return;  
  25. }  

5. Locate the line #include "DarkGDK.h" then declare the following variables below it:
  1. //handles the spriteid of the left box  
  2. int intred;  
  3. //handles the spriteid of the middle box  
  4. int intgreen;  
  5. //handles the spriteid of the right box  
  6. int intblue;  
  7. //handles our loop  
  8. int intcounter;  
  9. //determines if the spin button has been clicked  
  10. int intclicked;  
  11. //displays our status text  
  12. char* chrmessage;  
  13. //handles the balance  
  14. 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:
  1. //sets the window title  
  2. dbSetWindowTitle("Color Slot Machine");  
  3. //sets the window size  
  4. dbSetWindowSize(400,400);  
  5. //loads our images  
  6. dbLoadImage("red.png",1);  
  7. dbLoadImage("green.png",2);  
  8. dbLoadImage("blue.png",3);  
  9. dbLoadImage("backdrop.png",4);  
  10. dbLoadImage("Spin.png",5);  
  11. dbLoadImage("pointer.png",6);  
  12. //displays our images  
  13. dbSprite(4,0,0,4);  
  14. //sets our backdrop alpha transparency  
  15. //so that our text will be visible  
  16. dbSetSpriteAlpha(4,150);  
  17. dbSprite(5,240,380,5);  
  18. dbSprite(1,130,100,1);  
  19. dbSprite(2,260,100,2);  
  20. dbSprite(3,390,100,3);  

7. Locate the Line while ( LoopGDK ( ) ) then type the following after the open curly brace:
  1. //sets text size to 30 points  
  2. dbSetTextSize(30);  
  3. //displays the balance  
  4. //I still don’t know how to concanate text  
  5. dbText(0,0,"Balance:");  
  6. dbText(150,0,dbStr(intbalance));  
  7.   //displays our status text  
  8.   dbText(250,250,chrmessage);  
  9.   //pessimistic programming  
  10.   //assume that the button has not yet been clicked  
  11.   intclicked=0;  
  12.   //hides our customized mouse pointer  
  13.   //so that only the mouse pointer is visible  
  14.   dbHideSprite(6);  
  15.   //Enables dbRnd to generate an unpredictable  
  16.   //random number generation  
  17.   dbRandomize(dbTimer());  
  18.   dbSetSprite(6,1,100);  
  19.   //enables our sprite to follow the mouse  
  20.   dbSprite(6,dbMouseX(),dbMouseY(),6);  
  21.   //if our hidden sprite collided with the mouse  
  22.   //and the button has been clicked  
  23.   if(dbSpriteHit(6,5)==1 && dbMouseClick()==1)  
  24.   {  
  25.   //assigns 1 to inclicked variable  
  26.   //resets the value of chrmessage  
  27.   intclicked=1;  
  28.   chrmessage="";  
  29.   //generate a random color 10 times  
  30.   for(intcounter=1;intcounter<=10;intcounter++)  
  31.   {  
  32.    intred=1 + dbRnd(2);  
  33.    intblue=1 + dbRnd(2);  
  34.    intgreen=dbRnd(2)+1;  
  35.    dbSprite(1,130,100,intred);  
  36.    dbSprite(2,260,100,intgreen);  
  37.    dbSprite(3,390,100,intblue);  
  38.    dbSync();  
  39.   }  
  40.   }  
  41.     
  42.   //check if the button has been clicked  
  43.   //if it was clicked then  
  44.   
  45.   if (intclicked==1)  
  46.   {  
  47.    //check for color matches  
  48.    if (intred==1 && intblue==1 && intgreen==1)  
  49.    {  
  50.    chrmessage="You win 1000!";  
  51.    intbalance=intbalance+1000;  
  52.     
  53.    }  
  54.    else if (intred==2 && intblue==2 && intgreen==2)  
  55.    {  
  56.    chrmessage="You win 1000!";  
  57.    intbalance=intbalance+1000;  
  58.    }  
  59.    else if (intred==3 && intblue==3 && intgreen==3)  
  60.    {  
  61.    chrmessage="You win 1000!";  
  62.    intbalance=intbalance+1000;  
  63.    }  
  64.    else if (intred==1 && intblue==2 && intgreen==3)  
  65.    {  
  66.    chrmessage="You win 2000!";  
  67.    intbalance=intbalance+1000;  
  68.    }  
  69.    //if no color matches then  
  70.    else  
  71.    {  
  72.    chrmessage="You Lose!";  
  73.    intbalance=intbalance-1000;  
  74.    }  
  75.   }  
  76.   //if the balance is less than or equal to zero  
  77.   if (intbalance<=0)  
  78.   {  
  79.   dbCLS();  
  80.   dbSetTextSize(32);  
  81.   dbText(180,250,"Insufficient balance");  
  82.   dbText(250,300,"GAME OVER");  
  83.   dbSync();  
  84.   dbWait(4000);  
  85.   return;  
  86.  }  
8. Let’s have a look of our code once again and this time, crappy comments ommitted.
  1. // Dark GDK - The Game Creators - www.thegamecreators.com  
  2.   
  3. // the wizard has created a very simple project that uses Dark GDK  
  4. // it contains the basic code for a GDK application  
  5.   
  6. // whenever using Dark GDK you must ensure you include the header file  
  7. #include "DarkGDK.h"  
  8. int intred;  
  9. int intblue;  
  10. int intgreen;  
  11. int intcounter;  
  12. int intclicked;  
  13. char* chrmessage;  
  14. int intbalance=100000;  
  15. // the main entry point for the application is this function  
  16. void DarkGDK ( void )  
  17. {  
  18.   
  19.  // turn on sync rate and set maximum rate to 60 fps  
  20.  dbSyncOn   ( );  
  21.  dbSyncRate ( 20 );  
  22.  dbSetWindowTitle("Color Slot Machine");  
  23.  dbSetWindowSize(400,400);  
  24.  dbSetGamma(500,255,500);  
  25.  dbLoadImage("red.png",1);  
  26.  dbLoadImage("green.png",2);  
  27.  dbLoadImage("blue.png",3);  
  28.  dbLoadImage("backdrop.png",4);  
  29.  dbLoadImage("Spin.png",5);  
  30.  dbLoadImage("pointer.png",6);  
  31.  dbSprite(4,0,0,4);  
  32.  dbSetSpriteAlpha(4,150);  
  33.  dbSprite(5,240,380,5);  
  34.  dbSprite(1,130,100,1);  
  35.  dbSprite(2,260,100,2);  
  36.  dbSprite(3,390,100,3);  
  37.   
  38.  // our main loop  
  39.  while ( LoopGDK ( ) )  
  40.  {  
  41.   
  42.   dbSetTextSize(30);  
  43.   dbText(0,0,"Balance:");  
  44.   dbText(150,0,dbStr(intbalance));  
  45.   dbText(250,250,chrmessage);  
  46.   intclicked=0;  
  47.   dbHideSprite(6);  
  48.   dbRandomize(dbTimer());  
  49.   dbSetSprite(6,1,100);  
  50.   dbSprite(6,dbMouseX(),dbMouseY(),6);  
  51.   if(dbSpriteHit(6,5)==1 && dbMouseClick()==1)  
  52.   {  
  53.   intclicked=1;  
  54.   chrmessage="";  
  55.    for(intcounter=1;intcounter<=10;intcounter++)  
  56.    {  
  57.    intred=1 + dbRnd(2);  
  58.    intblue=1 + dbRnd(2);  
  59.    intgreen=dbRnd(2)+1;  
  60.    dbSprite(1,130,100,intred);  
  61.    dbSprite(2,260,100,intgreen);  
  62.    dbSprite(3,390,100,intblue);  
  63.    dbSync();  
  64.    }  
  65.   }  
  66.   
  67.   if (intclicked==1)  
  68.   {  
  69.    if (intred==1 && intblue==1 && intgreen==1)  
  70.    {  
  71.    chrmessage="You win 1000!";  
  72.    intbalance=intbalance+1000;  
  73.     
  74.    }  
  75.    else if (intred==2 && intblue==2 && intgreen==2)  
  76.    {  
  77.    chrmessage="You win 1000!";  
  78.    intbalance=intbalance+1000;  
  79.    }  
  80.    else if (intred==3 && intblue==3 && intgreen==3)  
  81.    {  
  82.    chrmessage="You win 1000!";  
  83.    intbalance=intbalance+1000;  
  84.    }  
  85.    else if (intred==1 && intblue==2 && intgreen==3)  
  86.    {  
  87.    chrmessage="You win 2000!";  
  88.    intbalance=intbalance+1000;  
  89.    }  
  90.    else  
  91.    {  
  92.    chrmessage="You Lose!";  
  93.    intbalance=intbalance-1000;  
  94.    }  
  95.   }  
  96.     
  97.   if (intbalance<=0)  
  98.   {  
  99.   dbCLS();  
  100.   dbSetTextSize(32);  
  101.   dbText(180,250,"Insufficient balance");  
  102.   dbText(250,300,"GAME OVER");  
  103.   dbSync();  
  104.   dbWait(4000);  
  105.   return;  
  106.   }  
  107.   
  108.   // update the screen  
  109.     
  110.   dbSync ( );  
  111.  }  
  112.   
  113.  // return back to windows  
  114.  return;  
  115. }  
9. Press F5 to test our game. Click the Spin button and see what happens.