Everything worked except the collision detection.
I have evolved the program quite a bit now.
It's currently:
#include "DarkGDK.h"
#include <string.h>
//Constants
const int REFRESH_RATE = 60; //screen refresh rate
const DWORD RED = dbRGB(255, 0, 0);
const DWORD WHITE = dbRGB(255, 255, 255);
const DWORD BLACK = dbRGB(0, 0, 0);
const int ASTEROID_SPRITE_NUM = 15;
const int ASTEROID_IMAGE_NUM = 15;
const int START_BUTTON_SPRITE_NUM = 1;
const int STARTS_X = 265;
const int STARTS_Y = 350;
const int FILE_NUM = 1;
const int WORD_SIZE = 30;
char playerName[WORD_SIZE];
char player1[WORD_SIZE];
//Class Declaration
class UFO
{
private:
public:
};
//function prototypes
void setUp();
bool onSprite(int, int, int);
bool mouseFullClick(int &, int&);
int menu();
void getInput(char[]);
void playGame(int);
char openInputFile();
void gameOver(char[]);
void DarkGDK (void)
{
//Set the window Title
dbSetWindowTitle("Asteroid Dash!");
dbLoadSound("resources\\music.wav",1);
int difficultyLevel, mouseX, mouseY;
//dbSyncOn ();
//dbSyncRate (REFRESH_RATE);
setUp();
/*if ( dbMouseClick() == 1)
{
if ( onSprite(START_BUTTON_SPRITE_NUM, mouseX, mouseY) )
{
dbCLS();
for ( int i = 1; i < 3; i++ )
{
dbDeleteSprite (i);
}
}
}*/
menu();
playGame(difficultyLevel);
openInputFile();
gameOver(player1);
}
void setUp()
{
dbDisableEscapeKey ();
dbRandomize ( dbTimer () );
dbSetImageColorKey (0, 255, 0 );
dbLoopSound(1);
dbLoadImage ("resources\\backdrop.bmp", 1);
dbLoadImage ("resources\\asteroid.bmp", ASTEROID_IMAGE_NUM);
dbLoadImage ("resources\\main title.bmp", 3);
dbLoadImage ("resources\\start button.bmp", 4);
dbLoadImage ("resources\\spaceship.bmp", 5);
dbLoadImage ("resources\\explosion.bmp", 6);
dbLoadImage ("resources\\asteroid boundary.bmp", 7);
dbSprite (3, 0, 0, 1);
dbSprite (2, 55, 50, 3);
dbSprite (START_BUTTON_SPRITE_NUM, STARTS_X, STARTS_Y, 4);
dbLoadSound("resources\\ufo.wav",2);
dbLoadSound("resources\\explosion.wav",3);
dbLoadSound("resources\\game over.wav",4);
dbWaitKey();
for ( int i = 1; i < 3; i++ )
{
dbDeleteSprite (i);
}
}
bool onSprite(int spriteNum, int pointX, int pointY)
{
bool insideSprite;
int upperX = dbSpriteX(spriteNum) - dbSpriteOffsetX(spriteNum);
int upperY = dbSpriteY(spriteNum) - dbSpriteOffsetY(spriteNum);
int lowerX = upperX + dbSpriteWidth(spriteNum);
int lowerY = upperY + dbSpriteHeight(spriteNum);
if( pointX >= upperX && pointY >= upperY && pointX <= lowerX && pointY <= lowerY )
{
insideSprite = true;
}
else
{
insideSprite = false;
}
return insideSprite;
}
bool mouseFullClick(int &x, int &y)
{
//
bool buttonClick = false;
if ( dbMouseClick() == 1)
{
x = dbMouseX();
y = dbMouseY();
while ( dbMouseClick() == 1)
{
//do nothing
}
buttonClick = true;
}
return buttonClick;
}
int menu()
{
int difficultyLevel;
dbInk(RED, BLACK);
dbSetTextSize(36);
dbSetTextFont("System");
dbSetTextToBold();
//instructions for the user
dbCenterText(319, 120, "SELECT DIFFICULTY");
dbCenterText(319, 170, "1 = EASY");
dbCenterText(319, 200, "2 = HARD");
dbCenterText(319, 230, "3 = IMPOSSIBLE");
dbCenterText(319, 260, "4 = Exit");
dbSetTextSize(2);
//set shape equal to user's input
difficultyLevel = atoi(dbInput()) ;
dbCLS();
// this switch statement determines the difficulty setting
switch (difficultyLevel)
{
//if the user selects 1
case 1:
difficultyLevel = 20;
break;
//if the user selects 2
case 2:
difficultyLevel = 25;
break;
//if the user selects 3
case 3:
difficultyLevel = 30;
break;
//if the user selects 4
case 4:
difficultyLevel = 0;
break;
//gameover();
//in case the user inputs a value outside the indicated range display an
//error message
default:
dbPrint("Error: Please select a difficulty setting!.");
}
return difficultyLevel;
}
void getInput()
{
/*Check if the file players_name.dat exists, if so delete it, and open
an output file to store the players name.*/
if(dbFileExist("players_name.dat"))
{
//If the players_name.dat exists delete it.
dbDeleteFile("players_name.dat");
//and open an output file to store the player's name
dbOpenToWrite(FILE_NUM, "players_name.dat");
}
else //If the file does not exist open output file
{
dbOpenToWrite(FILE_NUM, "players_name.dat");
}
dbInk(RED, BLACK);
dbSetTextSize(36);
dbSetTextFont("System");
dbSetTextToBold();
// Get the player's name.
dbCenterText(319, 120, "Please type your name and hit enter.");
strcpy(playerName, dbInput() );
if ( dbReturnKey())
{
dbWriteFile(FILE_NUM, playerName[WORD_SIZE]);
}
//close output file.
dbCloseFile(FILE_NUM);
//clear the screen.
dbCLS();
}
/*The openInputFile function checks if the players_name.dat input file exists
and opens it. If file does not exist, display an error message.*/
void playGame(int difficultyLevel)
{
dbCLS();
dbStopSound(1);
int ufoX = 319;
int ufoY = 440;
dbSprite (4, 0, 0, 5);
int offsetX = dbSpriteWidth(4) /2;
int offsetY = dbSpriteHeight(4) /2;
dbOffsetSprite (4, offsetX, offsetY);
dbSizeSprite (4, 106, 30);
int a;
int asteroidX = 640;
int asteroidY = 480;
//a equals number of asteroid sprites created
for ( a = ASTEROID_SPRITE_NUM; a < difficultyLevel; a++ )
{
dbCreateAnimatedSprite ( a, "resources//asteroid.bmp", 4, 4, a );
// position asteroids at random locations
dbSprite ( a, dbRnd (asteroidX), -dbRnd (asteroidY), a );
}
dbSyncOn ();
dbSyncRate (REFRESH_RATE);
// now we come to our main loop, we call LoopGDK so some internal
while ( LoopGDK () )
{
dbPasteImage (1, 0, 0, 1);
dbSprite (4, ufoX, ufoY, 5);
dbSprite (a, asteroidX, asteroidY, 7);
for ( int i = ASTEROID_SPRITE_NUM; 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 );
//reposition asteroid sprite if it goes off the screen
if ( dbSpriteY ( i ) > 480 )
{
dbSprite ( i, dbRnd (asteroidX), -dbRnd (asteroidY), i );
}
}
if ( dbRightKey() )
{
dbPlaySound(2);
ufoX++;
if ( dbSpaceKey())
{
ufoX += 5;
}
}
if ( dbLeftKey() )
{
dbPlaySound(2);
ufoX--;
if ( dbSpaceKey())
{
ufoX -= 5;
}
}
if ( dbUpKey() )
{
dbPlaySound(2);
ufoY--;
if ( dbSpaceKey())
{
ufoY -= 5;
}
}
if ( dbDownKey())
{
dbPlaySound(2);
ufoY++;
if ( dbSpaceKey())
{
ufoY += 5;
}
}
if ( dbSpriteCollision(4, a))
{
dbPlaySound(3);
dbCenterText(ufoX, ufoY, "YOU CRASHED!");
//dbSprite(6, ufoX, ufoY,1);
//dbHideSprite(3);
//dbSizeSprite (6, explosionSize, explosionSize);
//for ( int i = explosionSize; i < 200; i++ )
//{
// explosionSize++;
//}
//dbWait(500);
//dbHideSprite(ASTEROID_SPRITE_NUM);
dbWaitKey();
}
//escape to break loop
if ( dbEscapeKey ( ) )
break;
// here we make a call to update the contents of the screen
dbSync ( );
}
}
char openInputFile()
{
char player1 [WORD_SIZE];
//Check if the coordinates.dat file exists.
if(dbFileExist("players_name.dat") )
{
//Open the input file.
dbOpenToRead(FILE_NUM, "players_name.dat");
}
else
{
//Print an error message.
dbPrint("Error! The file does not exist.");
}
if(!dbFileEnd(FILE_NUM))
{
//Set the pointers X and Y coordinates from the input file
player1 [WORD_SIZE] = dbReadFile(FILE_NUM);
}
return player1[WORD_SIZE];
//Close the Input File.
dbCloseFile(FILE_NUM);
}
void gameOver(char name[])
{
const int LINE_SIZE = 200;
//arrays for the lines of output
char line1[LINE_SIZE];
dbInk(RED, BLACK);
dbSetTextSize(36);
dbSetTextFont("System");
dbSetTextToBold();
//instructions for the user
strcpy( line1, "Great job ");
strcat( line1, name );
strcat( line1, " , you sure can fly!");
// delete all the sprites, images and sound files
for ( int i = 1; i < 30; i++ )
{
dbDeleteSprite (i);
dbDeleteImage (i);
dbDeleteSound (i);
}
//Prompt the user to press any key to exit the program.
dbCenterText(319, 450, "press any key To exit");
dbWaitKey();
//return back to Windows
return;
}
Unfortunately, that comes with more problems. The mouseClick and onSprite functions do nothing. And now, the menu wont even display. I get the three sprites for the intro screen, but
nothing afterwards. Everything has gone haywire. I need to make sure the string input and file writing are working, and I need to move the ufo sprite into the ufo class along with its appropriate data and methods.
I have the if statement for the mouse detection commented out because it wouldn't work. The rest quit on me after the addition of the getInput and openInputFile functions. This is due in 12 hours and I'm starting to think I dont have a chance.