class for bullet in Car_race game

Revision:
11:61bbec4ede2c
Parent:
10:3726b7994e2f
Child:
12:170175334df8
diff -r 3726b7994e2f -r 61bbec4ede2c Bullet.cpp
--- a/Bullet.cpp	Fri Apr 21 21:45:54 2017 +0000
+++ b/Bullet.cpp	Mon May 01 22:27:40 2017 +0000
@@ -10,53 +10,39 @@
 
 }
 
-void Bullet::init(Vector2D _CarHead)
+void Bullet::init(Vector2D CarHead)
 {
-    _bullet_x =  _CarHead.x;
-    _bullet_y =  _CarHead.y + 2;
-    
+    _bullet_x =  CarHead.x;
+    _bullet_y =  CarHead.y + 2; 
     // 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.setPixel(_bullet_x,_bullet_y+2,false);
- // lcd.setPixel(_bullet_x,_bullet_y+3,false);
- // lcd.setPixel(_bullet_x+1,_bullet_y+2,false);
- // lcd.setPixel(_bullet_x+1,_bullet_y+3,false); 
-   
   // 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)
+void Bullet::update(N5110 &lcd,int bulletDirection)
 {   
-    //lcd.setPixel(_bullet_x,_bullet_y,false);
-    //lcd.setPixel(_bullet_x,_bullet_y+1,false);
-    //lcd.setPixel(_bullet_x+1,_bullet_y,false);
-    //lcd.setPixel(_bullet_x+1,_bullet_y+1,false);
     clearBullet(lcd);
     // printf("x=%d y=%d \n",_bullet_x,_bullet_y);
-    if(_bulletDirection==1) {
+    if(bulletDirection==1) {
     _bullet_x = _bullet_x;
     _bullet_y = _bullet_y - 3; 
     }
-     // printf("x=%d y=%d \n",_bullet_x,_bullet_y);
      
-    else if(_bulletDirection==2) {
+    else if(bulletDirection==2) {
     _bullet_x = _bullet_x - 3;
     _bullet_y = _bullet_y - 3; 
     }
     
-    else if(_bulletDirection==3) {
+    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 
+      // 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;
@@ -66,30 +52,30 @@
 
 void Bullet::clearBullet(N5110 &lcd)
 { 
-    lcd.setPixel(_bullet_x,_bullet_y,false);
-    lcd.setPixel(_bullet_x,_bullet_y+1,false);
-    lcd.setPixel(_bullet_x+1,_bullet_y,false);
-    lcd.setPixel(_bullet_x+1,_bullet_y+1,false);
+    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
 }
 
 void Bullet::destroyObstacles(N5110 &lcd)
 {     
   // printf("they are x=%d y=%d \n",_bullet_x,_bullet_y);
-  if ((lcd.getPixel(_bullet_x,_bullet_y))     ||  // this is for bullet itself
-     (lcd.getPixel(_bullet_x,_bullet_y+1))    ||  // this is for bullet itself
-     (lcd.getPixel(_bullet_x+1,_bullet_y))    ||  // this is for bullet itself
-     (lcd.getPixel(_bullet_x+1,_bullet_y+1))  ||  // this is for bullet itself
-     (lcd.getPixel(_bullet_x,_bullet_y-1))    ||  // up 
-     (lcd.getPixel(_bullet_x+1,_bullet_y-1))  ||  // up
-     (lcd.getPixel(_bullet_x,_bullet_y+2))  ||  // down
-     (lcd.getPixel(_bullet_x+1,_bullet_y+2)))   { // down
+  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) {
             for (int j=_bullet_y-4; j<_bullet_y+5; j+=1) {
-                lcd.setPixel(i,j,false);
+                lcd.setPixel(i,j,false);   // loop through and turn off pixels around that obstacle
                 }
                 }
-                _bullet_x = -10;
-                _bullet_y = -10;
+                _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
         }
 }
\ No newline at end of file