/* 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,300,"lihaifei"); while(!kbhit()) { setcolor(i); settextstyle(0,0,10); outtextxy(150,100,"TANK"); i++; } getch(); cleardevice(); getch(); } void play() { int key;int i;int num=0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AY : 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); }void allcircle(int i,int j)/*画坦克中圆圈的函数*/ { int x,y;x=30*i+15,y=30*j+15; arc(x+15,y+15, 90,180,8); arc(x+16,y+15, 0, 90,8); arc(x+15,y+16,180,270,8); arc(x+16,y+16,270,360,8); arc(x+15,y+15, 90,180,4); arc(x+16,y+15, 0, 90,4); arc(x+15,y+16,180,270,4); arc(x+16,y+16,270,360,4); }void uptank(int i,int j,int color){ int x,y;x=30*i+15,y=30*j+15; setcolor(color); line(x+1,y+4,x+1,y+27); line(x+7,y+4,x+7,y+27); line(x+24,y+4,x+24,y+27); line(x+30,y+4,x+30,y+27); arc(x+4,y+4,0,180,3); arc(x+4,y+27,180,360,3); arc(x+27,y+4,0,180,3); arc(x+27,y+27,180,360,3); allcircle(i,j); setfillstyle(1,color); bar(x+14,y+1,x+17,y+15); } void downtank(int i,int j,int color) { int x,y;x=30*i+15,y=30*j+15; setcolor(color); line(x+1,y+4,x+1,y+27); line(x+7,y+4,x+7,y+27); line(x+24,y+4,x+24,y+27); line(x+30,y+4,x+30,y+27); arc(x+4,y+4,0,180,3); arc(x+4,y+27,180,360,3); arc(x+27,y+4,0,180,3); arc(x+27,y+27,180,360,3); allcircle(i,j); line(x+14,y+16,x+14,y+30); line(x+15,y+16,x+15,y+30); line(x+16,y+16,x+16,y+30); line(x+17,y+16,x+17,y+30); }void lefttank(int i,int j,int color) { int x,y;x=30*i+15,y=30*j+15; setcolor(color); line(x+4,y+1,x+27,y+1); line(x+4,y+7,x+27,y+7); line(x+4,y+24,x+27,y+24); line(x+4,y+30,x+27,y+30); arc(x+4,y+4,90,270,3); arc(x+27,y+4,270,90,3); arc(x+4,y+27,90,270,3); arc(x+27,y+27,270,90,3); line(x+15,y+14,x+1,y+14); line(x+15,y+15,x+1,y+15); line(x+15,y+16,x+1,y+16); line(x+15,y+17,x+1,y+17); allcircle(i,j); }void righttank(int i,int j,int color) { int x,y;x=30*i+15,y=30*j+15; setcolor(color); line(x+4,y+1,x+27,y+1); line(x+4,y+7,x+27,y+7); line(x+4,y+24,x+27,y+24); line(x+4,y+30,x+27,y+30); arc(x+4,y+4,90,270,3); arc(x+27,y+4,270,90,3); arc(x+4,y+27,90,270,3); arc(x+27,y+27,270,90,3); line(x+16,y+14,x+30,y+14); line(x+16,y+15,x+30,y+15); line(x+16,y+16,x+30,y+16); line(x+16,y+17,x+30,y+17); allcircle(i,j); }void map_water(int i,int j) { int x,y;x=30*i+15,y=30*j+15; setfillstyle(1,9); bar(x+1,y+1,x+30,y+30); setfillstyle(1,7); bar(x+14,y+1,x+17,y+30); bar(x+1,y+14,x+30,y+17); } void map_steel(int i,int j) { int x,y;x=30*i+15,y=30*j+15; setfillstyle(1,8); bar(x+1,y+1,x+30,y+30); setfillstyle(1,15); bar(x+3,y+3,x+12,y+12); bar(x+19,y+3,x+28,y+12); bar(x+3,y+19,x+12,y+28); bar(x+19,y+19,x+28,y+28); } void map_wall(int i,int j) { int x,y;x=30*i+15,y=30*j+15; setfillstyle(1,13); bar(x+1,y+1,x+30,y+30); setcolor(15); line(x+1,y+1,x+30,y+30); line(x+1,y+30,x+30,y+1); } void end(void) { int i; getch(); for(i=0;i<640;i++) { setcolor(3); line(i,0,i,479);delay(2000); } closegraph(); printf("you killed %d enemy!
",3-enemynum); printf("Thank you very much!"); getch(); } void blank(int i,int j) { int x,y; x=30*i+15,y=30*j+15; setfillstyle(1,7); bar(x+1,y+1,x+30,y+30); } void map_border(int i,int j) { int x,y;x=30*i+15,y=30*j+15; setfillstyle(1,8); bar(x+1,y+1,x+30,y+30); } void map_base(int i,int j) { int x,y;x=30*i+15,y=30*j+15; setcolor(15); circle(x+15,y+15,10); circle(x+11,y+11,2); circle(x+19,y+11,2); arc(x+15,y+15,225,315,6); }void control_shoot(int m,int map[15][15],int i) { switch(m) { case 5:shoot[i].life=0; switch(shoot[i].way) { case UPWAY:map[(shoot[i].y-15)/30-1][(shoot[i].x-15)/30]=0;blank((shoot[i].x-15)/30,(shoot[i].y-15)/30-1);break; case DOWNWAY:map[(shoot[i].y-15)/30+1][(shoot[i].x-15)/30]=0;blank((shoot[i].x-15)/30,(shoot[i].y-15)/30+1);break; case LEFTWAY:map[(shoot[i].y-15)/30][(shoot[i].x-15)/30-1]=0;blank((shoot[i].x-15)/30-1,(shoot[i].y-15)/30);break; case RIGHTWAY:map[(shoot[i].y-15)/30][(shoot[i].x-15)/30+1]=0;blank((shoot[i].x-15)/30+1,(shoot[i].y-15)/30);break; } break; case 7:shoot[i].life=1; break; case 44:shoot[i].life=0; enemy[0].life=0;enemynum--; switch(shoot[i].way) { case UPWAY:map[(shoot[i].y-15)/30-1][(shoot[i].x-15)/30]=0;blank((shoot[i].x-15)/30,(shoot[i].y-15)/30-1);break; case DOWNWAY:map[(shoot[i].y-15)/30+1][(shoot[i].x-15)/30]=0;blank((shoot[i].x-15)/30,(shoot[i].y-15)/30+1);break; case LEFTWAY:map[(shoot[i].y-15)/30][(shoot[i].x-15)/30-1]=0;blank((shoot[i].x-15)/30-1,(shoot[i].y-15)/30);break; case RIGHTWAY:map[(shoot[i].y-15)/30][(shoot[i].x-15)/30+1]=0;blank((shoot[i].x-15)/30+1,(shoot[i].y-15)/30);break; } break; case 45:shoot[i].life=0; enemy[1].life=0;enemynum--; switch(shoot[i].way) { case UPWAY:map[(shoot[i].y-15)/30-1][(shoot[i].x-15)/30]=0;blank((shoot[i].x-15)/30,(shoot[i].y-15)/30-1);break; case DOWNWAY:map[(shoot[i].y-15)/30+1][(shoot[i].x-15)/30]=0;blank((shoot[i].x-15)/30,(shoot[i].y-15)/30+1);break; case LEFTWAY:map[(shoot[i].y-15)/30][(shoot[i].x-15)/30-1]=0;blank((shoot[i].x-15)/30-1,(shoot[i].y-15)/30);break; case RIGHTWAY:map[(shoot[i].y-15)/30][(shoot[i].x-15)/30+1]=0;blank((shoot[i].x-15)/30+1,(shoot[i].y-15)/30);break; } break; case 46:shoot[i].life=0; enemy[2].life=0;enemynum--; switch(shoot[i].way) { case UPWAY:map[(shoot[i].y-15)/30-1][(shoot[i].x-15)/30]=0;blank((shoot[i].x-15)/30,(shoot[i].y-15)/30-1);break; case DOWNWAY:map[(shoot[i].y-15)/30+1][(shoot[i].x-15)/30]=0;blank((shoot[i].x-15)/30,(shoot[i].y-15)/30+1);break; case LEFTWAY:map[(shoot[i].y-15)/30][(shoot[i].x-15)/30-1]=0;blank((shoot[i].x-15)/30-1,(shoot[i].y-15)/30);break; case RIGHTWAY:map[(shoot[i].y-15)/30][(shoot[i].x-15)/30+1]=0;blank((shoot[i].x-15)/30+1,(shoot[i].y-15)/30);break; } break; } }void enemy_control(int map[15][15]) { int i; for(i=0;i<3;i++) { enemy[i].control=random(50); if(enemy[i].life==1) { if(enemy[i].control>=0&&enemy[i].control<=9&&map[enemy[i].j-1][enemy[i].i]==0) { enemy[i].way=UPWAY; map[enemy[i].j][enemy[i].i]=0; blank(enemy[i].i,enemy[i].j); enemy[i].j--; map[enemy[i].j][enemy[i].i]=44+i; uptank(enemy[i].i,enemy[i].j,RED); } else if(enemy[i].control>=10&&enemy[i].control<=19&&map[enemy[i].j+1][enemy[i].i]==0) { enemy[i].way=DOWNWAY; map[enemy[i].j][enemy[i].i]=0; blank(enemy[i].i,enemy[i].j); enemy[i].j++; map[enemy[i].j][enemy[i].i]=44+i; downtank(enemy[i].i,enemy[i].j,RED); } else if(enemy[i].control>=20&&enemy[i].control<=29&&map[enemy[i].j][enemy[i].i-1]==0) { enemy[i].way=LEFTWAY; map[enemy[i].j][enemy[i].i]=0; blank(enemy[i].i,enemy[i].j); enemy[i].i--; map[enemy[i].j][enemy[i].i]=44+i; lefttank(enemy[i].i,enemy[i].j,RED); } else if(enemy[i].control>=30&&enemy[i].control<=39&&map[enemy[i].j][enemy[i].i+1]==0) { enemy[i].way=RIGHTWAY; map[enemy[i].j][enemy[i].i]=0; blank(enemy[i].i,enemy[i].j); enemy[i].i++; map[enemy[i].j][enemy[i].i]=44+i; righttank(enemy[i].i,enemy[i].j,RED); } else if(enemy[i].control>=40&&enemy[i].control<=49) { } } } }
收藏该网址