HaoZhang SID: 201199702

Dependencies:   mbed

Committer:
zh870524589
Date:
Thu May 14 17:45:05 2020 +0000
Revision:
2:867fdea920c1
Parent:
1:47fadd485c70
final

Who changed what in which revision?

UserRevisionLine numberNew contents of line
zh870524589 0:45ce241d316b 1 #include "MyTank.h"
zh870524589 0:45ce241d316b 2
zh870524589 0:45ce241d316b 3
zh870524589 0:45ce241d316b 4 // four different direction of Tank
zh870524589 0:45ce241d316b 5 const int boom[5][5] =
zh870524589 0:45ce241d316b 6 {
zh870524589 0:45ce241d316b 7 {1,0,1,0,1},
zh870524589 0:45ce241d316b 8 {0,1,1,1,0},
zh870524589 0:45ce241d316b 9 {1,1,1,1,1},
zh870524589 0:45ce241d316b 10 {0,1,1,1,0},
zh870524589 0:45ce241d316b 11 {1,0,1,0,1},
zh870524589 0:45ce241d316b 12 };
zh870524589 0:45ce241d316b 13
zh870524589 0:45ce241d316b 14 // four direction of mytank
zh870524589 0:45ce241d316b 15 const int run_up[5][5] =
zh870524589 0:45ce241d316b 16 {
zh870524589 0:45ce241d316b 17 {1,0,1,0,1},
zh870524589 0:45ce241d316b 18 {1,0,1,0,1},
zh870524589 0:45ce241d316b 19 {1,1,1,1,1},
zh870524589 0:45ce241d316b 20 {1,1,1,1,1},
zh870524589 0:45ce241d316b 21 {1,1,1,1,1},
zh870524589 0:45ce241d316b 22 };
zh870524589 0:45ce241d316b 23
zh870524589 0:45ce241d316b 24 const int run_down[5][5] =
zh870524589 0:45ce241d316b 25 {
zh870524589 0:45ce241d316b 26 {1,1,1,1,1},
zh870524589 0:45ce241d316b 27 {1,1,1,1,1},
zh870524589 0:45ce241d316b 28 {1,1,1,1,1},
zh870524589 0:45ce241d316b 29 {1,0,1,0,1},
zh870524589 0:45ce241d316b 30 {1,0,1,0,1},
zh870524589 0:45ce241d316b 31 };
zh870524589 0:45ce241d316b 32 const int run_left[5][5] =
zh870524589 0:45ce241d316b 33 {
zh870524589 0:45ce241d316b 34 {1,1,1,1,1},
zh870524589 0:45ce241d316b 35 {0,0,1,1,1},
zh870524589 0:45ce241d316b 36 {1,1,1,1,1},
zh870524589 0:45ce241d316b 37 {0,0,1,1,1},
zh870524589 0:45ce241d316b 38 {1,1,1,1,1},
zh870524589 0:45ce241d316b 39 };
zh870524589 0:45ce241d316b 40 const int run_right[5][5] =
zh870524589 0:45ce241d316b 41 {
zh870524589 0:45ce241d316b 42 {1,1,1,1,1},
zh870524589 0:45ce241d316b 43 {1,1,1,0,0},
zh870524589 0:45ce241d316b 44 {1,1,1,1,1},
zh870524589 0:45ce241d316b 45 {1,1,1,0,0},
zh870524589 0:45ce241d316b 46 {1,1,1,1,1},
zh870524589 0:45ce241d316b 47 };
zh870524589 0:45ce241d316b 48
zh870524589 0:45ce241d316b 49 void MyTank::init(Gamepad &pad,N5110 &lcd)
zh870524589 0:45ce241d316b 50 {
zh870524589 0:45ce241d316b 51 this->_x=30;
zh870524589 0:45ce241d316b 52 this->_y=43;
zh870524589 0:45ce241d316b 53 lcd.drawSprite(_x,_y,5,5,(int *)run_up);
zh870524589 0:45ce241d316b 54 this->lcd = lcd; //Initialize tank direction with LCD
zh870524589 0:45ce241d316b 55 this->_pre_d = N; //Initialize direction of bullet
zh870524589 0:45ce241d316b 56 this->count = 0; //use for the limitting of attack
zh870524589 0:45ce241d316b 57 this->attack_gap = 7;//use for the limitting of attack
zh870524589 0:45ce241d316b 58 this->HP = 3;
zh870524589 0:45ce241d316b 59 pad.led(1,1);
zh870524589 0:45ce241d316b 60 pad.led(2,1);
zh870524589 0:45ce241d316b 61 pad.led(3,1); // the led represent the HP
zh870524589 0:45ce241d316b 62 this->totalHP = 3;
zh870524589 0:45ce241d316b 63 }
zh870524589 0:45ce241d316b 64
zh870524589 0:45ce241d316b 65 void MyTank::subHP()
zh870524589 0:45ce241d316b 66 {
zh870524589 0:45ce241d316b 67 --HP;
zh870524589 0:45ce241d316b 68 }
zh870524589 0:45ce241d316b 69 void MyTank::addHP()
zh870524589 0:45ce241d316b 70 {
zh870524589 0:45ce241d316b 71 ++HP;
zh870524589 0:45ce241d316b 72 }
zh870524589 0:45ce241d316b 73 //for the shop :buy the HP
zh870524589 0:45ce241d316b 74 void MyTank::setHP(Gamepad &pad)
zh870524589 0:45ce241d316b 75 {
zh870524589 0:45ce241d316b 76 pad.tone(500.0,0.1);
zh870524589 0:45ce241d316b 77 pad.led(1,1);
zh870524589 0:45ce241d316b 78 pad.led(2,1);
zh870524589 0:45ce241d316b 79 pad.led(3,1);
zh870524589 0:45ce241d316b 80 HP = totalHP;
zh870524589 0:45ce241d316b 81 if(HP == 4)
zh870524589 0:45ce241d316b 82 pad.led(4,1);
zh870524589 0:45ce241d316b 83 if(HP == 5)
zh870524589 0:45ce241d316b 84 {
zh870524589 0:45ce241d316b 85 pad.led(5,1);
zh870524589 0:45ce241d316b 86 pad.led(4,1);
zh870524589 0:45ce241d316b 87 }
zh870524589 0:45ce241d316b 88 if(HP == 6){
zh870524589 0:45ce241d316b 89 pad.led(6,1);
zh870524589 0:45ce241d316b 90 pad.led(5,1);
zh870524589 0:45ce241d316b 91 pad.led(4,1);
zh870524589 0:45ce241d316b 92 }
zh870524589 0:45ce241d316b 93 }
zh870524589 0:45ce241d316b 94
zh870524589 0:45ce241d316b 95 int MyTank::MT_HP()
zh870524589 0:45ce241d316b 96 {
zh870524589 0:45ce241d316b 97 return HP;
zh870524589 0:45ce241d316b 98 }
zh870524589 0:45ce241d316b 99
zh870524589 0:45ce241d316b 100 Vector2D MyTank::get_pos()
zh870524589 0:45ce241d316b 101 {
zh870524589 0:45ce241d316b 102 Vector2D p = {_x,_y};
zh870524589 0:45ce241d316b 103 return p;
zh870524589 0:45ce241d316b 104 }
zh870524589 0:45ce241d316b 105
zh870524589 0:45ce241d316b 106 void MyTank::setpos(int x, int y)
zh870524589 0:45ce241d316b 107 {
zh870524589 0:45ce241d316b 108 this->_x = x;
zh870524589 0:45ce241d316b 109 this->_y = y;
zh870524589 0:45ce241d316b 110 }
zh870524589 0:45ce241d316b 111
zh870524589 0:45ce241d316b 112 void MyTank::read_input(Gamepad &pad){ _d = pad.get_direction();}
zh870524589 0:45ce241d316b 113
zh870524589 0:45ce241d316b 114 // Check the collision between MyTank and Etank
zh870524589 0:45ce241d316b 115 void MyTank::check_tank_collsion(list<ETank*>&etank)
zh870524589 0:45ce241d316b 116 {
zh870524589 0:45ce241d316b 117 Vector2D mt_pos = get_pos();
zh870524589 0:45ce241d316b 118
zh870524589 0:45ce241d316b 119 for(list<ETank*>::iterator it =etank.begin();it!=etank.end();it++)
zh870524589 0:45ce241d316b 120 {
zh870524589 0:45ce241d316b 121 Vector2D et_pos = (*it)->get_pos();
zh870524589 0:45ce241d316b 122
zh870524589 0:45ce241d316b 123 if((mt_pos.x >= et_pos.x && mt_pos.x <= et_pos.x+4) && (mt_pos.y >= et_pos.y && mt_pos.y <=et_pos.y+4)
zh870524589 0:45ce241d316b 124 ||(mt_pos.x +4 >= et_pos.x && mt_pos.x <= et_pos.x) && (mt_pos.y >= et_pos.y && mt_pos.y <=et_pos.y+4)
zh870524589 0:45ce241d316b 125 || (mt_pos.x >= et_pos.x && mt_pos.x <= et_pos.x+4) && (mt_pos.y +4>= et_pos.y && mt_pos.y <=et_pos.y)
zh870524589 0:45ce241d316b 126 ||(mt_pos.x +4 >= et_pos.x && mt_pos.x <= et_pos.x) &&(mt_pos.y +4>= et_pos.y && mt_pos.y <=et_pos.y))
zh870524589 0:45ce241d316b 127 {
zh870524589 0:45ce241d316b 128 if(_d == S){
zh870524589 0:45ce241d316b 129 _y -= 1;
zh870524589 0:45ce241d316b 130
zh870524589 0:45ce241d316b 131 } else if(_d == N){
zh870524589 0:45ce241d316b 132 _y += 1;
zh870524589 0:45ce241d316b 133
zh870524589 0:45ce241d316b 134 } else if(_d == E || _d == SE || _d == NE ){
zh870524589 0:45ce241d316b 135 _x -= 1;
zh870524589 0:45ce241d316b 136
zh870524589 0:45ce241d316b 137 } else if(_d == W || _d == SW || _d == NW){
zh870524589 0:45ce241d316b 138 _x += 1;
zh870524589 0:45ce241d316b 139 }
zh870524589 0:45ce241d316b 140 }
zh870524589 0:45ce241d316b 141
zh870524589 0:45ce241d316b 142
zh870524589 0:45ce241d316b 143 }
zh870524589 0:45ce241d316b 144 }
zh870524589 0:45ce241d316b 145 // check the coliison between river and MyTank
zh870524589 0:45ce241d316b 146 void MyTank::check_pixel()
zh870524589 0:45ce241d316b 147 {
zh870524589 0:45ce241d316b 148 Vector2D mt_pos = get_pos();
zh870524589 0:45ce241d316b 149
zh870524589 0:45ce241d316b 150 if((lcd.getPixel(mt_pos.x-1,mt_pos.y)&&(_d == W || _d == SW || _d == NW)) // left
zh870524589 0:45ce241d316b 151 ||(lcd.getPixel(mt_pos.x-1,mt_pos.y+4)&&(_d == W || _d == SW || _d == NW))
zh870524589 0:45ce241d316b 152 ||(lcd.getPixel(mt_pos.x+5,mt_pos.y)&&(_d == E || _d == SE || _d == NE)) //right
zh870524589 0:45ce241d316b 153 ||(lcd.getPixel(mt_pos.x+5,mt_pos.y+4)&&(_d == E || _d == SE || _d == NE))
zh870524589 0:45ce241d316b 154 ||(lcd.getPixel(mt_pos.x,mt_pos.y-1)&&_d==N) // top
zh870524589 0:45ce241d316b 155 ||(lcd.getPixel(mt_pos.x+4,mt_pos.y-1)&&_d==N)
zh870524589 0:45ce241d316b 156 ||(lcd.getPixel(mt_pos.x,mt_pos.y+5)&&_d==S) //botton
zh870524589 0:45ce241d316b 157 ||(lcd.getPixel(mt_pos.x+4,mt_pos.y+5)&&_d==S))
zh870524589 0:45ce241d316b 158 {
zh870524589 0:45ce241d316b 159 if(_d == S){
zh870524589 0:45ce241d316b 160 _y -= 1;
zh870524589 0:45ce241d316b 161
zh870524589 0:45ce241d316b 162
zh870524589 0:45ce241d316b 163 } else if(_d == N){
zh870524589 0:45ce241d316b 164 _y += 1;
zh870524589 0:45ce241d316b 165
zh870524589 0:45ce241d316b 166
zh870524589 0:45ce241d316b 167 } else if(_d == E || _d == SE || _d == NE ){
zh870524589 0:45ce241d316b 168 _x -= 1;
zh870524589 0:45ce241d316b 169
zh870524589 0:45ce241d316b 170
zh870524589 0:45ce241d316b 171 } else if(_d == W || _d == SW || _d == NW){
zh870524589 0:45ce241d316b 172 _x += 1;
zh870524589 0:45ce241d316b 173
zh870524589 0:45ce241d316b 174 }
zh870524589 0:45ce241d316b 175 }
zh870524589 0:45ce241d316b 176
zh870524589 0:45ce241d316b 177 }
zh870524589 0:45ce241d316b 178
zh870524589 0:45ce241d316b 179
zh870524589 0:45ce241d316b 180
zh870524589 0:45ce241d316b 181 // According to the user's direction, select the corresponding pixel tank
zh870524589 0:45ce241d316b 182 void MyTank::draw(N5110 &lcd)
zh870524589 0:45ce241d316b 183 {
zh870524589 0:45ce241d316b 184 if(HP == 0)
zh870524589 0:45ce241d316b 185 lcd.drawSprite(_x,_y,5,5,(int *)boom);
zh870524589 0:45ce241d316b 186 else
zh870524589 0:45ce241d316b 187 switch(_d)
zh870524589 0:45ce241d316b 188 {
zh870524589 0:45ce241d316b 189 case S:
zh870524589 0:45ce241d316b 190 lcd.drawSprite(_x,_y,5,5,(int *)run_down);
zh870524589 0:45ce241d316b 191 _pre_d = S;
zh870524589 0:45ce241d316b 192 this->lcd = lcd;
zh870524589 0:45ce241d316b 193 break;
zh870524589 0:45ce241d316b 194 case N:
zh870524589 0:45ce241d316b 195 lcd.drawSprite(_x,_y,5,5,(int *)run_up);
zh870524589 0:45ce241d316b 196 _pre_d = N;
zh870524589 0:45ce241d316b 197 this->lcd = lcd;
zh870524589 0:45ce241d316b 198 break;
zh870524589 0:45ce241d316b 199 case W:
zh870524589 0:45ce241d316b 200 lcd.drawSprite(_x,_y,5,5,(int *)run_left);
zh870524589 0:45ce241d316b 201 _pre_d = W;
zh870524589 0:45ce241d316b 202 this->lcd = lcd;
zh870524589 0:45ce241d316b 203 break;
zh870524589 0:45ce241d316b 204 case NW:
zh870524589 0:45ce241d316b 205 lcd.drawSprite(_x,_y,5,5,(int *)run_left);
zh870524589 0:45ce241d316b 206 _pre_d = W;
zh870524589 0:45ce241d316b 207 this->lcd = lcd;
zh870524589 0:45ce241d316b 208 break;
zh870524589 0:45ce241d316b 209 case SW:
zh870524589 0:45ce241d316b 210 lcd.drawSprite(_x,_y,5,5,(int *)run_left);
zh870524589 0:45ce241d316b 211 _pre_d = W;
zh870524589 0:45ce241d316b 212 this->lcd = lcd;
zh870524589 0:45ce241d316b 213 break;
zh870524589 0:45ce241d316b 214 case E:
zh870524589 0:45ce241d316b 215 lcd.drawSprite(_x,_y,5,5,(int *)run_right);
zh870524589 0:45ce241d316b 216 _pre_d = E ;
zh870524589 0:45ce241d316b 217 this->lcd = lcd;
zh870524589 0:45ce241d316b 218 break;
zh870524589 0:45ce241d316b 219 case SE:
zh870524589 0:45ce241d316b 220 lcd.drawSprite(_x,_y,5,5,(int *)run_right);
zh870524589 0:45ce241d316b 221 _pre_d = E;
zh870524589 0:45ce241d316b 222 this->lcd = lcd;
zh870524589 0:45ce241d316b 223 break;
zh870524589 0:45ce241d316b 224 case NE:
zh870524589 0:45ce241d316b 225 lcd.drawSprite(_x,_y,5,5,(int *)run_right);
zh870524589 0:45ce241d316b 226 _pre_d = E;
zh870524589 0:45ce241d316b 227 this->lcd = lcd;
zh870524589 0:45ce241d316b 228 break;
zh870524589 0:45ce241d316b 229 case CENTRE:
zh870524589 0:45ce241d316b 230 lcd = this->lcd; // keep the same as before
zh870524589 0:45ce241d316b 231 break;
zh870524589 0:45ce241d316b 232 }
zh870524589 0:45ce241d316b 233 }
zh870524589 0:45ce241d316b 234
zh870524589 0:45ce241d316b 235
zh870524589 0:45ce241d316b 236
zh870524589 0:45ce241d316b 237
zh870524589 0:45ce241d316b 238 void MyTank::update(list<ETank*>&etank)
zh870524589 0:45ce241d316b 239 {
zh870524589 0:45ce241d316b 240 check_pixel();
zh870524589 0:45ce241d316b 241 if (_d == S) {
zh870524589 0:45ce241d316b 242 _y+=1;
zh870524589 0:45ce241d316b 243
zh870524589 0:45ce241d316b 244 } else if (_d == N) {
zh870524589 0:45ce241d316b 245 _y-=1;
zh870524589 0:45ce241d316b 246
zh870524589 0:45ce241d316b 247 } else if (_d == E || _d == SE || _d == NE) {
zh870524589 0:45ce241d316b 248 _x+=1;
zh870524589 0:45ce241d316b 249
zh870524589 0:45ce241d316b 250 } else if (_d == W || _d == SW || _d == NW) {
zh870524589 0:45ce241d316b 251 _x-=1;
zh870524589 0:45ce241d316b 252
zh870524589 0:45ce241d316b 253 }
zh870524589 0:45ce241d316b 254 // check the y origin to ensure that the paddle doesn't go off limit
zh870524589 0:45ce241d316b 255 if (_y < 6 && _y >=5) {
zh870524589 0:45ce241d316b 256 _y = 6;
zh870524589 0:45ce241d316b 257 }
zh870524589 0:45ce241d316b 258 if (_x < 1 && _x >=0) {
zh870524589 0:45ce241d316b 259 _x = 1;
zh870524589 0:45ce241d316b 260 }
zh870524589 0:45ce241d316b 261 if(_y>43){
zh870524589 0:45ce241d316b 262 _y=43;
zh870524589 0:45ce241d316b 263 }
zh870524589 0:45ce241d316b 264 if(_x>79){
zh870524589 0:45ce241d316b 265 _x=79;
zh870524589 0:45ce241d316b 266 }
zh870524589 0:45ce241d316b 267 if(_x == -5||_y == -5){
zh870524589 0:45ce241d316b 268 _x = -5;
zh870524589 0:45ce241d316b 269 _y = -5;
zh870524589 0:45ce241d316b 270 }
zh870524589 0:45ce241d316b 271
zh870524589 0:45ce241d316b 272 check_tank_collsion(etank);
zh870524589 0:45ce241d316b 273 }
zh870524589 0:45ce241d316b 274
zh870524589 0:45ce241d316b 275 Direction MyTank::get_direction(){ return _d; }
zh870524589 0:45ce241d316b 276
zh870524589 0:45ce241d316b 277 //attack of MyTank
zh870524589 0:45ce241d316b 278 void MyTank::attack(list<Bullet*>&lstBullets,Vector2D v,Gamepad &pad,N5110 &lcd,list<ETank*>&etank,int &num_tank)
zh870524589 0:45ce241d316b 279 {
zh870524589 0:45ce241d316b 280 count++;
zh870524589 0:45ce241d316b 281 bool check = pad.check_event(Gamepad::A_PRESSED);
zh870524589 0:45ce241d316b 282 if(count>attack_gap)
zh870524589 0:45ce241d316b 283 if(check== true){
zh870524589 0:45ce241d316b 284 v = get_pos(); //get the position of the MyTank
zh870524589 0:45ce241d316b 285 Bullet* bullet = new Bullet(pad,v);// create the bullets when attack
zh870524589 0:45ce241d316b 286 lstBullets.push_back(bullet);
zh870524589 0:45ce241d316b 287 count = 0;
zh870524589 0:45ce241d316b 288 }
zh870524589 0:45ce241d316b 289 for(list<Bullet*>::iterator it = lstBullets.begin(); it != lstBullets.end();)
zh870524589 0:45ce241d316b 290 {
zh870524589 0:45ce241d316b 291 Vector2D bt_pos = (*it)->get_pos();
zh870524589 0:45ce241d316b 292 int destroy_check = 0;
zh870524589 0:45ce241d316b 293 for(list<ETank*>::iterator eit =etank.begin();eit!=etank.end();){
zh870524589 0:45ce241d316b 294 Vector2D et_pos = (*eit)->get_pos();
zh870524589 0:45ce241d316b 295 //Determine whether the enemy is under attack
zh870524589 0:45ce241d316b 296 if ((*it)->destroyT(et_pos))
zh870524589 0:45ce241d316b 297 {
zh870524589 0:45ce241d316b 298 destroy_check = 1;
zh870524589 0:45ce241d316b 299 lcd.drawSprite(et_pos.x,et_pos.y,5,5,(int *)boom);
zh870524589 0:45ce241d316b 300 (*eit)->setpos(-5,-5);
zh870524589 0:45ce241d316b 301 num_tank--;//ETANK destroyed, quantity -1
zh870524589 0:45ce241d316b 302 if((*eit)->lstBullets.empty()) //Avoid etank bullets being removed with etank
zh870524589 0:45ce241d316b 303 {
zh870524589 0:45ce241d316b 304 delete *eit;
zh870524589 0:45ce241d316b 305 eit = etank.erase(eit);
zh870524589 0:45ce241d316b 306 continue;
zh870524589 0:45ce241d316b 307 }
zh870524589 0:45ce241d316b 308 }
zh870524589 0:45ce241d316b 309 eit++;
zh870524589 0:45ce241d316b 310 }
zh870524589 0:45ce241d316b 311 if((*it)->destroy()||destroy_check == 1)
zh870524589 0:45ce241d316b 312 {
zh870524589 0:45ce241d316b 313 delete *it;
zh870524589 0:45ce241d316b 314 it = lstBullets.erase(it);
zh870524589 0:45ce241d316b 315 continue;
zh870524589 0:45ce241d316b 316 }
zh870524589 0:45ce241d316b 317
zh870524589 0:45ce241d316b 318 (*it)->update(lcd,_pre_d);
zh870524589 0:45ce241d316b 319 it++;
zh870524589 0:45ce241d316b 320 }
zh870524589 0:45ce241d316b 321
zh870524589 0:45ce241d316b 322 }
zh870524589 0:45ce241d316b 323
zh870524589 0:45ce241d316b 324
zh870524589 0:45ce241d316b 325
zh870524589 0:45ce241d316b 326
zh870524589 0:45ce241d316b 327