class for bullet in Car_race game
Revision 13:dd1ccafe3972, committed 2017-05-04
- Comitter:
- fy14aaz
- Date:
- Thu May 04 12:04:59 2017 +0000
- Parent:
- 12:170175334df8
- Commit message:
- final version
Changed in this revision
Bullet.cpp | Show annotated file Show diff for this revision Revisions of this file |
Bullet.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 170175334df8 -r dd1ccafe3972 Bullet.cpp --- a/Bullet.cpp Tue May 02 22:25:27 2017 +0000 +++ b/Bullet.cpp Thu May 04 12:04:59 2017 +0000 @@ -12,71 +12,74 @@ void Bullet::init(Vector2D CarHead) { - _bullet_x = CarHead.x; - _bullet_y = CarHead.y + 2; + _bullet_x = CarHead.x; // same x value as the car head + _bullet_y = CarHead.y + 2; // starts from the square right above the car head // printf("x=%d y=%d \n",_bullet_x,_bullet_y); } void Bullet::draw(N5110 &lcd) { - // printf("x=%d y=%d \n",_bullet_x,_bullet_y); - lcd.drawRect(_bullet_x,_bullet_y,2,2,FILL_BLACK); - // printf("Crashes 0 \n"); + // printf("x=%d y=%d \n",_bullet_x,_bullet_y); + lcd.drawRect(_bullet_x,_bullet_y,2,2,FILL_BLACK); + // printf("Crashes 0 \n"); } void Bullet::update(N5110 &lcd,int bulletDirection) -{ - clearBullet(lcd); +{ + clear_Bullet(lcd); // must clear the old bullet before updating otherwise bullets get shifted down with obstacles // printf("x=%d y=%d \n",_bullet_x,_bullet_y); if(bulletDirection==1) { - _bullet_x = _bullet_x; - _bullet_y = _bullet_y - 3; - } - - else if(bulletDirection==2) { - _bullet_x = _bullet_x - 3; - _bullet_y = _bullet_y - 3; + _bullet_x = _bullet_x; // Direction straight up + _bullet_y = _bullet_y - 3; + } // steps of three + + else if(bulletDirection==2) { // Direction diagonally left + _bullet_x = _bullet_x - 3; + _bullet_y = _bullet_y - 3; + } // steps of three + + else if(bulletDirection==3) { // Direction diagonally right + _bullet_x = _bullet_x + 3; + _bullet_y = _bullet_y - 3; + } // steps of three + + if ((_bullet_x == -2)) { // to avoid having _bullet_x = -1, + clear_Bullet(lcd); + _bullet_x = _bullet_x - 1; + _bullet_y = _bullet_y - 1; } - - else if(bulletDirection==3) { - _bullet_x = _bullet_x + 3; - _bullet_y = _bullet_y - 3; - } - // the following piece of code to get around the problem of the game getting stuck when the value of _bullet_x = 1 - if ((_bullet_x == -2)) { - clearBullet(lcd); - _bullet_x = _bullet_x - 1; - _bullet_y = _bullet_y - 1; - } - destroyObstacles(lcd); + destroy_Obstacles(lcd); // obstacles are destroyed if collision happens between them and bullet } -void Bullet::clearBullet(N5110 &lcd) -{ +void Bullet::clear_Bullet(N5110 &lcd) +{ + // loop through the bullet pixels lcd.setPixel(_bullet_x,_bullet_y,false); // top left lcd.setPixel(_bullet_x,_bullet_y+1,false); // bottom left lcd.setPixel(_bullet_x+1,_bullet_y,false); // top right lcd.setPixel(_bullet_x+1,_bullet_y+1,false); // bottom right + // printf("they are x=%d y=%d \n",_bullet_x,_bullet_y); } -void Bullet::destroyObstacles(N5110 &lcd) -{ - // printf("they are x=%d y=%d \n",_bullet_x,_bullet_y); - if ((lcd.getPixel(_bullet_x,_bullet_y)) || // top left - (lcd.getPixel(_bullet_x,_bullet_y+1)) || // bottom left - (lcd.getPixel(_bullet_x+1,_bullet_y)) || // top right - (lcd.getPixel(_bullet_x+1,_bullet_y+1)) || // bottom right - (lcd.getPixel(_bullet_x,_bullet_y-1)) || // up left - (lcd.getPixel(_bullet_x+1,_bullet_y-1)) || // up right - (lcd.getPixel(_bullet_x,_bullet_y+2)) || // down left - (lcd.getPixel(_bullet_x+1,_bullet_y+2))) { // down right - - for (int i=1; i<83; i+=1) { +void Bullet::destroy_Obstacles(N5110 &lcd) +{ + // printf("they are x=%d y=%d \n",_bullet_x,_bullet_y); // loop through the bullet pixels and surrounding pixels + if ((lcd.getPixel(_bullet_x,_bullet_y)) || // top left and check if any is on, so obstacle detected! + (lcd.getPixel(_bullet_x,_bullet_y+1)) || // bottom left + (lcd.getPixel(_bullet_x+1,_bullet_y)) || // top right + (lcd.getPixel(_bullet_x+1,_bullet_y+1)) || // bottom right + (lcd.getPixel(_bullet_x,_bullet_y-1)) || // up left + (lcd.getPixel(_bullet_x+1,_bullet_y-1)) || // up right + (lcd.getPixel(_bullet_x,_bullet_y+2)) || // down left + (lcd.getPixel(_bullet_x+1,_bullet_y+2))) { // down right + + for (int i=1; i<83; i+=1) { for (int j=_bullet_y-4; j<_bullet_y+5; j+=1) { - lcd.setPixel(i,j,false); // loop through and turn off pixels around that obstacle - } - } - _bullet_x = -10; // set the coordinates of the bullet out of the screen - _bullet_y = -10; // so it won't be drawn again until re-initialise the coordinates - } + lcd.setPixel(i,j,false); // loop through and set off pixels in and surround that obstacle + } // we covered wider area than obstacle's width since it + } // moves down by more than one increment! + + _bullet_x = -10; // send it away by setting the coordinates of the bullet out of the screen + _bullet_y = -10; // so it won't be drawn again until re-initialise the bullet's coordinates + } } \ No newline at end of file
diff -r 170175334df8 -r dd1ccafe3972 Bullet.h --- a/Bullet.h Tue May 02 22:25:27 2017 +0000 +++ b/Bullet.h Thu May 04 12:04:59 2017 +0000 @@ -5,25 +5,78 @@ #include "N5110.h" #include "Gamepad.h" - +/** Bullet Class +* Bullet.h +* @brief this header file will contain all required functions used to draw the bullet +* @brief and control it by directing, deleting and restricting when to be used +* +* @author Ahmed Abu Zaid +* +* @date 4/5/2017 +*/ class Bullet { public: Bullet(); ~Bullet(); + + /** + * init Initialises the bullet. + * + * @param CarHead Two dimentional array containing the car head coordinates + * + * This function sets the values of coordinates of the bullet + * depending on the car head coordinates + */ void init(Vector2D CarHead); + + /** + * draw draws the bullet. + * + * @param lcd The object for the lcd screen class + * + * This function prints the bullet using the lcd object, and + * depending on the updated values of the coordinates + */ void draw(N5110 &lcd); + + /** + * update it updates the values of the bullet's coordinates + * + * @param lcd The object for the lcd screen class + * @param bulletDirection The direction of the bullet + * + * This functions updates the bullet's coordinates depending on the direction the bullet + * going in which is supplied by the engine when a button is pressed + */ void update(N5110 &lcd,int bulletDirection); - void clearBullet(N5110 &lcd); - void destroyObstacles(N5110 &lcd); - // void accident(N5110 &lcd); + + /** + * clear_Bullet clears the bullet + * + * @param lcd The object for the lcd screen class + * + * This function is used to clear the bullet before getting updated and re-drawn + * in another place, or mybe re-initialised + */ + void clear_Bullet(N5110 &lcd); + + /** + * destroy_Obstacles clears obstacle when colliding with a bullet + * + * @param lcd The object for the lcd screen class + * + * This function clears any obstacle that collides with a bullet, it loops through + * the area of collision and clears the obstalce as well as the bullet + */ + void destroy_Obstacles(N5110 &lcd); private: - - int _bullet_x; - int _bullet_y; - int _speed; - + + // variables + int _bullet_x; // bullet x coordinate + int _bullet_y; // bullet y coordinate + }; #endif \ No newline at end of file