C语言坦克大战源码分析

/* Note:Your choice is C IDE */
#include "graphics.h"
#include "stdlib.h"
#include "stdio.h"
#define a_UP    0x4800/*定义A坦克按键*/
#define a_DOWN  0x5000
#define a_LEFT  0x4b00
#define a_RIGHT 0x4d00
#define a_shoot 0x1c0d

#define b_UP    0x1177/*定义B坦克按键*/
#define b_DOWN  0x1f73
#define b_LEFT  0x1e61
#define b_RIGHT 0x2064
#define b_shoot 0x246a

#define ESC   0x011b

#define UPWAY    1/*定义方向*/
#define DOWNWAY  2
#define LEFTWAY  3
#define RIGHTWAY 4
int enemynum=3;

struct SHOOT   /*定义炮弹结构体*/
{
 int life;
 int x,y;
 int way;
}shoot[2]={{0,0,0,UPWAY},{0,0,0,UPWAY}};

struct PLAYER  /*定义玩家结构体*/
{
 int life;
 int x,y;
 int i,j;
 int way; 
}player[2]={{3,0,0,3,13,UPWAY},{3,0,0,10,13,UPWAY}};

struct ENEMY  /*定义敌人结构体*/
{
 int life;
 int x,y;
 int i,j;
 int way;
 int control;
}enemy[5]={{1,0,0,1,1,DOWNWAY,0},{1,0,0,1,2,DOWNWAY,0},{1,0,0,1,3,DOWNWAY,0},{1,0,0,1,4,DOWNWAY,0},{1,0,0,1,5,DOWNWAY,0}};

/*声明区*/
void allcircle(int i,int j);
void start();
void play();

void map_all(int map[15][15]);
void control_shoot(int m,int map[15][15],int i);
void enemy_control(int map[15][15]);

void uptank(int i,int j,int color);/*画坦克函数*/
void downtank(int i,int j,int color);
void lefttank(int i,int j,int color);
void righttank(int i,int j,int color);
void blank(int i,int j);

void map_water(int i,int j);/*画地图函数*/
void map_steel(int i,int j);
void map_wall(int i,int j);
void map_border(int i,int j);
void map_base(int i,int j);

void end();

main()
{  
   
    start();
    play();
    end();
}
void start()
{
 int i;
 int driver=VGA,mode=VGAHI;/*初始化图形模式*/
    initgraph(&driver,&mode,"");
    setbkcolor(7);/*设定背景*/
 settextstyle(4,0,0);
 outtextxy(500,300,"made");
 outtextxy(540,300,"by");
 outtextxy(562,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8 ,8 ,8 ,8 ,8 ,8 ,8 ,8 ,8 ,8 }};
  map_all(map);
  while(1)
    {
     
     while(!kbhit())/*判断炮弹*/
      { 
       
       if(enemynum==0)
        goto aaa;
        
       for(i=0;i<2;i++)
         if(shoot[i].life==1)
         switch(shoot[i].way)
     {
      case UPWAY :
        if((shoot[i].y-15)%30==0)
         if(map[(shoot[i].y-15)/30-1][(shoot[i].x-15)/30]!=0)
          {
           shoot[i].life=0;
           control_shoot(map[(shoot[i].y-15)/30-1][(shoot[i].x-15)/30],map,i);
          }
        if(shoot[i].y<47)
         shoot[i].life=0;
        if(shoot[i].life!=0)
         {
          setfillstyle(1,1);
          bar(shoot[i].x+14,shoot[i].y-5,shoot[i].x+17,shoot[i].y-2);
          setcolor(7);
          line(shoot[i].x+14,shoot[i].y-1,shoot[i].x+17,shoot[i].y-1);
          shoot[i].y--;
         }
        else
         {
          setfillstyle(1,getpixel(shoot[i].x+14,shoot[i].y-6));
          bar(shoot[i].x+14,shoot[i].y-5,shoot[i].x+17,shoot[i].y-1);
         }
        break;
      case DOWNWAY:
        if((shoot[i].y-15)%30==0)
         if(map[(shoot[i].y-15)/30+1][(shoot[i].x-15)/30]!=0)
          {
           shoot[i].life=0;
           control_shoot(map[(shoot[i].y-15)/30+1][(shoot[i].x-15)/30],map,i);
          }
        if(shoot[i].y>405)
         shoot[i].life=0;
        if(shoot[i].life!=0)
         {
          setfillstyle(1,1);
          bar(shoot[i].x+14,shoot[i].y+35,shoot[i].x+17,shoot[i].y+32);setcolor(7);
          line(shoot[i].x+14,shoot[i].y+31,shoot[i].x+17,shoot[i].y+31);
          shoot[i].y++;
         }
        else
         {
          setfillstyle(1,getpixel(shoot[i].x+14,shoot[i].y+36));
          bar(shoot[i].x+14,shoot[i].y+35,shoot[i].x+17,shoot[i].y+31);
         }
        break;
      case LEFTWAY:
        if((shoot[i].x-15)%30==0)
         if(map[(shoot[i].y-15)/30][(shoot[i].x-15)/30-1]!=0)
          {
           shoot[i].life=0;
           control_shoot(map[(shoot[i].y-15)/30][(shoot[i].x-15)/30-1],map,i);
          }
        if(shoot[i].x<47)
         shoot[i].life=0;
        if(shoot[i].life!=0)
         {
          setfillstyle(1,1);
          bar(shoot[i].x-5,shoot[i].y+14,shoot[i].x-2,shoot[i].y+17);setcolor(7);
          line(shoot[i].x-1,shoot[i].y+14,shoot[i].x-1,shoot[i].y+17);
          shoot[i].x--;
         }
        else
         {
          setfillstyle(1,getpixel(shoot[i].x-6,shoot[i].y+14));
          bar(shoot[i].x-5,shoot[i].y+14,shoot[i].x-1,shoot[i].y+17);
         }
        break;
      case RIGHTWAY:
        if((shoot[i].x-15)%30==0)
         if(map[(shoot[i].y-15)/30][(shoot[i].x-15)/30+1]!=0)
          {
           shoot[i].life=0;
           control_shoot(map[(shoot[i].y-15)/30][(shoot[i].x-15)/30+1],map,i);
          }
        if(shoot[i].x>405)
         shoot[i].life=0;
        if(shoot[i].life!=0)
         {
          setfillstyle(1,1);
          bar(shoot[i].x+35,shoot[i].y+14,shoot[i].x+32,shoot[i].y+17);setcolor(7);
          line(shoot[i].x+31,shoot[i].y+14,shoot[i].x+31,shoot[i].y+17);
          shoot[i].x++;
         }
        else
         {
          setfillstyle(1,getpixel(shoot[i].x+36,shoot[i].y+14));
          bar(shoot[i].x+35,shoot[i].y+14,shoot[i].x+31,shoot[i].y+17);
         }
        break;
     }
     delay(1500);num++;
     if(num==30)
     {
      enemy_control(map);/*敌人坦克运动*/
      num=0;
     }
       }   
     key=bioskey(0);
  switch(key)/*判断坦克*/
  {
   case a_UP:
       if(map[player[0].j-1][player[0].i]==0)
        {
         if(player[0].way==UPWAY)
         {
          map[player[0].j][player[0].i]=0;
          blank(player[0].i,player[0].j);
          player[0].j--;map[player[0].j][player[0].i]=1;
          uptank(player[0].i,player[0].j,1);player[0].way=UPWAY;
         }
         else
         {
          blank(player[0].i,player[0].j);uptank(player[0].i,player[0].j,1);player[0].way=UPWAY;
         }
        }
      else
       {
        blank(player[0].i,player[0].j);uptank(player[0].i,player[0].j,1);player[0].way=UPWAY;
       }
       break;
   case a_DOWN:
       if(map[player[0].j+1][player[0].i]==0)
        {
         if(player[0].way==DOWNWAY)
         {
          map[player[0].j][player[0].i]=0;
          blank(player[0].i,player[0].j);
          player[0].j++;map[player[0].j][player[0].i]=1;
          downtank(player[0].i,player[0].j,1);player[0].way=DOWNWAY;
         }
         else
         {
          blank(player[0].i,player[0].j);downtank(player[0].i,player[0].j,1);player[0].way=DOWNWAY;
         }
        }
      else
       {
        blank(player[0].i,player[0].j);downtank(player[0].i,player[0].j,1);player[0].way=DOWNWAY;
       }
       break;
   case a_LEFT:
      if(map[player[0].j][player[0].i-1]==0)
       {
        if(player[0].way==LEFTWAY)
        {
         map[player[0].j][player[0].i]=0;
         blank(player[0].i,player[0].j);
         player[0].i--;map[player[0].j][player[0].i]=1;
         lefttank(player[0].i,player[0].j,1);player[0].way=LEFTWAY;
        }
        else
         {
          blank(player[0].i,player[0].j);lefttank(player[0].i,player[0].j,1);player[0].way=LEFTWAY;
         }
       }
      else
       {
        blank(player[0].i,player[0].j);lefttank(player[0].i,player[0].j,1);player[0].way=LEFTWAY;
       }
      break;
   case a_RIGHT:
      if(map[player[0].j][player[0].i+1]==0)
       {
        if(player[0].way==RIGHTWAY)
        {
         map[player[0].j][player[0].i]=0;
         blank(player[0].i,player[0].j);
         player[0].i++;map[player[0].j][player[0].i]=1;
         righttank(player[0].i,player[0].j,1);player[0].way=RIGHTWAY;
        }
        else
         {
          blank(player[0].i,player[0].j);righttank(player[0].i,player[0].j,1);player[0].way=RIGHTWAY;
         }
       }
      else
       {
        blank(player[0].i,player[0].j);righttank(player[0].i,player[0].j,1);player[0].way=RIGHTWAY;
       }
      break;
   case a_shoot:
      if(shoot[0].life==0)
       {
        shoot[0].life=1;
        shoot[0].x=player[0].i*30+15;  shoot[0].y=player[0].j*30+15;
        shoot[0].way=player[0].way;break;
       }
      else
       break;
   case b_UP:
       if(map[player[1].j-1][player[1].i]==0)
        {
         if(player[1].way==UPWAY)
         {
          map[player[1].j][player[1].i]=0;
          blank(player[1].i,player[1].j);
          player[1].j--;map[player[1].j][player[1].i]=1;
          uptank(player[1].i,player[1].j,GREEN);player[1].way=UPWAY;
         }
         else
         {
          blank(player[1].i,player[1].j);uptank(player[1].i,player[1].j,GREEN);player[1].way=UPWAY;
         }
        }
       else
       {
        blank(player[1].i,player[1].j);uptank(player[1].i,player[1].j,GREEN);player[1].way=UPWAY;
       }
       break;
   case b_DOWN:
       if(map[player[1].j+1][player[1].i]==0)
        {
         if(player[1].way==DOWNWAY)
         {
          map[player[1].j][player[1].i]=0;
          blank(player[1].i,player[1].j);
          player[1].j++;map[player[1].j][player[1].i]=1;
          downtank(player[1].i,player[1].j,GREEN);player[1].way=DOWNWAY;
         }
         else
         {
          blank(player[1].i,player[1].j);downtank(player[1].i,player[1].j,GREEN);player[1].way=DOWNWAY;
         }
        }
       else
       {
        blank(player[1].i,player[1].j);downtank(player[1].i,player[1].j,GREEN);player[1].way=DOWNWAY;
       }
       break;
   case b_LEFT:
      if(map[player[1].j][player[1].i-1]==0)
       {
        if(player[1].way==LEFTWAY)
        {
         map[player[1].j][player[1].i]=0;
         blank(player[1].i,player[1].j);
         player[1].i--;map[player[1].j][player[1].i]=1;
         lefttank(player[1].i,player[1].j,GREEN);player[1].way=LEFTWAY;
        }
        else
         {
          blank(player[1].i,player[1].j);lefttank(player[1].i,player[1].j,GREEN);player[1].way=LEFTWAY;
         }
       }
      else
       {
        blank(player[1].i,player[1].j);lefttank(player[1].i,player[1].j,GREEN);player[1].way=LEFTWAY;
       }
      break;
   case b_RIGHT:
      if(map[player[1].j][player[1].i+1]==0)
       {
        if(player[1].way==RIGHTWAY)
        {
         map[player[1].j][player[1].i]=0;
         blank(player[1].i,player[1].j);
         player[1].i++;map[player[1].j][player[1].i]=1;
         righttank(player[1].i,player[1].j,GREEN);player[1].way=RIGHTWAY;
        }
        else
         {
          blank(player[1].i,player[1].j);righttank(player[1].i,player[1].j,GREEN);player[1].way=RIGHTWAY;
         }
       }
      else
       {
        blank(player[1].i,player[1].j);righttank(player[1].i,player[1].j,GREEN);player[1].way=RIGHTWAY;
       }
      
      break;
   case b_shoot:
      if(shoot[1].life==0)
       {
        shoot[1].life=1;
        shoot[1].x=player[1].i*30+15;shoot[1].y=player[1].j*30+15;
        shoot[1].way=player[1].way;break;
       }
      else
       break;
   
   case ESC:
      goto aaa;
  }
  
   
    }
    aaa: ;
}
void map_all(int map[15][15])
{
 int i,j;
 for(i=0;i<15;i++)
  for(j=0;j<15;j++)
   switch(map[j][i])
    {
     case 0: break;
     
     case 5:map_wall(i,j);break;/*地形*/
     case 6:map_steel(i,j);break;
     case 7:map_water(i,j);break;
     case 8:map_border(i,j);break;
     case 9:map_base(i,j);break;
    }
 uptank(3,13,1);uptank(10,13,GREEN);
    
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwjzjf.html