topical media & game development

talk show tell print

lib-game-darkgdk-samples-LType-Main.cpp / cpp



  
  // Dark GDK - The Game Creators - www.thegamecreators.com
  
  // include Dark GDK header file
  include <DarkGDK.h>
  
  // Globals 
//////////////////

int gameover = 0; float gwrapspeed = 0; int strapx1 = 0; int strapx2 = 1024; int exc [ 10 ]; int bestgame = 0; int bestmiles = 1000; float fuel = 100.0f; float sx = 0; float sy = 0; int miles = 0; int sc = 0; int lift = 0; int firegun = 0; int killship = 0; float ix = 0.0f; float iy = 0.0f; float gh = 0.0f; float srx = 0.0f; float tiltc = 0.0f; float gwrap = 0.0f; float mh = 0.0f; float h = 0.0f; float platy = 0.0f; int objid = 0; float dx = 0; float dy = 0; int guncool = 0; int bobj = 0; int gameovercount = 0; int shipexplosion = 0; int inewmiles = 0; int ex = 0;
////////////////////////////

// Functions
////////////////

void newMatrix ( void ); void createStrap ( void ); void controlStrap ( void );
////////////////////////////

void DarkGDK ( void ) { dbSetDir ( "media\\" ); dbSyncOn ( ); dbSyncRate ( 60 ); dbBackdropOff ( ); dbHideMouse ( ); // Loading screen dbLoadBitmap ( "media\\gfx\\backdrop.jpg" , 1 ); dbCopyBitmap ( 1 , 0 , 0 , 640 , 480 , 0 , 0 , 0 , dbScreenWidth ( ) , dbScreenHeight ( ) ); dbSync ( ); dbDeleteBitmap ( 1 ); // Setup strapline createStrap ( ); // Load sky dbSetCameraRange ( 1 , 10000 ); dbLoadObject ( "media\\sky\\ds.x" , 1 ); dbSetObjectTexture ( 1 , 2 , 1 ); dbSetObjectLight ( 1 , 0 ); // Load ship dbLoadObject ( "media\\ship\\ship.x" , 101 ); dbScaleObject ( 101 , 50 , 50 , 50 ); dbXRotateObject ( 101 , 270 ); dbFixObjectPivot ( 101 ); dbRotateObject ( 101 , 0 , 0 , 0 ); dbSetObjectSpecular ( 101 , 0 ); // Create landscape dbLoadImage ( "media\\ground\\ground.jpg" , 1 ); dbMakeMatrix ( 1 , 3000 , 1500 , 15 , 15 ); dbPrepareMatrixTexture ( 1 , 1 , 1 , 1 ); dbPositionMatrix ( 1 , -750 , -300 , 0 ); newMatrix ( ); // Ship exhaust dbLoadImage ( "media\\gfx\\plasma.jpg" , 11 ); dbLoadImage ( "media\\gfx\\fire.jpg" , 12 ); dbLoadImage ( "media\\gfx\\debris.jpg" , 13 ); dbMakeParticles ( 1 , 11 , 20 , 35.0 ); dbPositionParticles ( 1 , 0 , 0 , 500 ); dbRotateParticles ( 1 , 0 , 0 , 90 ); dbSetParticleVelocity ( 1 , 10.0 ); dbSetParticleLife ( 1 , 5 ); // Debris cloud dbMakeParticles ( 21 , 13 , 10 , 200.0f ); dbPositionParticles ( 21 , 0 , 0 , 500 ); dbSetParticleVelocity ( 21 , 40.0 ); dbSetParticleEmissions ( 21 , 0 ); dbSetParticleSpeed ( 21 , 0.03f ); dbRotateParticles ( 21 , 90 , 0 , 0 ); dbSetParticleLife ( 21 , 10 ); dbHideParticles ( 21 ); // Load scuds and rockets int objid = 0; for ( int sc = 1 ; sc < 10 ; sc++ ) { objid = 200 + sc; // scud dbLoadObject ( "media\\scud\\fire.x" , objid ); dbScaleObject ( objid , 1000 , 1000 , 1000 ); dbPositionObject ( objid , ( float ) sc * 50 , -100 , 500 ); dbSetObjectSpeed ( objid , 100 ); dbSetObjectLight ( objid , 0 ); dbYRotateObject ( objid , 70 ); dbHideLimb ( objid , 11 ); dbPlayObject ( objid ); dbHideObject ( objid ); // missile dbLoadObject ( "media\\scud\\scud.x" , 100 + objid ); dbScaleObject ( 100 + objid , 500 , 500 , 500 ); dbSetObjectLight ( 100 + objid , 0 ); dbHideObject ( 100 + objid ); dbXRotateObject ( 100 + objid , 90 ); dbFixObjectPivot ( 100 + objid ); // particle for missile dbMakeParticles ( 1 + sc , 12 , 10 , 30.0 ); dbSetParticleLife ( 1 + sc , 10 ); dbHideParticles ( 1 + sc ); } // Make explosion objects dbLoadBitmap ( "media\\explosion\\explode.jpg" , 1 ); float xx = 512.0f / 4.0f; float yy = 512.0f / 4.0f; for ( int y = 0 ; y < 3 ; y++ ) { for ( int x = 0 ; x < 4 ; x++ ) { dbGetImage ( 501 + x + ( y * 4 ) , ( x * ( int ) xx ) , ( y * ( int ) yy ) , ( x * ( int ) xx ) + ( int ) xx , ( y * ( int ) yy ) + ( int ) yy ); } } dbGetImage ( 513 , 0 , 0 , 32 , 32 ); dbGetImage ( 514 , 0 , 0 , 32 , 32 ); dbGetImage ( 515 , 0 , 0 , 32 , 32 ); dbGetImage ( 516 , 0 , 0 , 32 , 32 ); for ( ex = 1 ; ex < 6 ; ex++ ) { dbMakeObjectPlane ( 500 + ex , 256 , 256 ); dbPositionObject ( 500 + ex , 0 , 0 , 500 ); dbSetObjectLight ( 500 + ex , 0 ); dbGhostObjectOn ( 500 + ex , 2 ); dbSetObject ( 500 + ex , 1 , 1 , 1 ); dbHideObject ( 500 + ex ); } dbDeleteBitmap ( 1 ); // Create laser for ship int bobj = 0; dbLoadImage ( "media\\gfx\\laser.jpg" , 102 ); for ( int bu = 1 ; bu < 6 ; bu++ ) { bobj = 101 + bu; dbMakeObjectPlane ( bobj , 256 , 64 ); dbTextureObject ( bobj , 102 ); dbPositionObject ( bobj , 0 , 0 , 500 ); dbSetObjectLight ( bobj , 0 ); dbGhostObjectOn ( bobj , 2 ); dbHideObject ( bobj ); } // Load gameover image dbLoadImage ( "media\\gfx\\gameover.jpg" , 601 ); dbLoadImage ( "media\\gfx\\bestmiles.jpg" , 602 ); // Load sounds dbLoadSound ( "media\\sounds\\jet.wav" , 1 ); dbLoopSound ( 1 ); dbLoadSound ( "media\\sounds\\fire.wav" , 2 ); dbLoadSound ( "media\\sounds\\shipbang.wav" , 3 ); dbLoadSound ( "media\\sounds\\rocket.wav" , 4 ); dbLoadSound ( "media\\sounds\\scudbang.wav" , 5 ); dbLoadSound ( "media\\sounds\\start.wav" , 6 ); dbLoadSound ( "media\\sounds\\gameover.wav" , 7 ); // Setup camera dbSetCameraFOV ( 30 ); dbPositionCamera ( 0 , 0 , -1000 ); dbRotateCamera ( 0 , 0 , 0 ); dbPlaySound ( 6 ); // Setup arrays and vars bestmiles = 1000; gwrapspeed = 0.0f; fuel = 100.0f; gameover = 0; // Ensure random different dbRandomize ( dbTimer ( ) ); // loop until the escape key is pressed while ( LoopGDK ( ) ) { if ( dbEscapeKey ( ) ) return; // Control scroll speed if ( gameover == 0 ) { if ( gwrapspeed < 8.0f ) gwrapspeed += 0.1f; } else { // game over prompt dbPositionCamera ( dbCameraPositionX ( 0 ) , dbCameraPositionY ( 0 ) + 3.0f , dbCameraPositionZ ( 0 ) ); if ( bestgame == 0 ) dbPasteImage ( 601 , ( dbScreenWidth ( ) - 512 ) / 2 , ( dbScreenHeight ( ) - 256 ) / 2,1 ); if ( bestgame == 1 ) dbPasteImage ( 602 , ( dbScreenWidth ( ) - 512 ) / 2 , ( dbScreenHeight ( ) - 256 ) / 2 , 1 ); gameovercount--; // restart game if ( gameovercount <= 0 ) { // reset camera dbPositionCamera ( 0 , 0 , -1000 ); // reset ship dbRotateObject ( 101 , 0 , 0 , 0 ); dbShowParticles ( 1 ); dbShowObject ( 101 ); sx = 0; sy = 0; fuel = 100.0f; miles = 0; dbLoopSound ( 1 ); // reset matrix newMatrix ( ); // reset scuds for ( int sc = 1 ; sc < 10 ; sc++ ) { dbHideObject ( 200 + sc ); dbPositionObject ( 200 + sc , 10000 , 0 , 0 ); } // reset missiles for ( int sc = 1 ; sc < 10 ; sc++ ) { dbHideObject ( 300 + sc ); dbHideParticles ( 1 + sc ); } // reset game dbPlaySound ( 6 ); gameover = 0; bestgame = 0; miles = 0; } } // Control ship lift = 0; firegun = 0; if ( gameover == 0 && killship == 0 ) { if ( dbUpKey ( ) == 1 && sy < 350.0f ) { iy = 6.0f; lift = 1; } if ( fuel < 2.0f ) iy = -3.0f; if ( dbDownKey ( ) == 1 ) { iy = -6.0; lift = 2; } if ( dbLeftKey ( ) == 1 && sx > -450.0f ) ix = -6.0; if ( dbRightKey ( ) == 1 && sx < 450.0 ) ix = 6.0; if ( dbShiftKey ( ) == 1 ) firegun = 1; ix /= 1.2f; iy /= 1.2f; sx += ix; sy += iy; dbPositionObject ( 101 , sx , sy , 500 ); } // Ensure ship cannot enter ground gh = dbGetMatrixHeight ( 1 , 7 + ( ( ( int ) sx - 200 ) / 200 ) , 5 ) - 300; if ( sy < gh && gameover == 0 ) { // ship explodes if ( dbAbs ( sy - gh ) > 30.0 ) { killship = 1; } else { sy = gh; } } // Animate ship if ( lift == 1 ) srx += 0.8f; if ( lift == 2 ) srx -= 0.8f; dbXRotateObject ( 101 , srx ); srx /= 1.05f; // Make particles emerge from ship dbPositionParticles ( 1 , sx - 20 , sy + 5 , 500 + 25 ); // Control wrapping sky dbYRotateObject ( 1 , dbWrapValue ( dbObjectAngleY ( 1 ) - ( gwrapspeed / 32.0f ) ) ); // Control tilt of camera tiltc = dbCurveAngle ( iy * -1.0f , tiltc , 20.0f ); dbZRotateCamera ( 0 , tiltc ); // Control ground wrapping gwrap += gwrapspeed; if ( gwrap >= 400.0 ) { // shift old ground gwrap -= 400.0f; dbShiftMatrixLeft ( 1 ); dbShiftMatrixLeft ( 1 ); // create new ground mh = ( float ) dbRnd ( 500 ); dbSetMatrixHeight ( 1 , 13 , 0 , 0 ); dbSetMatrixHeight ( 1 , 14 , 0 , 0 ); //int h; for ( int z = 1 ; z < 14 ; z++ ) { float h = mh * dbCos ( ( float ) ( z - 7 ) * 20 ); dbSetMatrixHeight ( 1 , 13 , z , h ); dbSetMatrixHeight ( 1 , 14 , z , h ); if ( z == 5 ) platy = h; mh += ( dbRnd ( 20 ) - 10 ); } dbSetMatrixHeight ( 1 , 13 , 14 , 0 ); dbSetMatrixHeight ( 1 , 14 , 14 , 0 ); dbUpdateMatrix ( 1 ); // if random chance if ( dbRnd ( 3 ) == 0 ) { // place scud int sc; for ( sc = 1 ; sc < 10 ; sc++ ) { objid = 200 + sc; if ( dbObjectVisible ( objid ) == 0 && dbObjectVisible ( 100 + objid ) == 0 ) break; } if ( sc <= 9 ) { dbPositionObject ( objid , ( 3000.0f - 750.0f ) - 500.0f , -300.0f + platy , 500.0f ); dbYRotateObject ( objid , 45.0f + dbRnd ( 60 ) ); dbShowObject ( objid ); } } } dbPositionMatrix ( 1 , -750 -200 -gwrap , -300 , 0 ); // Control scuds for ( int sc = 1 ; sc < 10 ; sc++ ) { // keep with ground objid = 200 + sc; dbPositionObject ( objid , dbObjectPositionX ( objid ) -gwrapspeed , dbObjectPositionY ( objid ) , dbObjectPositionZ ( objid ) ); // if close to X of ship, fire if ( dbAbs ( ( sx + 400 + dbRnd ( 200 ) ) - dbObjectPositionX ( objid ) ) < 200.0 && dbObjectPlaying ( objid ) == 0 && gameover == 0 ) { // fire animation dbLoopObject ( objid , 25 , 25 ); dbSetObjectFrame ( objid , 0 ); } if ( dbObjectPlaying (objid) == 1 && dbObjectFrame (objid) > 10 && dbObjectVisible ( objid ) == 1 && ( dbObjectVisible ( 100 + objid ) == 0 )) { // position missile dbPositionObject ( 100 + objid , dbObjectPositionX ( objid ) + 50 , dbObjectPositionY ( objid ) + 10 , dbObjectPositionZ ( objid ) ); dbRotateObject ( 100 + objid , 270 , 0 , 60 ); dbPositionParticles ( 1 + sc , 50 , -300 , 0 ); dbShowObject ( 100 + objid ); dbShowParticles ( 1 + sc ); dbPlaySound ( 4 ); } // keep missiles with ground too dbPositionObject ( 100 + objid , dbObjectPositionX ( 100 + objid ) - 8.0f , dbObjectPositionY ( 100 + objid ) , dbObjectPositionZ ( 100 + objid ) ); dbPositionParticles ( 1 + sc , dbParticlesPositionX ( 1 + sc ) - 8.0f , -300 ,0 ); // when off screen, reset if ( dbObjectPositionX ( objid ) < -600 ) { dbHideObject ( objid ); dbStopObject ( objid ); dbSetObjectFrame ( objid , 0 ); } // control missile if ( dbObjectVisible ( 100 + objid ) == 1 ) { // move until leave dbPositionObject ( 100 + objid , dbObjectPositionX ( 100 + objid ) - gwrapspeed , dbObjectPositionY ( 100 + objid ) + 4.0f , dbObjectPositionZ ( 100 + objid ) ); // when leave screen if ( dbObjectPositionX ( 100 + objid ) < -3000 || dbObjectPositionY ( 100 + objid ) > 2000 ) { dbHideObject ( 100 + objid ); dbHideParticles ( 1 + sc ); } } if ( dbObjectVisible ( 100 + objid ) == 1 ) { // when hit ship dx = dbAbs ( dbObjectPositionX ( 100 + objid ) - sx ); dy = dbAbs ( dbObjectPositionY ( 100 + objid ) - sy ); if ( dx < 10 && dy < 50 ) killship = 1 ; } } // Fire the ships gun int bobj = 0; int bopbj = 0; if ( guncool > 0 ) guncool--; if ( firegun == 1 && guncool == 0 ) { for ( int bu = 1 ; bu < 6 ; bu++ ) { bobj = 101 + bu; if ( dbObjectVisible ( bobj ) == 0 ) break; } if ( bopbj <= 106 ) { dbPositionObject ( bobj , sx , sy , 500 + 25 ); dbShowObject ( bobj ); guncool = 5; fuel -= 0.05f; dbPlaySound ( 2 ); } } // Handle bullets for ( int bu = 1 ; bu < 5 ; bu++) { bobj = 101 + bu; if ( dbObjectVisible ( bobj ) == 1 ) { // move bullet dbPositionObject ( bobj , dbObjectPositionX ( bobj ) + 64 ,dbObjectPositionY ( bobj ) , dbObjectPositionZ ( bobj ) ); if ( dbObjectPositionX ( bobj ) > 500 ) dbHideObject ( bobj ); } if ( dbObjectVisible ( bobj ) == 1 ) { // ensure bullet cannot enter ground gh = dbGetMatrixHeight( 1, 7 + ( int ) ( dbObjectPositionX ( ( bobj ) - 200 ) / 200 ), 4 ) - 300; if ( dbObjectPositionY ( bobj ) < gh ) dbHideObject ( bobj ); // bullet can destroy scuds for ( int sc = 1 ; sc < 10 ; sc++ ) { dx = dbAbs ( dbObjectPositionX ( bobj ) - dbObjectPositionX ( 200 + sc ) ); dy = dbAbs ( dbObjectPositionY ( bobj ) - dbObjectPositionY ( 200 + sc ) ); if ( dx < 50 && dy < 50.0 && dbObjectVisible ( 200 + sc ) == 1 ) { // trigger scud explosion dbHideObject ( bobj ); dbHideObject ( 200 + sc ); int ex; for ( ex = 2 ; ex < 6 ; ex++ ) { if ( exc [ ex ] == 0 ) { break; } } if ( ex <= 5 ) { exc[ ex ] = 501; dbPositionObject ( 500 + ex , dbObjectPositionX ( 200 + sc ) , dbObjectPositionY ( 200 + sc ) , 500 + 25 ); dbShowObject ( 500 + ex ); } // gain some fuel fuel += 10.0; if ( fuel > 100.0 ) fuel = 100.0; // make a noise dbPlaySound ( 5 ); } } } } // Trigger ship explosion if ( killship == 1 && shipexplosion == 0 ) { ex = 1; exc [ ex ] = 501; dbPositionObject ( 500 + ex , sx - 20 , sy + 5 , 500 + 25 ); dbShowObject ( 500 + ex ); dbHideParticles ( 1 ); dbPositionParticles ( 21 , sx - 20 , sy + 5 , 500 + 25 ); dbSetParticleEmissions ( 21 , 20 ); dbShowParticles ( 21 ); dbPlaySound ( 3 ); shipexplosion = 1; } // Control explosions for ( ex = 1 ; ex < 6 ; ex++ ) { if ( exc [ ex ] > 0 ) { if (exc[ex] > 0 ) exc[ex]++; if ( exc [ ex ] > 515 ) { // end particle and explosion // fix dbSetParticleEmissions ( 21 , 0 ); dbHideObject ( 500 + ex ); exc[ex] = 0; if ( ex == 1 ) { // destroy ship dbStopSound ( 1 ); dbHideObject ( 101 ); shipexplosion = 0; killship = 0; gameover = 1; gameovercount = 200; dbPlaySound ( 7 ); } } else { // update explosion dbTextureObject ( 500 + ex , exc [ex] ); if ( ex == 1 ) { // rock ship dbRotateObject ( 101 , dbObjectAngleX ( 101 ) + 10 , dbObjectAngleY ( 101 ) + 5 , dbObjectAngleZ ( 101 ) + 15 ); } } } } // During game if ( gameover == 0 ) { // Reduce fuel slowly fuel -= 0.05f; if ( fuel < 0 ) fuel=0.0f; if ( inewmiles++ > 100 ) { miles++; inewmiles = 0; } // Gain miles slowly //miles++; //miles += 0.1f; if ( miles > 100 ) miles = 100; } // Show fuel int rr; dbSetCursor ( 0 , 0 ); dbPrint ( "FUEL" ); ( fuel < 20 ) ? rr = 255 : rr = 0; dbBox ( 32 , 2 , 38 + ( ( dbScreenWidth ( ) - 16 ) / 100 ) * ( int ) fuel , 10 , dbRGB ( rr , 100 , 100 ), dbRGB ( rr , 100 , 100 ) , dbRGB ( rr , 255 , 255 ) , dbRGB ( rr , 255 , 255 ) ); // Show miles dbSetCursor ( 0 , 13 ); dbPrint ( "MILES" ); dbBox ( 32 , 15 , 38 + ( ( dbScreenWidth ( ) - 16 ) / 100 ) * miles , 15 + 8 , dbRGB ( 0 , 100 , 0 ) , dbRGB( 0 , 100 , 0 ) , dbRGB ( 0 , 255 , 0 ), dbRGB ( 0 , 255 , 0 ) ); dbSetCursor (( 38 + ( ( dbScreenWidth ( ) - 16 ) / 100 ) * miles ) + 4 , 13 ); // Record and show best miles dbSetCursor ( dbScreenWidth ( ) - 110 , 13 ); if ( miles > bestmiles ) { bestmiles = miles; bestgame = 1; } // Move strap line controlStrap ( ); // Endloop dbSync ( ); } } void newMatrix ( void ) { float tr = 50; float h = 0; float n = 0; for ( int x = 0 ; x < 16 ; x++ ) { dbSetMatrixHeight ( 1 , x , 0 , 0 ); for ( int z = 1 ; z < 15 ; z++ ) { h = dbCos ( ( ( z - 7 ) * 20 ) * 100.0f ); h += tr; if ( x < 13 ) h = 250.0f; n = 0.25f + ( dbRnd ( 10 ) * 0.75f ); n = ( float ) n * dbCos ( ( ( float ) z - 8 ) * 20 ); dbSetMatrixHeight ( 1 , x , z , h ); dbSetMatrixNormal ( 1 , x , z , n , n , n ); } dbSetMatrixHeight ( 1 , x , 14 , 0 ); tr += ( dbRnd ( 50 ) - 25 ); } dbUpdateMatrix ( 1 ); } void createStrap ( void ) { dbLoadImage ( "media\\gfx\\prompt2.jpg" , 2 ); dbLoadImage ( "media\\gfx\\strapblank.jpg" , 3 ); strapx1 = 0; strapx2 = 1024; } void controlStrap ( void ) { strapx1--; if ( strapx1 <= -1024 ) strapx1 = strapx2 + 1023; strapx2--; if ( strapx2 <= -1023 ) strapx2 = strapx1 + 1024; dbSprite ( 1 , strapx1 , dbScreenHeight ( ) -63 , 2 ); dbSprite ( 2 , strapx2 , dbScreenHeight ( ) -63 , 3 ); dbSetSpriteAlpha ( 1 , 196 ); dbSetSpriteAlpha ( 2 , 196 ); dbSetSprite ( 1 , 0 , 1 ); }


(C) Æliens 04/09/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.