Hao zhang
/
el17h3z
HaoZhang SID: 201199702
ETank/ETank.cpp@2:867fdea920c1, 2020-05-14 (annotated)
- Committer:
- zh870524589
- Date:
- Thu May 14 17:45:05 2020 +0000
- Revision:
- 2:867fdea920c1
- Parent:
- 1:47fadd485c70
final
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
zh870524589 | 0:45ce241d316b | 1 | #include "ETank.h" |
zh870524589 | 0:45ce241d316b | 2 | |
zh870524589 | 0:45ce241d316b | 3 | using namespace std; |
zh870524589 | 0:45ce241d316b | 4 | //four directions of ETank |
zh870524589 | 0:45ce241d316b | 5 | const int run_up[5][5] = |
zh870524589 | 0:45ce241d316b | 6 | { |
zh870524589 | 0:45ce241d316b | 7 | {1,0,1,0,1}, |
zh870524589 | 0:45ce241d316b | 8 | {1,0,1,0,1}, |
zh870524589 | 0:45ce241d316b | 9 | {1,1,1,1,1}, |
zh870524589 | 0:45ce241d316b | 10 | {1,1,1,1,1}, |
zh870524589 | 0:45ce241d316b | 11 | {1,0,1,0,1}, |
zh870524589 | 0:45ce241d316b | 12 | }; |
zh870524589 | 0:45ce241d316b | 13 | |
zh870524589 | 0:45ce241d316b | 14 | const int run_down[5][5] = |
zh870524589 | 0:45ce241d316b | 15 | { |
zh870524589 | 0:45ce241d316b | 16 | {1,0,1,0,1}, |
zh870524589 | 0:45ce241d316b | 17 | {1,1,1,1,1}, |
zh870524589 | 0:45ce241d316b | 18 | {1,1,1,1,1}, |
zh870524589 | 0:45ce241d316b | 19 | {1,0,1,0,1}, |
zh870524589 | 0:45ce241d316b | 20 | {1,0,1,0,1}, |
zh870524589 | 0:45ce241d316b | 21 | }; |
zh870524589 | 0:45ce241d316b | 22 | const int run_left[5][5] = |
zh870524589 | 0:45ce241d316b | 23 | { |
zh870524589 | 0:45ce241d316b | 24 | {1,1,1,1,1}, |
zh870524589 | 0:45ce241d316b | 25 | {0,0,1,1,0}, |
zh870524589 | 0:45ce241d316b | 26 | {1,1,1,1,1}, |
zh870524589 | 0:45ce241d316b | 27 | {0,0,1,1,0}, |
zh870524589 | 0:45ce241d316b | 28 | {1,1,1,1,1}, |
zh870524589 | 0:45ce241d316b | 29 | }; |
zh870524589 | 0:45ce241d316b | 30 | const int run_right[5][5] = |
zh870524589 | 0:45ce241d316b | 31 | { |
zh870524589 | 0:45ce241d316b | 32 | {1,1,1,1,1}, |
zh870524589 | 0:45ce241d316b | 33 | {0,1,1,0,0}, |
zh870524589 | 0:45ce241d316b | 34 | {1,1,1,1,1}, |
zh870524589 | 0:45ce241d316b | 35 | {0,1,1,0,0}, |
zh870524589 | 0:45ce241d316b | 36 | {1,1,1,1,1}, |
zh870524589 | 0:45ce241d316b | 37 | }; |
zh870524589 | 0:45ce241d316b | 38 | |
zh870524589 | 0:45ce241d316b | 39 | |
zh870524589 | 0:45ce241d316b | 40 | // create the ETank |
zh870524589 | 0:45ce241d316b | 41 | ETank::ETank(N5110 &lcd,Gamepad &pad) |
zh870524589 | 0:45ce241d316b | 42 | { |
zh870524589 | 0:45ce241d316b | 43 | while(1){ |
zh870524589 | 0:45ce241d316b | 44 | //The position ETanks are allowed to be born in |
zh870524589 | 0:45ce241d316b | 45 | int x = rand()%79; |
zh870524589 | 0:45ce241d316b | 46 | int y = 5+rand()%35; |
zh870524589 | 0:45ce241d316b | 47 | //Make sure ETank avoid overlap |
zh870524589 | 0:45ce241d316b | 48 | if(!(lcd.getPixel(x,y)||lcd.getPixel(x+1,y)||lcd.getPixel(x+2,y)||lcd.getPixel(x+3,y)||lcd.getPixel(x,y+4) |
zh870524589 | 0:45ce241d316b | 49 | ||lcd.getPixel(x+1,y)||lcd.getPixel(x+1,y+1)||lcd.getPixel(x+1,y+2)||lcd.getPixel(x+1,y+3)||lcd.getPixel(x+1,y+4) |
zh870524589 | 0:45ce241d316b | 50 | ||lcd.getPixel(x+2,y)||lcd.getPixel(x+2,y+1)||lcd.getPixel(x+2,y+2)||lcd.getPixel(x+2,y+3)||lcd.getPixel(x+2,y+4) |
zh870524589 | 0:45ce241d316b | 51 | ||lcd.getPixel(x+3,y)||lcd.getPixel(x+3,y+1)||lcd.getPixel(x+3,y+2)||lcd.getPixel(x+3,y+3)||lcd.getPixel(x+3,y+4) |
zh870524589 | 0:45ce241d316b | 52 | ||lcd.getPixel(x+4,y)||lcd.getPixel(x+4,y+1)||lcd.getPixel(x+4,y+2)||lcd.getPixel(x+4,y+3)||lcd.getPixel(x+4,y+4))) |
zh870524589 | 0:45ce241d316b | 53 | { |
zh870524589 | 0:45ce241d316b | 54 | _x = x; |
zh870524589 | 0:45ce241d316b | 55 | _y = y; |
zh870524589 | 0:45ce241d316b | 56 | count = 0;//To limit attack spacing |
zh870524589 | 0:45ce241d316b | 57 | _d = S;//init the direction of ETank |
zh870524589 | 0:45ce241d316b | 58 | pad.tone(100.0,0.1); |
zh870524589 | 0:45ce241d316b | 59 | break; |
zh870524589 | 0:45ce241d316b | 60 | } |
zh870524589 | 0:45ce241d316b | 61 | } |
zh870524589 | 0:45ce241d316b | 62 | } |
zh870524589 | 0:45ce241d316b | 63 | |
zh870524589 | 0:45ce241d316b | 64 | ETank::~ETank() |
zh870524589 | 0:45ce241d316b | 65 | { |
zh870524589 | 0:45ce241d316b | 66 | |
zh870524589 | 0:45ce241d316b | 67 | } |
zh870524589 | 0:45ce241d316b | 68 | |
zh870524589 | 0:45ce241d316b | 69 | void ETank::setpos(int x, int y) |
zh870524589 | 0:45ce241d316b | 70 | { |
zh870524589 | 0:45ce241d316b | 71 | this->_x = x; |
zh870524589 | 0:45ce241d316b | 72 | this->_y = y; |
zh870524589 | 0:45ce241d316b | 73 | } |
zh870524589 | 0:45ce241d316b | 74 | |
zh870524589 | 0:45ce241d316b | 75 | Vector2D ETank::get_pos() |
zh870524589 | 0:45ce241d316b | 76 | { |
zh870524589 | 0:45ce241d316b | 77 | Vector2D p = {_x,_y}; |
zh870524589 | 0:45ce241d316b | 78 | return p; |
zh870524589 | 0:45ce241d316b | 79 | } |
zh870524589 | 0:45ce241d316b | 80 | |
zh870524589 | 0:45ce241d316b | 81 | void ETank::direction(MyTank &mt) |
zh870524589 | 0:45ce241d316b | 82 | { |
zh870524589 | 0:45ce241d316b | 83 | int et_move = rand()%101; // The direction of movement is random |
zh870524589 | 0:45ce241d316b | 84 | Vector2D mt_pos = mt.get_pos();//get position of MyTank |
zh870524589 | 0:45ce241d316b | 85 | //Make sure the ETank is moving in the direction of the target |
zh870524589 | 0:45ce241d316b | 86 | if(_x<mt_pos.x) |
zh870524589 | 0:45ce241d316b | 87 | { |
zh870524589 | 0:45ce241d316b | 88 | if(_y<mt_pos.y && mt_pos.x -_x<60 && mt_pos.y -_y <40) //Make sure the ETank only moves towards MyTank when the target is nearby |
zh870524589 | 0:45ce241d316b | 89 | { |
zh870524589 | 0:45ce241d316b | 90 | if(et_move<62) |
zh870524589 | 0:45ce241d316b | 91 | _d = E; |
zh870524589 | 0:45ce241d316b | 92 | else if(et_move>62) |
zh870524589 | 0:45ce241d316b | 93 | _d = S; |
zh870524589 | 0:45ce241d316b | 94 | } |
zh870524589 | 0:45ce241d316b | 95 | else if(_y>mt_pos.y && mt_pos.x -_x<60 && _y-mt_pos.y <40)//Make sure the ETank only moves towards MyTank when the target is nearby |
zh870524589 | 0:45ce241d316b | 96 | { |
zh870524589 | 0:45ce241d316b | 97 | if(et_move<62) |
zh870524589 | 0:45ce241d316b | 98 | _d = E; |
zh870524589 | 0:45ce241d316b | 99 | else if(et_move>62) |
zh870524589 | 0:45ce241d316b | 100 | _d = N; |
zh870524589 | 0:45ce241d316b | 101 | } else if(_y==mt_pos.y) |
zh870524589 | 0:45ce241d316b | 102 | _d = E; |
zh870524589 | 0:45ce241d316b | 103 | else if(et_move<50) |
zh870524589 | 0:45ce241d316b | 104 | _d = E; |
zh870524589 | 0:45ce241d316b | 105 | // if 1 |
zh870524589 | 0:45ce241d316b | 106 | } else if(_x>mt_pos.x) |
zh870524589 | 0:45ce241d316b | 107 | { |
zh870524589 | 0:45ce241d316b | 108 | if(_y<mt_pos.y && _x -mt_pos.x <60 && mt_pos.y -_y <40)//Make sure the ETank only moves towards MyTank when the target is nearby |
zh870524589 | 0:45ce241d316b | 109 | { |
zh870524589 | 0:45ce241d316b | 110 | if(et_move<62) |
zh870524589 | 0:45ce241d316b | 111 | _d = W; |
zh870524589 | 0:45ce241d316b | 112 | else if(et_move>62) |
zh870524589 | 0:45ce241d316b | 113 | _d = S; |
zh870524589 | 0:45ce241d316b | 114 | } else if(_y>mt_pos.y && _x -mt_pos.x <60 && _y - mt_pos.y <40)//Make sure the ETank only moves towards MyTank when the target is nearby |
zh870524589 | 0:45ce241d316b | 115 | { |
zh870524589 | 0:45ce241d316b | 116 | if(et_move<62) |
zh870524589 | 0:45ce241d316b | 117 | _d = W; |
zh870524589 | 0:45ce241d316b | 118 | else if(et_move>62) |
zh870524589 | 0:45ce241d316b | 119 | _d = N; |
zh870524589 | 0:45ce241d316b | 120 | } else if(_y==mt_pos.y) |
zh870524589 | 0:45ce241d316b | 121 | _d = W; |
zh870524589 | 0:45ce241d316b | 122 | else if(et_move<50) |
zh870524589 | 0:45ce241d316b | 123 | _d = W; |
zh870524589 | 0:45ce241d316b | 124 | } // else if 2 |
zh870524589 | 0:45ce241d316b | 125 | else if(_x==mt_pos.x) |
zh870524589 | 0:45ce241d316b | 126 | { |
zh870524589 | 0:45ce241d316b | 127 | if(_y<mt_pos.y) |
zh870524589 | 0:45ce241d316b | 128 | { |
zh870524589 | 0:45ce241d316b | 129 | _d = S; |
zh870524589 | 0:45ce241d316b | 130 | } |
zh870524589 | 0:45ce241d316b | 131 | else if(_y>mt_pos.y) |
zh870524589 | 0:45ce241d316b | 132 | { |
zh870524589 | 0:45ce241d316b | 133 | _d = N; |
zh870524589 | 0:45ce241d316b | 134 | } |
zh870524589 | 0:45ce241d316b | 135 | } |
zh870524589 | 0:45ce241d316b | 136 | } |
zh870524589 | 0:45ce241d316b | 137 | |
zh870524589 | 0:45ce241d316b | 138 | //draw the ETank in four kinds of ways |
zh870524589 | 0:45ce241d316b | 139 | void ETank::drawE(N5110 &lcd) |
zh870524589 | 0:45ce241d316b | 140 | { |
zh870524589 | 0:45ce241d316b | 141 | switch (_d) |
zh870524589 | 0:45ce241d316b | 142 | { |
zh870524589 | 0:45ce241d316b | 143 | case S: |
zh870524589 | 0:45ce241d316b | 144 | lcd.drawSprite(_x,_y,5,5,(int *)run_down); |
zh870524589 | 0:45ce241d316b | 145 | break; |
zh870524589 | 0:45ce241d316b | 146 | |
zh870524589 | 0:45ce241d316b | 147 | case N: |
zh870524589 | 0:45ce241d316b | 148 | lcd.drawSprite(_x,_y,5,5,(int *)run_up); |
zh870524589 | 0:45ce241d316b | 149 | break; |
zh870524589 | 0:45ce241d316b | 150 | |
zh870524589 | 0:45ce241d316b | 151 | case W: |
zh870524589 | 0:45ce241d316b | 152 | lcd.drawSprite(_x,_y,5,5,(int *)run_left); |
zh870524589 | 0:45ce241d316b | 153 | break; |
zh870524589 | 0:45ce241d316b | 154 | |
zh870524589 | 0:45ce241d316b | 155 | case E: |
zh870524589 | 0:45ce241d316b | 156 | lcd.drawSprite(_x,_y,5,5,(int *)run_right); |
zh870524589 | 0:45ce241d316b | 157 | break; |
zh870524589 | 0:45ce241d316b | 158 | } |
zh870524589 | 0:45ce241d316b | 159 | } |
zh870524589 | 0:45ce241d316b | 160 | |
zh870524589 | 0:45ce241d316b | 161 | |
zh870524589 | 0:45ce241d316b | 162 | bool ETank::collsion_check(Vector2D t1,Vector2D t2) |
zh870524589 | 0:45ce241d316b | 163 | { |
zh870524589 | 0:45ce241d316b | 164 | if(( t1.x>=t2.x && t1.x <= t2.x+4) && (t1.y >= t2.y&& t1.y <=t2.y+4) |
zh870524589 | 0:45ce241d316b | 165 | ||( t1.x+4>=t2.x && t1.x <= t2.x) && (t1.y >= t2.y&& t1.y <=t2.y+4) |
zh870524589 | 0:45ce241d316b | 166 | ||(t1.x>=t2.x && t1.x <= t2.x+4) && (t1.y+4 >= t2.y&& t1.y <=t2.y) |
zh870524589 | 0:45ce241d316b | 167 | ||(t1.x +4>=t2.x && t1.x <= t2.x) && (t1.y+4 >= t2.y&& t1.y <=t2.y)) |
zh870524589 | 0:45ce241d316b | 168 | |
zh870524589 | 0:45ce241d316b | 169 | { |
zh870524589 | 0:45ce241d316b | 170 | return true; |
zh870524589 | 0:45ce241d316b | 171 | } |
zh870524589 | 0:45ce241d316b | 172 | else |
zh870524589 | 0:45ce241d316b | 173 | return false; |
zh870524589 | 0:45ce241d316b | 174 | |
zh870524589 | 0:45ce241d316b | 175 | } |
zh870524589 | 0:45ce241d316b | 176 | //Detect ETank to MyTank collisions |
zh870524589 | 0:45ce241d316b | 177 | void ETank::check_tank_collsion(N5110 &lcd,MyTank &mt) |
zh870524589 | 0:45ce241d316b | 178 | { |
zh870524589 | 0:45ce241d316b | 179 | Vector2D et_pos = get_pos(); |
zh870524589 | 0:45ce241d316b | 180 | Vector2D mt_pos = mt.get_pos(); |
zh870524589 | 0:45ce241d316b | 181 | |
zh870524589 | 0:45ce241d316b | 182 | if(collsion_check(et_pos,mt_pos)) |
zh870524589 | 0:45ce241d316b | 183 | |
zh870524589 | 0:45ce241d316b | 184 | { |
zh870524589 | 0:45ce241d316b | 185 | if(_d == S){ |
zh870524589 | 0:45ce241d316b | 186 | _y -= 1; |
zh870524589 | 0:45ce241d316b | 187 | |
zh870524589 | 0:45ce241d316b | 188 | } else if(_d == N){ |
zh870524589 | 0:45ce241d316b | 189 | _y += 1; |
zh870524589 | 0:45ce241d316b | 190 | |
zh870524589 | 0:45ce241d316b | 191 | } else if(_d == E || _d == SE || _d == NE ){ |
zh870524589 | 0:45ce241d316b | 192 | _x -= 1; |
zh870524589 | 0:45ce241d316b | 193 | |
zh870524589 | 0:45ce241d316b | 194 | } else if(_d == W || _d == SW || _d == NW){ |
zh870524589 | 0:45ce241d316b | 195 | _x += 1; |
zh870524589 | 0:45ce241d316b | 196 | } |
zh870524589 | 0:45ce241d316b | 197 | } |
zh870524589 | 0:45ce241d316b | 198 | |
zh870524589 | 0:45ce241d316b | 199 | |
zh870524589 | 0:45ce241d316b | 200 | } |
zh870524589 | 0:45ce241d316b | 201 | |
zh870524589 | 0:45ce241d316b | 202 | // Detect collisions among ETanks |
zh870524589 | 0:45ce241d316b | 203 | void ETank::check_self_collsion(list<ETank*>&etank) |
zh870524589 | 0:45ce241d316b | 204 | { |
zh870524589 | 0:45ce241d316b | 205 | Vector2D et_pos = get_pos(); |
zh870524589 | 0:45ce241d316b | 206 | |
zh870524589 | 0:45ce241d316b | 207 | for(list<ETank*>::iterator it =etank.begin();it!=etank.end();it++) |
zh870524589 | 0:45ce241d316b | 208 | { |
zh870524589 | 0:45ce241d316b | 209 | Vector2D eet_pos = (*it)->get_pos(); |
zh870524589 | 0:45ce241d316b | 210 | if(collsion_check(et_pos,eet_pos)&&eet_pos.x !=et_pos.x) // Make sure it's not itself |
zh870524589 | 0:45ce241d316b | 211 | { |
zh870524589 | 0:45ce241d316b | 212 | if(_d == S){ |
zh870524589 | 0:45ce241d316b | 213 | _y -= 1; |
zh870524589 | 0:45ce241d316b | 214 | |
zh870524589 | 0:45ce241d316b | 215 | } else if(_d == N){ |
zh870524589 | 0:45ce241d316b | 216 | _y += 1; |
zh870524589 | 0:45ce241d316b | 217 | |
zh870524589 | 0:45ce241d316b | 218 | } else if(_d == E || _d == SE || _d == NE ){ |
zh870524589 | 0:45ce241d316b | 219 | _x -= 1; |
zh870524589 | 0:45ce241d316b | 220 | |
zh870524589 | 0:45ce241d316b | 221 | } else if(_d == W || _d == SW || _d == NW){ |
zh870524589 | 0:45ce241d316b | 222 | _x += 1; |
zh870524589 | 0:45ce241d316b | 223 | } |
zh870524589 | 0:45ce241d316b | 224 | } else if(collsion_check(et_pos,eet_pos)&&eet_pos.y !=et_pos.y) // Make sure it's not itself |
zh870524589 | 0:45ce241d316b | 225 | { |
zh870524589 | 0:45ce241d316b | 226 | if(_d == S){ |
zh870524589 | 0:45ce241d316b | 227 | _y -= 1; |
zh870524589 | 0:45ce241d316b | 228 | |
zh870524589 | 0:45ce241d316b | 229 | } else if(_d == N){ |
zh870524589 | 0:45ce241d316b | 230 | _y += 1; |
zh870524589 | 0:45ce241d316b | 231 | |
zh870524589 | 0:45ce241d316b | 232 | } else if(_d == E || _d == SE || _d == NE ){ |
zh870524589 | 0:45ce241d316b | 233 | _x -= 1; |
zh870524589 | 0:45ce241d316b | 234 | |
zh870524589 | 0:45ce241d316b | 235 | } else if(_d == W || _d == SW || _d == NW){ |
zh870524589 | 0:45ce241d316b | 236 | _x += 1; |
zh870524589 | 0:45ce241d316b | 237 | } |
zh870524589 | 0:45ce241d316b | 238 | |
zh870524589 | 0:45ce241d316b | 239 | } |
zh870524589 | 0:45ce241d316b | 240 | |
zh870524589 | 0:45ce241d316b | 241 | } |
zh870524589 | 0:45ce241d316b | 242 | } |
zh870524589 | 0:45ce241d316b | 243 | |
zh870524589 | 0:45ce241d316b | 244 | |
zh870524589 | 0:45ce241d316b | 245 | |
zh870524589 | 0:45ce241d316b | 246 | void ETank::check_pixel(N5110 &lcd) |
zh870524589 | 0:45ce241d316b | 247 | { |
zh870524589 | 0:45ce241d316b | 248 | Vector2D mt_pos = get_pos(); |
zh870524589 | 0:45ce241d316b | 249 | |
zh870524589 | 0:45ce241d316b | 250 | if((lcd.getPixel(mt_pos.x-1,mt_pos.y)&&_d == W ) // Left of top left corner |
zh870524589 | 0:45ce241d316b | 251 | ||(lcd.getPixel(mt_pos.x-1,mt_pos.y+4)&&_d == W ) // Left of bottom left corner |
zh870524589 | 0:45ce241d316b | 252 | ||(lcd.getPixel(mt_pos.x+5,mt_pos.y)&&_d == E ) // Right of top right corner |
zh870524589 | 0:45ce241d316b | 253 | ||(lcd.getPixel(mt_pos.x+5,mt_pos.y+4)&&_d == E ) //Right of bottom right corner |
zh870524589 | 0:45ce241d316b | 254 | ||(lcd.getPixel(mt_pos.x,mt_pos.y-1)&&_d==N) // Top of Top left corner |
zh870524589 | 0:45ce241d316b | 255 | ||(lcd.getPixel(mt_pos.x+4,mt_pos.y-1)&&_d==N) //Top of Top right corner |
zh870524589 | 0:45ce241d316b | 256 | ||(lcd.getPixel(mt_pos.x,mt_pos.y+5)&&_d==S) //Bottom of left corner |
zh870524589 | 0:45ce241d316b | 257 | ||(lcd.getPixel(mt_pos.x+4,mt_pos.y+5)&&_d==S)) //Bottom of right corner |
zh870524589 | 0:45ce241d316b | 258 | { |
zh870524589 | 0:45ce241d316b | 259 | if(_d == S){ |
zh870524589 | 0:45ce241d316b | 260 | _y -= 1; |
zh870524589 | 0:45ce241d316b | 261 | |
zh870524589 | 0:45ce241d316b | 262 | |
zh870524589 | 0:45ce241d316b | 263 | } else if(_d == N){ |
zh870524589 | 0:45ce241d316b | 264 | _y += 1; |
zh870524589 | 0:45ce241d316b | 265 | |
zh870524589 | 0:45ce241d316b | 266 | |
zh870524589 | 0:45ce241d316b | 267 | } else if(_d == E || _d == SE || _d == NE ){ |
zh870524589 | 0:45ce241d316b | 268 | _x -= 1; |
zh870524589 | 0:45ce241d316b | 269 | |
zh870524589 | 0:45ce241d316b | 270 | |
zh870524589 | 0:45ce241d316b | 271 | } else if(_d == W || _d == SW || _d == NW){ |
zh870524589 | 0:45ce241d316b | 272 | _x += 1; |
zh870524589 | 0:45ce241d316b | 273 | |
zh870524589 | 0:45ce241d316b | 274 | } |
zh870524589 | 0:45ce241d316b | 275 | } |
zh870524589 | 0:45ce241d316b | 276 | |
zh870524589 | 0:45ce241d316b | 277 | } |
zh870524589 | 0:45ce241d316b | 278 | |
zh870524589 | 0:45ce241d316b | 279 | |
zh870524589 | 0:45ce241d316b | 280 | void ETank::update(N5110 &lcd,MyTank &mt,list<ETank*>&etank) |
zh870524589 | 0:45ce241d316b | 281 | { |
zh870524589 | 0:45ce241d316b | 282 | check_pixel(lcd); |
zh870524589 | 0:45ce241d316b | 283 | check_tank_collsion(lcd,mt); |
zh870524589 | 0:45ce241d316b | 284 | Vector2D mt_pos = mt.get_pos(); |
zh870524589 | 0:45ce241d316b | 285 | if(_x!= mt_pos.x && _y!= mt_pos.y) |
zh870524589 | 0:45ce241d316b | 286 | { |
zh870524589 | 0:45ce241d316b | 287 | if (_d == S) { |
zh870524589 | 0:45ce241d316b | 288 | this-> _y+=1; |
zh870524589 | 0:45ce241d316b | 289 | } else if (_d == N) { |
zh870524589 | 0:45ce241d316b | 290 | this-> _y-=1; |
zh870524589 | 0:45ce241d316b | 291 | } else if (_d == E || _d == SE || _d == NE) { |
zh870524589 | 0:45ce241d316b | 292 | this-> _x+=1; |
zh870524589 | 0:45ce241d316b | 293 | } else if (_d == W || _d == SW || _d == NW) { |
zh870524589 | 0:45ce241d316b | 294 | this-> _x-=1; |
zh870524589 | 0:45ce241d316b | 295 | } |
zh870524589 | 0:45ce241d316b | 296 | // ensure the ETank will not move out of the scope |
zh870524589 | 0:45ce241d316b | 297 | if (_y < 6 && _y >=5) { |
zh870524589 | 0:45ce241d316b | 298 | this-> _y = 6; |
zh870524589 | 0:45ce241d316b | 299 | } |
zh870524589 | 0:45ce241d316b | 300 | if (_x < 1 && _x >=0) { |
zh870524589 | 0:45ce241d316b | 301 | this-> _x = 1; |
zh870524589 | 0:45ce241d316b | 302 | } |
zh870524589 | 0:45ce241d316b | 303 | if(_y>43){ |
zh870524589 | 0:45ce241d316b | 304 | this-> _y=43; |
zh870524589 | 0:45ce241d316b | 305 | } |
zh870524589 | 0:45ce241d316b | 306 | if(_x>79){ |
zh870524589 | 0:45ce241d316b | 307 | this-> _x=79; |
zh870524589 | 0:45ce241d316b | 308 | } |
zh870524589 | 0:45ce241d316b | 309 | //step of destroy the ETank |
zh870524589 | 0:45ce241d316b | 310 | if(_x == -5||_y == -5){ |
zh870524589 | 0:45ce241d316b | 311 | this-> _x = -5; |
zh870524589 | 0:45ce241d316b | 312 | this-> _y = -5; |
zh870524589 | 0:45ce241d316b | 313 | } |
zh870524589 | 0:45ce241d316b | 314 | } |
zh870524589 | 0:45ce241d316b | 315 | |
zh870524589 | 0:45ce241d316b | 316 | check_self_collsion(etank); |
zh870524589 | 0:45ce241d316b | 317 | } |
zh870524589 | 0:45ce241d316b | 318 | |
zh870524589 | 0:45ce241d316b | 319 | void ETank::attack(Vector2D v,Gamepad &pad,N5110 &lcd,MyTank &mt,list<Bullet*> &mtbullet,int et_attack,int timegap) |
zh870524589 | 0:45ce241d316b | 320 | { |
zh870524589 | 0:45ce241d316b | 321 | count++; |
zh870524589 | 0:45ce241d316b | 322 | if(count>timegap) // Time interval between attacks |
zh870524589 | 0:45ce241d316b | 323 | if(et_attack>40){//possibility of attack |
zh870524589 | 0:45ce241d316b | 324 | v = get_pos(); // get the position of the ETank |
zh870524589 | 0:45ce241d316b | 325 | Bullet* bullet = new Bullet(pad,v); // create the bullets when attack |
zh870524589 | 0:45ce241d316b | 326 | lstBullets.push_back(bullet); |
zh870524589 | 0:45ce241d316b | 327 | count = 0; |
zh870524589 | 0:45ce241d316b | 328 | } |
zh870524589 | 0:45ce241d316b | 329 | Vector2D mt_pos = mt.get_pos(); |
zh870524589 | 0:45ce241d316b | 330 | for(list<Bullet*>::iterator it = lstBullets.begin(); it != lstBullets.end();) |
zh870524589 | 0:45ce241d316b | 331 | { |
zh870524589 | 0:45ce241d316b | 332 | if((*it)->destroy()) |
zh870524589 | 0:45ce241d316b | 333 | { |
zh870524589 | 0:45ce241d316b | 334 | delete *it; |
zh870524589 | 0:45ce241d316b | 335 | it = lstBullets.erase(it); // delete the bullet |
zh870524589 | 0:45ce241d316b | 336 | continue; |
zh870524589 | 0:45ce241d316b | 337 | } |
zh870524589 | 0:45ce241d316b | 338 | else if ((*it)->destroyT(mt_pos)) |
zh870524589 | 0:45ce241d316b | 339 | { |
zh870524589 | 0:45ce241d316b | 340 | delete *it; |
zh870524589 | 0:45ce241d316b | 341 | it = lstBullets.erase(it); |
zh870524589 | 0:45ce241d316b | 342 | mt.subHP(); |
zh870524589 | 0:45ce241d316b | 343 | int M_HP = mt.MT_HP(); // MyTank's health is reduced when the bullet hits the target |
zh870524589 | 0:45ce241d316b | 344 | pad.tone(500.0,0.1); |
zh870524589 | 0:45ce241d316b | 345 | if(M_HP == 5) |
zh870524589 | 0:45ce241d316b | 346 | pad.led(6,0); |
zh870524589 | 0:45ce241d316b | 347 | if(M_HP == 4) |
zh870524589 | 0:45ce241d316b | 348 | pad.led(5,0); |
zh870524589 | 0:45ce241d316b | 349 | if(M_HP == 3) |
zh870524589 | 0:45ce241d316b | 350 | pad.led(4,0); |
zh870524589 | 0:45ce241d316b | 351 | if(M_HP==2) |
zh870524589 | 0:45ce241d316b | 352 | pad.led(3,0); |
zh870524589 | 0:45ce241d316b | 353 | if(M_HP==1) |
zh870524589 | 0:45ce241d316b | 354 | pad.led(2,0); |
zh870524589 | 0:45ce241d316b | 355 | if(M_HP==0){ |
zh870524589 | 0:45ce241d316b | 356 | pad.led(1,0); |
zh870524589 | 0:45ce241d316b | 357 | mt.draw(lcd); |
zh870524589 | 0:45ce241d316b | 358 | } |
zh870524589 | 0:45ce241d316b | 359 | continue; |
zh870524589 | 0:45ce241d316b | 360 | } |
zh870524589 | 0:45ce241d316b | 361 | (*it)->et_update(lcd,_d);//update the bullet |
zh870524589 | 0:45ce241d316b | 362 | (*it)->bullet_collison(lstBullets,mtbullet); //check the collsion between bulltes |
zh870524589 | 0:45ce241d316b | 363 | it++; |
zh870524589 | 0:45ce241d316b | 364 | } |
zh870524589 | 0:45ce241d316b | 365 | } |
zh870524589 | 0:45ce241d316b | 366 |