anxuanyu
Embed:
(wiki syntax)
Show/hide line numbers
main2.cpp
00001 #include "mbed.h" 00002 #include "N5110.h" 00003 #include "main2.h" 00004 #include "main.h" 00005 00006 DigitalIn button_A(p29); 00007 DigitalIn button_B(p28); 00008 DigitalIn button_C(p27); 00009 DigitalIn button_D(p26); 00010 AnalogIn joy_v(p20); 00011 AnalogIn joy_h(p19); 00012 AnalogIn pot0(p20); 00013 AnalogIn joy_button(p17); // could be DigitalIn, but use AnalogIn so pot can also be used 00014 Ticker ticker; 00015 // JP1 must be in 2/3 position 00016 N5110 lcd(p8,p9,p10,p11,p13,p21); 00017 00018 const int map[9][11] = { 00019 { 0,0,0,0,0,0,0,0,0,0,0}, 00020 { 0,1,0,2,1,1,1,2,0,1,0}, 00021 { 0,1,0,2,0,1,0,2,0,1,0}, 00022 { 0,0,0,0,0,0,0,0,0,0,0}, 00023 { 2,2,0,1,2,2,2,1,0,2,2}, 00024 { 0,0,0,0,0,0,0,0,0,0,0}, 00025 { 0,1,0,2,0,1,0,2,0,1,0}, 00026 { 0,1,0,2,1,1,1,2,0,1,0}, 00027 { 0,0,0,0,0,0,0,0,0,0,0}, 00028 };//9*11⼤⼩的地图,1是墙,2是灌⽊ 00029 const int wall[5][7] = { 00030 { 1,1,1,1,1,1,1}, 00031 { 1,1,0,0,0,1,1}, 00032 { 1,1,0,0,0,1,1}, 00033 { 1,1,0,0,0,1,1}, 00034 { 1,1,1,1,1,1,1}, 00035 };//5*7的墙,不可破坏 00036 const int bush[5][7] = { 00037 { 1,0,1,0,1,0,1}, 00038 { 0,1,0,1,0,1,0}, 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 };//5*7的g灌⽊,可破坏 00043 const int destroyed_bush[5][7] = { 00044 { 1,0,0,1,0,0,1}, 00045 { 0,0,1,0,0,1,0}, 00046 { 0,1,0,0,1,0,0}, 00047 { 1,0,0,1,0,0,1}, 00048 { 0,0,1,0,0,1,0}, 00049 };//被击中的灌⽊ 00050 const int tank1[4][5][7] = { 00051 { 00052 { 0,1,0,1,0,1,0}, 00053 { 0,1,1,1,1,1,0}, 00054 { 0,1,1,0,1,1,0}, 00055 { 0,1,1,0,1,1,0}, 00056 { 0,1,0,0,0,1,0} 00057 }, 00058 { 00059 { 0,1,0,0,0,1,0}, 00060 { 0,1,1,0,1,1,0}, 00061 { 0,1,1,0,1,1,0}, 00062 { 0,1,1,1,1,1,0}, 00063 { 0,1,0,1,0,1,0} 00064 }, 00065 { 00066 { 0,1,1,1,1,1,0}, 00067 { 0,0,1,1,1,0,0}, 00068 { 0,1,1,0,0,0,0}, 00069 { 0,0,1,1,1,0,0}, 00070 { 0,1,1,1,1,1,0} 00071 }, 00072 { 00073 { 0,1,1,1,1,1,0}, 00074 { 0,0,1,1,1,0,0}, 00075 { 0,0,0,0,1,1,0}, 00076 { 0,0,1,1,1,0,0}, 00077 { 0,1,1,1,1,1,0} 00078 } 00079 }; 00080 //四个⽅向的坦克1 00081 const int tank2[4][5][7] = { 00082 { 00083 { 0,1,0,1,0,1,0}, 00084 { 0,1,0,1,0,1,0}, 00085 { 0,1,1,1,1,1,0}, 00086 { 0,1,0,0,0,1,0}, 00087 { 0,1,0,0,0,1,0} 00088 }, 00089 { 00090 { 0,1,0,0,0,1,0}, 00091 { 0,1,0,0,0,1,0}, 00092 { 0,1,1,1,1,1,0}, 00093 { 0,1,0,1,0,1,0}, 00094 { 0,1,0,1,0,1,0} 00095 }, 00096 { 00097 { 0,1,1,1,1,1,0}, 00098 { 0,0,0,1,0,0,0}, 00099 { 0,1,1,1,0,0,0}, 00100 { 0,0,0,1,0,0,0}, 00101 { 0,1,1,1,1,1,0} 00102 }, 00103 { 00104 { 0,1,1,1,1,1,0}, 00105 { 0,0,0,1,0,0,0}, 00106 { 0,0,0,1,1,1,0}, 00107 { 0,0,0,1,0,0,0}, 00108 { 0,1,1,1,1,1,0} 00109 } 00110 };//四个⽅向的坦克2 00111 const int tank_exp[5][7] = { 00112 { 0,1,0,1,0,1,0}, 00113 { 0,0,1,0,1,0,0}, 00114 { 1,1,0,0,0,1,1}, 00115 { 0,0,1,0,1,0,0}, 00116 { 0,1,0,1,0,1,0}, 00117 };//击毁的坦克 00118 const int heart[5][7] = { 00119 { 0,1,1,0,1,1,0}, 00120 { 1,1,1,1,1,1,1}, 00121 { 0,1,1,1,1,1,0}, 00122 { 1,0,1,1,1,0,1}, 00123 { 1,1,0,1,0,1,1}, 00124 };//⾎量的⼼形 00125 const int bullet[5][7] = { 00126 { 0,0,0,0,0,0,0}, 00127 { 0,0,1,1,0,0,0}, 00128 { 0,0,1,1,0,0,0}, 00129 { 0,0,0,0,0,0,0}, 00130 { 0,0,0,0,0,0,0}, 00131 };//炮弹 00132 float pot_val; 00133 float pot_val_new; 00134 const int bullet_v=3;//炮弹的速度为坦克三倍 00135 const int tanks_num=2;//坦克数量 00136 const int t_start[2][2] = {{0,5},{8,5}}; //坦克初始坐标 00137 void map_scan(int map_adj[9][11]) //扫描地图函数 00138 { 00139 int x = 0; 00140 int y = 0; //地图扫描坐标 00141 lcd.drawRect(0,0,84,1,FILL_BLACK); 00142 lcd.drawRect(0,46,84,2,FILL_BLACK); 00143 lcd.drawRect(0,0,3,48,FILL_BLACK); 00144 lcd.drawRect(80,0,4,48,FILL_BLACK);//边框 00145 for (x=0; x<11; x++) { 00146 for (y=0; y<9; y++) { 00147 if(map_adj[y][x]==1) { 00148 lcd.drawSprite(x*7+3,y*5+1,5,7,(int *)wall); 00149 } else if(map_adj[y][x]==2) { 00150 lcd.drawSprite(x*7+3,y*5+1,5,7,(int *)bush); 00151 } 00152 } 00153 } 00154 } 00155 void hp_show(int hp1,int hp2) //显示⾎量函数 00156 { 00157 int heart_num=0; 00158 for (heart_num=0; heart_num<3; heart_num++) { 00159 if(heart_num<hp1) { 00160 lcd.drawSprite(4*7+heart_num*7+3,5*1+1,5,7,(int *)heart); 00161 } 00162 if(heart_num<hp2) { 00163 lcd.drawSprite(4*7+heart_num*7+3,7*5+1,5,7,(int *)heart); 00164 } 00165 } 00166 } 00167 void tank_show(int tank1_state,int tank2_state,int t1[2],int t2[2]) //显示坦克函数 00168 { 00169 lcd.drawSprite(t1[1]*7+3,t1[0]*5+1,5,7,(int *)tank1[tank1_state]); 00170 lcd.drawSprite(t2[1]*7+3,t2[0]*5+1,5,7,(int *)tank2[tank2_state]); 00171 } 00172 void init_buttons(); 00173 00174 void main2() 00175 { 00176 // need to initialise the lcd first, do this once outside of the loop 00177 lcd.init(); 00178 int command[2][4]= {{0,0,0,0},{0,0,0,0}}; //处理后的指令 00179 int i = 0; 00180 int j = 0; //地图复制计数 00181 int ii = 0; 00182 int ji = 0;//坦克初始坐标复制计数 00183 int m = 0; 00184 int n = 0; //炮弹刷新计数 00185 int shoot = 0;//开⽕计数 00186 int t[tanks_num][2]; //坦克坐标 00187 int b[tanks_num][3][2]; //炮弹坐标 00188 int bullet_cont=0;//发射炮弹计数 00189 int bullet_direct[tanks_num][3];//炮弹的⽅向 00190 int bullet_val[tanks_num][3];//炮弹是否被阻挡 00191 int tank_state[tanks_num]= {0,0}; //坦克的f⽅向 00192 int hp[tanks_num] = {3,3}; //坦克⾎量 00193 int tank_count;//坦克单位的计数 00194 int map_adj[9][11]; 00195 for (i=0; i<11; i++) { 00196 for (j=0; j<9; j++) { 00197 map_adj[j][i]=map[j][i]; 00198 } 00199 }//复制地图 00200 for (ii=0; ii<2; ii++) { 00201 for (ji=0; ji<2; ji++) { 00202 t[ji][ii]=t_start[ji][ii]; 00203 } 00204 } //复制指令 00205 //welcome(); 00206 while(1) { 00207 lcd.clear(); 00208 map_scan(map_adj);//地图显示 00209 hp_show(hp[0],hp[1]);//⾎量显示 00210 tank_show(tank_state[0],tank_state[1],t[0],t[1]);//坦克显示 00211 pot_val_new = pot0.read(); 00212 if(shoot==0) { 00213 command[0][3] = button_A.read(); 00214 command[0][0] = button_B.read(); 00215 command[0][2] = button_C.read(); 00216 command[0][1] = button_D.read(); //复制指令 00217 if(joy_h.read()<0.3) { 00218 command[1][0] = 0; 00219 command[1][1] = 0; 00220 command[1][2] = 0; 00221 command[1][3] = 1; 00222 } else if(joy_h.read()>0.7) { 00223 command[1][0] = 0; 00224 command[1][1] = 0; 00225 command[1][2] = 1; 00226 command[1][3] = 0; 00227 } else if(joy_v.read()<0.3) { 00228 command[1][0] = 0; 00229 command[1][1] = 1; 00230 command[1][2] = 0; 00231 command[1][3] = 0; 00232 } else if(joy_v.read()>0.7) { 00233 command[1][0] = 1; 00234 command[1][1] = 0; 00235 command[1][2] = 0; 00236 command[1][3] = 0; 00237 } else { 00238 command[1][0] = 0; 00239 command[1][1] = 0; 00240 command[1][2] = 0; 00241 command[1][3] = 0; 00242 } 00243 for(m=0; m<tanks_num; m++) { 00244 00245 } 00246 bullet_cont++; 00247 if(bullet_cont==3) { 00248 bullet_cont=0; 00249 } 00250 for(tank_count=0; tank_count<tanks_num; tank_count++) { 00251 command[tank_count][3]=(~command[tank_count][0])&(~command[tank_count][1])&(~command[tank_count][2])&command[tank_count][3]; 00252 command[tank_count][2]=(~command[tank_count][0])&(~command[tank_count][1])&command[tank_count][2]; 00253 command[tank_count][1]=(~command[tank_count][0])&command[tank_count][1]; 00254 command[tank_count][0]=command[tank_count][0]; 00255 00256 00257 00258 t[tank_count][0]=t[tank_count][0]-command[tank_count][0]+command[tank_count][1]; 00259 t[tank_count][1]=t[tank_count][1]-command[tank_count][2]+command[tank_count][3]; 00260 //指令控制坦克 00261 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) { 00262 t[tank_count][0]=t[tank_count][0]+command[tank_count][0]-command[tank_count][1]; 00263 t[tank_count][1]=t[tank_count][1]+command[tank_count][2]-command[tank_count][3]; 00264 }//障碍物判断,前⽅有障碍物时指令⽆效 00265 if(command[tank_count][0]||command[tank_count][1]||command[tank_count][2]||command[tank_count][3]) { 00266 tank_state[tank_count]=command[tank_count][1]*1+command[tank_count][2]*2+command[tank_count][3]*3; 00267 } //坦克⽅向计算 00268 00269 if (joy_button.read()==0 || tank_count ==0 ) { 00270 bullet_direct[tank_count][bullet_cont]=tank_state[tank_count]; 00271 b[tank_count][bullet_cont][0]=t[tank_count][0]; 00272 b[tank_count][bullet_cont][1]=t[tank_count][1]; 00273 //坦克发射炮弹技炮弹坐标⽅向计算 00274 bullet_val[tank_count][bullet_cont]=1; 00275 } 00276 00277 } 00278 } 00279 for(tank_count=0; tank_count<tanks_num; tank_count++) { //游戏⾥坦克依次进⾏运算 00280 for(n=0; n<bullet_v; n++) { //炮弹⽐坦克快三倍运算也快三倍 00281 b[tank_count][n][0]=b[tank_count][n][0]+(!(bullet_direct[tank_count] 00282 [n]>>1))*(-1+2*(bullet_direct[tank_count][n]%2)); 00283 b[tank_count][n][1]=b[tank_count][n][1]+(bullet_direct[tank_count] 00284 [n]>>1)*(-1+2*(bullet_direct[tank_count][n]%2));//炮弹坐标刷新 00285 if(b[tank_count][n][1]>=0&&b[tank_count][n][1]<=10&&b[tank_count][n] 00286 [0]>=0&&b[tank_count][n][0]<=8&&bullet_val[tank_count][n]==1) { 00287 //炮弹有效且未出界 00288 if(b[tank_count][n][1]==t[!tank_count][1]&&b[tank_count][n][0]==t[!tank_count][0]) { //炮弹击杀坦克的情况 00289 lcd.drawSprite(t[!tank_count][1]*7+3,t[!tank_count][0]*5+1,5,7,(int *)tank_exp); 00290 t[!tank_count][0]=t_start[!tank_count][0]; 00291 t[!tank_count][1]=t_start[!tank_count][1];//坦克回出⽣点 00292 hp[!tank_count]=hp[!tank_count]-1;//⾎量减⼀ 00293 if (hp[!tank_count]==0) { 00294 lcd.clear(); 00295 lcd.printString("Game Over", 15, 0); 00296 lcd.printString("Winner:", 20, 2); 00297 if(tank_count==0) { 00298 lcd.printString("Left player win", 10, 4); 00299 } else { 00300 lcd.printString("Right player win", 7, 4); 00301 } 00302 lcd.refresh(); 00303 wait(1); 00304 while(1) { 00305 if (button_B.read() == 1) { 00306 main(); 00307 } 00308 } 00309 } 00310 bullet_val[tank_count][n]=0;//炮弹失效 00311 } else if(map_adj[b[tank_count][n][0]][b[tank_count][n][1]]==0) { //炮弹⽆障碍⻜⾏的情况 00312 lcd.drawSprite(b[tank_count][n][1]*7+3,b[tank_count][n][0]*5+1,5,7,(int *)bullet); 00313 } else if (map_adj[b[tank_count][n][0]][b[tank_count][n][1]]==2) { //炮弹打到灌⽊的情况 00314 lcd.drawSprite(b[tank_count][n][1]*7+3,b[tank_count][n][0]*5+1,5,7,(int 00315 *)destroyed_bush); 00316 map_adj[b[tank_count][n][0]][b[tank_count][n][1]]=0; 00317 bullet_val[tank_count][n]=0; 00318 } else if (map_adj[b[tank_count][n][0]][b[tank_count][n][1]]==1) { //炮弹打墙的情况 00319 bullet_val[tank_count][n]=0; 00320 } 00321 } 00322 } 00323 } 00324 shoot=shoot+1; 00325 if(shoot==bullet_v) { 00326 shoot=0; 00327 } 00328 lcd.refresh(); // refresh the LCD so the pixels appear 00329 wait_ms(600/bullet_v); // 画⾯按炮弹移动时间刷新,600为坦克移动时间 00330 } 00331 } 00332 void init_buttons() 00333 { 00334 // PCB has external pull-down resistors so turn the internal ones off 00335 // (default for DigitalIn) 00336 button_A.mode(PullNone); 00337 button_B.mode(PullNone); 00338 button_C.mode(PullNone); 00339 button_D.mode(PullNone); 00340 }
Generated on Wed Jul 27 2022 08:00:53 by
1.7.2