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
- Committer:
- YPA_YPA
- Date:
- 2021-05-04
- Revision:
- 2:ce24c50fc0a6
- Parent:
- 0:a9ef8a5c769a
File content as of revision 2:ce24c50fc0a6:
#include "mbed.h"
#include "N5110.h"
#include "main.h"
#ifndef bullet_v
#define bullet_v 3//炮弹的速度为坦克三倍
#endif
#ifndef tank_num
#define tank_num 4//坦克数量
#endif
DigitalIn button_A3(p29);
DigitalIn button_B3(p28);
DigitalIn button_C3(p27);
DigitalIn button_D3(p26);
AnalogIn joy_button3(p17); // could be DigitalIn, but use AnalogIn so pot can also be used
// JP1 must be in 2/3 position
N5110 lcd3(p8,p9,p10,p11,p13,p21);
const int map[9][11] = {
{ 0,0,0,0,0,0,0,0,0,0,0},
{ 0,1,0,2,1,1,1,2,0,1,0},
{ 2,1,2,2,2,2,2,2,2,1,2},
{ 0,0,0,0,0,0,0,0,0,0,0},
{ 2,2,2,1,2,2,2,1,2,2,2},
{ 0,0,1,1,1,1,1,1,1,0,0},
{ 0,1,0,4,0,1,0,5,0,1,0},
{ 0,1,2,2,1,1,1,2,2,1,0},
{ 0,0,0,0,0,6,0,0,0,0,0},
};//9*11大小的地图,1是墙,2是灌木
const int wall[5][7] = {
{ 1,1,1,1,1,1,1},
{ 1,1,0,0,0,1,1},
{ 1,1,0,0,0,1,1},
{ 1,1,0,0,0,1,1},
{ 1,1,1,1,1,1,1},
};//5*7的墙,不可破坏
const int bush[5][7] = {
{ 1,0,1,0,1,0,1},
{ 0,1,0,1,0,1,0},
{ 1,0,1,0,1,0,1},
{ 0,1,0,1,0,1,0},
{ 1,0,1,0,1,0,1},
};//5*7的g灌木,可破坏
const int destroyed_bush[5][7] = {
{ 1,0,0,1,0,0,1},
{ 0,0,1,0,0,1,0},
{ 0,1,0,0,1,0,0},
{ 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 0,1,1,1,1,1,0},
{ 0,0,0,1,0,0,0},
{ 0,1,1,1,1,0,0},
{ 0,0,0,1,0,0,0},
{ 0,1,1,1,1,1,0}
},
{
{ 0,1,1,1,1,1,0},
{ 0,0,0,1,0,0,0},
{ 0,0,1,1,1,1,0},
{ 0,0,0,1,0,0,0},
{ 0,1,1,1,1,1,0}
}
}, //tank3
{
{
{ 0,1,0,1,0,1,0},
{ 0,1,0,1,0,1,0},
{ 0,1,1,1,1,1,0},
{ 0,1,0,0,1,1,0},
{ 0,1,0,0,0,1,0}
},
{
{ 0,1,0,0,0,1,0},
{ 0,1,1,0,0,1,0},
{ 0,1,1,1,1,1,0},
{ 0,1,0,1,0,1,0},
{ 0,1,0,1,0,1,0}
},
{
{ 0,1,1,1,1,1,0},
{ 0,0,0,1,1,0,0},
{ 0,1,1,1,0,0,0},
{ 0,0,0,1,0,0,0},
{ 0,1,1,1,1,1,0}
},
{
{ 0,1,1,1,1,1,0},
{ 0,0,0,1,0,0,0},
{ 0,0,0,1,1,1,0},
{ 0,0,1,1,0,0,0},
{ 0,1,1,1,1,1,0}
}
}//tank4
};
const int tank_exp[5][7] = {
{ 0,1,0,1,0,1,0},
{ 0,0,1,0,1,0,0},
{ 1,1,0,0,0,1,1},
{ 0,0,1,0,1,0,0},
{ 0,1,0,1,0,1,0},
};//击毁的坦克
const int bullet[5][7] = {
{ 0,0,0,0,0,0,0},
{ 0,0,1,1,0,0,0},
{ 0,0,1,1,0,0,0},
{ 0,0,0,0,0,0,0},
{ 0,0,0,0,0,0,0},
};//炮弹
const int hp_ai[5][7] = {
{ 0,1,1,0,1,1,0},
{ 1,1,1,1,1,1,1},
{ 0,1,1,1,1,1,0},
{ 1,0,1,1,1,0,1},
{ 1,1,0,1,0,1,1},
};//炮弹
const int t_start[tank_num][2] = {{0,5},{6,3},{6,7},{8,5}}; //坦克初始坐标
void map_scan3(int map_adj[9][11]) //扫描地图函数
{
int x = 0;
int y = 0; //地图扫描坐标
lcd3.drawRect(0,0,84,1,FILL_BLACK);
lcd3.drawRect(0,46,84,2,FILL_BLACK);
lcd3.drawRect(0,0,3,48,FILL_BLACK);
lcd3.drawRect(80,0,4,48,FILL_BLACK);//边框
for (x=0; x<11; x++) {
for (y=0; y<9; y++) {
if(map_adj[y][x]==1) {
lcd3.drawSprite(x*7+3,y*5+1,5,7,(int *)wall);
} else if(map_adj[y][x]==2) {
lcd3.drawSprite(x*7+3,y*5+1,5,7,(int *)bush);
}
}
}
}
void hp_show3(int hp[4]) //显示血量函数
{
int i;
for(i=1; i<tank_num; i++) {
if(hp[i]==1) {
lcd3.drawSprite(4*7+(i-1)*7+3,7*5+1,5,7,(int *)hp_ai);
}
}
}
void tank_show3(int tank_state[4],int t[4][2],int hp[4]) //显示坦克函数
{
int i;
for(i=0; i<tank_num; i++) {
if(hp[i]!=0) {
lcd3.drawSprite(t[i][1]*7+3,t[i][0]*5+1,5,7,(int *)tank[i][tank_state[i]]);
}
}
}
void main3()
{
// need to initialise the lcd3 first, do this once outside of the loop
lcd3.init();
//int command[2][4]={{0,0,0,0},{0,0,0,0}};//处理后的指令
int command[4]= {0,0,0,0};
int i;
int j; //地图复制计数
int ii;
int k;
int l;
int n; //炮弹刷新计数
int shoot;//开火计数
int t[tank_num][2]; //坦克坐标
int b[3][2]; //炮弹坐标
int bullet_cont=0;//发射炮弹计数
int bullet_direct[3];//炮弹的方向
int bullet_val[3];//炮弹是否被阻挡
int tank_state[tank_num]; //坦克的f方向
int hp_set[tank_num]= {3,1,1,1}; //坦克血量预设
int hp[tank_num]= {0,0,0,0}; //坦克血量
int map_adj[9][11];
int tank_des;//serial number of destoried tank destroy
int obj_kind;
for (k=0; k<tank_num; k++) {
hp[k]=hp_set[k];
tank_state[k]=0;
map_adj[t_start[k][0]][t_start[k][1]]=k+3;
for (l=0; l<bullet_v; l++) {
bullet_val[l]=0;
}
for (ii=0; ii<2; ii++) {
t[k][ii]=t_start[k][ii];
} //复制坦克坐标
}
for (i=0; i<11; i++) {
for (j=0; j<9; j++) {
map_adj[j][i]=map[j][i];
}
}//复制地图
shoot=0;
while(1) {
lcd3.clear();
map_scan3(map_adj);//地图显示
hp_show3(hp);//血量显示
tank_show3(tank_state,t,hp);//坦克显示
if(shoot==0) {
command[3] = button_A3.read();
command[0] = button_B3.read();
command[2] = button_C3.read();
command[1] = button_D3.read(); //复制指令
bullet_cont++;
if(bullet_cont==3) {
bullet_cont=0;
}
command[3]=(~command[0])&(~command[1])&(~command[2])&command[3];
command[2]=(~command[0])&(~command[1])&command[2];
command[1]=(~command[0])&command[1];
command[0]=command[0];
map_adj[t[0][0]][t[0][1]]=0;
t[0][0]=t[0][0]-command[0]+command[1];
t[0][1]=t[0][1]-command[2]+command[3];//指令控制坦克
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) {
t[0][0]=t[0][0]+command[0]-command[1];
t[0][1]=t[0][1]+command[2]-command[3];
}//障碍物判断,前方有障碍物时指令无效
map_adj[t[0][0]][t[0][1]]=3;
if(command[0]||command[1]||command[2]||command[3]) {
tank_state[0]=command[1]*1+command[2]*2+command[3]*3;
} //坦克方向计算
if (joy_button3.read()==0) {
bullet_val[bullet_cont]=1; //坦克发射炮弹技炮弹坐标方向计算
bullet_direct[bullet_cont]=tank_state[0];
b[bullet_cont][0]=t[0][0];
b[bullet_cont][1]=t[0][1];
}
}
for(n=0; n<bullet_v; n++) { //炮弹比坦克快三倍运算也快三倍
b[n][0]=b[n][0]+(!(bullet_direct[n]>>1))*(-1+2*(bullet_direct[n]%2));
b[n][1]=b[n][1]+(bullet_direct[n]>>1)*(-1+2*(bullet_direct[n]%2));//炮弹坐标刷新
if(b[n][1]>=0&&b[n][1]<=10&&b[n][0]>=0&&b[n][0]<=8&&bullet_val[n]==1) {
//炮弹有效且未出界
obj_kind=map_adj[b[n][0]][b[n][1]];
if(obj_kind==0) { //炮弹无障碍飞行的情况
lcd3.drawSprite(b[n][1]*7+3,b[n][0]*5+1,5,7,(int *)bullet);
} else if (obj_kind==2) { //炮弹打到灌木的情况
lcd3.drawSprite(b[n][1]*7+3,b[n][0]*5+1,5,7,(int *)destroyed_bush);
map_adj[b[n][0]][b[n][1]]=0;
bullet_val[n]=0;
} else if (obj_kind==1) { //炮弹打墙的情况
bullet_val[n]=0;
} else if(obj_kind>3) { //炮弹击杀坦克的情况
lcd3.printString("1", 0, 0);
tank_des=map_adj[b[n][0]][b[n][1]]-3;
map_adj[b[n][0]][b[n][1]]=0;
lcd3.drawSprite(t[tank_des][1]*7+3,t[tank_des][0]*5+1,5,7,(int *)tank_exp);
hp[tank_des]=hp[tank_des]-1;//血量减一
bullet_val[n]=0;//炮弹失效
if((hp[1]+hp[2]+hp[3])==0) {
lcd3.clear();
lcd3.printString("Exercise", 20, 1);
lcd3.printString("Completed", 18, 3);
lcd3.refresh();
wait(1);
while(1) {
if (button_C3.read() == 1) {
main();
}
}
}
}
}
}
shoot=shoot+1;
if(shoot==bullet_v) {
shoot=0;
}
lcd3.refresh(); // refresh the lcd3 so the pixels appear
wait_ms(600/bullet_v); // 画面按炮弹移动s时间刷新,600为坦克移动时间
}
}
void init_buttons3()
{
// PCB has external pull-down resistors so turn the internal ones off
// (default for DigitalIn)
button_A3.mode(PullNone);
button_B3.mode(PullNone);
button_C3.mode(PullNone);
button_D3.mode(PullNone);
}