As far as I know the problem is in the asteroids.cpp file, in the laserCollision function. From commenting out blocks of code it seems to be the dbSpriteCollision function that is causing something to go wrong..
I have also attached the entire project if you would like to download it and try it out for yourself.
Main.cpp:
#include "DarkGDK.h"
#include "spaceship.h"
#include "asteroid.h"
#include "lose.h"
#include "level.h"
#include "levelUp.h"
using namespace std;
bool lost = false;
int oldLevel = 0;
int currentLevel = 1;
bool play = false;
bool loaded = false;
// the main entry point for the application is this function
void DarkGDK ( void )
{
// turn on sync rate and set maximum rate to 60 fps
dbSetDisplayMode(SCREENWIDTH, SCREENHEIGHT, 32);
dbSyncOn ( );
dbSyncRate ( 60 );
for (int i = 102; i < 131; i++)
{
dbLoadImage("Media\\laser.png", i);
dbLoadSound("Media\\laser.wav", i + 1000);
dbSetSoundVolume(i + 1000, 90);
}
dbLoadImage("Media\\background.png", 132);
dbSprite(132, 0, 0, 132);
dbHideSprite(132);
dbCreateAnimatedSprite(133, "Media\\explosion.png", 4, 4, 133);
dbLoadImage("Media\\spaceship.png", 101);
dbSprite(101, 500, 300, 101);
dbOffsetSprite(101, 16, 32);
dbLoadSound("Media\\explosionSound.wav", 134);
dbLoadMusic("Media\\music1.mp3", 135);
dbLoopMusic(135);
Spaceship spaceship;
Asteroid asteroid;
// our main loop
while ( LoopGDK ( ) )
{
dbPasteSprite(132, 0, 0);
if (lost == false && oldLevel == currentLevel)
{
dbSetTextSize(20);
dbText(0, 580, "FPS: ");
dbText(35, 580, dbStr(dbScreenFPS()));
dbText(600, 580, "Asteroids Left: ");
dbText(740, 580, dbStr(asteroid.asteroidsToKill));
dbText(0, 540, "Health: ");
dbText(60, 540, dbStr(spaceship.health));
}
spaceship.movement(lost);
spaceship.shoot(oldLevel, currentLevel);
asteroid.movement();
asteroid.laserCollision();
asteroid.shipCollision(spaceship.health, oldLevel, currentLevel);
if (lost == true)
{
lose();
}
if (oldLevel < currentLevel)
{
play = false;
level(oldLevel, currentLevel, play);
}
if (play == true && loaded == false)
{
asteroid.active(oldLevel, currentLevel, loaded);
}
if (oldLevel == currentLevel && loaded == true && asteroid.asteroidsToKill == 0)
{
levelUp(currentLevel, loaded, play);
}
// update the screen
dbSync ( );
}
// return back to windows
return;
}
Spaceship.h:
#include "DarkGDK.h"
#define SCREENWIDTH 800
#define SCREENHEIGHT 600
class Spaceship
{
private:
int spaceship;
public:
int positionX, positionY;
bool alive;
bool laserActive[32];
int laserTimer[32];
int reloadCounter;
int laser;
//int counter;
int reloadTime;
int health;
bool exploded;
int explosionTimer;
bool positioned;
Spaceship()
{
positioned = false;
alive = true;
explosionTimer = 0;
exploded = false;
health = 100;
spaceship = 101;
laser = 102;
//counter = 2;
reloadTime = 10;
reloadCounter = reloadTime;
for (int i = 0; i < 32; i++)
{
laserTimer[i] = 0;
laserActive[i] = false;
}
}
void movement(bool &lost);
void shoot(int &oldLevel, int ¤tLevel);
void explode(bool &lost);
};
Spaceship.cpp:
#include "spaceship.h"
void Spaceship::movement(bool &lost)
{
if (alive == true)
{
if (dbUpKey())
{
dbMoveSprite(spaceship, 5);
}
if (dbDownKey())
{
dbMoveSprite(spaceship, -5);
}
if (dbLeftKey())
{
dbRotateSprite(spaceship, dbSpriteAngle(spaceship) -5);
}
if (dbRightKey())
{
dbRotateSprite(spaceship, dbSpriteAngle(spaceship) +5);
}
if (dbSpriteX(spaceship) > SCREENWIDTH)
{
dbSprite(spaceship, 0, dbSpriteY(spaceship), spaceship);
}
if (dbSpriteX(spaceship) < 0)
{
dbSprite(spaceship, SCREENWIDTH, dbSpriteY(spaceship), spaceship);
}
if (dbSpriteY(spaceship) > SCREENHEIGHT)
{
dbSprite(spaceship, dbSpriteX(spaceship), 0, spaceship);
}
if (dbSpriteY(spaceship) < 0)
{
dbSprite(spaceship, dbSpriteX(spaceship), SCREENHEIGHT, spaceship);
}
if (health <= 0 && exploded == false)
{
explode(lost);
}
}
}
void Spaceship::shoot(int &oldLevel, int ¤tLevel)
{
if (dbSpriteExist(spaceship))
{
if (reloadCounter < reloadTime)
{
reloadCounter++;
}
if (laser == 132)
{
laser = 102;
}
if (dbSpaceKey())
{
if (reloadCounter == reloadTime)
{
dbSprite(laser, dbSpriteX(spaceship), dbSpriteY(spaceship), laser);
dbRotateSprite(laser, dbSpriteAngle(spaceship));
dbPlaySound(laser + 1000);
laserActive[laser - 100] = true;
reloadCounter = 0;
laser++;
}
}
for (int i = 2; i < 31; i++)
{
if (laserActive[i] == true)
{
dbMoveSprite(i + 100, 10);
laserTimer[i] ++;
}
if (laserTimer[i] > 100)
{
dbDeleteSprite(i + 100);
laserActive[i] = false;
laserTimer[i] = 0;
}
}
}
}
void Spaceship::explode(bool &lost)
{
if (positioned == false)
{
positionX = dbSpriteX(spaceship);
positionY = dbSpriteY(spaceship);
positioned = true;
}
dbSprite(133, positionX, positionY, 133);
dbPlaySprite(133, 1, 16, 45);
explosionTimer++;
dbDeleteSprite(spaceship);
dbPlaySound(134);
for (int i = 102; i < 131; i++)
{
dbDeleteSprite(i);
}
if (explosionTimer == 45)
{
exploded = true;
alive = false;
lost = true;
}
}
Asteroid.h:
#include "DarkGDK.h"
#define SCREENWIDTH 800
#define SCREENHEIGHT 600
class Asteroid
{
public:
int asteroidName;
int positionX[100];
int positionY[100];
bool alive;
int speed[100];
int speed2[100];
int size[100];
int split[100];
bool loaded;
bool loaded2;
int asteroidCounter;
int sz;
int deadPositionX;
int deadPositionY;
int asteroidsToKill;
int random1;
int random2;
Asteroid()
{
asteroidsToKill = 0;
asteroidCounter = 1;
asteroidName = 1;
sz = 3;
}
void create();
void movement();
void laserCollision();
void shipCollision(int &health, int &oldLevel, int ¤tLevel);
void active(int &oldLevel, int ¤tLevel, bool &loaded);
};
Asteroid.cpp:
#include "asteroid.h"
void Asteroid::movement()
{
for (int i = 1; i < 100; i++)
{
if (dbSpriteExist(i))
{
positionX[i] += speed[i];
positionY[i] += speed2[i];
dbSprite(i, positionX[i], positionY[i], i);
dbPlaySprite(i, 1, 8, 80);
if (positionX[i] > SCREENWIDTH)
{
positionX[i] = 0;
}
if (positionX[i] < 0)
{
positionX[i] = SCREENWIDTH;
}
if (positionY[i] > SCREENHEIGHT)
{
positionY[i] = 0;
}
if (positionY[i] < 0)
{
positionY[i] = SCREENHEIGHT;
}
}
}
}
void Asteroid::laserCollision()
{
for (int i = 1; i < 100; i++)
{
if (dbSpriteExist(i));
{
for (int tmp = 102; tmp < 131; tmp++)
{
if (dbSpriteExist(tmp))
{
if (dbSpriteCollision(tmp, i))
{
if (size[i] == 3)
{
dbDeleteSprite(tmp);
sz = 2;
create();
asteroidName++;
create();
asteroidName++;
deadPositionX = dbSpriteX(i);
deadPositionY = dbSpriteY(i);
dbDeleteSprite(i);
asteroidsToKill--;
}
else if (size[i] == 2)
{
dbDeleteSprite(tmp);
sz = 1;
create();
asteroidName++;
create();
asteroidName++;
deadPositionX = positionX[i];
deadPositionY = positionY[i];
dbDeleteSprite(i);
asteroidsToKill--;
}
else if (size[i] == 1)
{
dbDeleteSprite(tmp);
sz = 0;
create();
asteroidName++;
create();
asteroidName++;
deadPositionX = positionX[i];
deadPositionY = positionY[i];
dbDeleteSprite(i);
asteroidsToKill--;
}
else
{
dbDeleteSprite(tmp);
dbDeleteSprite(i);
asteroidsToKill--;
}
}
}
}
}
}
}
void Asteroid::create()
{
dbRandomize(dbTimer());
size[asteroidName] = sz;
if (size[asteroidName] == 3)
{
positionX[asteroidName] = dbRND(SCREENWIDTH);
positionY[asteroidName] = dbRND(SCREENHEIGHT);
asteroidsToKill += 15;
}
else if (size[asteroidName] < 3)
{
positionX[asteroidName] = deadPositionX;
positionY[asteroidName] = deadPositionY;
}
if (size[asteroidName] == 3)
{
dbCreateAnimatedSprite(asteroidName, "Media\\asteroid3.png", 8, 1, asteroidName);
}
else if (size[asteroidName] == 2)
{
dbCreateAnimatedSprite(asteroidName, "Media\\asteroid2.png", 8, 1, asteroidName);
}
else if (size[asteroidName] == 1)
{
dbCreateAnimatedSprite(asteroidName, "Media\\asteroid1.png", 8, 1, asteroidName);
}
else if (size[asteroidName] == 0)
{
dbCreateAnimatedSprite(asteroidName, "Media\\asteroid0.png", 8, 1, asteroidName);
}
dbSprite(asteroidName, positionX[asteroidName], positionY[asteroidName], asteroidName);
random1 = dbRND(5);
if (random1 == 0)
{
speed[asteroidName] = 1;
}
else if (random1 == 1)
{
speed[asteroidName] = 2;
}
else if (random1 == 2)
{
speed[asteroidName] = 3;
}
else if (random1 == 3)
{
speed[asteroidName] = -1;
}
else if (random1 == 4)
{
speed[asteroidName] = -2;
}
else if (random1 == 5)
{
speed[asteroidName] = -3;
}
random2 = dbRND(5);
if (random2 == 0)
{
speed2[asteroidName] = 1;
}
else if (random2 == 1)
{
speed2[asteroidName] = 2;
}
else if (random2 == 2)
{
speed2[asteroidName] = 3;
}
else if (random2 == 3)
{
speed2[asteroidName] = -1;
}
else if (random2 == 4)
{
speed2[asteroidName] = -2;
}
else if (random2 == 5)
{
speed2[asteroidName] = -3;
}
/*if (asteroidName == 100)
{
asteroidName = 1;
}*/
}
void Asteroid::shipCollision(int &health, int &oldLevel, int ¤tLevel)
{
if (oldLevel == currentLevel)
{
for (int i = 1; i < 100; i++)
{
if (dbSpriteExist(i))
{
if (dbSpriteCollision(i, 101))
{
health--;
}
}
}
}
}
void Asteroid::active(int &oldLevel, int ¤tLevel, bool &loaded)
{
if (asteroidsToKill == 0)
{
asteroidName = 1;
}
for (int i = 0; i < currentLevel; i++)
{
sz = 3;
create();
asteroidName++;
}
loaded = true;
}
Level.h:
#include "DarkGDK.h"
int counter = 0;
void level(int &oldLevel, int ¤tLevel, bool &play)
{
dbSetTextSize(100);
dbText(250, 250, "Level");
dbText(470, 250, dbStr(currentLevel));
counter++;
if (counter == 100)
{
oldLevel = currentLevel;
counter = 0;
play = true;
}
}
LevelUp.h:
#include "DarkGDK.h"
void levelUp(int ¤tLevel, bool &loaded, bool &play)
{
for (int i = 102; i < 131; i++)
{
dbDeleteSprite(i);
}
currentLevel++;
loaded = false;
play = false;
}
Lose.h:
#include "DarkGDK.h"
void lose()
{
dbSetTextSize(100);
dbText(200, 250, "Game Over");
}