class for obstacles in Car_race game
Revision 11:c0922d4fda7b, committed 2017-05-04
- Comitter:
- fy14aaz
- Date:
- Thu May 04 12:04:51 2017 +0000
- Parent:
- 10:515df7535b2f
- Commit message:
- final version
Changed in this revision
Obstacles.cpp | Show annotated file Show diff for this revision Revisions of this file |
Obstacles.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/Obstacles.cpp Tue May 02 22:25:19 2017 +0000 +++ b/Obstacles.cpp Thu May 04 12:04:51 2017 +0000 @@ -12,81 +12,81 @@ void Obstacles::init(int seed) { - _Obstacle_x = seed; - _score = 0; + _Obstacle_pos_x = seed; + _score = 0; // initialise scores _totalscore = 0; - // try to draw them here in this function as we need the draw function for another purpose - // are we done though? we might need to split the draw function into two, one for drawing the car - // and the other for drawing the obstacles as they are drawn in a time basis + // printf("_Obstacle_pos_x=%d \n",_Obstacle_pos_x); } void Obstacles::draw(N5110 &lcd,int seed) { - _Obstacle_x = seed; - - switch(_Obstacle_x) { + _Obstacle_pos_x = seed; // give a random number to the x position of the obstacle + // printf("_Obstacle_pos_x=%d \n",_Obstacle_pos_x); + switch(_Obstacle_pos_x) { case 0: - lcd.drawRect(2,11,25,2,FILL_BLACK); // it was lcd.drawRect(2,2,25,2,FILL_BLACK); + lcd.drawRect(2,11,25,2,FILL_BLACK); // draw in the left break; case 1: - lcd.drawRect(28,11,27,2,FILL_BLACK); // it was lcd.drawRect(28,2,27,2,FILL_BLACK); + lcd.drawRect(28,11,27,2,FILL_BLACK); // draw in the middle break; case 2: - lcd.drawRect(56,11,25,2,FILL_BLACK); // it was lcd.drawRect(56,2,25,2,FILL_BLACK); + lcd.drawRect(56,11,25,2,FILL_BLACK); // draw in the right break; } } -void Obstacles::update(N5110 &lcd) // adjust this function to be able to store all pixels values +void Obstacles::update(N5110 &lcd) { - // then they are used to shift the screen - addScore(lcd); - char pixelstate[82][37]; // it was char pixelstate[82][46]; - for (int i=1; i<83; i+=1) { // it was for (int i=1; i<83; i+=1) { - for (int j=10; j<47; j+=1) { // it was for (int j=1; j<47; j+=1) { - if (lcd.getPixel(i,j)) { // if (lcd.getPixel(i,j)) { - pixelstate[i-1][j-10]=1; // pixelstate[i-1][j-1]=1; + add_Score(lcd); + + char pixelstate[82][37]; // generate an array to store pixels' values + for (int i=1; i<83; i+=1) { + for (int j=10; j<47; j+=1) { // notice that we start from the 10th row as to allow enough for the + if (lcd.getPixel(i,j)) { // real time prints to be displayed in the first 10 rows + pixelstate[i-1][j-10]=1; } else { - pixelstate[i-1][j-10]=0; // pixelstate[i-1][j-1]=0; + pixelstate[i-1][j-10]=0; } } } - for (int i=1; i<83; i+=1) { // it was for (int i=1; i<83; i+=1) { - for (int j=10; j<47; j+=1) { // it was for (int j=1; j<47; j+=1) { - if ((pixelstate[i-1][j-10]) ) { // it was if ((pixelstate[i-1][j-1]) ) { - lcd.setPixel(i,j+2); // lcd.setPixel(i,j+2); - } // CAN INCREASE THE SPEED BY ADDING 1 FOR Y VALUES - if ((pixelstate[i-1][j-11]==0) ) { // if ((pixelstate[i-1][j-2]==0) ) { - lcd.setPixel(i,j+1,false); // lcd.setPixel(i,j+1,false); + for (int i=1; i<83; i+=1) { // loop through the array values now and then + for (int j=10; j<47; j+=1) { // shift all the pixels down + if ((pixelstate[i-1][j-10]) ) { + lcd.setPixel(i,j+2); // pixels move in an increment of two for higher speed + } + if ((pixelstate[i-1][j-11]==0) ) { + lcd.setPixel(i,j+1,false); } } } } -void Obstacles::addScore(N5110 &lcd) +void Obstacles::add_Score(N5110 &lcd) { - for (int i=2; i<83; i+=1) { - if (lcd.getPixel(i,46)) { - _score++; - _totalscore++; + // loop through the last row of the screen and check if a pixel is detected + for (int i=2; i<83; i+=1) { // if so, then an obstacle is passing, hence break out from the loop since + if (lcd.getPixel(i,46)) { // only one pixel is sufficient to prove that + _score++; // increase adjustable score + _totalscore++; // increase total/final score + // printf("Done \n"); break; } } } -void Obstacles::updateScore() +void Obstacles::reduce_Score() { - _score -= 10; + _score -= 10; // reduce score when feature is used in other functions } -int Obstacles::getScore() +int Obstacles::get_Score() { - return _score; + return _score; // adjustable score } -int Obstacles::getTotalScore() +int Obstacles::get_TotalScore() { - return _totalscore; + return _totalscore; // total/final score }
--- a/Obstacles.h Tue May 02 22:25:19 2017 +0000 +++ b/Obstacles.h Thu May 04 12:04:51 2017 +0000 @@ -5,29 +5,93 @@ #include "N5110.h" #include "Gamepad.h" +/** Car Obstacles +* Obstacles.h +* @brief this header file will contain all required functions used to draw the Obstacles +* @brief updating them, checking for collisions as well as tracking the score +* +* @author Ahmed Abu Zaid +* +* @date 4/5/2017 +*/ class Obstacles { - - public: - - Obstacles(); - ~Obstacles(); - - void init(int seed); - void draw(N5110 &lcd,int seed); - void update(N5110 &lcd); - void addScore(N5110 &lcd); - void updateScore(); - int getScore(); - int getTotalScore(); - - private: - - - int _Obstacle_x; - int _score; - int _totalscore; + +public: + + Obstacles(); + ~Obstacles(); + + /** + * init Initialises the Obstacles. + * + * @param seed random number that specifies which position the obstacle shall be drawn at + * + * This function sets the initial values of scores and the seed for the obstacle + */ + void init(int seed); + + /** + * draw draws the obstacles. + * + * @param lcd The object for the lcd screen class + * @param seed random number that specifies which position the obstacle shall be drawn at + * + * This function prints the obstalces in one of three positions at the top of the screen + * that are determined by the seed + */ + + void draw(N5110 &lcd,int seed); + /** + * update it updates the values of the car's coordinates + * + * @param lcd The object for the lcd screen class + * + * This functions loops through the entire screen and stores the valuse of the pixels' + * states, in order to shift the whole thing down + */ + void update(N5110 &lcd); + + /** + * add_Score it increments the value of the score + * + * @param lcd The object for the lcd screen class + * + * This functions is called to increment the value of the score, which happens + * when an obstacle leaves the screen from the bottom + */ + void add_Score(N5110 &lcd); + + /** + * reduce_Score reduces the score + * + * This functions is called to reduce the value of the score each time a feature is used + * such as R/L jumps, bullet fired etc + */ + void reduce_Score(); + + /** + * get_Score + * + * This functions returns the value of the adjustable score which is used in the + * use of the features such as R/L jumps, bullet fired etc + */ + int get_Score(); + + /** + * get_TotalScore + * + * This functions returns the value of the total score that is printed on the screen + * while the game running, as well as when the game is over + */ + int get_TotalScore(); + +private: + + int _Obstacle_pos_x; // x position of obstacle + int _score; // adjustable score + int _totalscore; // total/final score }; #endif \ No newline at end of file