Movement with cursor and X,Y tracking, some simple collision (2D) by dwilson3216th May 2005 21:50
|
---|
Summary This is a small C++ .net program that demonstrates simple 2D collision (very simple) and demonstrates the dbPrint function as well as some other simple db functions via the darkSDK Description Code ` This code was downloaded from The Game Creators ` It is reproduced here with full permission ` http://www.thegamecreators.com #include "iostream" #include "stdio.h" #include "string" #include "DarkSDK.h" using namespace std; class sphere_dude { public: int X, Y; char drawCursor; const static int R = 10; const static int iUpperXLimit = 400; const static int iLowerXLimit = 10; const static int iUpperYLimit = 400; const static int iLowerYLimit = 10; // Create a string to hold coords that will be printed to the screen. string X_string; sphere_dude () { // dummy constructor } void setStartingPoint (int XAdj, int YAdj) { Y = YAdj; X = XAdj; this->draw(X, Y); } void moveRight (int XAdj) { X = X + XAdj; this->draw(X, Y); } void moveLeft (int XAdj) { X = X - XAdj; this->draw(X, Y); } void moveDown (int YAdj) { Y = Y + YAdj; this->draw(X, Y); } void moveUp (int YAdj) { Y = Y - YAdj; this->draw(X, Y); } void draw (int X, int Y) { this->collisionCheck(); dbCircle (X, Y, R); } void drawLastKnown () { dbCircle (X, Y, R); } void collisionCheck () { if ((X - R) <= iLowerXLimit) { drawCursor = 'R'; X = iLowerXLimit + R; } if ((X + R) >= iUpperXLimit) { drawCursor = 'L'; X = iUpperXLimit - R; } if ((Y - R) <= iLowerYLimit) { drawCursor = 'B'; Y = iLowerYLimit + R; } if ((Y + R) >= iUpperYLimit) { drawCursor = 'T'; Y = iUpperYLimit - R; } } void returnPos (void) { X_string.append ( "X: " ); X_string.append ( dbStr(X) ); X_string.append ( " | Y: " ); X_string.append ( dbStr(Y) ); //dbPrint ( iX_string ); switch (drawCursor) { case 'T': dbSetCursor (X - (R * 6), (Y - (R * 5))); break; case 'B': dbSetCursor (X - (R * 6), (Y + (R * 5))); break; case 'R': dbSetCursor (X + (R * 6), Y - (R * 5)); break; case 'L': dbSetCursor (X - (R * 6), Y - (R * 5)); break; default: dbSetCursor (X - (R * 6), (Y - R * 5)); break; } dbPrint ( const_cast<char*>(X_string.c_str()) ); X_string = " "; } }; void DarkSDK ( void ) { // define and initialize the counter and switches int iCounter, iX_reverse, iY_reverse, iLeft, iUp, iRandom; iCounter = 0; iX_reverse = 0; iY_reverse = 0; iLeft = 0; iUp = 0; iRandom = 0; // Define our object sphereDude of type sphere_dude sphere_dude sphereDude; // define and initialize the coord variables and set movement speed. int iXSpeed, iYSpeed, iUpperXLimit, iLowerXLimit, iUpperYLimit, iLowerYLimit; int iX, iY; iXSpeed = 5; iYSpeed = 5; sphereDude.setStartingPoint (200, 200); // Create a string to hold coords and initialize cursor. string iX_string; dbSetCursor (50, 50); // set sync on and sync rate to 60 frames per second dbSyncOn ( ); dbSyncRate ( 60 ); // loop until the escape key is pressed while ( !dbEscapeKey ( ) ) { if (dbUpKey()==1) { sphereDude.moveUp(iXSpeed); } else { if (dbDownKey()==1) { sphereDude.moveDown(iXSpeed); } else { if (dbRightKey()==1) { sphereDude.moveRight(iYSpeed); } else { if (dbLeftKey()==1) { sphereDude.moveLeft(iYSpeed); } else { sphereDude.drawLastKnown(); } } } } sphereDude.returnPos(); //iX_string = sphereDude.returnPos(); iCounter = 0; dbSync ( ); dbCLS(); } } |