设为首页收藏本站
查看: 2511|回复: 9

C语言游戏代码第三期——坦克大战

[复制链接]
  • TA的每日心情

    2019-5-22 22:04
  • 签到天数: 59 天

    [LV.5]常住居民I

    发表于 2018-12-1 11:35:18 | 显示全部楼层 |阅读模式
    #include<stdio.h>
    #include<easyx.h>
    #include<time.h>
    #include<conio.h>
    #include<graphics.h>
    #define ok 0
    #define eror 1
    #define NUME 15
    #pragma comment ( lib, "Winmm.lib" )
    /*mciSendString( "open ./source/skycity.mp3 alias skycity", 0, 0, 0 );
    mciSendString( "play skycity repeat ", 0, 0, 0 );*/
    enum direction
    {
    left,up,right,down
    };
    enum condition
    {
    normal,addSpeed,addAggressivity,invincible,addOneLife,killAll
    };
    struct property
    {
    condition con;
    int x;
    int y;
    int clock;
    int flag;
    };


    struct ENEMY
    {
    int x;
    int y;
    int life;    //有25 50 75三种生命值坦克
    int aggressivity;   //默认为20
    direction dir;
    };
    struct USER     //速度为3
    {
    int x;
    int y;
    int life;   //默认为50
    int aggressivity;  //默认为25
    direction dir;
    condition speed;  //加子弹速度状态
    condition atack;  //加攻击力状态
    condition defence;  //无敌状态
    int clocks;
    int clocka;
    int clockd;
    };
    struct BOMB   // r=4,circle
    {
    int x;
    int y;
    int aggressivity;
    int speed;          //速度默认为5
    direction dir;
    };
    ENEMY enm[ NUME ];
    USER use1,use2;
    int MODEL=0;  //为0表示单玩家,为1表示双玩家
    int enemyNum=0, use1BNum=0,use2BNum=0;
    BOMB enemyBomb[NUME],use1Bomb,use2Bomb;
    bool FLAGBOMB1=false,FLAGBOMB2=false,flagB[NUME],flagLife[NUME];


    int moveLength=5,BombNormalSpeed=5,BombAddSpeed=5,endOfGame=1;
    int wall[10][3],use1life=3,use2life=3,use1Score=0,use2Score=0,highmark=0;
    property pro[5];


    int controlEnemy();
    int controlUser();
    int drawtank(int i,int j,int life,direction dir);
    int drawBomb();
    int controlBomb();
    int tankMove();
    int initGame();
    int drawHome();
    int is_collision(int x1,int y1,int x2,int y2,direction dir);
    bool isTarget(USER tar,BOMB bomb);
    bool isTarget2(ENEMY tar,BOMB bomb);
    bool is_warded(int x,int y,direction dir,int tank);
    int start_interface();
    int control_property();
    int control_userInfo(int count);
    int init_userinfo();

    void main()
    {
    int i=0,flag=0,temp1,count=0;
    for(i=0;i<NUME;i++)
    {
    flagLife[i]=false;
    flagB[i]=false;
    }
    for(i=0;i<4;i++)
    {
    wall[i][0]=430,wall[i][1]=630-i*20,wall[i][2]=1;
    wall[i+5][0]=520,wall[i+5][1]=630-i*20,wall[i+5][2]=1;
    pro[i].flag=0;
    }
    pro[4].flag=0;
    wall[4][0]=480,wall[4][1]=590,wall[4][2]=1;
    wall[9][0]=480,wall[9][1]=570,wall[9][2]=1;
    for(i=0;i<3;i++)
    {
    temp1=wall[4][i];
    wall[4][i]=wall[8][i];
    wall[8][i]=temp1;
    }
    use1life=3,use2life=3;
    use1.life=0,use2.life=0;
    use1Score=0,use2Score=0,highmark=0;
    moveLength=5,BombNormalSpeed=5,BombAddSpeed=5,endOfGame=1;
    FLAGBOMB1=false,FLAGBOMB2=false;
    enemyNum=0, use1BNum=0,use2BNum=0;
    srand((unsigned)time(0));
    initgraph(1200,650);
    start_interface();

    mciSendString( "open ./source/intimateLover.mp3 alias Lover", 0, 0, 0 );
    mciSendString( "open ./source/begin.mp3 alias begin", 0, 0, 0 );
    mciSendString( "open ./source/bombing.mp3 alias bombing", 0, 0, 0 );
    mciSendString( "open ./source/eatting.mp3 alias eatting", 0, 0, 0 );
    mciSendString( "open ./source/getLife.mp3 alias getLife", 0, 0, 0 );
    mciSendString( "open ./source/launchBomb.mp3 alias launchBomb", 0, 0, 0 );
    mciSendString( "open ./source/lose.mp3 alias lose", 0, 0, 0 );
    mciSendString( "open ./source/moveSelf.mp3 alias moveSelf", 0, 0, 0 );
    mciSendString( "open ./source/background.mp3 alias background", 0, 0, 0 );
    mciSendString( "open ./source/shoot.mp3 alias shoot", 0, 0, 0 );
    mciSendString( "open ./source/bomb.wav alias bomb", 0, 0, 0 );
    mciSendString( "play moveSelf repeat", 0, 0, 0 );
    mciSendString( "pause moveSelf", 0, 0, 0 );
    mciSendString( "play Lover", 0, 0, 0 );

    init_userinfo();
    mciSendString( "play begin ", 0, 0, 0 );
    initGame();
    }
    int initGame()
    {
    IMAGE p;
    int i=0,k=0,m,count=0,countBeginMusic=0;
    loadimage(&p,"./source/home.jpg", 40, 40);
    BeginBatchDraw();
    setfont( 30, 0,"楷体");
    drawtank(use1.x,use1.y,use1.life,use1.dir);
    drawtank(use2.x,use2.y,use2.life,use2.dir);
    while(endOfGame)
    {
    controlEnemy();
    controlUser();
    controlBomb();
    control_property();
    for(i=0;i<NUME;i++)           //判断敌军子弹是否出界
    {
    if(flagB[i]==true)
    if(enemyBomb[i].x<0||enemyBomb[i].y<0||enemyBomb[i].x>1000||enemyBomb[i].y>650)
    {
    flagB[i]=false;
    }
    }
    tankMove();
    drawBomb();
    for(i=0;i<NUME;i++)
    {
    if(flagLife[i]==true)
    {
    drawtank(enm[i].x,enm[i].y,enm[i].life,enm[i].dir);
    }
    }
    if(use1.defence==invincible)
    {
    for(m=use1.x+5;m<=use1.x+45;m+=5)
    {
    circle(m,use1.y,5);
    circle(m,use1.y+50,5);
    }
    for(m=use1.y+5;m<=use1.y+45;m+=5)
    {
    circle(use1.x,m,5);
    circle(use1.x+50,m,5);
    }
    clearrectangle(use1.x,use1.y,use1.x+50,use1.y+50);
    }
    drawtank(use1.x,use1.y,use1.life,use1.dir);
    if(MODEL)
    {
    if(use2.defence==invincible)
    {
    for(m=use2.x+5;m<=use2.x+45;m+=5)
    {
    circle(m,use2.y,5);
    circle(m,use2.y+50,5);
    }
    for(m=use2.y+5;m<=use2.y+45;m+=5)
    {
    circle(use2.x,m,5);
    circle(use2.x+50,m,5);
    }
    clearrectangle(use2.x,use2.y,use2.x+50,use2.y+50);
    }
    drawtank(use2.x,use2.y,use2.life,use2.dir);
    }
    drawHome();
    setfillcolor(GREEN);
       solidrectangle(1000,0,1010,650);
    control_userInfo(count);
    putimage(480, 610, &p, SRCINVERT);
    FlushBatchDraw();
    count++;
    countBeginMusic++;
    if(count==301)
    count=0;
    if(countBeginMusic==200)
    {
    mciSendString( "close begin", 0, 0, 0 );
    mciSendString( "play background repeat", 0, 0, 0 );
    }
    if(countBeginMusic==600)
    countBeginMusic=230;


    Sleep(20);
    mciSendString( "pause moveSelf", 0, 0, 0 );
    clearrectangle(0,0,1000,650);
    }
    Sleep(50000);
    return ok;
    }
    int init_userinfo()
    {
    FILE * fp;
    int score=0;
    if(use1life>0&&use1.life<=0)
    {
    use1.x=380;
    use1.y=600;
    use1.life=50;
    use1.aggressivity=25;
    use1.dir=up;
    use1.speed=normal;
    use1.atack=normal;
    use1.defence=normal;
    }
    else
    {
    if(MODEL==1)
    {
    if(use2life<=0&&use2.life<=0)
    {
    setfont( 30, 0,"楷体");
    outtextxy(350,300,"Game End!!");
    if(use1Score>use2Score)
    {
    score=use1Score;
    }
    else
    {
    score=use2Score;
    }
    if(score>highmark)
    {
    if((fp=fopen("gameMark.txt","w"))==NULL)
    {
    printf("File open eror!\n");
    exit(eror);
    }
    else
    {
    fprintf(fp,"%d",score);
    if(fclose(fp))
    {
    printf("file close eror\n");
    exit(eror);
    }
    }
    }
    }
    }
    else if(use1life<=0&&use1.life<=0)
    {
    setfont( 30, 0,"楷体");
    outtextxy(350,300,"Game End!!");
    if(use1Score>highmark)
    {
    if((fp=fopen("gameMark.txt","w"))==NULL)
    {
    printf("File open eror!\n");
    exit(eror);
    }
    else
    {
    fprintf(fp,"%d",use1Score);
    if(fclose(fp))
    {
    printf("file close eror\n");
    exit(eror);
    }
    }
    }
    }
    }
    if(MODEL==1)
    {
    if(use2life>0&&use2.life<=0)
    {
    use2.x=570;
    use2.y=600;
    use2.life=50;
    use2.aggressivity=25;
    use2.dir=up;
    use2.speed=normal;
    use2.atack=normal;
    use2.defence=normal;
    }
    }
    return ok;
    }
    int control_userInfo(int count)
    {
    char num[10];
    FILE * fp;
    int k;
    if(count==0)  //读出历史最高分
    {
    if((fp=fopen("gameMark.txt","r"))==NULL)
    {
    printf("File open eror!\n");
    exit(eror);
    }
    else
    {
    fscanf(fp,"%d",&highmark);
    if(fclose(fp))
    {
    printf("file close eror\n");
    exit(eror);
    }
    }
    }
    setfont( 20, 0,"楷体");
    outtextxy(1020,50,"Score:");
    sprintf(num, "%d", use1Score);
    outtextxy(1080,50,num);
    outtextxy(1020,90,"HP:");
    setcolor(WHITE);
    rectangle(1059,89,1161,111);
    k=use1.life*2;
    if(k<0)
    k=0;
    setfillcolor(RED);
    solidrectangle(1060,90,1060+k,110);
    setfillcolor(BLUE);
    solidrectangle(1060+k,90,1160,110);
    outtextxy(1020,120,"life:");
    sprintf(num, "%d", use1life);
    outtextxy(1080,120,num);
    outtextxy(1070,200,"Player1");


    outtextxy(1020,320,"Highrest:");
    sprintf(num, "%d", highmark);
    outtextxy(1110,320,num);


    if(MODEL==1)
    {
    outtextxy(1020,350,"Score:");
    sprintf(num, "%d", use2Score);
    outtextxy(1080,350,num);


    outtextxy(1020,90,"HP:");
    k=use1.life*2;
    if(k<0)
    k=0;
    setfillcolor(RED);
       solidrectangle(1060,390,1060+k,410);
    setfillcolor(BLUE);
    solidrectangle(1060+k,390,1160,410);
    outtextxy(1020,420,"life:");
        sprintf(num, "%d", use2life);
        outtextxy(1080,420,num);
    outtextxy(1070,500,"Player2");
    }
    if(count==300)  //存入历史记录最高分
    {
    if((MODEL==0&&use1Score>highmark)||(MODEL==1&&use1Score>highmark&&use1Score>use2Score))
    {
    if((fp=fopen("gameMark.txt","w"))==NULL)
    {
    printf("File open eror!\n");
    exit(eror);
    }
    else
    {
    fprintf(fp,"%d",use1Score);
    if(fclose(fp))
    {
    printf("file close eror\n");
    exit(eror);
    }
    }
    }
    else if(MODEL==1&&use2Score>highmark&&use2Score>use1Score)
    {
    if((fp=fopen("gameMark.txt","w"))==NULL)
    {
    printf("File open eror!\n");
    exit(eror);
    }
    else
    {
    fprintf(fp,"%d",use2Score);
    if(fclose(fp))
    {
    printf("file close eror\n");
    exit(eror);
    }
    }
    }
    }
    return ok;
    }
    int control_property()
    {
    int i=0,j=0;
    for(i=0;i<5;i++)//控制生成道具并对已经生成的道具存在时间递减,当clock等于0,道具消失。
    {
    if(pro[i].flag==0)
    {
    j=rand()%500;
    if(j==1)
    {
    //控制道具生成位置不与玩家坦克位置相冲突。
    pro[i].flag=1;
    while(1)
    {
    pro[i].x=rand()%960;
       pro[i].y=rand()%610;
    if(MODEL==0)
    {
    if(pro[i].x-use1.x>-50&&pro[i].x-use1.x<50&&pro[i].y-use1.y>-50&&pro[i].y-use1.y<50)
    continue;
    else
    break;
    }
    else
    {
    if((pro[i].x-use1.x>-50&&pro[i].x-use1.x<50&&pro[i].y-use1.y>-50&&pro[i].y-use1.y<50)
    ||(pro[i].x-use2.x>-50&&pro[i].x-use2.x<50&&pro[i].y-use2.y>-50&&pro[i].y-use2.y<50))
    continue;
    else
    break;
    }
    }
    pro[i].clock=500;
    j=rand()%5;
    switch(j)
    {
    case 0:
    pro[i].con=addSpeed;
    break;
    case 1:
    pro[i].con=addAggressivity;
    break;
    case 2:
    pro[i].con=invincible;
    break;
    case 3:
    pro[i].con=addOneLife;
    break;
    case 4:
    pro[i].con=killAll;
    break;
    }
    }
    }
    else
    {
    if(pro[i].clock<=0)
    pro[i].flag=0;
    else
    pro[i].clock--;

    }
    }
    //判断玩家坦克是否接到道具,并修改接到道具的玩家坦克的相应成员变量。
  • TA的每日心情

    2019-5-22 22:04
  • 签到天数: 59 天

    [LV.5]常住居民I

     楼主| 发表于 2018-12-1 11:40:28 | 显示全部楼层
    文章转载于:
    https://blog.csdn.net/wang_dong001/article/details/47086037


    for(i=0;i<5;i++)
    {
    if(pro[i].flag==1)
    {
    if(use1.x-pro[i].x>-50&&use1.x-pro[i].x<40&&use1.y-pro[i].y>-50&&use1.y-pro[i].y<40)
    {
    pro[i].flag=0;
    mciSendString( "close eatting", 0, 0, 0 );
    mciSendString( "open ./source/eatting.mp3 alias eatting", 0, 0, 0 );
    mciSendString( "play eatting", 0, 0, 0 );
    if(pro[i].con==addOneLife)
    {
    use1life++;
    mciSendString( "close getLife", 0, 0, 0 );
       mciSendString( "open ./source/getLife.mp3 alias getLife", 0, 0, 0 );
       mciSendString( "play getLife", 0, 0, 0 );
    }
    else if(pro[i].con==killAll)
    {
    for(j=0;j<NUME;j++)
    {
    if(flagLife[j]==true)
    {
    use1Score += enm[j].life;
    flagLife[j]=false;
    enemyNum--;
    }
    }
    }
    else if(pro[i].con==addAggressivity)
    {
    use1.atack=addAggressivity;
    use1.aggressivity = 100;
    use1.clocka = 500;
    }
    else if(pro[i].con==addSpeed)
    {
    use1.speed = addSpeed;
    use1.clocks = 500;
    }
    else
    {
    use1.defence = invincible;
    use1.clockd = 500;
    }
    }
    if(MODEL==1)
    {
    if(use2.x-pro[i].x>-50&&use2.x-pro[i].x<40&&use2.y-pro[i].y>-50&&use2.y-pro[i].y<40)
    {
    mciSendString( "close eatting", 0, 0, 0 );
           mciSendString( "open ./source/eatting.mp3 alias eatting", 0, 0, 0 );
       mciSendString( "play eatting", 0, 0, 0 );
    pro[i].flag=0;
    if(pro[i].con==addOneLife)
    {
    use2life++;
    }
    else if(pro[i].con==killAll)
    {
    for(j=0;j<NUME;j++)
    {
    if(flagLife[j]==true)

    {
    use2Score += enm[j].life;
    flagLife[j]=false;
    enemyNum--;
    }
    }
    }
    else if(pro[i].con==addAggressivity)
    {
    use2.atack=addAggressivity;
    use2.aggressivity = 100;
    use2.clocka = 500;
    }
    else if(pro[i].con==addSpeed)
    {
    use2.speed = addSpeed;
    use2.clocks = 500;
    }
    else
    {
    use2.defence = invincible;
    use2.clockd = 500;
    }
    }
    }
    }
    }
    //玩家的道具状态计时器递减并恢复计时器到达0的道具状态效果。
    if(use1.defence==invincible)
    {
    use1.clockd--;
    if(use1.clockd<=0)
    use1.defence=normal;
    }
    if(use1.atack==addAggressivity)
    {
    use1.clocka--;
    if(use1.clocka<=0)
    use1.atack=normal;
    }
    if(use1.speed==addSpeed)
    {
    use1.clocks--;
    if(use1.clocks<=0)
    use1.speed=normal;
    }
    if(MODEL==1)
    {
    if(use2.defence==invincible)
    {
    use2.clockd--;
    if(use1.clockd<=0)
       use1.defence=normal;
    }
    if(use2.atack==addAggressivity)
    {
    use2.clocka--;
    if(use2.clocka<=0)
    {
    use2.atack=normal;
    use2.aggressivity=25;
    }
    }
    if(use2.speed==addSpeed)
    {
    use2.clocks--;
    if(use2.clocks<=0)
    {
    use2.speed=normal;
    }
    }
    }
    //draw 道具
    for(i=0;i<5;i++)
    {
    if(pro[i].flag==1)
    {
    switch(pro[i].con)
    {
    case addSpeed:
    rectangle(pro[i].x+13,pro[i].y+10,pro[i].x+27,pro[i].y+30);
    line(pro[i].x+13,pro[i].y+10,pro[i].x+20,pro[i].y);
    line(pro[i].x+27,pro[i].y+10,pro[i].x+20,pro[i].y);
    line(pro[i].x+13,pro[i].y+30,pro[i].x,pro[i].y+40);
    line(pro[i].x+27,pro[i].y+30,pro[i].x+40,pro[i].y+40);
    line(pro[i].x+20,pro[i].y+30,pro[i].x+20,pro[i].y+40);
    break;
    case addAggressivity:
    rectangle(pro[i].x,pro[i].y+20,pro[i].x+40,pro[i].y+40);
    solidcircle(pro[i].x+20,pro[i].y+30,5);
    rectangle(pro[i].x+17,pro[i].y,pro[i].x+23,pro[i].y+25);
    break;
    case addOneLife:
    setfillcolor(RED);
    solidrectangle(pro[i].x,pro[i].y+15,pro[i].x+40,pro[i].y+40);
    solidrectangle(pro[i].x+18,pro[i].y+4,pro[i].x+22,pro[i].y+15);
    solidellipse(pro[i].x+17,pro[i].y,pro[i].x+23,pro[i].y+4);
    break;
    case invincible:
    line(pro[i].x,pro[i].y,pro[i].x,pro[i].y+30);
    line(pro[i].x+40,pro[i].y,pro[i].x+40,pro[i].y+30);
    line(pro[i].x+20,pro[i].y+10,pro[i].x+20,pro[i].y+40);
    line(pro[i].x,pro[i].y,pro[i].x+20,pro[i].y+10);
    line(pro[i].x+40,pro[i].y,pro[i].x+20,pro[i].y+10);
    line(pro[i].x,pro[i].y+30,pro[i].x+20,pro[i].y+40);
    line(pro[i].x+40,pro[i].y+30,pro[i].x+20,pro[i].y+40);
    line(pro[i].x,pro[i].y+15,pro[i].x+20,pro[i].y+25);
    line(pro[i].x+40,pro[i].y+15,pro[i].x+20,pro[i].y+25);
    break;
    case killAll:
    setfillcolor(YELLOW);
    solidcircle(pro[i].x+20,pro[i].y+25,15);
    solidrectangle(pro[i].x+18,pro[i].y+4,pro[i].x+22,pro[i].y+10);
    solidrectangle(pro[i].x+15,pro[i].y,pro[i].x+25,pro[i].y+4);
    line(pro[i].x+15,pro[i].y+2,pro[i].x+10,pro[i].y+7);
    break;
    }
    }
    }
    return ok;
    }
    int controlBomb()  //对击中的坦克的生命值减定量,并改变子弹位置。
    {
    int i;
    for(i=0;i<NUME;i++)  //判断是否击中玩家坦克
    {
    if(MODEL==0&&flagB[i]==true)
    {
    if(isTarget(use1,enemyBomb[i]))
    {
    if(use1.defence==normal)
    {
    use1.life -= enemyBomb[i].aggressivity;
    }
    flagB[i]=false;
    }
    }
    else if(flagB[i]==true)
    {
    if(isTarget(use1,enemyBomb[i]))
    {
    flagB[i]=false;
    }
    else
    {
    if(isTarget(use2,enemyBomb[i]))
    {
    flagB[i]=false;
    }
    }
    }
    }
    for(i=0;i<NUME;i++)  //判断敌军坦克和玩家坦克子弹是否发生碰撞
    {
    if(flagB[i]==true&&FLAGBOMB1==true)
    {
    if((enemyBomb[i].x-use1Bomb.x<8&&enemyBomb[i].x-use1Bomb.x>-8)
    &&(enemyBomb[i].y-use1Bomb.y<8&&enemyBomb[i].y-use1Bomb.y>-8))
    {
    mciSendString( "close bombing", 0, 0, 0 );
       mciSendString( "open ./source/bombing.mp3 alias bombing", 0, 0, 0 );
    mciSendString( "play bombing", 0, 0, 0 );
    flagB[i]=false;
    FLAGBOMB1=false;
    }
    else if(MODEL==1&&FLAGBOMB2==true&&(enemyBomb[i].x-use2Bomb.x<8&&enemyBomb[i].x-use2Bomb.x>-8)
    &&(enemyBomb[i].y-use2Bomb.y<8&&enemyBomb[i].y-use2Bomb.y>-8))
    {
    mciSendString( "close bombing", 0, 0, 0 );
       mciSendString( "open ./source/bombing.mp3 alias bombing", 0, 0, 0 );
    mciSendString( "play bombing", 0, 0, 0 );
    flagB[i]=false;
    FLAGBOMB2=false;
    }
    }
    }
    /*for(i=0;i<NUME;i++)  //判断敌军坦克和玩家坦克子弹是否发生碰撞
    {
    if(flagB[i]==true&&FLAGBOMB1==true)
    {
    if((enemyBomb[i].x-use1Bomb.x<8&&enemyBomb[i].x-use1Bomb.x>-8)
    &&(enemyBomb[i].y-use1Bomb.y<8&&enemyBomb[i].y-use1Bomb.y>-8))
    {
    mciSendString( "play bombing ", 0, 0, 0 );
    flagB[i]=false;
    FLAGBOMB1=false;
    }
    else if(MODEL==1&&FLAGBOMB2==true&&(enemyBomb[i].x-use2Bomb.x<8&&enemyBomb[i].x-use2Bomb.x>-8)
    &&(enemyBomb[i].y-use2Bomb.y<8&&enemyBomb[i].y-use2Bomb.y>-8))
    {
    flagB[i]=false;
    FLAGBOMB2=false;
    }
    }
    }*/
    for(i=0;i<NUME;i++)  //改变敌军子弹位置
    {
    if(flagB[i]==true)
    {
    switch (enemyBomb[i].dir)
    {
    case up:
    enemyBomb[i].y += enemyBomb[i].speed;
    break;
    case down:
    enemyBomb[i].y -= enemyBomb[i].speed;
    break;
    case left:
    enemyBomb[i].x -= enemyBomb[i].speed;
    break;
    case right:
    enemyBomb[i].x += enemyBomb[i].speed;
    break;
    }
    }
    }
    if(FLAGBOMB1==true)   //改变玩家坦克子弹位置
    {
    switch (use1Bomb.dir)
    {
    case up:
    use1Bomb.y -= use1Bomb.speed;
    break;
    case down:
    use1Bomb.y += use1Bomb.speed;
    break;
    case left:
    use1Bomb.x -= use1Bomb.speed;
    break;
    case right:
    use1Bomb.x += use1Bomb.speed;
    break;
    }
    if(use1Bomb.x<0||use1Bomb.y<0||use1Bomb.x>1000||use1Bomb.y>650)    //判断玩家1子弹是否出界
    {
        mciSendString( "close bombing", 0, 0, 0 );
    mciSendString( "open ./source/bombing.mp3 alias bombing", 0, 0, 0 );
    mciSendString( "play bombing", 0, 0, 0 );
    FLAGBOMB1=false;
    }
    }
    if(MODEL==1&&FLAGBOMB2==true)
    {
    switch (use2Bomb.dir)
    {
    case up:
    use2Bomb.y -= moveLength;
    break;
    case down:
    use2Bomb.y += moveLength;
    break;
    case left:
    use2Bomb.x -= moveLength;
    break;
    case right:
    use2Bomb.x += moveLength;
    break;
    }
    if(use2Bomb.x<0||use2Bomb.y<0||use2Bomb.x>1000||use2Bomb.y>650)  //判断玩家2子弹是否出界
    {
    mciSendString( "close bombing", 0, 0, 0 );
    mciSendString( "open ./source/bombing.mp3 alias bombing", 0, 0, 0 );
       mciSendString( "play bombing", 0, 0, 0 );
    FLAGBOMB2=false;
    }
    }
    for(i=0;i<NUME;i++)           //判断敌军子弹是否出界
    {
    if(flagB[i]==true)
    if(enemyBomb[i].x<0||enemyBomb[i].y<0||enemyBomb[i].x>1000||enemyBomb[i].y>650)
    {
       flagB[i]=false;
    }
    }
    return ok;

    }
    bool isTarget(USER tar,BOMB bomb)
    {
    if(bomb.x-tar.x>-4&&bomb.x-tar.x<54&&bomb.y-tar.y>-4&&bomb.y-tar.y<54)
    return true;
    else
    return false;
    }
    bool isTarget2(ENEMY tar,BOMB bomb)
    {
    if(bomb.x-tar.x>-4&&bomb.x-tar.x<54&&bomb.y-tar.y>-4&&bomb.y-tar.y<54)
    return true;
    else
    return false;
    }
    int controlUser()//监控键盘,控制玩家坦克的位置、状态、生命
    {
    char b;
    int flag=0,i=0;
    if(use1.life<=0||use2.life<=0)
    init_userinfo();
    if(kbhit())
    {
    b=getch();
    if((b==32)&&FLAGBOMB1==false)  //监控空格键玩家1发射子弹
    {
    mciSendString( "close launchBomb ", 0, 0, 0 );
    mciSendString( "open ./source/launchBomb.mp3 alias launchBomb", 0, 0, 0 );
    mciSendString( "play launchBomb ", 0, 0, 0 );
    FLAGBOMB1=true;
    switch (use1.dir)
    {
    case up:
    use1Bomb.dir=up;
    use1Bomb.x=use1.x+25;
    use1Bomb.y=use1.y+4;
    break;
    case down:
    use1Bomb.dir=down;
    use1Bomb.x=use1.x+25;
    use1Bomb.y=use1.y+54;
    break;
    case left:
    use1Bomb.dir=left;
    use1Bomb.x=use1.x-4;
    use1Bomb.y=use1.y+25;
    break;
    case right:
    use1Bomb.dir=right;
    use1Bomb.x=use1.x+54;
    use1Bomb.y=use1.y+25;
    break;
    }
    use1Bomb.aggressivity=use1.aggressivity;
    if(use1.speed==addSpeed)
    use1Bomb.speed = BombNormalSpeed + BombAddSpeed;
    else
    use1Bomb.speed = BombNormalSpeed;
    }
    else
    {
    switch(b)
    {
    case 'w':  //控制玩家1坦克行驶
    case 'W':
    回复 支持 2 反对 0

    使用道具 举报

  • TA的每日心情

    2019-5-22 22:04
  • 签到天数: 59 天

    [LV.5]常住居民I

     楼主| 发表于 2018-12-1 11:43:01 | 显示全部楼层
    mciSendString( "resume moveSelf ", 0, 0, 0 );
    use1.dir=up;
    if(is_warded(use1.x,use1.y,use1.dir,0))
           flag=1;
    if(use1.y>=5)
    {
    for(i=0;i<NUME;i++)
    {
    if(flagLife[i]==true&&is_collision(use1.x,use1.y,enm[i].x,enm[i].y,up))
    flag=1;
    }
    if(flag==0)
    {
    if(use1.speed==addSpeed)
    use1.y -=10;
    else
    use1.y -=5;
    }
    }
    break;
    case 's':
    case 'S':
    mciSendString( "resume moveSelf ", 0, 0, 0 );
    use1.dir=down;
    if(is_warded(use1.x,use1.y,use1.dir,0))
           flag=1;
    if(use1.y<=595)
    {
    for(i=0;i<NUME;i++)
    {
    if(flagLife[i]==true&&is_collision(use1.x,use1.y,enm[i].x,enm[i].y,down))
    flag=1;
    }
    if(flag==0)
    {
    if(use1.speed==addSpeed)
    use1.y +=10;
    else
    use1.y +=5;
    }
    }
    break;
    case 'a':
    case 'A':
    mciSendString( "resume moveSelf ", 0, 0, 0 );
    use1.dir=left;
    if(is_warded(use1.x,use1.y,use1.dir,0))
           flag=1;
    if(use1.x>=5)
    {
    for(i=0;i<NUME;i++)
    {
    if(flagLife[i]==true&&is_collision(use1.x,use1.y,enm[i].x,enm[i].y,left))
    flag=1;
    }
    if(flag==0)
    {
    if(use1.speed==addSpeed)
    use1.x -=10;
    else
    use1.x -=5;
    }
    }
    break;
    case 'd':
    case 'D':
    mciSendString( "resume moveSelf ", 0, 0, 0 );
    use1.dir=right;
    if(is_warded(use1.x,use1.y,use1.dir,0))
           flag=1;
    if(use1.x<=945)
    {
    for(i=0;i<NUME;i++)
    {
    if(flagLife[i]==true&&is_collision(use1.x,use1.y,enm[i].x,enm[i].y,right))
    flag=1;
    }
    if(flag==0)
    {
    if(use1.speed==addSpeed)
    use1.x +=10;
    else
    use1.x +=5;
    }
    }
    break;
    }
    }
    switch(b) //控制玩家1或玩家2坦克行驶
    {
    case 72:
    mciSendString( "resume moveSelf ", 0, 0, 0 );
    if(MODEL==0)
    {
    use1.dir=up;
    if(is_warded(use1.x,use1.y,use1.dir,0))
           flag=1;

    if(use1.y>=5)
    {
    for(i=0;i<NUME;i++)
    {
    if(flagLife[i]==true&&is_collision(use1.x,use1.y,enm[i].x,enm[i].y,up))
    flag=1;
    }
    if(flag==0)
    {
    if(use1.speed==addSpeed)
    use1.y -=10;
    else
    use1.y -=5;
    }
    }
    }
    else
    {
    use2.dir=up;
    if(is_warded(use2.x,use2.y,use2.dir,0))
           flag=1;
    if(use2.y>=5)
    {
    for(i=0;i<NUME;i++)
    {
    if(flagLife[i]==true&&is_collision(use2.x,use2.y,enm[i].x,enm[i].y,up))
    flag=1;
    }
    if(flag==0)
    {
    if(use2.speed==addSpeed)
    use2.y -=10;
    else
    use2.y -=5;
    }
    }
    }
    break;
    case 80:
    mciSendString( "resume moveSelf ", 0, 0, 0 );
    if(MODEL ==0)
    {
    use1.dir=down;
    if(is_warded(use1.x,use1.y,use1.dir,0))
           flag=1;
    if(use1.y<=595)
    {
    for(i=0;i<NUME;i++)
    {
    if(flagLife[i]==true&&is_collision(use1.x,use1.y,enm[i].x,enm[i].y,down))
    flag=1;
    }
    if(flag==0)
    {
    if(use1.speed==addSpeed)
    use1.y +=10;
    else
    use1.y +=5;
    }
    }
    }
    else
    {
    use2.dir=down;
    if(is_warded(use2.x,use2.y,use2.dir,0))
           flag=1;
    if(use2.y<=595)
    {
    for(i=0;i<NUME;i++)
    {
    if(flagLife[i]==true&&is_collision(use2.x,use2.y,enm[i].x,enm[i].y,down))
    flag=1;
    }
    if(flag==0)
    {
    if(use2.speed==addSpeed)
    use2.y -=10;
    else
    use2.y -=5;
    }
    }
    }
    break;
    case 75:
    mciSendString( "resume moveSelf ", 0, 0, 0 );
    if(MODEL==0)
    {
    use1.dir=left;
    if(is_warded(use1.x,use1.y,use1.dir,0))
           flag=1;
    if(use1.x>=5)
    {
    for(i=0;i<NUME;i++)
    {
    if(flagLife[i]==true&&is_collision(use1.x,use1.y,enm[i].x,enm[i].y,left))
    flag=1;
    }
    if(flag==0)
    {
    if(use1.speed==addSpeed)
    use1.x -=10;
    else
    use1.x -=5;
    }
    }
    }
    else
    {
    use2.dir=left;
    if(is_warded(use2.x,use2.y,use2.dir,0))
           flag=1;
    if(use2.x>=5)
    {
    for(i=0;i<NUME;i++)
    {
    if(flagLife[i]==true&&is_collision(use2.x,use2.y,enm[i].x,enm[i].y,left))
    flag=1;
    }
    if(flag==0)
    {
    if(use2.speed==addSpeed)
    use2.x -=10;
    else
    use2.x -=5;
    }
    }
    }
    break;
    case 77:
    mciSendString( "resume moveSelf ", 0, 0, 0 );
    if(MODEL==0)
    {
    use1.dir=right;
    if(is_warded(use1.x,use1.y,use1.dir,0))
           flag=1;
    if(use1.x<=945)
    {
    for(i=0;i<NUME;i++)
    {
    if(flagLife[i]==true&&is_collision(use1.x,use1.y,enm[i].x,enm[i].y,right))
    flag=1;
    }
    if(flag==0)
    {
    if(use1.speed==addSpeed)
    use1.x +=10;
    else
    use1.x +=5;
    }
    }
    }
    else
    {
    use2.dir=right;
    if(is_warded(use2.x,use2.y,use2.dir,0))
           flag=1;
    if(use2.x<=945)
    {
    for(i=0;i<NUME;i++)
    {
    if(flagLife[i]==true&&is_collision(use2.x,use2.y,enm[i].x,enm[i].y,right))
    flag=1;
    }
    if(flag==0)
    {
    if(use2.speed==addSpeed)
    use2.x +=10;
    else
    use2.x +=5;
    }
    }
    }
    break;
    }
    if(MODEL==1)
    {
    if((b=='0'||b=='l'||b=='L')&&FLAGBOMB2==false)  //监控玩家2发射子弹
    {
    mciSendString( "close launchBomb ", 0, 0, 0 );
       mciSendString( "open ./source/launchBomb.mp3 alias launchBomb", 0, 0, 0 );
        mciSendString( "play launchBomb ", 0, 0, 0 );
    FLAGBOMB2=true;
    switch (use2.dir)
    {
    case up:
    use2Bomb.dir=up;
    use2Bomb.x=use2.x+25;
    use2Bomb.y=use2.y+4;
    break;
    case down:
    use2Bomb.dir=down;
    use2Bomb.x=use2.x+25;
    use2Bomb.y=use2.y+54;
    break;
    case left:
    use2Bomb.dir=left;
    use2Bomb.x=use2.x-4;
    use2Bomb.y=use2.y+25;
    break;
    case right:
    use2Bomb.dir=right;
    use2Bomb.x=use2.x+54;
    use2Bomb.y=use2.y+25;
    break;
    }
    use2Bomb.aggressivity=use2.aggressivity;
    if(use2.atack==addAggressivity)
    use2Bomb.aggressivity += 25;
    if(use2.speed==addSpeed)
    use2Bomb.speed = BombNormalSpeed + BombAddSpeed;
    else
    use2Bomb.speed = BombNormalSpeed;
    }
    }
    }
    return ok;
    }
    int controlEnemy()  //控制敌军坦克的生成、击毁消失和发射炮弹。
    {
    int add=0,i=0,x=0,y=0,flag=0,j=0,k=0,pos=0;
    if(enemyNum<4)  //坦克数量少于4就新生成坦克
    {
       add=rand()%4+1;
    for(i=0;i<add;)
    {
    x=rand()%951;
    for(j=0;j<NUME;j++)
    {
    if(flagLife[j]==true)
    if(enm[j].x-x<=50&&enm[j].x-x>=-50) //坦克位置发生冲突
       flag=1;
    }
    if(flag==0)
    {
    for(pos=0;pos<NUME;pos++)
    {
    if(flagLife[pos]==false)
    break;
    }
    if(flagLife[pos]==false)
    {
    enm[pos].x=x;
    enm[pos].y=y;
    enm[pos].life=(rand()%3+1)*25;
    enm[pos].aggressivity=20;
    j=rand()%4;
    switch(j)
    {
    case 0:
    enm[pos].dir=up;
    break;
    case 1:
    enm[pos].dir=right;
    break;
    case 2:
    enm[pos].dir=down;
    break;
    case 3:
    enm[pos].dir=left;
    break;
    }
    i++;
    enemyNum++;
    flagLife[pos]=true;
    }
    else
    {
    break;  //没有足够位置生成新坦克。
    }
    }
    else
    flag=0;
    }
    }
    for(i=0;i<NUME;i++)  //判断是否击中敌军坦克
    {
    if(MODEL==0&&flagLife[i]==true&&FLAGBOMB1==true)
    {
    if(isTarget2(enm[i],use1Bomb))
    {
    mciSendString( "close bombing", 0, 0, 0 );
       mciSendString( "open ./source/bombing.mp3 alias bombing", 0, 0, 0 );
    mciSendString( "play bombing", 0, 0, 0 );
    if(enm[i].life>use1Bomb.aggressivity)
    use1Score += use1Bomb.aggressivity;
    else
    use1Score += enm[i].life;
    enm[i].life -= use1Bomb.aggressivity;
    FLAGBOMB1=false;
    break;
    }
    }
    else if(MODEL==1&&flagLife[i]==true&&FLAGBOMB1==true)
    {
    if(isTarget2(enm[i],use1Bomb))
    {
    mciSendString( "close bombing", 0, 0, 0 );
       mciSendString( "open ./source/bombing.mp3 alias bombing", 0, 0, 0 );
    mciSendString( "play bombing", 0, 0, 0 );
    if(enm[i].life>use1Bomb.aggressivity)
    use1Score += use1Bomb.aggressivity;
    else
    use1Score += enm[i].life;
    enm[i].life -= use1Bomb.aggressivity;
    FLAGBOMB1=false;
    break;
    }
    if(MODEL==1&&flagLife[i]==true&&FLAGBOMB2==true)
    {
    if(isTarget2(enm[i],use2Bomb))
    {
    mciSendString( "close bombing", 0, 0, 0 );
    mciSendString( "open ./source/bombing.mp3 alias bombing", 0, 0, 0 );
    mciSendString( "play bombing", 0, 0, 0 );
    if(enm[i].life>use2Bomb.aggressivity)
    use2Score += use2Bomb.aggressivity;
    else
    use2Score += enm[i].life;
    enm[i].life -= use2Bomb.aggressivity;
    FLAGBOMB2 = false;
    break;
    }
    }
    }
    }
    for(i=0;i<NUME;i++)   //坦克被击毁
    {
    if(flagLife[i]==true&&enm[i].life<=0)
    {
    flagLife[i]=false;
    enemyNum--;
    }
    if(flagLife[i]==true&&flagB[i]==false)  // 1/35的概率发射炮弹
    {
    k=rand()%50;
    if(k==3)  //k=3代表发炮
    {
    flagB[i]=1;
    enemyBomb[i].aggressivity=enm[i].aggressivity;
    enemyBomb[i].dir=enm[i].dir;
    enemyBomb[i].speed=BombNormalSpeed;
    switch (enm[i].dir)
    {
    case up:
    enemyBomb[i].x=enm[i].x+25;
    enemyBomb[i].y=enm[i].y+4;
    break;
    case down:
    enemyBomb[i].x=enm[i].x+25;
    enemyBomb[i].y=enm[i].y+54;
    break;
    case left:
    enemyBomb[i].x=enm[i].x-4;
    enemyBomb[i].y=enm[i].y+25;
    break;
    case right:
    enemyBomb[i].x=enm[i].x+54;
    enemyBomb[i].y=enm[i].y+25;
    break;
    }
    }
    }
    }
    return ok;
    }
    int tankMove()
    {
    int i,d=0,j=0,k=0,flag=0;
    for(i=0;i<NUME;i++)
    {
    if(flagLife[i]==true)
    {
    d=rand()%30;
    if(d==4)  // 1/5概率改变方向
    {
    if(enm[i].dir=up)
    j=0;
    else if(enm[i].dir=right)
    j=1;
    else if(enm[i].dir=down)
    j=2;
    else
    j=3;
    do{
    d=rand()%4;
    }while(d==j);
    switch(d)
    {
    case 0:
    enm[i].dir=up;
    break;
    case 1:
    enm[i].dir=right;
    break;
    case 2:
    enm[i].dir=down;
    break;
    case 3:
    enm[i].dir=left;
    break;
    }
    }
    else     //坦克位置改变。
    {
    switch (enm[i].dir)
    {
    case up:
    if(enm[i].y>=2)
    {
    回复 支持 1 反对 0

    使用道具 举报

  • TA的每日心情
    擦汗
    昨天 20:56
  • 签到天数: 86 天

    [LV.6]常住居民II

    发表于 2018-12-1 13:17:20 | 显示全部楼层
    建议用文档形式分享  这样不好看
    回复 支持 1 反对 0

    使用道具 举报

  • TA的每日心情
    无聊
    5 小时前
  • 签到天数: 262 天

    [LV.8]以坛为家I

    发表于 2018-12-1 23:53:50 | 显示全部楼层
    太长了/笑哭
    回复 支持 反对

    使用道具 举报

    头像被屏蔽
  • TA的每日心情
    难过
    3 天前
  • 签到天数: 194 天

    [LV.7]常住居民III

    发表于 2018-12-2 06:58:25 | 显示全部楼层
    提示: 作者被禁止或删除 内容自动屏蔽
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    擦汗
    2019-5-15 08:04
  • 签到天数: 155 天

    [LV.7]常住居民III

    发表于 2018-12-2 07:46:05 | 显示全部楼层
    收藏了,回头慢慢没看
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2019-6-9 10:31
  • 签到天数: 165 天

    [LV.7]常住居民III

    发表于 2018-12-2 10:15:41 | 显示全部楼层
    感谢分享源码,
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    慵懒
    2019-2-27 19:13
  • 签到天数: 37 天

    [LV.5]常住居民I

    发表于 2018-12-27 17:57:19 | 显示全部楼层
    感谢楼主分享
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    郁闷
    3 天前
  • 签到天数: 16 天

    [LV.4]偶尔看看III

    发表于 6 天前 来自手机 | 显示全部楼层
    菜鸟的我看了表示很赞
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 登录 | 注册

    本版积分规则

    关闭

    站长推荐 上一条 /1 下一条

    红盟社区--中国红客联盟 

    Processed in 0.097262 second(s), 23 queries.

    站点统计| 举报| Archiver| 手机版| 黑屋 |   

    Powered by HUC © 2001-2017 Comsenz Inc.

    手机扫我进入移动触屏客户端

    关注我们可获取更多热点资讯

    Honor accompaniments. theme macfee

    快速回复 返回顶部 返回列表