/* Filename: MouseInput.cpp * * Author: Br. Isidore Minerd * * Date: July 31, 2009 * * Description: This application allows the user to move a space ship around the screen * using the arrow keys which are defined as follows: * UP => Forward * DOWN => Backward * LEFT => Rotate Counter-Clockwise * RIGHT => Rotate Clockwise * * Images used with permission from: * The Game Creators * (http://www.thegamecreators.com) * and * Mr. Mark Simpson (deviantART pseudoname "PrinzEugn") * (http://prinzeugn.deviantart.com/) */ #include "DarkGDK.h" #include "math.h" const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; // Function Prototypes void spriteWrapAround(int spriteId); void DarkGDK ( void ) { float spriteVelocity = 10.0f, spriteRotationAmount = 7.5f, spriteAngle = 0.0f, asteroidVelocity = 15.0; long startTime,elapsedTime,elapsedHoursPart,elapsedMinutesPart,elapsedSecondsPart; char endText[256]; dbSyncOn(); dbSyncRate(10); // These first several function calls should look familiar dbSetWindowTitle("A Simple Asteroid Avoiding Game"); dbSetDisplayMode(SCREEN_WIDTH,SCREEN_HEIGHT,32); // Load very basic sprites dbLoadImage("Space.png", 1); dbLoadImage("Ship.png", 2); dbLoadImage("Asteroid.png", 3); dbSprite(1,0,0,1); dbSprite(2,300,300,2); dbSprite(3,50,50,3); // The offset must be moved in order to rotate around the center of the ship image // instead of its upper-left corner dbOffsetSprite(2,dbSpriteWidth(2) / 2, dbSpriteHeight(2) / 2); dbOffsetSprite(3,dbSpriteWidth(3) / 2, dbSpriteHeight(3) / 2); dbRandomize(dbTimer()); dbRotateSprite(3,dbRND(360)); startTime = dbTimer(); while(LoopGDK() && !dbSpriteHit (2,3)) { // Check for forward / backward thrust if(dbUpKey()) { dbMoveSprite(2,spriteVelocity); } else if (dbDownKey()) { dbMoveSprite(2,-spriteVelocity); } // Check for rotation if(dbLeftKey()) { spriteAngle -= spriteRotationAmount; dbRotateSprite(2,spriteAngle); } else if(dbRightKey()) { spriteAngle += spriteRotationAmount; dbRotateSprite(2,spriteAngle); } dbMoveSprite(3,asteroidVelocity); // See if the sprites went off the screen and act appropriately spriteWrapAround(2); spriteWrapAround(3); dbSync(); } elapsedTime = (dbTimer() - startTime) / 1000; elapsedHoursPart = elapsedTime / 3600; elapsedMinutesPart = (elapsedTime % 3600) / 60; elapsedSecondsPart = elapsedTime % 60; dbHideAllSprites(); for(int i=1; i <= 3; i++) { dbDeleteSprite(i); dbDeleteImage(i); } // Display end-of-game stats dbCLS(dbRgb(0,0,0)); dbInk(dbRgb(255,0,0),dbRgb(0,0,0)); _snprintf(endText,256,"You lasted %d hours:%d minutes:%d seconds",elapsedHoursPart,elapsedMinutesPart,elapsedSecondsPart); dbCenterText(SCREEN_WIDTH/2,SCREEN_HEIGHT/2 - 15,"Game Over"); dbCenterText(SCREEN_WIDTH/2,SCREEN_HEIGHT/2,endText); dbCenterText(SCREEN_WIDTH/2,SCREEN_HEIGHT/2 + 15,"Press esc to exit"); dbSync(); // A very simple way to prevent dbWaitKey from picking up any held down buttons. while(LoopGDK()){dbWait(1);} } /* Given: spriteId The ID of the sprite which is to be tested for screen boundary wrap-around Task: Check whether the given sprite has gone of the screen. If so, it is moved appropriately in order to wrap it around the screen. Return: None Notes: This is very generalized and therefore wastes more processing time than really is necessary. It probably should be a macro, but this is not an intense application. */ void spriteWrapAround(int spriteId) { int spriteX = dbSpriteX(spriteId),offsetX = dbSpriteOffsetX(spriteId), offsetSpriteX = spriteX - offsetX, spriteY = dbSpriteY(spriteId), offsetY = dbSpriteOffsetY(spriteId),offsetSpriteY = spriteY - offsetY, spriteWidth = dbSpriteWidth(spriteId), spriteHeight = dbSpriteHeight(spriteId); /* NB: In each "else" statement, "spriteHeight" is used because this presupposes that forward-backward motion is the only type possible Therefore, the sprite passes even the left boundary when it goes its height off the screen in that direction. For our asteroid, it does not matter since it is square. We should take all of this into account by using the rotation of the sprite and other meta-data indicating the configuration of the sprite image within its total bounding box. */ if(offsetSpriteX > SCREEN_WIDTH) { dbSprite(spriteId,-spriteHeight + offsetY,spriteY,spriteId); } else if(offsetSpriteX < - spriteHeight) { dbSprite(spriteId,SCREEN_WIDTH,spriteY,spriteId); } if(offsetSpriteY > SCREEN_HEIGHT) { dbSprite(spriteId,spriteX,-spriteHeight + offsetY,spriteId); } else if(offsetSpriteY < -spriteHeight) { dbSprite(spriteId,spriteX,SCREEN_HEIGHT,spriteId); } }