I'm fairly new to the Dark GDK and C++ in general. I'm trying to write this simple program that detects a mouseclick over a sprite, then plays a sprite animation and finally displays a random sprite. It a coin toss game in short. However I currently have 3 issues:
1) I cannot get the mouseclick to do anything, but my program moves forward with a key press, I believe this is due to the if statement in my game loop.
2)The Sprite animation will not play, this has to be something Im overseeing.
3)The random integer call in my cointoss function does not seem to really return a random number, it always returns 1!
The other thing I'd like to do is loop this so that you can continue to click on the coin and keep "flipping" it till you hit esc.
Thanks to anyone who takes the time to look this over for me, shouldn't take to long as I'm sure this is an elementary level program.
//include directive for DarkGDK.h file
#include "DarkGDK.h"
//constant variables
const int coinHead = 1; //
const int coinTails = 2; //
const int coinAnimation = 3; //
const int headsSprite = 1; //
const int tailsSprite = 2; //
const int coinFlipSprite= 3; //
const int coinX = 260; //
const int coinY = 100; //
const int refreshRate = 60; // stores the refresh rate
//function prototypes
void setUp();
void coinToss();
bool overSprite(int, int, int);
void DarkGDK()
{
// Set window title.
dbSetWindowTitle("Coin Flip");
// Variables to hold the mouse pointer's X and Y coordinates.
int mouseX, mouseY;
// Perform setup operations.
setUp();
// Game loop
while ( LoopGDK() )
{
dbCenterText(319, 10, "heads or tails?");
dbCenterText(319, 22, "make your bet and click the coin!");
// Determine whether the mouse pointer moved over the image.
if (overSprite(headsSprite, mouseX, mouseY) && dbMouseClick() == 1)
{
coinToss();
}
// Replace the heads image with the tails image.
// Refresh the screen.
dbSync();
}
}
void setUp()
{
// Set color key.
dbSetImageColorKey(0, 255, 0);
// Load the images.
dbLoadImage("resources\\Heads.bmp", coinHead);
dbLoadImage("resources\\Tails.bmp", coinTails);
dbLoadImage("resources\\Coin Toss Animation.bmp", coinAnimation);
dbCreateAnimatedSprite(coinFlipSprite, "resources\\Coin Toss Animation.bmp", 4, 1,
coinAnimation);
// Make the sprites, using the images.
dbSprite(headsSprite, coinX, coinY, coinHead);
//dbSprite(tailsSprite, coinX, coinY, coinTails);
// Disable auto refresh and set the refresh rate.
dbSyncOn();
dbSyncRate(refreshRate);
}
void coinToss()
{
int headsOrTails = dbRND(2);
dbPlaySprite(coinFlipSprite, 1, 4, 1000);
dbSprite(coinFlipSprite, coinX, coinY, coinAnimation);
if (headsOrTails == 0)
{
dbSprite(headsSprite, coinX, coinY, coinHead);
}
else if (headsOrTails == 1)
{
dbSprite(tailsSprite, coinX, coinY, coinTails);
}
}
bool overSprite(int spriteNum, int mouseX, int mouseY)
{
// Variable to hold the value to return.
bool insideSprite;
// Get the X coordinate of the sprite's upper-left corner.
int upperX = dbSpriteX(spriteNum) - dbSpriteOffsetX(spriteNum);
// Get the Y coordinate of the sprite's upper-left corner.
int upperY = dbSpriteY(spriteNum) - dbSpriteOffsetY(spriteNum);
// Get the X coordinate of the sprite's lower-right corner.
int lowerX = upperX + dbSpriteWidth(spriteNum);
// Get the Y coordinate of the sprite's lower-right corner.
int lowerY = upperY + dbSpriteHeight(spriteNum);
// Determine whether (mouseX, mouseY) is inside the
// sprite's bounding rectangle.
if (mouseX >= upperX && mouseY >= upperY &&
mouseX <= lowerX && mouseY <= lowerY)
{
insideSprite = true;
}
else
{
insideSprite = false;
}
// Return the value of insideSprite.
return insideSprite;
}