Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main3.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_A3(p29); 00012 DigitalIn button_B3(p28); 00013 DigitalIn button_C3(p27); 00014 DigitalIn button_D3(p26); 00015 00016 AnalogIn joy_button3(p17); // could be DigitalIn, but use AnalogIn so pot can also be used 00017 00018 // JP1 must be in 2/3 position 00019 N5110 lcd3(p8,p9,p10,p11,p13,p21); 00020 const int map[9][11] = { 00021 { 0,0,0,0,0,0,0,0,0,0,0}, 00022 { 0,1,0,2,1,1,1,2,0,1,0}, 00023 { 2,1,2,2,2,2,2,2,2,1,2}, 00024 { 0,0,0,0,0,0,0,0,0,0,0}, 00025 { 2,2,2,1,2,2,2,1,2,2,2}, 00026 { 0,0,1,1,1,1,1,1,1,0,0}, 00027 { 0,1,0,4,0,1,0,5,0,1,0}, 00028 { 0,1,2,2,1,1,1,2,2,1,0}, 00029 { 0,0,0,0,0,6,0,0,0,0,0}, 00030 };//9*11大小的地图,1是墙,2是灌木 00031 const int wall[5][7] = { 00032 { 1,1,1,1,1,1,1}, 00033 { 1,1,0,0,0,1,1}, 00034 { 1,1,0,0,0,1,1}, 00035 { 1,1,0,0,0,1,1}, 00036 { 1,1,1,1,1,1,1}, 00037 };//5*7的墙,不可破坏 00038 const int bush[5][7] = { 00039 { 1,0,1,0,1,0,1}, 00040 { 0,1,0,1,0,1,0}, 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 00045 };//5*7的g灌木,可破坏 00046 const int destroyed_bush[5][7] = { 00047 { 1,0,0,1,0,0,1}, 00048 { 0,0,1,0,0,1,0}, 00049 { 0,1,0,0,1,0,0}, 00050 { 1,0,0,1,0,0,1}, 00051 { 0,0,1,0,0,1,0}, 00052 00053 };//被击中的灌木 00054 const int tank[tank_num][4][5][7] = { 00055 { 00056 { 00057 { 0,1,0,1,0,1,0}, 00058 { 0,1,1,1,1,1,0}, 00059 { 0,1,1,0,1,1,0}, 00060 { 0,1,1,0,1,1,0}, 00061 { 0,1,0,0,0,1,0} 00062 }, 00063 { 00064 { 0,1,0,0,0,1,0}, 00065 { 0,1,1,0,1,1,0}, 00066 { 0,1,1,0,1,1,0}, 00067 { 0,1,1,1,1,1,0}, 00068 { 0,1,0,1,0,1,0} 00069 }, 00070 { 00071 { 0,1,1,1,1,1,0}, 00072 { 0,0,1,1,1,0,0}, 00073 { 0,1,1,0,0,0,0}, 00074 { 0,0,1,1,1,0,0}, 00075 { 0,1,1,1,1,1,0} 00076 }, 00077 { 00078 { 0,1,1,1,1,1,0}, 00079 { 0,0,1,1,1,0,0}, 00080 { 0,0,0,0,1,1,0}, 00081 { 0,0,1,1,1,0,0}, 00082 { 0,1,1,1,1,1,0} 00083 } 00084 },//tank1 00085 { 00086 { 00087 { 0,1,0,1,0,1,0}, 00088 { 0,1,0,1,0,1,0}, 00089 { 0,1,1,1,1,1,0}, 00090 { 0,1,1,0,0,1,0}, 00091 { 0,1,0,0,0,1,0} 00092 00093 }, 00094 { 00095 { 0,1,0,0,0,1,0}, 00096 { 0,1,0,0,1,1,0}, 00097 { 0,1,1,1,1,1,0}, 00098 { 0,1,0,1,0,1,0}, 00099 { 0,1,0,1,0,1,0} 00100 }, 00101 { 00102 { 0,1,1,1,1,1,0}, 00103 { 0,0,0,1,0,0,0}, 00104 { 0,1,1,1,0,0,0}, 00105 { 0,0,0,1,1,0,0}, 00106 { 0,1,1,1,1,1,0} 00107 }, 00108 { 00109 { 0,1,1,1,1,1,0}, 00110 { 0,0,1,1,0,0,0}, 00111 { 0,0,0,1,1,1,0}, 00112 { 0,0,0,1,0,0,0}, 00113 { 0,1,1,1,1,1,0} 00114 } 00115 },//tank2 00116 { 00117 { 00118 { 0,1,0,1,0,1,0}, 00119 { 0,1,0,1,0,1,0}, 00120 { 0,1,1,1,1,1,0}, 00121 { 0,1,0,1,0,1,0}, 00122 { 0,1,0,0,0,1,0} 00123 00124 }, 00125 { 00126 { 0,1,0,0,0,1,0}, 00127 { 0,1,0,1,0,1,0}, 00128 { 0,1,1,1,1,1,0}, 00129 { 0,1,0,1,0,1,0}, 00130 { 0,1,0,1,0,1,0} 00131 }, 00132 { 00133 { 0,1,1,1,1,1,0}, 00134 { 0,0,0,1,0,0,0}, 00135 { 0,1,1,1,1,0,0}, 00136 { 0,0,0,1,0,0,0}, 00137 { 0,1,1,1,1,1,0} 00138 }, 00139 { 00140 { 0,1,1,1,1,1,0}, 00141 { 0,0,0,1,0,0,0}, 00142 { 0,0,1,1,1,1,0}, 00143 { 0,0,0,1,0,0,0}, 00144 { 0,1,1,1,1,1,0} 00145 } 00146 }, //tank3 00147 { 00148 { 00149 { 0,1,0,1,0,1,0}, 00150 { 0,1,0,1,0,1,0}, 00151 { 0,1,1,1,1,1,0}, 00152 { 0,1,0,0,1,1,0}, 00153 { 0,1,0,0,0,1,0} 00154 00155 }, 00156 { 00157 { 0,1,0,0,0,1,0}, 00158 { 0,1,1,0,0,1,0}, 00159 { 0,1,1,1,1,1,0}, 00160 { 0,1,0,1,0,1,0}, 00161 { 0,1,0,1,0,1,0} 00162 }, 00163 { 00164 { 0,1,1,1,1,1,0}, 00165 { 0,0,0,1,1,0,0}, 00166 { 0,1,1,1,0,0,0}, 00167 { 0,0,0,1,0,0,0}, 00168 { 0,1,1,1,1,1,0} 00169 }, 00170 { 00171 { 0,1,1,1,1,1,0}, 00172 { 0,0,0,1,0,0,0}, 00173 { 0,0,0,1,1,1,0}, 00174 { 0,0,1,1,0,0,0}, 00175 { 0,1,1,1,1,1,0} 00176 } 00177 }//tank4 00178 }; 00179 const int tank_exp[5][7] = { 00180 { 0,1,0,1,0,1,0}, 00181 { 0,0,1,0,1,0,0}, 00182 { 1,1,0,0,0,1,1}, 00183 { 0,0,1,0,1,0,0}, 00184 { 0,1,0,1,0,1,0}, 00185 };//击毁的坦克 00186 const int bullet[5][7] = { 00187 { 0,0,0,0,0,0,0}, 00188 { 0,0,1,1,0,0,0}, 00189 { 0,0,1,1,0,0,0}, 00190 { 0,0,0,0,0,0,0}, 00191 { 0,0,0,0,0,0,0}, 00192 };//炮弹 00193 const int hp_ai[5][7] = { 00194 { 0,1,1,0,1,1,0}, 00195 { 1,1,1,1,1,1,1}, 00196 { 0,1,1,1,1,1,0}, 00197 { 1,0,1,1,1,0,1}, 00198 { 1,1,0,1,0,1,1}, 00199 };//炮弹 00200 const int t_start[tank_num][2] = {{0,5},{6,3},{6,7},{8,5}}; //坦克初始坐标 00201 void map_scan3(int map_adj[9][11]) //扫描地图函数 00202 { 00203 int x = 0; 00204 int y = 0; //地图扫描坐标 00205 lcd3.drawRect(0,0,84,1,FILL_BLACK); 00206 lcd3.drawRect(0,46,84,2,FILL_BLACK); 00207 lcd3.drawRect(0,0,3,48,FILL_BLACK); 00208 lcd3.drawRect(80,0,4,48,FILL_BLACK);//边框 00209 00210 for (x=0; x<11; x++) { 00211 for (y=0; y<9; y++) { 00212 if(map_adj[y][x]==1) { 00213 lcd3.drawSprite(x*7+3,y*5+1,5,7,(int *)wall); 00214 } else if(map_adj[y][x]==2) { 00215 lcd3.drawSprite(x*7+3,y*5+1,5,7,(int *)bush); 00216 } 00217 } 00218 } 00219 } 00220 00221 void hp_show3(int hp[4]) //显示血量函数 00222 { 00223 int i; 00224 for(i=1; i<tank_num; i++) { 00225 if(hp[i]==1) { 00226 lcd3.drawSprite(4*7+(i-1)*7+3,7*5+1,5,7,(int *)hp_ai); 00227 } 00228 00229 } 00230 } 00231 void tank_show3(int tank_state[4],int t[4][2],int hp[4]) //显示坦克函数 00232 { 00233 int i; 00234 for(i=0; i<tank_num; i++) { 00235 if(hp[i]!=0) { 00236 lcd3.drawSprite(t[i][1]*7+3,t[i][0]*5+1,5,7,(int *)tank[i][tank_state[i]]); 00237 } 00238 } 00239 } 00240 void main3() 00241 { 00242 // need to initialise the lcd3 first, do this once outside of the loop 00243 lcd3.init(); 00244 //int command[2][4]={{0,0,0,0},{0,0,0,0}};//处理后的指令 00245 int command[4]= {0,0,0,0}; 00246 int i; 00247 int j; //地图复制计数 00248 int ii; 00249 int k; 00250 int l; 00251 int n; //炮弹刷新计数 00252 int shoot;//开火计数 00253 int t[tank_num][2]; //坦克坐标 00254 int b[3][2]; //炮弹坐标 00255 int bullet_cont=0;//发射炮弹计数 00256 int bullet_direct[3];//炮弹的方向 00257 int bullet_val[3];//炮弹是否被阻挡 00258 int tank_state[tank_num]; //坦克的f方向 00259 int hp_set[tank_num]= {3,1,1,1}; //坦克血量预设 00260 int hp[tank_num]= {0,0,0,0}; //坦克血量 00261 int map_adj[9][11]; 00262 int tank_des;//serial number of destoried tank destroy 00263 int obj_kind; 00264 for (k=0; k<tank_num; k++) { 00265 hp[k]=hp_set[k]; 00266 tank_state[k]=0; 00267 map_adj[t_start[k][0]][t_start[k][1]]=k+3; 00268 for (l=0; l<bullet_v; l++) { 00269 bullet_val[l]=0; 00270 } 00271 for (ii=0; ii<2; ii++) { 00272 t[k][ii]=t_start[k][ii]; 00273 } //复制坦克坐标 00274 } 00275 00276 for (i=0; i<11; i++) { 00277 for (j=0; j<9; j++) { 00278 map_adj[j][i]=map[j][i]; 00279 } 00280 }//复制地图 00281 shoot=0; 00282 while(1) { 00283 lcd3.clear(); 00284 map_scan3(map_adj);//地图显示 00285 hp_show3(hp);//血量显示 00286 tank_show3(tank_state,t,hp);//坦克显示 00287 00288 if(shoot==0) { 00289 command[3] = button_A3.read(); 00290 command[0] = button_B3.read(); 00291 command[2] = button_C3.read(); 00292 command[1] = button_D3.read(); //复制指令 00293 bullet_cont++; 00294 if(bullet_cont==3) { 00295 bullet_cont=0; 00296 } 00297 00298 00299 command[3]=(~command[0])&(~command[1])&(~command[2])&command[3]; 00300 command[2]=(~command[0])&(~command[1])&command[2]; 00301 command[1]=(~command[0])&command[1]; 00302 command[0]=command[0]; 00303 00304 map_adj[t[0][0]][t[0][1]]=0; 00305 t[0][0]=t[0][0]-command[0]+command[1]; 00306 t[0][1]=t[0][1]-command[2]+command[3];//指令控制坦克 00307 00308 if(map_adj[t[0][0]][t[0][1]]!=0||t[0][1]<0||t[0][1]>10||t[0][0]<0||t[0][0]>8) { 00309 t[0][0]=t[0][0]+command[0]-command[1]; 00310 t[0][1]=t[0][1]+command[2]-command[3]; 00311 }//障碍物判断,前方有障碍物时指令无效 00312 map_adj[t[0][0]][t[0][1]]=3; 00313 if(command[0]||command[1]||command[2]||command[3]) { 00314 tank_state[0]=command[1]*1+command[2]*2+command[3]*3; 00315 } //坦克方向计算 00316 if (joy_button3.read()==0) { 00317 bullet_val[bullet_cont]=1; //坦克发射炮弹技炮弹坐标方向计算 00318 bullet_direct[bullet_cont]=tank_state[0]; 00319 b[bullet_cont][0]=t[0][0]; 00320 b[bullet_cont][1]=t[0][1]; 00321 } 00322 } 00323 00324 for(n=0; n<bullet_v; n++) { //炮弹比坦克快三倍运算也快三倍 00325 b[n][0]=b[n][0]+(!(bullet_direct[n]>>1))*(-1+2*(bullet_direct[n]%2)); 00326 b[n][1]=b[n][1]+(bullet_direct[n]>>1)*(-1+2*(bullet_direct[n]%2));//炮弹坐标刷新 00327 if(b[n][1]>=0&&b[n][1]<=10&&b[n][0]>=0&&b[n][0]<=8&&bullet_val[n]==1) { 00328 //炮弹有效且未出界 00329 obj_kind=map_adj[b[n][0]][b[n][1]]; 00330 if(obj_kind==0) { //炮弹无障碍飞行的情况 00331 lcd3.drawSprite(b[n][1]*7+3,b[n][0]*5+1,5,7,(int *)bullet); 00332 } else if (obj_kind==2) { //炮弹打到灌木的情况 00333 lcd3.drawSprite(b[n][1]*7+3,b[n][0]*5+1,5,7,(int *)destroyed_bush); 00334 map_adj[b[n][0]][b[n][1]]=0; 00335 bullet_val[n]=0; 00336 } else if (obj_kind==1) { //炮弹打墙的情况 00337 bullet_val[n]=0; 00338 } else if(obj_kind>3) { //炮弹击杀坦克的情况 00339 lcd3.printString("1", 0, 0); 00340 tank_des=map_adj[b[n][0]][b[n][1]]-3; 00341 map_adj[b[n][0]][b[n][1]]=0; 00342 lcd3.drawSprite(t[tank_des][1]*7+3,t[tank_des][0]*5+1,5,7,(int *)tank_exp); 00343 hp[tank_des]=hp[tank_des]-1;//血量减一 00344 bullet_val[n]=0;//炮弹失效 00345 if((hp[1]+hp[2]+hp[3])==0) { 00346 lcd3.clear(); 00347 lcd3.printString("Exercise", 20, 1); 00348 lcd3.printString("Completed", 18, 3); 00349 lcd3.refresh(); 00350 wait(1); 00351 while(1) { 00352 if (button_C3.read() == 1) { 00353 main(); 00354 } 00355 } 00356 } 00357 } 00358 } 00359 } 00360 shoot=shoot+1; 00361 if(shoot==bullet_v) { 00362 shoot=0; 00363 } 00364 lcd3.refresh(); // refresh the lcd3 so the pixels appear 00365 wait_ms(600/bullet_v); // 画面按炮弹移动s时间刷新,600为坦克移动时间 00366 } 00367 } 00368 void init_buttons3() 00369 { 00370 // PCB has external pull-down resistors so turn the internal ones off 00371 // (default for DigitalIn) 00372 button_A3.mode(PullNone); 00373 button_B3.mode(PullNone); 00374 button_C3.mode(PullNone); 00375 button_D3.mode(PullNone); 00376 }
Generated on Wed Jul 27 2022 08:00:53 by
