xuanyu an / Mbed 2 deprecated tankworld

Dependencies:   mbed N5110

main1.cpp

Committer:
an_xuanyu
Date:
2021-05-04
Revision:
1:47967167a3a6
Parent:
0:a9ef8a5c769a
Child:
2:ce24c50fc0a6

File content as of revision 1:47967167a3a6:

#include "mbed.h"
#include "N5110.h"
#define bullet_v 3//炮弹的速度为坦克三倍
#define tank_num 4//坦克数量
DigitalIn button_A(p29);
DigitalIn button_B(p28);
DigitalIn button_C(p27);
DigitalIn button_D(p26);

AnalogIn  joy_v(p20);
AnalogIn  joy_h(p19);
AnalogIn joy_button(p17); // could be DigitalIn, but use AnalogIn so pot can also be used

// JP1 must be in 2/3 position
N5110 lcd(p8,p9,p10,p11,p13,p21);
const int command[15][4]= {
    { 0,0,0,1},
    { 0,0,0,1},
    { 0,0,0,1},
    { 0,0,0,1},
    { 0,1,0,0},
    { 0,1,0,0},
    { 0,1,0,0},
    { 0,0,0,1},
    { 0,0,0,1},
    { 1,0,0,0},
    { 1,0,0,0},
    { 1,0,0,0},
    { 0,0,1,0},
    { 0,0,1,0},
    { 0,0,1,0},
};//模拟输入的指令,一共十组,每个指令数组分别对应上,下,左,右;上方坦克的指令
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},
    { 0,1,0,2,0,1,0,2,0,1,0},
    { 0,0,0,0,0,0,0,0,0,0,0},
    { 2,2,0,1,2,2,2,1,0,2,2},
    { 0,0,0,0,0,0,0,0,0,0,0},
    { 0,1,0,2,0,1,0,2,0,1,0},
    { 0,1,0,2,1,1,1,2,0,1,0},
    { 0,0,0,0,0,0,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},
    { 1,0,0,1,0,0,1},
    { 0,0,1,0,0,1,0},

};//被击中的灌木
const int tank[tank_num][4][5][7] = {
    {
        {
            { 0,1,0,1,0,1,0},
            { 0,1,1,1,1,1,0},
            { 0,1,1,0,1,1,0},
            { 0,1,1,0,1,1,0},
            { 0,1,0,0,0,1,0}
        },
        {
            { 0,1,0,0,0,1,0},
            { 0,1,1,0,1,1,0},
            { 0,1,1,0,1,1,0},
            { 0,1,1,1,1,1,0},
            { 0,1,0,1,0,1,0}
        },
        {
            { 0,1,1,1,1,1,0},
            { 0,0,1,1,1,0,0},
            { 0,1,1,0,0,0,0},
            { 0,0,1,1,1,0,0},
            { 0,1,1,1,1,1,0}
        },
        {
            { 0,1,1,1,1,1,0},
            { 0,0,1,1,1,0,0},
            { 0,0,0,0,1,1,0},
            { 0,0,1,1,1,0,0},
            { 0,1,1,1,1,1,0}
        }
    },//tank1
    {
        {
            { 0,1,0,1,0,1,0},
            { 0,1,0,1,0,1,0},
            { 0,1,1,1,1,1,0},
            { 0,1,1,0,0,1,0},
            { 0,1,0,0,0,1,0}

        },
        {
            { 0,1,0,0,0,1,0},
            { 0,1,0,0,1,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,0,0,0},
            { 0,1,1,1,0,0,0},
            { 0,0,0,1,1,0,0},
            { 0,1,1,1,1,1,0}
        },
        {
            { 0,1,1,1,1,1,0},
            { 0,0,1,1,0,0,0},
            { 0,0,0,1,1,1,0},
            { 0,0,0,1,0,0,0},
            { 0,1,1,1,1,1,0}
        }
    },//tank2
    {
        {
            { 0,1,0,1,0,1,0},
            { 0,1,0,1,0,1,0},
            { 0,1,1,1,1,1,0},
            { 0,1,0,1,0,1,0},
            { 0,1,0,0,0,1,0}

        },
        {
            { 0,1,0,0,0,1,0},
            { 0,1,0,1,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,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 heart[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 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 t_start[tank_num][2] = {{0,5},{8,1},{8,5},{8,9}}; //坦克初始坐标
const int ai_mod[3]= {0,1,0};
const int ai_changetime[3]= {6,8,5};
void map_scan(int map_adj[9][11]) //扫描地图函数
{
    int x = 0;
    int y = 0; //地图扫描坐标
    lcd.drawRect(0,0,84,1,FILL_BLACK);
    lcd.drawRect(0,46,84,2,FILL_BLACK);
    lcd.drawRect(0,0,3,48,FILL_BLACK);
    lcd.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) {
                lcd.drawSprite(x*7+3,y*5+1,5,7,(int *)wall);
            } else if(map_adj[y][x]==2) {
                lcd.drawSprite(x*7+3,y*5+1,5,7,(int *)bush);
            }
        }
    }
}

void hp_show(int hp1,int hp2) //显示血量函数
{
    int heart_num=0;
    for  (heart_num=0; heart_num<3; heart_num++) {
        if(heart_num<hp1) {
            lcd.drawSprite(4*7+heart_num*7+3,5*1+1,5,7,(int *)heart);
        }
        if(heart_num<hp2) {
            lcd.drawSprite(4*7+heart_num*7+3,7*5+1,5,7,(int *)heart);
        }
    }
}

void tank_show(int tank_state[4],int t[4][2]) //显示坦克函数
{
    int i;
    for(i=0; i<tank_num; i++) {
        lcd.drawSprite(t[i][1]*7+3,t[i][0]*5+1,5,7,(int *)tank[i][tank_state[i]]);

    }
}
int main()
{
    // need to initialise the lcd first, do this once outside of the loop
    lcd.init();
    //int command[2][4]={{0,0,0,0},{0,0,0,0}};//处理后的指令
    int command[4]= {0,0,0,0};
    int turn=0;
    int a;
    int stop;//stop command for ai
    int i;
    int j; //地图复制计数
    int ii;
    int k;
    int l;
    int n; //炮弹刷新计数
    int shoot;//开火计数
    int t[tank_num][2]; //坦克坐标
    int t_pre[tank_num][2]; //坦克预测坐标(npc)
    int b[tank_num][3][2]; //炮弹坐标
    int bullet_cont=0;//发射炮弹计数
    int bullet_direct[tank_num][3];//炮弹的方向
    int bullet_val[tank_num][3];//炮弹是否被阻挡
    int tank_state[tank_num]; //坦克的f方向
    int hp_set[tank_num]= {3,3,3,3}; //坦克血量预设
    int hp[tank_num]; //坦克血量
    int tank_count;//坦克单位的计数
    int trace[tank_num-1][3]= {{0,0,0},{0,0,0},{0,0,0}};
    int map_adj[9][11];
    int tank_des;//serial number of destoried tank destroy
    int hit;
    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[k][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];
        }
    }//复制地图

    while(1) {
        lcd.clear();
        map_scan(map_adj);//地图显示
        hp_show(hp[0],hp[1]);//血量显示
        tank_show(tank_state,t);//坦克显示
        if(shoot==0) {
            command[3] = button_A.read();
            command[0] = button_B.read();
            command[2] = button_C.read();
            command[1] = button_D.read(); //复制指令
            turn=turn+1;
            if(turn==15) {
                turn=0;
            }//各种计数
            bullet_cont++;
            if(bullet_cont==3) {
                bullet_cont=0;
            }

            for(tank_count=0; tank_count<tank_num; tank_count++) {
                if(tank_count!=0) {
                    stop=0;
                    t_pre[tank_count][0]=t[tank_count][0];
                    t_pre[tank_count][1]=t[tank_count][1];
                    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的下方
                    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的右
                    trace[tank_count][2]=(abs(t[0][0]-t[tank_count][0]))>(abs(t[0][1]-t[tank_count][1]));//1代表坦克竖直距离大于水平距离
                    for(a=0; a<4; a++) {
                        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]));
                        t_pre[tank_count][0]=t[tank_count][0]+(!(tank_state[tank_count]>>1))*(2*(tank_state[tank_count]%2)-1);
                        t_pre[tank_count][1]=t[tank_count][1]+(tank_state[tank_count]>>1)*(2*(tank_state[tank_count]%2)-1);
                        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)) {
                            break;
                        }
                        if(a==3) stop=1;
                    }
                    map_adj[t[tank_count][0]][t[tank_count][1]]=0;
                    if(stop==0) {
                        t[tank_count][tank_state[tank_count]>>1]=t[tank_count][tank_state[tank_count]>>1]+2*(tank_state[tank_count]%2)-1;
                    }
                    map_adj[t[tank_count][0]][t[tank_count][1]]=tank_count+3;
                    //bullet_val[tank_count][bullet_cont]=1; //坦克发射炮弹技炮弹坐标方向计算
                } else {
                    map_adj[t[tank_count][0]][t[tank_count][1]]=0;
                    t[tank_count][0]=t[tank_count][0]-command[0]+command[1];
                    t[tank_count][1]=t[tank_count][1]-command[2]+command[3];//指令控制坦克

                    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) {
                        t[tank_count][0]=t[tank_count][0]+command[0]-command[1];
                        t[tank_count][1]=t[tank_count][1]+command[2]-command[3];
                    }//障碍物判断,前方有障碍物时指令无效
                    map_adj[t[tank_count][0]][t[tank_count][1]]=tank_count+3;
                    if(command[0]||command[1]||command[2]||command[3]) {
                        tank_state[tank_count]=command[1]*1+command[2]*2+command[3]*3;
                    } //坦克方向计算
                    
                }
                
                bullet_direct[tank_count][bullet_cont]=tank_state[tank_count];
                if (joy_button.read()==0&&tank_count==1) {
                        bullet_val[tank_count][bullet_cont]=1; //坦克发射炮弹技炮弹坐标方向计算
                    }
                //pot_val = pot0.read();
                b[tank_count][bullet_cont][0]=t[tank_count][0];
                b[tank_count][bullet_cont][1]=t[tank_count][1];
            }

        }
        for(tank_count=0; tank_count<tank_num; tank_count++) { //游戏里坦克依次进行运算
            for(n=0; n<bullet_v; n++) { //炮弹比坦克快三倍运算也快三倍
                b[tank_count][n][0]=b[tank_count][n][0]+(!(bullet_direct[tank_count][n]>>1))*(-1+2*(bullet_direct[tank_count][n]%2));
                b[tank_count][n][1]=b[tank_count][n][1]+(bullet_direct[tank_count][n]>>1)*(-1+2*(bullet_direct[tank_count][n]%2));//炮弹坐标刷新
                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) {
                    //炮弹有效且未出界
                    obj_kind=map_adj[b[tank_count][n][0]][b[tank_count][n][1]];
                    hit=(((tank_count<4)+(obj_kind<4))%2)&&(obj_kind>=3);
                    if(obj_kind==0) { //炮弹无障碍飞行的情况
                        lcd.drawSprite(b[tank_count][n][1]*7+3,b[tank_count][n][0]*5+1,5,7,(int *)bullet);
                    } else if (obj_kind==2) { //炮弹打到灌木的情况
                        lcd.drawSprite(b[tank_count][n][1]*7+3,b[tank_count][n][0]*5+1,5,7,(int *)destroyed_bush);
                        map_adj[b[tank_count][n][0]][b[tank_count][n][1]]=0;
                        bullet_val[tank_count][n]=0;
                    } else if (obj_kind==1) { //炮弹打墙的情况
                        bullet_val[tank_count][n]=0;
                    } /*else if(obj_kind==3) { //炮弹击杀坦克的情况
                        tank_des=map_adj[b[tank_count][n][0]][b[tank_count][n][1]]-3;
                        map_adj[b[tank_count][n][0]][b[tank_count][n][1]]=0;
                        lcd.drawSprite(t[tank_des][1]*7+3,t[tank_des][0]*5+1,5,7,(int *)tank_exp);
                        t[tank_des][0]=t_start[tank_des][0];
                        t[tank_des][1]=t_start[tank_des][1];//坦克回出生点
                        hp[tank_des]=hp[tank_des]-1;//血量减一
                        bullet_val[tank_count][n]=0;//炮弹失效
                    }*/
                }
            }
        }
        shoot=shoot+1;
        if(shoot==bullet_v) {
            shoot=0;
        }
        lcd.refresh(); // refresh the LCD so the pixels appear
        wait_ms(600/bullet_v); // 画面按炮弹移动s时间刷新,600为坦克移动时间
    }
}
void init_buttons()
{
    // PCB has external pull-down resistors so turn the internal ones off
    // (default for DigitalIn)
    button_A.mode(PullNone);
    button_B.mode(PullNone);
    button_C.mode(PullNone);
    button_D.mode(PullNone);
}