📄 tank.c
字号:
// this function scans the background behind a sprite so that when the sprite
// is draw, the background isnn'y obliterated
char far *work_back;
int work_offset=0,offset,y;
// alias a pointer to sprite background for ease of access
work_back = sprite->background;
// compute offset of background in video buffer
offset = (sprite->y << 8) + (sprite->y << 6) + sprite->x;
for (y=0; y<SPRITE_HEIGHT; y++)
{
// copy the next row out off screen buffer into sprite background buffer
_fmemmove((void far *)&work_back[work_offset],
(void far *)&video_buffer[offset],
SPRITE_WIDTH);
// move to next line in video buffer and in sprite background buffer
offset += SCREEN_WIDTH;
work_offset += SPRITE_WIDTH;
} // end for y
} // end Behind_Sprite
//////////////////////////////////////////////////////////////////////////////
void Erase_Sprite(sprite_ptr sprite)
{
// replace the background that was behind the sprite
// this function replaces the background that was saved from where a sprite
// was going to be placed
char far *work_back;
int work_offset=0,offset,y;
// alias a pointer to sprite background for ease of access
work_back = sprite->background;
// compute offset of background in video buffer
offset = (sprite->y << 8) + (sprite->y << 6) + sprite->x;
for (y=0; y<SPRITE_HEIGHT; y++)
{
// copy the next row out off screen buffer into sprite background buffer
_fmemmove((void far *)&video_buffer[offset],
(void far *)&work_back[work_offset],
SPRITE_WIDTH);
// move to next line in video buffer and in sprite background buffer
offset += SCREEN_WIDTH;
work_offset += SPRITE_WIDTH;
} // end for y
} // end Erase_Sprite
void Fill_Screen(int value)
{
_fmemset(video_buffer,(char)value,SCREEN_WIDTH*SCREEN_HEIGHT1+1);
}
void Clear_Key_Buffer(void)
{
int offset;
offset=peek(0x40,0x1a);
pokeb(0x40,0x1c,offset);
}
void Sheer(void)
{
long index;
int x,y;
x=rand()%320;
y=rand()%200;
for(index=0;index<100000;index++)
{
x+=17;
y+=13;
if(x>319)
x=x-319;
if(y>199)
y=y-199;
Plot_Pixel_Fast(x,y,0);
}
}
int Sprite_Collide(sprite_ptr sprite_1,sprite_ptr sprite_2)
{
int dx,dy;
dx=abs(sprite_1->x-sprite_2->x);
dy=abs(sprite_1->y-sprite_2->y);
if(dx<(SPRITE_WIDTH-(SPRITE_WIDTH>>3))&&dy<(SPRITE_HEIGHT-(SPRITE_HEIGHT>>3)))
{
return(1);
}
else
{
return(0);
}
}
int bullet_Collide(sprite_ptr sprite,bullet_ptr bullet)
{
int dx,dy;
dx=abs(sprite->x+7-bullet->x);
dy=abs(sprite->y+7-bullet->y);
if(dx*2<(SPRITE_WIDTH-(SPRITE_WIDTH>>3))&&dy*2<(SPRITE_HEIGHT-(SPRITE_HEIGHT>>3)))
{
return(1);
}
else
{
return(0);
}
}
void Sprite_Free(void)
{
now=head;
do{
Sprite_Delete(now);
now=now->next;
}while(now!=NULL);
}
int all_key=0;
void tank1(sprite_ptr sprite)
{
static int sprite_direction=0;
static float dx,dy,dxx=0,dyy=0,angle;
static int i,j=2,k,l=1;
char get_key;
l=-l;
k=0;
Erase_Sprite(sprite);
if(sprite->bullet.flag==1)
{
Plot_Pixel_Fast(sprite->bullet.x,sprite->bullet.y,sprite->bullet.color_gd);
sprite->bullet.x+=sprite->bullet.direction.x*16;
sprite->bullet.y+=sprite->bullet.direction.y*16;
if(sprite->bullet.x>336||sprite->bullet.x<-16||sprite->bullet.y>184||sprite->bullet.y<-16)
{
sprite->bullet.flag=0;
sprite->bullet.x=sprite->bullet.y=400;
}
}
if(l==1)
{
if(kbhit()||all_key!=0)
{
k=1;
dx=dy=0;
if(all_key==0)
all_key=get_key=getch();
else
{
get_key=all_key;
all_key=0;
}
if(get_key==sprite->key.up)
{
sprite_direction=0;
angle=(90+360-360/(MAX_SPRITE_FRAMES/2)*(float)sprite_direction);
dxx=dx=sprite_SPEED*cos(PI*angle/180);
dyy=dy=sprite_SPEED*sin(PI*angle/180);
}
if(get_key==sprite->key.right)
{
sprite_direction=1;
angle=(90+360-360/(MAX_SPRITE_FRAMES/2)*(float)sprite_direction);
dxx=dx=sprite_SPEED*cos(PI*angle/180);
dyy=dy=sprite_SPEED*sin(PI*angle/180);
}
if(get_key==sprite->key.down)
{
sprite_direction=2;
angle=(90+360-360/(MAX_SPRITE_FRAMES/2)*(float)sprite_direction);
dxx=dx=sprite_SPEED*cos(PI*angle/180);
dyy=dy=sprite_SPEED*sin(PI*angle/180);
}
if(get_key==sprite->key.left)
{
sprite_direction=3;
angle=(90+360-360/(MAX_SPRITE_FRAMES/2)*(float)sprite_direction);
dxx=dx=sprite_SPEED*cos(PI*angle/180);
dyy=dy=sprite_SPEED*sin(PI*angle/180);
}
if(get_key==sprite->key.quit)
{
Sprite_Delete(sprite);
Sheer();
Set_Video_Mode(TEXT_MODE);
exit(1);
}
if(get_key==sprite->key.fight)
{
if(sprite->bullet.flag!=1)
{
sprite->bullet.flag=1;
if(sprite_direction==0)
{
sprite->bullet.direction.y=-1;
sprite->bullet.direction.x=0;
sprite->bullet.x=sprite->x+7;
sprite->bullet.y=sprite->y-1;
}
else if(sprite_direction==1)
{
sprite->bullet.direction.x=1;
sprite->bullet.direction.y=0;
sprite->bullet.x=sprite->x+16;
sprite->bullet.y=sprite->y+7;
}
else if(sprite_direction==2)
{
sprite->bullet.direction.y=1;
sprite->bullet.direction.x=0;
sprite->bullet.x=sprite->x+8;
sprite->bullet.y=sprite->y+16;
}
else if(sprite_direction==3)
{
sprite->bullet.direction.x=-1;
sprite->bullet.direction.y=0;
sprite->bullet.x=sprite->x-1;
sprite->bullet.y=sprite->y+8;
}
sprite->bullet.color=15;
}
}
sprite->x+=(int)(dx+.5);
if(sprite->x>319-(int)SPRITE_WIDTH)
sprite->x=319-(int)SPRITE_WIDTH;
else if(sprite->x<0)
sprite->x=0;
sprite->y-=(int)(dy+.5);
if(sprite->y>199-(int)SPRITE_HEIGHT)
sprite->y=199-(int)SPRITE_HEIGHT;
else if(sprite->y<0)
sprite->y=0;
sprite->curr_frame=sprite_direction;
}
if(k==0)
if(get_key!=sprite->key.stop)
{
sprite->x+=(int)(dxx+.5);
if(sprite->x>319-(int)SPRITE_WIDTH)
sprite->x=319-(int)SPRITE_WIDTH;
else if(sprite->x<0)
sprite->x=0;
sprite->y-=(int)(dyy+.5);
if(sprite->y>199-(int)SPRITE_HEIGHT)
sprite->y=199-(int)SPRITE_HEIGHT;
else if(sprite->y<0)
sprite->y=0;
sprite->curr_frame=sprite_direction;
}
}
else
{
j=-j;
sprite->curr_frame=sprite_direction+2+j;
}
Behind_Sprite(sprite);
if(sprite->bullet.flag==1)
sprite->bullet.color_gd=Get_Pixel(sprite->bullet.x,sprite->bullet.y);
Draw_Sprite(sprite);
if(sprite->bullet.flag==1)
Plot_Pixel_Fast(sprite->bullet.x,sprite->bullet.y,sprite->bullet.color);
Delay(2);
}
void tank2(sprite_ptr sprite)
{
static int sprite_direction=0;
static float dx,dy,dxx=0,dyy=0,angle;
static int i,j=2,k,l=1;
char get_key;
l=-l;
k=0;
Erase_Sprite(sprite);
if(sprite->bullet.flag==1)
{
Plot_Pixel_Fast(sprite->bullet.x,sprite->bullet.y,sprite->bullet.color_gd);
sprite->bullet.x+=sprite->bullet.direction.x*16;
sprite->bullet.y+=sprite->bullet.direction.y*16;
if(sprite->bullet.x>336||sprite->bullet.x<-16||sprite->bullet.y>184||sprite->bullet.y<-16)
{
sprite->bullet.flag=0;
sprite->bullet.x=sprite->bullet.y=400;
}
}
if(l==1)
{
if(kbhit()||all_key!=0)
{
k=1;
dx=dy=0;
if(all_key==0)
all_key=get_key=getch();
else
{
get_key=all_key;
all_key=0;
}
if(get_key==sprite->key.up)
{
sprite_direction=0;
angle=(90+360-360/(MAX_SPRITE_FRAMES/2)*(float)sprite_direction);
dxx=dx=sprite_SPEED*cos(PI*angle/180);
dyy=dy=sprite_SPEED*sin(PI*angle/180);
}
if(get_key==sprite->key.right)
{
sprite_direction=1;
angle=(90+360-360/(MAX_SPRITE_FRAMES/2)*(float)sprite_direction);
dxx=dx=sprite_SPEED*cos(PI*angle/180);
dyy=dy=sprite_SPEED*sin(PI*angle/180);
}
if(get_key==sprite->key.down)
{
sprite_direction=2;
angle=(90+360-360/(MAX_SPRITE_FRAMES/2)*(float)sprite_direction);
dxx=dx=sprite_SPEED*cos(PI*angle/180);
dyy=dy=sprite_SPEED*sin(PI*angle/180);
}
if(get_key==sprite->key.left)
{
sprite_direction=3;
angle=(90+360-360/(MAX_SPRITE_FRAMES/2)*(float)sprite_direction);
dxx=dx=sprite_SPEED*cos(PI*angle/180);
dyy=dy=sprite_SPEED*sin(PI*angle/180);
}
if(get_key==sprite->key.quit)
{
Sprite_Delete(sprite);
Sheer();
Set_Video_Mode(TEXT_MODE);
exit(1);
}
if(get_key==sprite->key.fight)
{
if(sprite->bullet.flag!=1)
{
sprite->bullet.flag=1;
if(sprite_direction==0)
{
sprite->bullet.direction.y=-1;
sprite->bullet.direction.x=0;
sprite->bullet.x=sprite->x+7;
sprite->bullet.y=sprite->y-1;
}
else if(sprite_direction==1)
{
sprite->bullet.direction.x=1;
sprite->bullet.direction.y=0;
sprite->bullet.x=sprite->x+16;
sprite->bullet.y=sprite->y+7;
}
else if(sprite_direction==2)
{
sprite->bullet.direction.y=1;
sprite->bullet.direction.x=0;
sprite->bullet.x=sprite->x+8;
sprite->bullet.y=sprite->y+16;
}
else if(sprite_direction==3)
{
sprite->bullet.direction.x=-1;
sprite->bullet.direction.y=0;
sprite->bullet.x=sprite->x-1;
sprite->bullet.y=sprite->y+8;
}
sprite->bullet.color=15;
}
}
sprite->x+=(int)(dx+.5);
if(sprite->x>319-(int)SPRITE_WIDTH)
sprite->x=319-(int)SPRITE_WIDTH;
else if(sprite->x<0)
sprite->x=0;
sprite->y-=(int)(dy+.5);
if(sprite->y>199-(int)SPRITE_HEIGHT)
sprite->y=199-(int)SPRITE_HEIGHT;
else if(sprite->y<0)
sprite->y=0;
sprite->curr_frame=sprite_direction;
}
if(k==0)
if(get_key!=sprite->key.stop)
{
sprite->x+=(int)(dxx+.5);
if(sprite->x>319-(int)SPRITE_WIDTH)
sprite->x=319-(int)SPRITE_WIDTH;
else if(sprite->x<0)
sprite->x=0;
sprite->y-=(int)(dyy+.5);
if(sprite->y>199-(int)SPRITE_HEIGHT)
sprite->y=199-(int)SPRITE_HEIGHT;
else if(sprite->y<0)
sprite->y=0;
sprite->curr_frame=sprite_direction;
}
}
else
{
j=-j;
sprite->curr_frame=sprite_direction+2+j;
}
Behind_Sprite(sprite);
if(sprite->bullet.flag==1)
sprite->bullet.color_gd=Get_Pixel(sprite->bullet.x,sprite->bullet.y);
Draw_Sprite(sprite);
if(sprite->bullet.flag==1)
Plot_Pixel_Fast(sprite->bullet.x,sprite->bullet.y,sprite->bullet.color);
Delay(2);
}
void Judge_Sprite(void)
{
sprite_ptr sprite;
sprite=head;
do{
sprite->spr(sprite);
sprite=sprite->next;
}while(sprite!=NULL);
}
void Judge_Died(void)
{
sprite_ptr tank1,tank2;
tank1=head;
do{
tank2=tank1->next;
while(tank2!=NULL)
{
if(Sprite_Collide(tank1,tank2)||bullet_Collide(tank2,(bullet_ptr)&tank1->bullet)||bullet_Collide(tank1,(bullet_ptr)&tank2->bullet))
{
Sprite_Free();
Sheer();
Set_Video_Mode(TEXT_MODE);
exit(1);
}
tank2=tank2->next;
}
tank1=tank1->next;
}while(tank1!=NULL);
}
void Sprite_Build(void)
{
int index;
pcx_picture objects_pcx;
char key[2][7]={'8','2','4','6','5','q','0','w','x','a','d','s','q','z'};
PCX_Init((pcx_picture_ptr)&objects_pcx);
PCX_Load("attank2.pcx",(pcx_picture_ptr)&objects_pcx,1);
now=pre=(struct sprite *)malloc(sizeof(struct sprite_type));
head=now;
Sprite_Init(now,0,0,0,0,0,0,tank1,key[0]);
for(index=0;index<MAX_SPRITE_FRAMES;index++)
{
PCX_Grab_Bitmap((pcx_picture_ptr)&objects_pcx,now,index,index,0);
}
now=(struct sprite *)malloc(sizeof(struct sprite_type));
pre->next=now;
pre=now;
Sprite_Init(now,0,100,0,0,0,0,tank2,key[1]);
for(index=0;index<MAX_SPRITE_FRAMES;index++)
{
PCX_Grab_Bitmap((pcx_picture_ptr)&objects_pcx,now,index,index,1);
}
pre->next=NULL;
PCX_Delete((pcx_picture_ptr)&objects_pcx);
}
void main(void)
{
long index;
Set_Video_Mode(VGA256);
PCX_Load_Screen("poem.pcx",1);
Sprite_Build();
now=head;
do{
Behind_Sprite(now);
now=now->next;
}while(now!=NULL);
while(1){
Judge_Sprite();
Judge_Died();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -