I'm taking a C++ class and we're using the DarkGDK library for codes and the Tony Gaddis book: Games & Graphics in C++. In Ch 12 there is a programming Exercise called Dice Simulator where I have to create a Die class that can simulate the rolling of a dice when the space bar is pressed (I wrote an animation for this). I'm having a hard time with this object-oriented programming (and programming in general) but I came up with something using sprites. The only problem is that when I press the space bar to start the dice animation it doesn't do it on both dices at the same time but it runs the animation on one dice then once it's done it runs the animation on the second dice. Could anyone help me with this? By the way this is my first programming class ever and I'm not very familiar with everything the DarkGDK library offers or what I can do with C++ aaand this is my first time using the forums.
/*
The program consists of a class named Die that can simulate the rolling of a dice.
The class has a member function named toss that randomly determines which side of
the die is facing up (a value in the range of 1 through 6). When the toss member
function is called, it displays an image of the die side that is facing up. In the
program, two Die objects are created to simulate a pair of dice. Each time the user
presses the spacebar, the program simulates the rolling of the dice.
*/
#include "DarkGDK.h"
//***************************************************************************************************************
// The Die Class. A class is a code that specifies the member variables and functions for a type of object. *
// It serves as a blueprint that describes a type of object. In this program, the Coin class simulates the *
// rolling of the dice. *
//***************************************************************************************************************
class Die
{
// The member variables are in the private section because they can only be accessed by the class's member
// functions.
private:
int sideUp; // Holds the image number that will be displayed at the beginning of the program
// The functions in the public section can be accessed by code outside the class.
public:
Die(); // Constructor
void setSideUp(int); // mutator function
int getSideUp() const; // accessor function
void showDice(int, int) const; // member function prototype
void playSprite(int, int, int, int) const; // member function prototype
void toss(int, int, int) const; // member function prototype
};
//***************************************************************************************************************
// The Constructor helps construct the object. It contains the images to be used in the program. It *
// does not return a value or accepts arguments. It is automatically executed when an object is created. *
//***************************************************************************************************************
Die::Die()
{
// Load images.
dbLoadImage("1Die.bmp", 1);
dbLoadImage("2Die.bmp", 2);
dbLoadImage("3Die.bmp", 3);
dbLoadImage("4Die.bmp", 4);
dbLoadImage("5Die.bmp", 5);
dbLoadImage("6Die.bmp", 6);
}
//***************************************************************************************************************
// The setSideUp member function has the ability to access the private member variable and change it, that's *
// why it is called a mutator function. It accepts an argument for the sideUp variable. *
//***************************************************************************************************************
void Die::setSideUp(int faceSideThatIsUp)
{
sideUp = faceSideThatIsUp;
}
//***************************************************************************************************************
// The getSideUp function returns the sideUp value. It has the ability to access the private member variable *
// but not change it, it is called an accesor function for that reason. The word const at the end specifies *
// the the function does not modify a class's member variables. *
//***************************************************************************************************************
int Die::getSideUp() const
{
return sideUp;
}
//***************************************************************************************************************
// The showDice function shows the sprite with the sideUp value, which is the image number stored there *
// by the setSideUp function. It has const at the end specifying that it cannot change member variables. *
//***************************************************************************************************************
void Die::showDice(int x, int y) const
{
dbPasteImage(sideUp, x, y);
}
//***************************************************************************************************************
// The playSprite function plays the coin sprite animation and makes sure that sprite 2 is not visible. *
//***************************************************************************************************************
void Die::playSprite(int spriteNum, int x, int y, int hideSprite) const
{
// Check if sprite 2 is showing, if it is hide it.
if( dbSpriteVisible(hideSprite) )
{
dbHideSprite(hideSprite);
}
// To hold the image number of the current image to display.
int currentImage = 1;
// Play the animation by using the for loop which shows every image.
for( int count = 1; count < 30; count++ )
{
// Display sprite 1 using the current image.
dbSprite(spriteNum, x, y, currentImage);
// Update the current image number for the next loop iteration.
if (currentImage == 6)
{
currentImage = 1;
}
else
{
currentImage++;
}
// Refresh screen.
dbSync();
}
}
//***************************************************************************************************************
// The toss function that randomly determines whether the coin is face up or face down, and displays the *
// appropriate coin image. *
//***************************************************************************************************************
void Die::toss(int spriteNum, int x, int y) const
{
// Get a random number in the range of 1-6
int rolledSide = dbRND(5) + 1;
// Show sprite.
dbSprite(spriteNum, x, y, rolledSide);
dbShowSprite(spriteNum);
dbSetSpritePriority(2, 1);
}
// Global Constants
const int DIE1_PLAY_SPRITE = 1;
const int DIE1_X = 200;
const int DIE1_Y = 207;
const int DIE1_TOSS_SPRITE = 2;
const int DIE2_PLAY_SPRITE = 3;
const int DIE2_X = 300;
const int DIE2_Y = 207;
const int DIE2_TOSS_SPRITE = 4;
// Function prototypes
void setUp();
//***************************************************************************************************************
// The DarkGDK function *
//***************************************************************************************************************
void DarkGDK()
{
// Perform setup operations.
setUp();
//// Create a Die object named die1.
Die die1;
//// Display an image of dice's side for object die1.
die1.setSideUp(1);
die1.showDice(DIE1_X, DIE1_Y);
// Create a Die object named die2.
Die die2;
// Display an image of a dice's side for obect die2.
die2.setSideUp(3);
die2.showDice(DIE2_X, DIE2_Y);
// Turn off auto refresh and set the refresh rate.
dbSyncOn();
dbSyncRate(30);
// Game loop
while( LoopGDK() )
{
// Check if spacebar has been pressed.
if( dbSpaceKey() )
{
// Play animated sprites for both objects and display a random image of a dice.
die1.playSprite(DIE1_PLAY_SPRITE, DIE1_X, DIE1_Y, DIE1_TOSS_SPRITE);
die1.toss(DIE1_TOSS_SPRITE, DIE1_X, DIE1_Y);
die2.playSprite(DIE2_PLAY_SPRITE, DIE2_X, DIE2_Y, DIE2_TOSS_SPRITE);
die2.toss(DIE2_TOSS_SPRITE, DIE2_X, DIE2_Y);
}
// Refresh the screen.
dbSync();
}
}
//***************************************************************************************************************
// The setUp function *
//***************************************************************************************************************
void setUp()
{
// Set window title to "Dice Simulator"
dbSetWindowTitle("Dice Simulator");
// Set key color to green
dbSetImageColorKey(0, 255, 0);
// Seed the random number generator.
dbRandomize( dbTimer() );
}