For some reason the code isn't showing up when I use the code box. I'm posting it both in the message area and as an attachment
////////////////////////////////////////////////
// Program: Space Rocks //
// Author: Nathan Merzke //
// Version: 1.0 //
////////////////////////////////////////////////
////////////////////////////////////////////////
// HEADERS //
////////////////////////////////////////////////
#include "DarkSDK.h"
#include "resource.h"
#include <cstdio>
#include <cstring>
////////////////////////////////////////////////
// CONSTANTS //
////////////////////////////////////////////////
const int NEW_GAME = 1;
const int HIGH_SCORES = 2;
const int HELP = 3;
const int EXIT = 0;
const int TEXT_SIZE = 36;
const int MENU_DEL = 5;
const int STAR_NUM = 3000;
const int STAR_LAYERS = 5;
const float STAR_SPEED = 1.5;
const float SHIP_SPEED = 5.0;
const float SHIP_DSPEED = SHIP_SPEED * (float)0.7071;
const int MAX_SHOTS = 40;
const int SHOT_LEN = 12;
const int GUN_ROF = 6;
const float SHOT_SPEED = 7.5;
const int NUM_ROCKS = 40;
const int ROCK_SPAWN = 5;
const int BIG_HITS = 10;
const int MED_HITS = 5;
const int SML_HITS = 1;
const int RSIZE_FACTOR = 2;
const float RSIZE_MUL = 50;
const int RSPEED_FACTOR = 6;
const int RSPEED_SUB = 4;
const float RSPEED_MUL = 2.0;
const float MIN_RSPEED = 4.0;
const int ROCK_DELAY = 4;
const int EXPLODE_DELAY = 2;
const int BIG_PNT = 10;
const int MED_PNT = 5;
const int SML_PNT = 1;
const int PNT_DEL = 20;
const int GAME_OVER = 200;
////////////////////////////////////////////////
// ENUMERATIONS //
////////////////////////////////////////////////
enum STATE { dead, alive, explode };
////////////////////////////////////////////////
// STRUCTURES //
////////////////////////////////////////////////
struct MENUITEM {
char text[30];
bool active;
int y;
};
struct SCORE {
int num;
char name[4];
};
struct STARFIELD {
float x;
float y;
float z;
};
struct SHIP {
float x;
float y;
STATE state;
int frame;
int delay;
int rof;
};
struct ROCK {
float x;
float y;
float speed;
float angle;
int num;
float size;
STATE state;
int hits;
int frame;
int delay;
static int cur_rocks;
};
struct SHOT {
float x;
float y;
static int cur_shots;
bool active;
};
int SHOT::cur_shots = 0;
int ROCK::cur_rocks = 0;
////////////////////////////////////////////////
// FUNCTION PROTOTYPES //
////////////////////////////////////////////////
void srInit(void);
int srMainMenu(void);
void srHighScores(void);
void srScoreUpdate(int score);
void srHelp(void);
int srPlay(void);
void srStarInit(STARFIELD *stars);
void srShipInit(SHIP &ship);
void srRockInit(ROCK *rocks);
void srMiscInit(SHOT *shots);
void srTakeoff(SHIP &ship, ROCK *rocks, STARFIELD *stars, SHOT *shots);
bool srUpdateScreen(SHIP &ship, ROCK *rocks, STARFIELD *stars, SHOT *shots, int &points);
void srCollision(SHIP &ship, ROCK *rocks, SHOT *shots, int &points);
void srControl(SHIP &ship, STARFIELD *stars, SHOT *shots);
////////////////////////////////////////////////
// MAIN //
////////////////////////////////////////////////
void DarkSDK(void){
int score, menu = 0;
srInit();
for(;;){
menu = srMainMenu();
switch(menu){
case NEW_GAME:
dbDeleteSprite(1);
dbDeleteImage(1);
score = srPlay();
srScoreUpdate(score);
srHighScores();
break;
case HIGH_SCORES:
srHighScores();
break;
case HELP:
srHelp();
break;
case EXIT:
return;
}
dbCLS(0);
dbSync();
dbWait(1000);
}
}
////////////////////////////////////////////////
// PROGRAM INITIALIZATION //
////////////////////////////////////////////////
void srInit(void){
dbRandomize(dbTimer());
dbSetWindowTitle("Space Rocks");
dbSetDisplayMode(800, 600, 16);
dbSetWindowLayout(0, 0, 1);
dbMaximiseWindow();
dbHideMouse();
dbSyncOn();
dbSyncRate(30);
}
////////////////////////////////////////////////
// Main Menu //
////////////////////////////////////////////////
int srMainMenu(void){
MENUITEM menu[4];
int a, index = 0, timer = MENU_DEL;
menu[0].active = true;
strcpy(menu[0].text, "New Game");
menu[0].y = 240;
menu[1].active = false;
strcpy(menu[1].text, "High Scores");
menu[1].y = 270;
menu[2].active = false;
strcpy(menu[2].text, "Help");
menu[2].y = 300;
menu[3].active = false;
strcpy(menu[3].text, "Exit");
menu[3].y = 330;
dbSetTextToBold();
dbSetTextSize(TEXT_SIZE);
if(!dbSpriteExist(1)){
dbLoadImage("media\\title.bmp", 1);
dbSprite(1, 400, 150, 1);
dbOffsetSprite(1, 150, 30);
dbScaleSprite(1, 150);
}
dbCLS(0);
dbSync();
dbWait(1000);
// Update screen.
do{
dbCLS(0);
dbSprite(1, 400, 150, 1);
for(a = 0; a < 4; a++){
if(menu[a].active) dbInk(dbRgb(255, 255, 0), dbRgb(0, 0, 0));
else dbInk(dbRgb(0, 0, 255), dbRgb(0, 0, 0));
dbCenterText(400, menu[a].y, menu[a].text);
}
if(timer == MENU_DEL){
if(dbUpKey()){
menu[index].active = false;
index--;
if(index < 0) index = 3;
menu[index].active = true;
timer--;
}
}
if(timer == MENU_DEL){
if(dbDownKey()){
menu[index].active = false;
index++;
if(index > 3) index = 0;
menu[index].active = true;
timer--;
}
}
if(timer == MENU_DEL){
if(dbReturnKey()){
switch(index){
case 0:
return NEW_GAME;
case 1:
return HIGH_SCORES;
case 2:
return HELP;
case 3:
return EXIT;
}
}
}
else{
if(!timer) timer = MENU_DEL;
else timer--;
}
dbSync();
}while(!dbEscapeKey());
return EXIT;
}
////////////////////////////////////////////////
// HIGH SCORES //
////////////////////////////////////////////////
void srHighScores(void){
SCORE scores[10];
int a, b;
char out[20];
for(a = 0; a < 10; a++){
strcpy(scores[a].name, "---");
scores[a].num = 0;
}
if(!dbFileExist("highscores.dat")){
dbOpenToWrite(1, "highscores.dat");
for(a = 0; a < 10; a++){
for(b = 0; b < 3; b++) dbWriteByte(1, '-');
dbWriteLong(1, 0);
}
dbCloseFile(1);
}
dbOpenToRead(1, "highscores.dat");
for(a = 0; a < 10; a++){
for(b = 0; b < 3; b++) scores[a].name[b] = (char)dbReadByte(1);
scores[a].num = dbReadFile(1);
}
dbCloseFile(1);
dbSetTextToBold();
dbSetTextSize(TEXT_SIZE);
dbInk(dbRgb(0, 0, 255), dbRgb(0, 0, 0));
dbCLS(0);
dbSync();
dbWait(1000);
// Update screen.
do{
dbCLS(0);
dbSprite(1, 400, 150, 1);
dbCenterText(400, 195, "High Scores");
for(b = 0; b < 10; b++){
_snprintf(out, 20, "%2d. %3.3s %7d", b + 1, scores[b].name, scores[b].num);
dbCenterText(400, 195 + (b + 1) * 30, out);
}
dbCenterText(400, 195 + (b + 1) * 30, "Press any key...");
dbSync();
}while(!dbScanCode());
}
////////////////////////////////////////////////
// UPDATE SCORE //
////////////////////////////////////////////////
void srScoreUpdate(int score){
SCORE scores[10];
int a, b, c, control, delay = MENU_DEL, index = 0;
char out[35], place[5], name[4] = " ";
bool done = false;
for(a = 0; a < 10; a++){
strcpy(scores[a].name, "---");
scores[a].num = 0;
}
if(!dbFileExist("highscores.dat")){
dbOpenToWrite(1, "highscores.dat");
for(a = 0; a < 10; a++){
for(b = 0; b < 3; b++) dbWriteByte(1, '-');
dbWriteLong(1, 0);
}
dbCloseFile(1);
}
dbOpenToRead(1, "highscores.dat");
for(a = 0; a < 10; a++){
for(b = 0; b < 3; b++) scores[a].name[b] = (char)dbReadByte(1);
scores[a].name[3] = 0;
scores[a].num = dbReadFile(1);
}
dbCloseFile(1);
dbSetTextToBold();
dbSetTextSize(TEXT_SIZE);
for(a = 0; a < 10; a++){
if(scores[a].num < score){
switch(a){
case 0:
strcpy(place, "1st");
break;
case 1:
strcpy(place, "2nd");
break;
case 2:
strcpy(place, "3rd");
break;
default:
_snprintf(place, 4, "%dth", a + 1);
break;
}
dbCLS(0);
dbSync();
dbWait(1000);
do{
dbCLS(0);
dbInk(dbRgb(30, 180, 30), dbRgb(0, 0, 0));
dbBox(615 + ((index + 1) * 17), 300, 632 + ((index + 1) * 17), 330);
dbInk(dbRgb(0, 0, 255), dbRgb(0, 0, 0));
_snprintf(out, 35, "You made %s place!", place);
dbCenterText(400, 270, out);
_snprintf(out, 35, "Please enter you're initials: %s", name);
dbCenterText(400, 300, out);
control = dbScanCode();
if(control && delay == MENU_DEL){
delay--;
switch(control){
case 1:
done = true;
break;
case 2:
if(index < 3){
name[index] = '1';
index++;
}
break;
case 3:
if(index < 3){
name[index] = '2';
index++;
}
break;
case 4:
if(index < 3){
name[index] = '3';
index++;
}
break;
case 5:
if(index < 3){
name[index] = '4';
index++;
}
break;
case 6:
if(index < 3){
name[index] = '5';
index++;
}
break;
case 7:
if(index < 3){
name[index] = '6';
index++;
}
break;
case 8:
if(index < 3){
name[index] = '7';
index++;
}
break;
case 9:
if(index < 3){
name[index] = '8';
index++;
}
break;
case 10:
if(index < 3){
name[index] = '9';
index++;
}
break;
case 11:
if(index < 3){
name[index] = '0';
index++;
}
break;
case 14:
if(index > 0){
index--;
name[index] = ' ';
}
break;
case 16:
if(index < 3){
name[index] = 'Q';
index++;
}
break;
case 17:
if(index < 3){
name[index] = 'W';
index++;
}
break;
case 18:
if(index < 3){
name[index] = 'E';
index++;
}
break;
case 19:
if(index < 3){
name[index] = 'R';
index++;
}
break;
case 20:
if(index < 3){
name[index] = 'T';
index++;
}
break;
case 21:
if(index < 3){
name[index] = 'Y';
index++;
}
break;
case 22:
if(index < 3){
name[index] = 'U';
index++;
}
break;
case 23:
if(index < 3){
name[index] = 'I';
index++;
}
break;
case 24:
if(index < 3){
name[index] = 'O';
index++;
}
break;
case 25:
if(index < 3){
name[index] = 'P';
index++;
}
break;
case 28:
done = true;
break;
case 30:
if(index < 3){
name[index] = 'A';
index++;
}
break;
case 31:
if(index < 3){
name[index] = 'S';
index++;
}
break;
case 32:
if(index < 3){
name[index] = 'D';
index++;
}
break;
case 33:
if(index < 3){
name[index] = 'F';
index++;
}
break;
case 34:
if(index < 3){
name[index] = 'G';
index++;
}
break;
case 35:
if(index < 3){
name[index] = 'H';
index++;
}
break;
case 36:
if(index < 3){
name[index] = 'J';
index++;
}
break;
case 37:
if(index < 3){
name[index] = 'K';
index++;
}
break;
case 38:
if(index < 3){
name[index] = 'L';
index++;
}
break;
case 44:
if(index < 3){
name[index] = 'Z';
index++;
}
break;
case 45:
if(index < 3){
name[index] = 'X';
index++;
}
break;
case 46:
if(index < 3){
name[index] = 'C';
index++;
}
break;
case 47:
if(index < 3){
name[index] = 'V';
index++;
}
break;
case 48:
if(index < 3){
name[index] = 'B';
index++;
}
break;
case 49:
if(index < 3){
name[index] = 'N';
index++;
}
break;
case 50:
if(index < 3){
name[index] = 'M';
index++;
}
break;
case 57:
if(index < 3){
name[index] = ' ';
index++;
}
break;
}
}
if(!delay) delay = MENU_DEL;
else delay--;
dbSync();
}while(!done);
for(b = 9; b > a; b--){
strcpy(scores[b].name, scores[b - 1].name);
scores[b].num = scores[b - 1].num;
}
strcpy(scores[a].name, name);
scores[a].num = score;
dbDeleteFile("highscores.dat");
dbOpenToWrite(1, "highscores.dat");
for(b = 0; b < 10; b++){
for(c = 0; c < 3; c++) dbWriteByte(1, scores[b].name[c]);
dbWriteLong(1, scores[b].num);
}
dbCloseFile(1);
break;
}
}
dbLoadImage("media\\title.bmp", 1);
dbSprite(1, 400, 150, 1);
dbOffsetSprite(1, 150, 30);
dbScaleSprite(1, 150);
}
////////////////////////////////////////////////
// GAME HELP //
////////////////////////////////////////////////
void srHelp(void){
dbCLS(0);
dbSync();
dbWait(1000);
dbSetTextToBold();
dbSetTextSize(TEXT_SIZE);
dbInk(dbRgb(0, 0, 255), dbRgb(0, 0, 0));
do{
dbCLS(0);
dbCenterText(400, 270, "Game Controls");
dbCenterText(400, 300, "Move Ship = Arrow Keys");
dbCenterText(400, 330, " Fire Gun = Space Bar ");
dbCenterText(400, 360, " Pause = Enter Key ");
dbCenterText(400, 390, " Quit = Escape Key");
dbCenterText(400, 525, "Press any key to continue...");
dbSync();
}while(!dbScanCode());
}
////////////////////////////////////////////////
// GAME //
////////////////////////////////////////////////
int srPlay(void){
// Local Variables
STARFIELD stars[STAR_NUM];
SHIP ship;
SHOT shots[MAX_SHOTS];
ROCK rocks[NUM_ROCKS];
int a, points = 0;
// Initializations
srStarInit(stars);
srShipInit(ship);
srRockInit(rocks);
srMiscInit(shots);
//Intro
srTakeoff(ship, rocks, stars, shots);
// Game Loop
do{
if(srUpdateScreen(ship, rocks, stars, shots, points)) break;
srCollision(ship, rocks, shots, points);
srControl(ship, stars, shots);
dbSync();
}while(!dbEscapeKey());
dbStopMusic(1);
dbDeleteMusic(1);
dbStopSound(2);
dbStopSound(3);
dbStopSound(4);
dbStopSound(5);
dbDeleteSound(2);
dbDeleteSound(3);
dbDeleteSound(4);
dbDeleteSound(5);
for(a = 1; a < (NUM_ROCKS + 4); a++)
if(dbSpriteExist(a)) dbDeleteSprite(a);
for(a = 1; a < 47; a++)
if(dbImageExist(a)) dbDeleteImage(a);
return points;
}
////////////////////////////////////////////////
// INITIALIZE STARFIELD //
////////////////////////////////////////////////
void srStarInit(STARFIELD *stars){
// Initialize the starfield.
for(register int a = 0; a < STAR_NUM; a++){
stars[a].x = (float)dbRnd(800);
stars[a].y = (float)dbRnd(600);
stars[a].z = (float)((a / (STAR_NUM / STAR_LAYERS)) + 1);
}
}
////////////////////////////////////////////////
// INITIALIZE SHIP //
////////////////////////////////////////////////
void srShipInit(SHIP &ship){
// Initialize the player's ship.
ship.x = 400;
ship.y = 600;
ship.state = alive;
ship.frame = 1;
ship.delay = EXPLODE_DELAY;
ship.rof = 0;
dbLoadImage("media\\heavy.bmp", 1);
dbSprite(1, (int)ship.x, (int)ship.y, 1);
dbOffsetSprite(1, 62, 63);
dbScaleSprite(1, 50);
}
////////////////////////////////////////////////
// INITALIZE ROCKS //
////////////////////////////////////////////////
void srRockInit(ROCK *rocks){
int a;
ROCK::cur_rocks = 0;
// Initialize the rocks.
dbLoadBitmap("media\\spacerock.bmp", 1);
dbSetCurrentBitmap(1);
for(a = 0; a < 3; a++){
for(register int b = 0; b < 8; b++) {
dbGetImage((a * 8) + b + 2, b * 64, a * 64, (b * 64) + 63, (a * 64) + 63);
}
}
dbSetCurrentBitmap(0);
dbDeleteBitmap(1);
for(a = 0; a < NUM_ROCKS; a++){
rocks[a].angle = (float)(45 + dbRnd(90));
rocks[a].delay = ROCK_DELAY;
rocks[a].frame = 1 + dbRnd(7);
rocks[a].num = a + 2;
rocks[a].size = RSIZE_MUL * (2 + dbRnd(RSIZE_FACTOR));
rocks[a].speed = (MIN_RSPEED + (RSPEED_MUL * (dbRnd(RSPEED_FACTOR) - RSPEED_SUB)));
if(rocks[a].speed < MIN_RSPEED) rocks[a].speed = MIN_RSPEED;
rocks[a].state = dead;
switch((int)rocks[a].size){
case 100:
rocks[a].hits = SML_HITS;
break;
case 150:
rocks[a].hits = MED_HITS;
break;
case 200:
rocks[a].hits = BIG_HITS;
break;
}
rocks[a].x = (float)dbRnd(800);
rocks[a].y = 0;
dbSprite(rocks[a].num, (int)rocks[a].x, (int)rocks[a].y, rocks[a].frame + 1);
dbScaleSprite(rocks[a].num, rocks[a].size);
dbOffsetSprite(rocks[a].num, 32, 32);
}
}
////////////////////////////////////////////////
// MISC. INITIALIZE //
////////////////////////////////////////////////
void srMiscInit(SHOT *shots){
int a;
// Initialize weapons.
for(a = 0; a < MAX_SHOTS; a++){
shots[a].active = false;
shots[a].x = 0;
shots[a].y = 0;
}
SHOT::cur_shots = 0;
dbLoadSound("media\\Rocket Takeoff.wav", 1);
dbLoadSound("media\\explosion.wav", 2);
dbLoadSound("media\\Boom 1.wav", 3);
dbLoadSound("media\\Boom 2.wav", 4);
dbLoadSound("media\\gun.wav", 5);
dbLoadMusic("media\\Music.mid", 1);
// Initialize the explosion animation.
dbLoadBitmap("media\\explosion.bmp", 1);
dbSetCurrentBitmap(1);
for(register int a = 0; a < 4; a++){
for(register int b = 0; b < 5; b++) {
dbGetImage((a * 5) + b + 26, b * 64, a * 64, (b * 64) + 63, (a * 64) + 63);
}
}
dbSetCurrentBitmap(0);
dbDeleteBitmap(1);
dbSprite(NUM_ROCKS + 2, 400, 600, 26);
dbOffsetSprite(NUM_ROCKS + 2, 32, 32);
dbScaleSprite(NUM_ROCKS + 2, 300);
dbLoadImage("media\\paused.bmp", 46);
dbSprite(NUM_ROCKS + 3, 400, 300, 46);
dbOffsetSprite(NUM_ROCKS + 3, 120, 30);
dbHideSprite(NUM_ROCKS + 3);
}
////////////////////////////////////////////////
// TAKEOFF //
////////////////////////////////////////////////
void srTakeoff(SHIP &ship, ROCK *rocks, STARFIELD *stars, SHOT *shots){
float sy;
int a, temp_col;
// Play starting sound.
dbPlaySound(1);
dbLoopMusic(1);
// Lift-Off
dbHideAllSprites();
dbShowSprite(1);
for(a = 0; a < 100; a++){
dbCLS(0);
for(register int b = 0; b < STAR_NUM; b++){
sy = stars[b].y;
temp_col = (int)((255 / STAR_LAYERS) * stars[b].z);
dbDot((int)stars[b].x, (int)sy, dbRgb(temp_col, temp_col, temp_col));
sy += STAR_SPEED * stars[b].z;
if(sy > 600) sy = 0;
stars[b].y = sy;
}
ship.y--;
dbSprite(1, (int)ship.x, (int)ship.y, 1);
dbSync();
}
dbStopSound(1);
dbDeleteSound(1);
}
////////////////////////////////////////////////
// UPDATE SCREEN //
////////////////////////////////////////////////
bool srUpdateScreen(SHIP &ship, ROCK *rocks, STARFIELD *stars, SHOT *shots, int &points){
static int rock_del = ROCK_SPAWN, fade = 255, pnt_del = PNT_DEL;
int a, temp_col;
float sy;
// Update screen.
dbCLS(0);
for(a = 0; a < STAR_NUM; a++){
sy = stars[a].y;
temp_col = (int)((255 / STAR_LAYERS) * stars[a].z);
dbDot((int)stars[a].x, (int)sy, dbRgb(temp_col, temp_col, temp_col));
sy += STAR_SPEED * stars[a].z;
if(sy > 600) sy = 0;
stars[a].y = sy;
}
if(ship.state){
dbSprite(1, (int)ship.x, (int)ship.y, 1);
dbSetSpriteAlpha(1, fade);
}
if(ship.state == alive){
if(pnt_del) pnt_del--;
else {
pnt_del = PNT_DEL;
points++;
}
}
if(ship.state == explode){
dbSprite(NUM_ROCKS + 2, (int)ship.x, (int)ship.y, ship.frame + 25);
if(!(--ship.delay)){
fade -= 15;
if(fade < 0) fade = 0;
ship.delay = EXPLODE_DELAY;
if((ship.frame++) > 20) {
ship.state = dead;
ship.frame = 1;
ship.delay = EXPLODE_DELAY;
dbHideSprite(NUM_ROCKS + 2);
dbDeleteSprite(1);
dbDeleteImage(1);
dbLoadImage("media\\gameover.bmp", 1);
dbSprite(1, 400, 300, 1);
dbOffsetSprite(1, 150, 15);
dbSetSpritePriority(1, 1);
}
}
}
if(!(--rock_del)){
rock_del = ROCK_SPAWN;
if(ROCK::cur_rocks < NUM_ROCKS){
for(a = 0; a < NUM_ROCKS; a++){
if(!rocks[a].state){
rocks[a].state = alive;
ROCK::cur_rocks++;
dbShowSprite(rocks[a].num);
break;
}
}
}
}
for(a = 0; a < NUM_ROCKS; a++){
if(rocks[a].state){
rocks[a].x += (rocks[a].speed * dbCos(rocks[a].angle));
rocks[a].y += (rocks[a].speed * dbSin(rocks[a].angle));
if(rocks[a].x < 0 || rocks[a].x > 800 ||
rocks[a].y < 0 || rocks[a].y > 600) {
rocks[a].angle = (float)(45 + dbRnd(90));
rocks[a].delay = ROCK_DELAY;
rocks[a].frame = 1 + dbRnd(7);
rocks[a].size = RSIZE_MUL * (2 + dbRnd(RSIZE_FACTOR));
rocks[a].speed = (MIN_RSPEED + (RSPEED_MUL * (dbRnd(RSPEED_FACTOR) - RSPEED_SUB)));
if(rocks[a].speed < MIN_RSPEED) rocks[a].speed = MIN_RSPEED;
rocks[a].state = dead;
switch((int)rocks[a].size){
case 100:
rocks[a].hits = SML_HITS;
break;
case 150:
rocks[a].hits = MED_HITS;
break;
case 200:
rocks[a].hits = BIG_HITS;
break;
}
rocks[a].x = (float)dbRnd(800);
rocks[a].y = 0;
dbScaleSprite(rocks[a].num, rocks[a].size);
dbHideSprite(rocks[a].num);
ROCK::cur_rocks--;
}
if(rocks[a].state == alive){
if(!(--rocks[a].delay)) {
rocks[a].delay = ROCK_DELAY;
if(++rocks[a].frame > 8) rocks[a].frame = 1;
}
}
if(rocks[a].state == explode){
if(!(--rocks[a].delay)){
rocks[a].delay = EXPLODE_DELAY;
if(++rocks[a].frame > 24){
rocks[a].angle = (float)(45 + dbRnd(90));
rocks[a].delay = ROCK_DELAY;
rocks[a].frame = 1 + dbRnd(7);
rocks[a].size = RSIZE_MUL * (2 + dbRnd(RSIZE_FACTOR));
rocks[a].speed = (MIN_RSPEED + (RSPEED_MUL * (dbRnd(RSPEED_FACTOR) - RSPEED_SUB)));
if(rocks[a].speed < MIN_RSPEED) rocks[a].speed = MIN_RSPEED;
rocks[a].state = dead;
switch((int)rocks[a].size){
case 100:
rocks[a].hits = SML_HITS;
break;
case 150:
rocks[a].hits = MED_HITS;
break;
case 200:
rocks[a].hits = BIG_HITS;
break;
}
rocks[a].x = (float)dbRnd(800);
rocks[a].y = 0;
dbScaleSprite(rocks[a].num, rocks[a].size);
dbHideSprite(rocks[a].num);
}
}
}
dbSprite(rocks[a].num, (int)rocks[a].x, (int)rocks[a].y, rocks[a].frame + 1);
}
}
if(ship.rof) ship.rof--;
if(SHOT::cur_shots){
for(a = 0; a < MAX_SHOTS; a++){
if(shots[a].active){
shots[a].y -= SHOT_SPEED;
if(shots[a].y < 0){
shots[a].active = false;
SHOT::cur_shots--;
} else {
dbInk(dbRgb(0, 200, 0), dbRgb(0, 0, 0));
dbLine((int)shots[a].x, (int)shots[a].y, (int)shots[a].x, (int)(shots[a].y + SHOT_LEN));
}
}
}
}
if(ship.state == dead){
fade++;
dbSprite(1, 400, 300, 1);
if(dbScanCode() || fade > GAME_OVER){
dbHideSprite(1);
fade = 255;
return true;
}
}
dbSetTextSize(16);
dbInk(dbRgb(255, 0, 0), dbRgb(0, 0, 0));
dbSetCursor(2, 2);
dbPrintC("Score: ");
dbPrint((LONGLONG)points);
return false;
}
////////////////////////////////////////////////
// COLLISION TEST //
////////////////////////////////////////////////
void srCollision(SHIP &ship, ROCK *rocks, SHOT *shots, int &points){
float distance, ax, ay;
int a;
if(ship.state == alive){
for(a = 0; a < NUM_ROCKS; a++){
if(rocks[a].state == alive){
ax = dbAbs(ship.x - rocks[a].x);
ay = dbAbs(ship.y - rocks[a].y);
distance = dbSqrt((ax * ax) + (ay * ay));
if(((12.7279 * rocks[a].size) / 100) + 28 > distance){
dbSprite(NUM_ROCKS + 2, (int)ship.x, (int)ship.y, ship.frame + 25);
dbShowSprite(NUM_ROCKS + 2);
ship.state = explode;
dbPlaySound(2);
dbDeleteSprite(NUM_ROCKS + 3);
dbDeleteImage(46);
break;
}
}
}
}
if(SHOT::cur_shots){
for(a = 0; a < NUM_ROCKS; a++){
if(rocks[a].state == alive){
for(register int b = 0; b < MAX_SHOTS; b++){
if(shots[b].active){
ax = dbAbs(shots[b].x - rocks[a].x);
ay = dbAbs(shots[b].y - rocks[a].y);
distance = dbSqrt((ax * ax) + (ay * ay));
if(((12.7279 * rocks[a].size) / 100) > distance){
SHOT::cur_shots--;
shots[b].active = false;
dbPlaySound(3);
if(rocks[a].hits) rocks[a].hits--;
if(!rocks[a].hits){
rocks[a].state = explode;
rocks[a].delay = EXPLODE_DELAY;
rocks[a].frame = 9;
dbPlaySound(4);
switch((int)rocks[a].size){
case 100:
points += SML_PNT;
break;
case 150:
points += MED_PNT;
break;
case 200:
points += BIG_PNT;
break;
}
ROCK::cur_rocks--;
}
}
}
}
}
}
}
}
////////////////////////////////////////////////
// CONTROLS //
////////////////////////////////////////////////
void srControl(SHIP &ship, STARFIELD *stars, SHOT *shots){
int code = 0, s = 0, control = 0;
if(ship.state == alive){
// Get the user's input.
if(dbLeftKey()) control += 1;
if(dbRightKey()) control += 2;
if(dbUpKey()) control += 4;
if(dbDownKey()) control += 8;
if(dbSpaceKey()){
if(!ship.rof){
for(register int a = 0; a < 2; a++){
if(SHOT::cur_shots < MAX_SHOTS){
while(s < MAX_SHOTS && shots[s].active) s++;
shots[s].x = ((a)?(ship.x + 2):(ship.x - 2));
shots[s].y = ship.y - 26;
shots[s].active = true;
SHOT::cur_shots++;
dbPlaySound(5);
}
}
ship.rof = GUN_ROF;
}
}
if(dbReturnKey()){
int a, temp_col;
dbCLS(0);
dbSetSpritePriority(NUM_ROCKS + 3, 1);
dbShowSprite(NUM_ROCKS + 3);
for(a = 0; a < STAR_NUM; a++){
temp_col = (int)((255 / STAR_LAYERS) * stars[a].z);
dbDot((int)stars[a].x, (int)stars[a].y, dbRgb(temp_col, temp_col, temp_col));
}
dbSync();
dbWait(1000);
for(;;){
for(a = 0; a < STAR_NUM; a++){
temp_col = (int)((255 / STAR_LAYERS) * stars[a].z);
dbDot((int)stars[a].x, (int)stars[a].y, dbRgb(temp_col, temp_col, temp_col));
}
dbSprite(NUM_ROCKS + 3, 400, 300, 46);
if(dbReturnKey() || dbEscapeKey()){
dbCLS(0);
dbSetSpritePriority(NUM_ROCKS + 3, 1);
dbShowSprite(NUM_ROCKS + 3);
for(a = 0; a < STAR_NUM; a++){
temp_col = (int)((255 / STAR_LAYERS) * stars[a].z);
dbDot((int)stars[a].x, (int)stars[a].y, dbRgb(temp_col, temp_col, temp_col));
}
dbSync();
dbWait(500);
dbHideSprite(NUM_ROCKS + 3);
break;
}
dbSync();
}
}
switch(control) {
case 1:
case 13:
ship.x -= SHIP_SPEED;
break;
case 2:
case 14:
ship.x += SHIP_SPEED;
break;
case 4:
case 7:
ship.y -= SHIP_SPEED;
break;
case 8:
case 11:
ship.y += SHIP_SPEED;
break;
case 5:
ship.x -= SHIP_DSPEED;
ship.y -= SHIP_DSPEED;
break;
case 6:
ship.x += SHIP_DSPEED;
ship.y -= SHIP_DSPEED;
break;
case 9:
ship.x -= SHIP_DSPEED;
ship.y += SHIP_DSPEED;
break;
case 10:
ship.x += SHIP_DSPEED;
ship.y += SHIP_DSPEED;
break;
}
if(ship.x < 32) ship.x = 32;
if(ship.x > 768) ship.x = 768;
if(ship.y < 32) ship.y = 32;
if(ship.y > 568) ship.y = 568;
}
}