class for bullet in Car_race game

Committer:
fy14aaz
Date:
Thu May 04 12:04:59 2017 +0000
Revision:
13:dd1ccafe3972
Parent:
12:170175334df8
final version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
fy14aaz 1:6bd0cbdf15f7 1 #include "Bullet.h"
fy14aaz 1:6bd0cbdf15f7 2
fy14aaz 1:6bd0cbdf15f7 3 Bullet::Bullet()
fy14aaz 1:6bd0cbdf15f7 4 {
fy14aaz 1:6bd0cbdf15f7 5
fy14aaz 1:6bd0cbdf15f7 6 }
fy14aaz 1:6bd0cbdf15f7 7
fy14aaz 1:6bd0cbdf15f7 8 Bullet::~Bullet()
fy14aaz 1:6bd0cbdf15f7 9 {
fy14aaz 1:6bd0cbdf15f7 10
fy14aaz 1:6bd0cbdf15f7 11 }
fy14aaz 1:6bd0cbdf15f7 12
fy14aaz 11:61bbec4ede2c 13 void Bullet::init(Vector2D CarHead)
fy14aaz 1:6bd0cbdf15f7 14 {
fy14aaz 13:dd1ccafe3972 15 _bullet_x = CarHead.x; // same x value as the car head
fy14aaz 13:dd1ccafe3972 16 _bullet_y = CarHead.y + 2; // starts from the square right above the car head
fy14aaz 7:b852cf8f4f36 17 // printf("x=%d y=%d \n",_bullet_x,_bullet_y);
fy14aaz 1:6bd0cbdf15f7 18 }
fy14aaz 1:6bd0cbdf15f7 19
fy14aaz 1:6bd0cbdf15f7 20 void Bullet::draw(N5110 &lcd)
fy14aaz 1:6bd0cbdf15f7 21 {
fy14aaz 13:dd1ccafe3972 22 // printf("x=%d y=%d \n",_bullet_x,_bullet_y);
fy14aaz 13:dd1ccafe3972 23 lcd.drawRect(_bullet_x,_bullet_y,2,2,FILL_BLACK);
fy14aaz 13:dd1ccafe3972 24 // printf("Crashes 0 \n");
fy14aaz 1:6bd0cbdf15f7 25 }
fy14aaz 1:6bd0cbdf15f7 26
fy14aaz 11:61bbec4ede2c 27 void Bullet::update(N5110 &lcd,int bulletDirection)
fy14aaz 13:dd1ccafe3972 28 {
fy14aaz 13:dd1ccafe3972 29 clear_Bullet(lcd); // must clear the old bullet before updating otherwise bullets get shifted down with obstacles
fy14aaz 7:b852cf8f4f36 30 // printf("x=%d y=%d \n",_bullet_x,_bullet_y);
fy14aaz 11:61bbec4ede2c 31 if(bulletDirection==1) {
fy14aaz 13:dd1ccafe3972 32 _bullet_x = _bullet_x; // Direction straight up
fy14aaz 13:dd1ccafe3972 33 _bullet_y = _bullet_y - 3;
fy14aaz 13:dd1ccafe3972 34 } // steps of three
fy14aaz 13:dd1ccafe3972 35
fy14aaz 13:dd1ccafe3972 36 else if(bulletDirection==2) { // Direction diagonally left
fy14aaz 13:dd1ccafe3972 37 _bullet_x = _bullet_x - 3;
fy14aaz 13:dd1ccafe3972 38 _bullet_y = _bullet_y - 3;
fy14aaz 13:dd1ccafe3972 39 } // steps of three
fy14aaz 13:dd1ccafe3972 40
fy14aaz 13:dd1ccafe3972 41 else if(bulletDirection==3) { // Direction diagonally right
fy14aaz 13:dd1ccafe3972 42 _bullet_x = _bullet_x + 3;
fy14aaz 13:dd1ccafe3972 43 _bullet_y = _bullet_y - 3;
fy14aaz 13:dd1ccafe3972 44 } // steps of three
fy14aaz 13:dd1ccafe3972 45
fy14aaz 13:dd1ccafe3972 46 if ((_bullet_x == -2)) { // to avoid having _bullet_x = -1,
fy14aaz 13:dd1ccafe3972 47 clear_Bullet(lcd);
fy14aaz 13:dd1ccafe3972 48 _bullet_x = _bullet_x - 1;
fy14aaz 13:dd1ccafe3972 49 _bullet_y = _bullet_y - 1;
fy14aaz 5:fd945bf405dc 50 }
fy14aaz 13:dd1ccafe3972 51 destroy_Obstacles(lcd); // obstacles are destroyed if collision happens between them and bullet
fy14aaz 6:581257752de1 52 }
fy14aaz 6:581257752de1 53
fy14aaz 13:dd1ccafe3972 54 void Bullet::clear_Bullet(N5110 &lcd)
fy14aaz 13:dd1ccafe3972 55 {
fy14aaz 13:dd1ccafe3972 56 // loop through the bullet pixels
fy14aaz 11:61bbec4ede2c 57 lcd.setPixel(_bullet_x,_bullet_y,false); // top left
fy14aaz 11:61bbec4ede2c 58 lcd.setPixel(_bullet_x,_bullet_y+1,false); // bottom left
fy14aaz 11:61bbec4ede2c 59 lcd.setPixel(_bullet_x+1,_bullet_y,false); // top right
fy14aaz 11:61bbec4ede2c 60 lcd.setPixel(_bullet_x+1,_bullet_y+1,false); // bottom right
fy14aaz 13:dd1ccafe3972 61 // printf("they are x=%d y=%d \n",_bullet_x,_bullet_y);
fy14aaz 8:89a98a3d7233 62 }
fy14aaz 8:89a98a3d7233 63
fy14aaz 13:dd1ccafe3972 64 void Bullet::destroy_Obstacles(N5110 &lcd)
fy14aaz 13:dd1ccafe3972 65 {
fy14aaz 13:dd1ccafe3972 66 // printf("they are x=%d y=%d \n",_bullet_x,_bullet_y); // loop through the bullet pixels and surrounding pixels
fy14aaz 13:dd1ccafe3972 67 if ((lcd.getPixel(_bullet_x,_bullet_y)) || // top left and check if any is on, so obstacle detected!
fy14aaz 13:dd1ccafe3972 68 (lcd.getPixel(_bullet_x,_bullet_y+1)) || // bottom left
fy14aaz 13:dd1ccafe3972 69 (lcd.getPixel(_bullet_x+1,_bullet_y)) || // top right
fy14aaz 13:dd1ccafe3972 70 (lcd.getPixel(_bullet_x+1,_bullet_y+1)) || // bottom right
fy14aaz 13:dd1ccafe3972 71 (lcd.getPixel(_bullet_x,_bullet_y-1)) || // up left
fy14aaz 13:dd1ccafe3972 72 (lcd.getPixel(_bullet_x+1,_bullet_y-1)) || // up right
fy14aaz 13:dd1ccafe3972 73 (lcd.getPixel(_bullet_x,_bullet_y+2)) || // down left
fy14aaz 13:dd1ccafe3972 74 (lcd.getPixel(_bullet_x+1,_bullet_y+2))) { // down right
fy14aaz 13:dd1ccafe3972 75
fy14aaz 13:dd1ccafe3972 76 for (int i=1; i<83; i+=1) {
fy14aaz 8:89a98a3d7233 77 for (int j=_bullet_y-4; j<_bullet_y+5; j+=1) {
fy14aaz 13:dd1ccafe3972 78 lcd.setPixel(i,j,false); // loop through and set off pixels in and surround that obstacle
fy14aaz 13:dd1ccafe3972 79 } // we covered wider area than obstacle's width since it
fy14aaz 13:dd1ccafe3972 80 } // moves down by more than one increment!
fy14aaz 13:dd1ccafe3972 81
fy14aaz 13:dd1ccafe3972 82 _bullet_x = -10; // send it away by setting the coordinates of the bullet out of the screen
fy14aaz 13:dd1ccafe3972 83 _bullet_y = -10; // so it won't be drawn again until re-initialise the bullet's coordinates
fy14aaz 13:dd1ccafe3972 84 }
fy14aaz 1:6bd0cbdf15f7 85 }