Tanks有点问题:求助:!
#include<stdio.h>#include<conio.h>
#include<math.h>
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define UP 0x4800
#define DOWN 0x5000
#define ENTER 0x1c0d
#define ESC 0x011b
struct
{
struct add
{int x;
int y;
} a[10];
enum
{
North,South,Weast,East
}Dir;
}tank[5];
struct
{
int x;
int y;
enum
{
north,south,weast,east
}dir;
} bullets[100]={0},Bull[100]={0};
int n=0,k=0;
int m;
void loadtanks(int ) ;
int left(int ) ;
int right(int ) ;
int up(int ) ;
int down(int );
void shout(int );
void Shout();
void prin() ;
void gameover();
void begin() ;
void main()
{
int key;
int i;
int x;
int token=1;
begin();
loadtanks(0);
loadtanks(1);
loadtanks(2);
loadtanks(3);
loadtanks(4);
do{ delay(90000);
prin();
while(kbhit())
{
key=bioskey(0);
switch(key)
{ case ENTER : Shout();break;
case ESC : token=0;break;
case LEFT : left(0); break;
case RIGHT : right(0); break;
case UP : up(0); break;
case DOWN : down(0); break;
}
break;
}
for(i=1;i<=4;i++)
{
x=i*rand();
x=abs(x);
x=x/32768.0*4;
x%=4;
switch(x)
{
case 0 : left(i); break;
case 1 : right(i); break;
case 2 : up(i); break;
case 3 : down(i); break;
}
}
x=rand();
x=abs(x);
x=x/32768.0*4;
x%=4;
x++;
switch(x)
{
case 1: shout(1);break;
case 2: shout(2);break;
case 3: shout(3);break;
case 4: shout(4);break;
}
prin();
}while(token);
gameover();
}
void loadtanks(int m)
{ switch(m)
{
case 0:
tank[0].a[0].x=40; tank[0].a[0].y=10;
tank[0].a[1].x=39; tank[0].a[1].y=11;
tank[0].a[2].x=40; tank[0].a[2].y=11;
tank[0].a[3].x=41; tank[0].a[3].y=11;
tank[0].a[4].x=39; tank[0].a[4].y=12;
tank[0].a[5].x=40; tank[0].a[5].y=12;
tank[0].a[6].x=41; tank[0].a[6].y=12;
tank[0].a[7].x=39; tank[0].a[7].y=13;
tank[0].a[8].x=40; tank[0].a[8].y=13;
tank[0].a[9].x=41; tank[0].a[9].y=13;
break;
case 1:
tank[1].a[0].x=4; tank[1].a[0].y=4;
tank[1].a[1].x=1; tank[1].a[1].y=3;
tank[1].a[2].x=2; tank[1].a[2].y=3;
tank[1].a[3].x=3; tank[1].a[3].y=3;
tank[1].a[4].x=1; tank[1].a[4].y=4;
tank[1].a[5].x=2; tank[1].a[5].y=4;
tank[1].a[6].x=3; tank[1].a[6].y=4;
tank[1].a[7].x=1; tank[1].a[7].y=5;
tank[1].a[8].x=2; tank[1].a[8].y=5;
tank[1].a[9].x=3; tank[1].a[9].y=5;
break;
case 2:
tank[2].a[0].x=77; tank[2].a[0].y=4;
tank[2].a[1].x=78; tank[2].a[1].y=3;
tank[2].a[2].x=79; tank[2].a[2].y=3;
tank[2].a[3].x=80; tank[2].a[3].y=3;
tank[2].a[4].x=78; tank[2].a[4].y=4;
tank[2].a[5].x=79; tank[2].a[5].y=4;
tank[2].a[6].x=80; tank[2].a[6].y=4;
tank[2].a[7].x=78; tank[2].a[7].y=5;
tank[2].a[8].x=79; tank[2].a[8].y=5;
tank[2].a[9].x=80; tank[2].a[9].y=5;
break;
case 3:
tank[3].a[0].x=4; tank[3].a[0].y=24;
tank[3].a[1].x=1; tank[3].a[1].y=23;
tank[3].a[2].x=2; tank[3].a[2].y=23;
tank[3].a[3].x=3; tank[3].a[3].y=23;
tank[3].a[4].x=1; tank[3].a[4].y=24;
tank[3].a[5].x=2; tank[3].a[5].y=24;
tank[3].a[6].x=3; tank[3].a[6].y=24;
tank[3].a[7].x=1; tank[3].a[7].y=25;
tank[3].a[8].x=2; tank[3].a[8].y=25;
tank[3].a[9].x=3; tank[3].a[9].y=25;
break;
case 4:
tank[4].a[0].x=77; tank[4].a[0].y=24;
tank[4].a[1].x=78; tank[4].a[1].y=23;
tank[4].a[2].x=79; tank[4].a[2].y=23;
tank[4].a[3].x=80; tank[4].a[3].y=23;
tank[4].a[4].x=78; tank[4].a[4].y=24;
tank[4].a[5].x=79; tank[4].a[5].y=24;
tank[4].a[6].x=80; tank[4].a[6].y=24;
tank[4].a[7].x=78; tank[4].a[7].y=25;
tank[4].a[8].x=79; tank[4].a[8].y=25;
tank[4].a[9].x=80; tank[4].a[9].y=25;
break;
}
}
int left(int m)
{
int j=m+1;
int i,p,q;
tank[m].a[0].x=tank[m].a[4].x-1;
tank[m].a[0].y=tank[m].a[4].y;
tank[m].Dir=2;
for(p=0;p<4;p++)
{
j%=5;
for(i=1;i<=7;i+=3)
{
for(q=0;q<10;q++)
{
if((tank[m].a[i].x-1==tank[j].a[q].x&&tank[m].a[i].y==tank[j].a[q].y)||tank[m].a[i].x-1<1)
return ;
}
}
j++;
}
for(i=0;i<=10;i++)
{
tank[m].a[i].x--;
}
}
int right(int m)
{
int j=m+1;
int i,p,q;
tank[m].a[0].x=tank[m].a[6].x+1;
tank[m].a[0].y=tank[m].a[6].y;
tank[m].Dir=3;
for(p=0;p<4;p++)
{
j%=5;
for(i=3;i<=9;i+=3)
{
for(q=0;q<10;q++)
{
if((tank[m].a[i].x+1==tank[j].a[q].x&&tank[m].a[i].y==tank[j].a[q].y)||tank[m].a[i].x+1>80)
return ;
}
}
j++;
}
for(i=0;i<=10;i++)
{
tank[m].a[i].x++;
}
}
int up(int m)
{
int j=m+1;
int i,p,q;
tank[m].a[0].x=tank[m].a[2].x;
tank[m].a[0].y=tank[m].a[2].y-1;
tank[m].Dir=0;
for(p=0;p<4;p++)
{
j%=5;
for(i=1;i<=3;i++)
{
for(q=0;q<10;q++)
{
if((tank[m].a[i].x==tank[j].a[q].x&&tank[m].a[i].y-1==tank[j].a[q].y)||tank[m].a[i].y<3)
return ;
}
}
j++;
}
for(i=0;i<=10;i++)
{
tank[m].a[i].y--;
}
}
int down(int m)
{
int j=m+1;
int i,p,q;
tank[m].a[0].x=tank[m].a[8].x;
tank[m].a[0].y=tank[m].a[8].y+1;
tank[m].Dir=1;
for(p=0;p<4;p++) /*if the left have other tanks or the wall,not turn.*/
{
j%=5;
for(i=7;i<=9;i++)
{
for(q=0;q<10;q++)
{
if((tank[m].a[i].x==tank[j].a[q].x&&tank[m].a[i].y+1==tank[j].a[q].y)||tank[m].a[i].x>25)
return ;
}
}
j++;
}
for(i=0;i<=10;i++) /*go left. */
{
tank[m].a[i].y++;
}
}
void shout(int m) /*the enemies'bullets.*/
{
bullets[k].x=tank[m].a[0].x;
bullets[k].y=tank[m].a[0].y;
bullets[k].dir=tank[m].Dir;
k++;
}
void Shout()
{ /*mine bullets.*/
Bull[n].x=tank[0].a[0].x;
Bull[n].y=tank[0].a[0].y;
Bull[n].dir=tank[0].Dir;
n++;
}
void prin() /*this is print the tanksand the bullets. */
{
int i,j,p,q;
textmode(C80);
window(0,0,80,25);
clrscr();
textbackground(BLUE);
textcolor(GREEN);
gotoxy(35,1);
cprintf("Score:");
for(i=1;i<=80;i++)
{
gotoxy(i,2);
putch(4);
}
for(i=0;i<5;i++) /*print the tanks. */
{
for(j=0;j<10;j++)
{
gotoxy(tank[i].a[j].x,tank[i].a[j].y);
putch(3);
}
}
for(i=0;i<k;i++) /*print the enemies'bullets.*/
{
if(bullets[i].x<1||bullets[i].x>80||bullets[i].y<3||bullets[i].y>25)
continue;
for(j=0;j<10;j++)
{ if(bullets[i].x==tank[0].a[j].x&&bullets[i].y==tank[0].a[j].y)
gameover();
}
switch(bullets[i].dir)
{
case north: bullets[i].y--; break;
case south : bullets[i].y++; break;
case weast : bullets[i].x--; break;
case east : bullets[i].x++; break;
}
gotoxy(bullets[i].x,bullets[i].y);
putch(7);
}
for(i=0;i<n;i++) /*print mine bullets. */
{
if(Bull[i].x<1||Bull[i].x>80||Bull[i].y<3||Bull[i].y>25)
continue;
for(p=1;p<=4;p++)
{
for(j=0;j<10;j++)
{
if(Bull[i].x==tank[p].a[j].x&&Bull[i].y==tank[p].a[j].y)
{
loadtanks(p);
break;
}
}
}
switch(Bull[i].dir)
{
case north: Bull[i].y--; break;
case south : Bull[i].y++; break;
case weast : Bull[i].x--; break;
case east : Bull[i].x++; break;
}
gotoxy(Bull[i].x,Bull[i].y);
putch(7);
}
}
void gameover()
{
textmode(C80);
window(0,0,80,25);
clrscr();
textbackground(BLUE);
textcolor(GREEN);
gotoxy(35,12);
cprintf("Game Over");
getch();
}
void begin()
{
textmode(C80);
window(0,0,80,25);
textbackground(BLUE);
textcolor(GREEN);
clrscr();
gotoxy(15,10);
cprintf("Welcome to this game");
gotoxy(15,11);
cprintf("TANKS");
gotoxy(15,13);
cprintf("Are you Ready");
gotoxy(15,14);
cprintf("Let's Go!");
gotoxy(15,19) ;
cprintf("Press anykey to begin");
getch();
}