class for bullet in Car_race game
Bullet.cpp@12:170175334df8, 2017-05-02 (annotated)
- Committer:
- fy14aaz
- Date:
- Tue May 02 22:25:27 2017 +0000
- Revision:
- 12:170175334df8
- Parent:
- 11:61bbec4ede2c
- Child:
- 13:dd1ccafe3972
polishing the code, encapsulating the functions into smaller ones and added the instructions part
Who changed what in which revision?
User | Revision | Line number | New 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 | 11:61bbec4ede2c | 15 | _bullet_x = CarHead.x; |
fy14aaz | 11:61bbec4ede2c | 16 | _bullet_y = CarHead.y + 2; |
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 | 8:89a98a3d7233 | 22 | // printf("x=%d y=%d \n",_bullet_x,_bullet_y); |
fy14aaz | 5:fd945bf405dc | 23 | lcd.drawRect(_bullet_x,_bullet_y,2,2,FILL_BLACK); |
fy14aaz | 8:89a98a3d7233 | 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 | 5:fd945bf405dc | 28 | { |
fy14aaz | 8:89a98a3d7233 | 29 | clearBullet(lcd); |
fy14aaz | 7:b852cf8f4f36 | 30 | // printf("x=%d y=%d \n",_bullet_x,_bullet_y); |
fy14aaz | 11:61bbec4ede2c | 31 | if(bulletDirection==1) { |
fy14aaz | 4:a32443aee8f8 | 32 | _bullet_x = _bullet_x; |
fy14aaz | 5:fd945bf405dc | 33 | _bullet_y = _bullet_y - 3; |
fy14aaz | 5:fd945bf405dc | 34 | } |
fy14aaz | 5:fd945bf405dc | 35 | |
fy14aaz | 11:61bbec4ede2c | 36 | else if(bulletDirection==2) { |
fy14aaz | 5:fd945bf405dc | 37 | _bullet_x = _bullet_x - 3; |
fy14aaz | 5:fd945bf405dc | 38 | _bullet_y = _bullet_y - 3; |
fy14aaz | 5:fd945bf405dc | 39 | } |
fy14aaz | 5:fd945bf405dc | 40 | |
fy14aaz | 11:61bbec4ede2c | 41 | else if(bulletDirection==3) { |
fy14aaz | 5:fd945bf405dc | 42 | _bullet_x = _bullet_x + 3; |
fy14aaz | 5:fd945bf405dc | 43 | _bullet_y = _bullet_y - 3; |
fy14aaz | 5:fd945bf405dc | 44 | } |
fy14aaz | 11:61bbec4ede2c | 45 | // the following piece of code to get around the problem of the game getting stuck when the value of _bullet_x = 1 |
fy14aaz | 8:89a98a3d7233 | 46 | if ((_bullet_x == -2)) { |
fy14aaz | 7:b852cf8f4f36 | 47 | clearBullet(lcd); |
fy14aaz | 6:581257752de1 | 48 | _bullet_x = _bullet_x - 1; |
fy14aaz | 6:581257752de1 | 49 | _bullet_y = _bullet_y - 1; |
fy14aaz | 6:581257752de1 | 50 | } |
fy14aaz | 12:170175334df8 | 51 | destroyObstacles(lcd); |
fy14aaz | 6:581257752de1 | 52 | } |
fy14aaz | 6:581257752de1 | 53 | |
fy14aaz | 6:581257752de1 | 54 | void Bullet::clearBullet(N5110 &lcd) |
fy14aaz | 6:581257752de1 | 55 | { |
fy14aaz | 11:61bbec4ede2c | 56 | lcd.setPixel(_bullet_x,_bullet_y,false); // top left |
fy14aaz | 11:61bbec4ede2c | 57 | lcd.setPixel(_bullet_x,_bullet_y+1,false); // bottom left |
fy14aaz | 11:61bbec4ede2c | 58 | lcd.setPixel(_bullet_x+1,_bullet_y,false); // top right |
fy14aaz | 11:61bbec4ede2c | 59 | lcd.setPixel(_bullet_x+1,_bullet_y+1,false); // bottom right |
fy14aaz | 8:89a98a3d7233 | 60 | } |
fy14aaz | 8:89a98a3d7233 | 61 | |
fy14aaz | 8:89a98a3d7233 | 62 | void Bullet::destroyObstacles(N5110 &lcd) |
fy14aaz | 8:89a98a3d7233 | 63 | { |
fy14aaz | 9:4ea00d3a8ce0 | 64 | // printf("they are x=%d y=%d \n",_bullet_x,_bullet_y); |
fy14aaz | 11:61bbec4ede2c | 65 | if ((lcd.getPixel(_bullet_x,_bullet_y)) || // top left |
fy14aaz | 11:61bbec4ede2c | 66 | (lcd.getPixel(_bullet_x,_bullet_y+1)) || // bottom left |
fy14aaz | 11:61bbec4ede2c | 67 | (lcd.getPixel(_bullet_x+1,_bullet_y)) || // top right |
fy14aaz | 11:61bbec4ede2c | 68 | (lcd.getPixel(_bullet_x+1,_bullet_y+1)) || // bottom right |
fy14aaz | 11:61bbec4ede2c | 69 | (lcd.getPixel(_bullet_x,_bullet_y-1)) || // up left |
fy14aaz | 11:61bbec4ede2c | 70 | (lcd.getPixel(_bullet_x+1,_bullet_y-1)) || // up right |
fy14aaz | 11:61bbec4ede2c | 71 | (lcd.getPixel(_bullet_x,_bullet_y+2)) || // down left |
fy14aaz | 11:61bbec4ede2c | 72 | (lcd.getPixel(_bullet_x+1,_bullet_y+2))) { // down right |
fy14aaz | 8:89a98a3d7233 | 73 | |
fy14aaz | 8:89a98a3d7233 | 74 | for (int i=1; i<83; i+=1) { |
fy14aaz | 8:89a98a3d7233 | 75 | for (int j=_bullet_y-4; j<_bullet_y+5; j+=1) { |
fy14aaz | 11:61bbec4ede2c | 76 | lcd.setPixel(i,j,false); // loop through and turn off pixels around that obstacle |
fy14aaz | 8:89a98a3d7233 | 77 | } |
fy14aaz | 8:89a98a3d7233 | 78 | } |
fy14aaz | 11:61bbec4ede2c | 79 | _bullet_x = -10; // set the coordinates of the bullet out of the screen |
fy14aaz | 11:61bbec4ede2c | 80 | _bullet_y = -10; // so it won't be drawn again until re-initialise the coordinates |
fy14aaz | 8:89a98a3d7233 | 81 | } |
fy14aaz | 1:6bd0cbdf15f7 | 82 | } |