anxuanyu
Embed:
(wiki syntax)
Show/hide line numbers
main1.cpp
00001 #include "mbed.h" 00002 #include "N5110.h" 00003 #include "main.h" 00004 #ifndef bullet_v 00005 #define bullet_v 3//炮弹的速度为坦克三倍 00006 #endif 00007 #ifndef tank_num 00008 #define tank_num 4//坦克数量 00009 #endif 00010 00011 DigitalIn button_A2(p29); 00012 DigitalIn button_B2(p28); 00013 DigitalIn button_C2(p27); 00014 DigitalIn button_D2(p26); 00015 00016 AnalogIn joy_v2(p20); 00017 AnalogIn joy_h2(p19); 00018 AnalogIn joy_button2(p17); // could be DigitalIn, but use AnalogIn so pot can also be used 00019 00020 // JP1 must be in 2/3 position 00021 N5110 lcd2(p8,p9,p10,p11,p13,p21); 00022 const int map[9][11] = { 00023 { 0,0,0,0,0,0,0,0,0,0,0}, 00024 { 0,1,0,2,1,1,1,2,0,1,0}, 00025 { 0,1,0,2,0,1,0,2,0,1,0}, 00026 { 0,0,0,0,0,0,0,0,0,0,0}, 00027 { 2,2,0,1,2,2,2,1,0,2,2}, 00028 { 0,0,0,0,0,0,0,0,0,0,0}, 00029 { 0,1,0,2,0,1,0,2,0,1,0}, 00030 { 0,1,0,2,1,1,1,2,0,1,0}, 00031 { 0,0,0,0,0,0,0,0,0,0,0}, 00032 };//9*11大小的地图,1是墙,2是灌木 00033 const int wall[5][7] = { 00034 { 1,1,1,1,1,1,1}, 00035 { 1,1,0,0,0,1,1}, 00036 { 1,1,0,0,0,1,1}, 00037 { 1,1,0,0,0,1,1}, 00038 { 1,1,1,1,1,1,1}, 00039 };//5*7的墙,不可破坏 00040 const int bush[5][7] = { 00041 { 1,0,1,0,1,0,1}, 00042 { 0,1,0,1,0,1,0}, 00043 { 1,0,1,0,1,0,1}, 00044 { 0,1,0,1,0,1,0}, 00045 { 1,0,1,0,1,0,1}, 00046 00047 };//5*7的g灌木,可破坏 00048 const int destroyed_bush[5][7] = { 00049 { 1,0,0,1,0,0,1}, 00050 { 0,0,1,0,0,1,0}, 00051 { 0,1,0,0,1,0,0}, 00052 { 1,0,0,1,0,0,1}, 00053 { 0,0,1,0,0,1,0}, 00054 00055 };//被击中的灌木 00056 const int tank[tank_num][4][5][7] = { 00057 { 00058 { 00059 { 0,1,0,1,0,1,0}, 00060 { 0,1,1,1,1,1,0}, 00061 { 0,1,1,0,1,1,0}, 00062 { 0,1,1,0,1,1,0}, 00063 { 0,1,0,0,0,1,0} 00064 }, 00065 { 00066 { 0,1,0,0,0,1,0}, 00067 { 0,1,1,0,1,1,0}, 00068 { 0,1,1,0,1,1,0}, 00069 { 0,1,1,1,1,1,0}, 00070 { 0,1,0,1,0,1,0} 00071 }, 00072 { 00073 { 0,1,1,1,1,1,0}, 00074 { 0,0,1,1,1,0,0}, 00075 { 0,1,1,0,0,0,0}, 00076 { 0,0,1,1,1,0,0}, 00077 { 0,1,1,1,1,1,0} 00078 }, 00079 { 00080 { 0,1,1,1,1,1,0}, 00081 { 0,0,1,1,1,0,0}, 00082 { 0,0,0,0,1,1,0}, 00083 { 0,0,1,1,1,0,0}, 00084 { 0,1,1,1,1,1,0} 00085 } 00086 },//tank1 00087 { 00088 { 00089 { 0,1,0,1,0,1,0}, 00090 { 0,1,0,1,0,1,0}, 00091 { 0,1,1,1,1,1,0}, 00092 { 0,1,1,0,0,1,0}, 00093 { 0,1,0,0,0,1,0} 00094 00095 }, 00096 { 00097 { 0,1,0,0,0,1,0}, 00098 { 0,1,0,0,1,1,0}, 00099 { 0,1,1,1,1,1,0}, 00100 { 0,1,0,1,0,1,0}, 00101 { 0,1,0,1,0,1,0} 00102 }, 00103 { 00104 { 0,1,1,1,1,1,0}, 00105 { 0,0,0,1,0,0,0}, 00106 { 0,1,1,1,0,0,0}, 00107 { 0,0,0,1,1,0,0}, 00108 { 0,1,1,1,1,1,0} 00109 }, 00110 { 00111 { 0,1,1,1,1,1,0}, 00112 { 0,0,1,1,0,0,0}, 00113 { 0,0,0,1,1,1,0}, 00114 { 0,0,0,1,0,0,0}, 00115 { 0,1,1,1,1,1,0} 00116 } 00117 },//tank2 00118 { 00119 { 00120 { 0,1,0,1,0,1,0}, 00121 { 0,1,0,1,0,1,0}, 00122 { 0,1,1,1,1,1,0}, 00123 { 0,1,0,1,0,1,0}, 00124 { 0,1,0,0,0,1,0} 00125 00126 }, 00127 { 00128 { 0,1,0,0,0,1,0}, 00129 { 0,1,0,1,0,1,0}, 00130 { 0,1,1,1,1,1,0}, 00131 { 0,1,0,1,0,1,0}, 00132 { 0,1,0,1,0,1,0} 00133 }, 00134 { 00135 { 0,1,1,1,1,1,0}, 00136 { 0,0,0,1,0,0,0}, 00137 { 0,1,1,1,1,0,0}, 00138 { 0,0,0,1,0,0,0}, 00139 { 0,1,1,1,1,1,0} 00140 }, 00141 { 00142 { 0,1,1,1,1,1,0}, 00143 { 0,0,0,1,0,0,0}, 00144 { 0,0,1,1,1,1,0}, 00145 { 0,0,0,1,0,0,0}, 00146 { 0,1,1,1,1,1,0} 00147 } 00148 }, //tank3 00149 { 00150 { 00151 { 0,1,0,1,0,1,0}, 00152 { 0,1,0,1,0,1,0}, 00153 { 0,1,1,1,1,1,0}, 00154 { 0,1,0,0,1,1,0}, 00155 { 0,1,0,0,0,1,0} 00156 00157 }, 00158 { 00159 { 0,1,0,0,0,1,0}, 00160 { 0,1,1,0,0,1,0}, 00161 { 0,1,1,1,1,1,0}, 00162 { 0,1,0,1,0,1,0}, 00163 { 0,1,0,1,0,1,0} 00164 }, 00165 { 00166 { 0,1,1,1,1,1,0}, 00167 { 0,0,0,1,1,0,0}, 00168 { 0,1,1,1,0,0,0}, 00169 { 0,0,0,1,0,0,0}, 00170 { 0,1,1,1,1,1,0} 00171 }, 00172 { 00173 { 0,1,1,1,1,1,0}, 00174 { 0,0,0,1,0,0,0}, 00175 { 0,0,0,1,1,1,0}, 00176 { 0,0,1,1,0,0,0}, 00177 { 0,1,1,1,1,1,0} 00178 } 00179 }//tank4 00180 }; 00181 const int tank_exp[5][7] = { 00182 { 0,1,0,1,0,1,0}, 00183 { 0,0,1,0,1,0,0}, 00184 { 1,1,0,0,0,1,1}, 00185 { 0,0,1,0,1,0,0}, 00186 { 0,1,0,1,0,1,0}, 00187 };//击毁的坦克 00188 const int bullet[5][7] = { 00189 { 0,0,0,0,0,0,0}, 00190 { 0,0,1,1,0,0,0}, 00191 { 0,0,1,1,0,0,0}, 00192 { 0,0,0,0,0,0,0}, 00193 { 0,0,0,0,0,0,0}, 00194 };//炮弹 00195 const int hp_ai[2][5][7] = {{ 00196 { 0,1,1,0,0,0,0}, 00197 { 1,1,1,1,0,0,0}, 00198 { 0,1,1,1,0,0,0}, 00199 { 1,0,1,1,0,0,1}, 00200 { 1,1,0,1,0,1,1}, 00201 },{ 00202 { 0,1,1,0,1,1,0}, 00203 { 1,1,1,1,1,1,1}, 00204 { 0,1,1,1,1,1,0}, 00205 { 1,0,1,1,1,0,1}, 00206 { 1,1,0,1,0,1,1}, 00207 } 00208 };//炮弹 00209 const int t_start[tank_num][2] = {{0,5},{8,1},{8,5},{8,9}}; //坦克初始坐标 00210 const int ai_mod[3]= {0,1,0}; 00211 const int ai_changetime[3]= {6,8,5}; 00212 void map_scan2(int map_adj[9][11]) //扫描地图函数 00213 { 00214 int x = 0; 00215 int y = 0; //地图扫描坐标 00216 lcd2.drawRect(0,0,84,1,FILL_BLACK); 00217 lcd2.drawRect(0,46,84,2,FILL_BLACK); 00218 lcd2.drawRect(0,0,3,48,FILL_BLACK); 00219 lcd2.drawRect(80,0,4,48,FILL_BLACK);//边框 00220 00221 for (x=0; x<11; x++) { 00222 for (y=0; y<9; y++) { 00223 if(map_adj[y][x]==1) { 00224 lcd2.drawSprite(x*7+3,y*5+1,5,7,(int *)wall); 00225 } else if(map_adj[y][x]==2) { 00226 lcd2.drawSprite(x*7+3,y*5+1,5,7,(int *)bush); 00227 } 00228 } 00229 } 00230 } 00231 00232 void hp_show2(int hp[4],int level) //显示血量函数 00233 { 00234 int heart_num; 00235 int i; 00236 for (heart_num=0; heart_num<3; heart_num++) { 00237 if(heart_num<hp[0]) { 00238 lcd2.drawSprite(4*7+heart_num*7+3,5*1+1,5,7,(int *)hp_ai[0]); 00239 for(i=1; i<tank_num-level; i++) { 00240 if(heart_num<hp[i]) { 00241 lcd2.drawSprite(4*7+(i-1)*7+3,7*5+1,5,7,(int *)hp_ai[hp[i]-1]); 00242 } 00243 } 00244 } 00245 00246 } 00247 } 00248 00249 void tank_show2(int tank_state[4],int t[4][2],int hp[4],int level) //显示坦克函数 00250 { 00251 int i; 00252 for(i=0; i<tank_num-level; i++) { 00253 if(hp[i]!=0) { 00254 lcd2.drawSprite(t[i][1]*7+3,t[i][0]*5+1,5,7,(int *)tank[i][tank_state[i]]); 00255 } 00256 } 00257 } 00258 int body(int level) 00259 { 00260 // need to initialise the lcd2 first, do this once outside of the loop 00261 lcd2.init(); 00262 //int command[2][4]={{0,0,0,0},{0,0,0,0}};//处理后的指令 00263 int command[4]= {0,0,0,0}; 00264 int turn=0; 00265 int a; 00266 int stop;//stop command for ai 00267 int i; 00268 int j; //地图复制计数 00269 int ii; 00270 int k; 00271 int l; 00272 int n; //炮弹刷新计数 00273 int o; 00274 int shoot;//开火计数 00275 int t[tank_num][2]; //坦克坐标 00276 int t_pre[tank_num][2]; //坦克预测坐标(npc) 00277 int b[tank_num][3][2]; //炮弹坐标 00278 int bullet_cont=0;//发射炮弹计数 00279 int bullet_direct[tank_num][3];//炮弹的方向 00280 int bullet_val[tank_num][3];//炮弹是否被阻挡 00281 int tank_state[tank_num]; //坦克的f方向 00282 int hp_set[tank_num]= {3,2,2,2}; //坦克血量预设 00283 int hp[tank_num]= {0,0,0,0}; //坦克血量 00284 int tank_count;//坦克单位的计数 00285 int trace[tank_num-1][3]= {{0,0,0},{0,0,0},{0,0,0}}; 00286 int map_adj[9][11]; 00287 int tank_des;//serial number of destoried tank destroy 00288 int hit; 00289 int obj_kind; 00290 for (k=0; k<tank_num-level; k++) { 00291 hp[k]=hp_set[k]; 00292 tank_state[k]=0; 00293 map_adj[t_start[k][0]][t_start[k][1]]=k+3; 00294 for (l=0; l<bullet_v; l++) { 00295 bullet_val[k][l]=0; 00296 bullet_direct[k][l]=0; 00297 for(o=0;o<2;o++){ 00298 b[k][l][o]=0; 00299 } 00300 } 00301 for (ii=0; ii<2; ii++) { 00302 t[k][ii]=t_start[k][ii]; 00303 } //复制坦克坐标 00304 } 00305 00306 for (i=0; i<11; i++) { 00307 for (j=0; j<9; j++) { 00308 map_adj[j][i]=map[j][i]; 00309 } 00310 }//复制地图 00311 shoot=0; 00312 tank_count=0; 00313 while(1) { 00314 lcd2.clear(); 00315 map_scan2(map_adj);//地图显示 00316 hp_show2(hp,level);//血量显示 00317 tank_show2(tank_state,t,hp,level);//坦克显示 00318 00319 if(shoot==0) { 00320 command[3] = button_A2.read(); 00321 command[0] = button_B2.read(); 00322 command[2] = button_C2.read(); 00323 command[1] = button_D2.read(); //复制指令 00324 turn=turn+1; 00325 if(turn==15) { 00326 turn=0; 00327 }//各种计数 00328 bullet_cont++; 00329 if(bullet_cont==3) { 00330 bullet_cont=0; 00331 } 00332 00333 for(tank_count=0; tank_count<tank_num-level; tank_count++) { 00334 if(tank_count!=0&&hp[tank_count]!=0) { 00335 stop=0; 00336 t_pre[tank_count][0]=t[tank_count][0]; 00337 t_pre[tank_count][1]=t[tank_count][1]; 00338 trace[tank_count][0]=(ai_changetime[tank_count-1]<turn)^ai_mod[tank_count-1]^(t[tank_count][0]>t[0][0]);//1代表坦克1在坦克2的下方 00339 trace[tank_count][1]=(ai_changetime[tank_count-1]<turn)^ai_mod[tank_count-1]^(t[tank_count][1]>t[0][1]);//1代表坦克1在坦克2的右 00340 trace[tank_count][2]=(abs(t[0][0]-t[tank_count][0]))>(abs(t[0][1]-t[tank_count][1]));//1代表坦克竖直距离大于水平距离 00341 for(a=0; a<4; a++) { 00342 tank_state[tank_count]=2*(!((a%2)^trace[tank_count][2]))+(!((a>>1)^trace[tank_count][0]))*((a%2)^trace[tank_count][2])+(!((a>>1)^trace[tank_count][1]))*(!((a%2)^trace[tank_count][2])); 00343 t_pre[tank_count][0]=t[tank_count][0]+(!(tank_state[tank_count]>>1))*(2*(tank_state[tank_count]%2)-1); 00344 t_pre[tank_count][1]=t[tank_count][1]+(tank_state[tank_count]>>1)*(2*(tank_state[tank_count]%2)-1); 00345 if((map_adj[t_pre[tank_count][0]][t_pre[tank_count][1]]==0)&&(t_pre[tank_count][1]>=0)&&(t_pre[tank_count][1]<=10)&&(t_pre[tank_count][0]>=0)&&(t_pre[tank_count][0]<=8)) { 00346 break; 00347 } 00348 if(a==3) stop=1; 00349 } 00350 map_adj[t[tank_count][0]][t[tank_count][1]]=0; 00351 if(stop==0) { 00352 t[tank_count][tank_state[tank_count]>>1]=t[tank_count][tank_state[tank_count]>>1]+2*(tank_state[tank_count]%2)-1; 00353 } 00354 map_adj[t[tank_count][0]][t[tank_count][1]]=tank_count+3; 00355 bullet_val[tank_count][bullet_cont]=1; //坦克发射炮弹技炮弹坐标方向计算 00356 bullet_direct[tank_count][bullet_cont]=tank_state[tank_count]; 00357 b[tank_count][bullet_cont][0]=t[tank_count][0]; 00358 b[tank_count][bullet_cont][1]=t[tank_count][1]; 00359 } else if(hp[tank_count]!=0&&tank_count==0) { 00360 command[3]=(~command[0])&(~command[1])&(~command[2])&command[3]; 00361 command[2]=(~command[0])&(~command[1])&command[2]; 00362 command[1]=(~command[0])&command[1]; 00363 command[0]=command[0]; 00364 00365 map_adj[t[tank_count][0]][t[tank_count][1]]=0; 00366 t[tank_count][0]=t[tank_count][0]-command[0]+command[1]; 00367 t[tank_count][1]=t[tank_count][1]-command[2]+command[3];//指令控制坦克 00368 00369 if(map_adj[t[tank_count][0]][t[tank_count][1]]!=0||t[tank_count][1]<0||t[tank_count][1]>10||t[tank_count][0]<0||t[tank_count][0]>8) { 00370 t[tank_count][0]=t[tank_count][0]+command[0]-command[1]; 00371 t[tank_count][1]=t[tank_count][1]+command[2]-command[3]; 00372 }//障碍物判断,前方有障碍物时指令无效 00373 map_adj[t[tank_count][0]][t[tank_count][1]]=tank_count+3; 00374 if(command[0]||command[1]||command[2]||command[3]) { 00375 tank_state[tank_count]=command[1]*1+command[2]*2+command[3]*3; 00376 } //坦克方向计算 00377 if (joy_button2.read()==0) { 00378 bullet_val[tank_count][bullet_cont]=1; //坦克发射炮弹技炮弹坐标方向计算 00379 bullet_direct[tank_count][bullet_cont]=tank_state[tank_count]; 00380 b[tank_count][bullet_cont][0]=t[tank_count][0]; 00381 b[tank_count][bullet_cont][1]=t[tank_count][1]; 00382 } 00383 } 00384 00385 } 00386 00387 } 00388 for(tank_count=0; tank_count<tank_num-level; tank_count++) { //游戏里坦克依次进行运算 00389 if(hp[tank_count]!=0) { 00390 for(n=0; n<bullet_v; n++) { //炮弹比坦克快三倍运算也快三倍 00391 b[tank_count][n][0]=b[tank_count][n][0]+(!(bullet_direct[tank_count][n]>>1))*(-1+2*(bullet_direct[tank_count][n]%2)); 00392 b[tank_count][n][1]=b[tank_count][n][1]+(bullet_direct[tank_count][n]>>1)*(-1+2*(bullet_direct[tank_count][n]%2));//炮弹坐标刷新 00393 if(b[tank_count][n][1]>=0&&b[tank_count][n][1]<=10&&b[tank_count][n][0]>=0&&b[tank_count][n][0]<=8&&bullet_val[tank_count][n]==1) { 00394 //炮弹有效且未出界 00395 obj_kind=map_adj[b[tank_count][n][0]][b[tank_count][n][1]]; 00396 hit=(((tank_count==0)+(obj_kind<4))%2)&&(obj_kind>=3); 00397 if(obj_kind==0) { //炮弹无障碍飞行的情况 00398 lcd2.drawSprite(b[tank_count][n][1]*7+3,b[tank_count][n][0]*5+1,5,7,(int *)bullet); 00399 } else if (obj_kind==2) { //炮弹打到灌木的情况 00400 lcd2.drawSprite(b[tank_count][n][1]*7+3,b[tank_count][n][0]*5+1,5,7,(int *)destroyed_bush); 00401 map_adj[b[tank_count][n][0]][b[tank_count][n][1]]=0; 00402 bullet_val[tank_count][n]=0; 00403 } else if (obj_kind==1) { //炮弹打墙的情况 00404 bullet_val[tank_count][n]=0; 00405 } else if(obj_kind>=3&&hit) { //炮弹击杀坦克的情况 00406 tank_des=map_adj[b[tank_count][n][0]][b[tank_count][n][1]]-3; 00407 map_adj[b[tank_count][n][0]][b[tank_count][n][1]]=0; 00408 lcd2.drawSprite(t[tank_des][1]*7+3,t[tank_des][0]*5+1,5,7,(int *)tank_exp); 00409 t[tank_des][0]=t_start[tank_des][0]; 00410 t[tank_des][1]=t_start[tank_des][1];//坦克回出生点 00411 hp[tank_des]=hp[tank_des]-1;//血量减一 00412 bullet_val[tank_count][n]=0;//炮弹失效 00413 if(hp[0]==0) { 00414 return 0; 00415 } else if((hp[1]+hp[2]+hp[3])==0) { 00416 return 1; 00417 } 00418 } 00419 } 00420 } 00421 } 00422 } 00423 shoot=shoot+1; 00424 if(shoot==bullet_v) { 00425 shoot=0; 00426 } 00427 lcd2.refresh(); // refresh the lcd2 so the pixels appear 00428 wait_ms(600/bullet_v); // 画面按炮弹移动s时间刷新,600为坦克移动时间 00429 } 00430 } 00431 void init_buttons2() 00432 { 00433 // PCB has external pull-down resistors so turn the internal ones off 00434 // (default for DigitalIn) 00435 button_A2.mode(PullNone); 00436 button_B2.mode(PullNone); 00437 button_C2.mode(PullNone); 00438 button_D2.mode(PullNone); 00439 } 00440 void main1() 00441 { 00442 int result; 00443 result=body(1); 00444 if (result==0) { 00445 lcd2.clear(); 00446 lcd2.printString("Game Over", 15, 1); 00447 lcd2.printString("You lose:", 17, 3); 00448 lcd2.refresh(); 00449 wait(1); 00450 while(1) { 00451 if (button_A2.read() == 1) { 00452 main(); 00453 } 00454 } 00455 } else if (result==1) { 00456 lcd2.clear(); 00457 lcd2.printString("Level 1: win", 10, 1); 00458 lcd2.printString("A:Next level", 10, 3); 00459 lcd2.refresh(); 00460 wait(1); 00461 while(1) { 00462 if (button_A2.read() == 1) { 00463 result=body(0); 00464 if(result==0) { 00465 lcd2.clear(); 00466 lcd2.printString("Game Over", 15, 1); 00467 lcd2.printString("You lose:", 17, 3); 00468 lcd2.refresh(); 00469 } else if (result==1) { 00470 lcd2.clear(); 00471 lcd2.printString("You win", 20, 1); 00472 lcd2.printString("A:Menu", 25, 3); 00473 lcd2.refresh(); 00474 00475 } 00476 wait(1); 00477 while(1) { 00478 if (button_A2.read() == 1) { 00479 main(); 00480 } 00481 } 00482 } 00483 } 00484 } 00485 }
Generated on Wed Jul 27 2022 08:00:53 by
1.7.2