A simple 2D Color Slot Machine in Dark GDK

Posted by Rey Dacoco in

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.

0 comments

Post a Comment

Contributor

My photo
I am just a simple man with a simple mind. And this is my simple blog. New Posts will be available soon. Enjoy reading and never stop learning:)

Total Pageviews

Link List

Archives

Got questions? You can reach me here: