class for bullet in Car_race game
Bullet.cpp@10:3726b7994e2f, 2017-04-21 (annotated)
- Committer:
- fy14aaz
- Date:
- Fri Apr 21 21:45:54 2017 +0000
- Revision:
- 10:3726b7994e2f
- Parent:
- 9:4ea00d3a8ce0
- Child:
- 11:61bbec4ede2c
worked on the bullet with the jumping functions
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 | 4:a32443aee8f8 | 13 | void Bullet::init(Vector2D _CarHead) |
fy14aaz | 1:6bd0cbdf15f7 | 14 | { |
fy14aaz | 4:a32443aee8f8 | 15 | _bullet_x = _CarHead.x; |
fy14aaz | 4:a32443aee8f8 | 16 | _bullet_y = _CarHead.y + 2; |
fy14aaz | 4:a32443aee8f8 | 17 | |
fy14aaz | 7:b852cf8f4f36 | 18 | // printf("x=%d y=%d \n",_bullet_x,_bullet_y); |
fy14aaz | 1:6bd0cbdf15f7 | 19 | } |
fy14aaz | 1:6bd0cbdf15f7 | 20 | |
fy14aaz | 1:6bd0cbdf15f7 | 21 | void Bullet::draw(N5110 &lcd) |
fy14aaz | 1:6bd0cbdf15f7 | 22 | { |
fy14aaz | 5:fd945bf405dc | 23 | // printf("x=%d y=%d \n",_bullet_x,_bullet_y); |
fy14aaz | 5:fd945bf405dc | 24 | |
fy14aaz | 5:fd945bf405dc | 25 | // lcd.setPixel(_bullet_x,_bullet_y+2,false); |
fy14aaz | 5:fd945bf405dc | 26 | // lcd.setPixel(_bullet_x,_bullet_y+3,false); |
fy14aaz | 5:fd945bf405dc | 27 | // lcd.setPixel(_bullet_x+1,_bullet_y+2,false); |
fy14aaz | 5:fd945bf405dc | 28 | // lcd.setPixel(_bullet_x+1,_bullet_y+3,false); |
fy14aaz | 5:fd945bf405dc | 29 | |
fy14aaz | 8:89a98a3d7233 | 30 | // printf("x=%d y=%d \n",_bullet_x,_bullet_y); |
fy14aaz | 5:fd945bf405dc | 31 | lcd.drawRect(_bullet_x,_bullet_y,2,2,FILL_BLACK); |
fy14aaz | 8:89a98a3d7233 | 32 | // printf("Crashes 0 \n"); |
fy14aaz | 1:6bd0cbdf15f7 | 33 | } |
fy14aaz | 1:6bd0cbdf15f7 | 34 | |
fy14aaz | 5:fd945bf405dc | 35 | void Bullet::update(N5110 &lcd,int _bulletDirection) |
fy14aaz | 5:fd945bf405dc | 36 | { |
fy14aaz | 10:3726b7994e2f | 37 | //lcd.setPixel(_bullet_x,_bullet_y,false); |
fy14aaz | 8:89a98a3d7233 | 38 | //lcd.setPixel(_bullet_x,_bullet_y+1,false); |
fy14aaz | 8:89a98a3d7233 | 39 | //lcd.setPixel(_bullet_x+1,_bullet_y,false); |
fy14aaz | 8:89a98a3d7233 | 40 | //lcd.setPixel(_bullet_x+1,_bullet_y+1,false); |
fy14aaz | 8:89a98a3d7233 | 41 | clearBullet(lcd); |
fy14aaz | 7:b852cf8f4f36 | 42 | // printf("x=%d y=%d \n",_bullet_x,_bullet_y); |
fy14aaz | 5:fd945bf405dc | 43 | if(_bulletDirection==1) { |
fy14aaz | 4:a32443aee8f8 | 44 | _bullet_x = _bullet_x; |
fy14aaz | 5:fd945bf405dc | 45 | _bullet_y = _bullet_y - 3; |
fy14aaz | 5:fd945bf405dc | 46 | } |
fy14aaz | 5:fd945bf405dc | 47 | // printf("x=%d y=%d \n",_bullet_x,_bullet_y); |
fy14aaz | 5:fd945bf405dc | 48 | |
fy14aaz | 6:581257752de1 | 49 | else if(_bulletDirection==2) { |
fy14aaz | 5:fd945bf405dc | 50 | _bullet_x = _bullet_x - 3; |
fy14aaz | 5:fd945bf405dc | 51 | _bullet_y = _bullet_y - 3; |
fy14aaz | 5:fd945bf405dc | 52 | } |
fy14aaz | 5:fd945bf405dc | 53 | |
fy14aaz | 6:581257752de1 | 54 | else if(_bulletDirection==3) { |
fy14aaz | 5:fd945bf405dc | 55 | _bullet_x = _bullet_x + 3; |
fy14aaz | 5:fd945bf405dc | 56 | _bullet_y = _bullet_y - 3; |
fy14aaz | 5:fd945bf405dc | 57 | } |
fy14aaz | 6:581257752de1 | 58 | |
fy14aaz | 7:b852cf8f4f36 | 59 | // 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 | 60 | if ((_bullet_x == -2)) { |
fy14aaz | 7:b852cf8f4f36 | 61 | clearBullet(lcd); |
fy14aaz | 6:581257752de1 | 62 | _bullet_x = _bullet_x - 1; |
fy14aaz | 6:581257752de1 | 63 | _bullet_y = _bullet_y - 1; |
fy14aaz | 6:581257752de1 | 64 | } |
fy14aaz | 6:581257752de1 | 65 | } |
fy14aaz | 6:581257752de1 | 66 | |
fy14aaz | 6:581257752de1 | 67 | void Bullet::clearBullet(N5110 &lcd) |
fy14aaz | 6:581257752de1 | 68 | { |
fy14aaz | 6:581257752de1 | 69 | lcd.setPixel(_bullet_x,_bullet_y,false); |
fy14aaz | 6:581257752de1 | 70 | lcd.setPixel(_bullet_x,_bullet_y+1,false); |
fy14aaz | 6:581257752de1 | 71 | lcd.setPixel(_bullet_x+1,_bullet_y,false); |
fy14aaz | 6:581257752de1 | 72 | lcd.setPixel(_bullet_x+1,_bullet_y+1,false); |
fy14aaz | 8:89a98a3d7233 | 73 | } |
fy14aaz | 8:89a98a3d7233 | 74 | |
fy14aaz | 8:89a98a3d7233 | 75 | void Bullet::destroyObstacles(N5110 &lcd) |
fy14aaz | 8:89a98a3d7233 | 76 | { |
fy14aaz | 9:4ea00d3a8ce0 | 77 | // printf("they are x=%d y=%d \n",_bullet_x,_bullet_y); |
fy14aaz | 8:89a98a3d7233 | 78 | if ((lcd.getPixel(_bullet_x,_bullet_y)) || // this is for bullet itself |
fy14aaz | 8:89a98a3d7233 | 79 | (lcd.getPixel(_bullet_x,_bullet_y+1)) || // this is for bullet itself |
fy14aaz | 8:89a98a3d7233 | 80 | (lcd.getPixel(_bullet_x+1,_bullet_y)) || // this is for bullet itself |
fy14aaz | 8:89a98a3d7233 | 81 | (lcd.getPixel(_bullet_x+1,_bullet_y+1)) || // this is for bullet itself |
fy14aaz | 8:89a98a3d7233 | 82 | (lcd.getPixel(_bullet_x,_bullet_y-1)) || // up |
fy14aaz | 8:89a98a3d7233 | 83 | (lcd.getPixel(_bullet_x+1,_bullet_y-1)) || // up |
fy14aaz | 8:89a98a3d7233 | 84 | (lcd.getPixel(_bullet_x,_bullet_y+2)) || // down |
fy14aaz | 8:89a98a3d7233 | 85 | (lcd.getPixel(_bullet_x+1,_bullet_y+2))) { // down |
fy14aaz | 8:89a98a3d7233 | 86 | |
fy14aaz | 8:89a98a3d7233 | 87 | for (int i=1; i<83; i+=1) { |
fy14aaz | 8:89a98a3d7233 | 88 | for (int j=_bullet_y-4; j<_bullet_y+5; j+=1) { |
fy14aaz | 8:89a98a3d7233 | 89 | lcd.setPixel(i,j,false); |
fy14aaz | 8:89a98a3d7233 | 90 | } |
fy14aaz | 8:89a98a3d7233 | 91 | } |
fy14aaz | 8:89a98a3d7233 | 92 | _bullet_x = -10; |
fy14aaz | 8:89a98a3d7233 | 93 | _bullet_y = -10; |
fy14aaz | 8:89a98a3d7233 | 94 | } |
fy14aaz | 1:6bd0cbdf15f7 | 95 | } |