⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tank.h

📁 C语言游戏编程
💻 H
📖 第 1 页 / 共 2 页
字号:
#define SPRITE_SPEED 4
#define PI (float)3.14159

#define CHAR_HEIGHT 8
#define CHAR_WIDTH 8

#define sprite_SPEED 4

#define SPRITE_DEAD 0
#define SPRITE_ALIVE 1

#define TANK_SPEED 4

#define SPRITE_WIDTH 16
#define SPRITE_HEIGHT 16

#define MAX_SPRITE_FRAMES 8

typedef struct direct_type
{
int x;
int y;
}direct;
typedef struct bullet_type
{
int x;
int y;
char color;
char color_gd;
direct direction;
int flag;
}bullet,*bullet_ptr;




typedef struct key
{
char up;
char down;
char left;
char right;
char stop;
char quit;
char fight;
}key,key_ptr;

typedef struct sprite_type
{
int x,y;
int x_old,y_old;
int width,height;
int anim_clock;
int anim_speed;
int motion_clock;
int motion_speed;
char far *frames[MAX_SPRITE_FRAMES];
int curr_frame;
int num_frames;
int state;
char far *background;
void far *extra_data;
struct sprite_type *next;
void (far *spr)(struct sprite_type *spr);
key key;
bullet bullet;
}sprite,*sprite_ptr;

sprite_ptr head,now,pre;


void PCX_Grab_Bitmap(pcx_picture_ptr image,
		    sprite_ptr sprite,
		    int sprite_frame,
		    int grab_x, int grab_y)

{
// this function will grap a bitmap from the pcx frame buffer. it uses the
// convention that the 320x200 pixel matrix is sub divided into a smaller
// matrix of 12x8 adjacent squares each being a 24x24 pixel bitmap
// the caller sends the pcx picture along with the sprite to save the image
// into and the frame of the sprite.  finally, the position of the bitmap
// that should be grabbed is sent

int x_off,y_off, x,y, index;
char far *sprite_data;

// first allocate the memory for the sprite in the sprite structure

sprite->frames[sprite_frame] = (char far *)malloc(SPRITE_WIDTH * SPRITE_HEIGHT);

// create an alias to the sprite frame for ease of access

sprite_data = sprite->frames[sprite_frame];

// now load the sprite data into the sprite frame array from the pcx picture

// we need to find which bitmap to scan, remember the pcx picture is really a
// 12x8 matrix of bitmaps where each bitmap is 24x24 pixels. note:0,0 is upper
// left bitmap and 11,7 is the lower right bitmap.

x_off = (SPRITE_WIDTH) * grab_x;
y_off = (SPRITE_HEIGHT) * grab_y ;

// compute starting y address

y_off = y_off * 320;

for (y=0; y<SPRITE_HEIGHT; y++)
    {

    for (x=0; x<SPRITE_WIDTH; x++)
	{

	// get the next byte of current row and place into next position in
	// sprite frame data buffer

	sprite_data[y*SPRITE_WIDTH + x] = image->buffer[y_off + x_off + x];

	} // end for x

	// move to next line of picture buffer

	y_off+=320;

    } // end for y

// increment number of frames

sprite->num_frames++;

// done!, let's bail!

} // end PCX_Grap_Bitmap



void Sprite_Init(sprite_ptr sprite,int x,int y,int ac,int as,int mc,int ms,void (far *spr),char key[7])
{
// this function initializes a sprite with the sent data

int index;

sprite->x            = x;
sprite->y            = y;
sprite->x_old        = x;
sprite->y_old        = y;
sprite->width        = SPRITE_WIDTH;
sprite->height       = SPRITE_HEIGHT;
sprite->anim_clock   = ac;
sprite->anim_speed   = as;
sprite->motion_clock = mc;
sprite->motion_speed = ms;
sprite->curr_frame   = 0;
sprite->state        = SPRITE_DEAD;
sprite->num_frames   = 0;
sprite->background   = (char far *)malloc(SPRITE_WIDTH * SPRITE_HEIGHT+1);
sprite->spr=spr;
sprite->key.up=key[0];
sprite->key.down=key[1];
sprite->key.left=key[2];
sprite->key.right=key[3];
sprite->key.stop=key[4];
sprite->key.quit=key[5];
sprite->key.fight=key[6];
// set all bitmap pointers to null

for (index=0; index<MAX_SPRITE_FRAMES; index++)
    sprite->frames[index] = NULL;

} // end Sprite_Init

//////////////////////////////////////////////////////////////////////////////

void Sprite_Delete(sprite_ptr sprite)
{
// this function deletes all the memory associated with a sprire

int index;

farfree(sprite->background);

// now de-allocate all the animation frames

for (index=0; index<MAX_SPRITE_FRAMES; index++)
    farfree(sprite->frames[index]);
free(sprite);
} // end Sprite_Delete

void Draw_Sprite(sprite_ptr sprite)
{

// this function draws a sprite on the screen row by row very quickly
// note the use of shifting to implement multplication

char far *work_sprite;
int work_offset=0,offset,x,y;
//unsigned char data;

// alias a pointer to sprite for ease of access

work_sprite = sprite->frames[sprite->curr_frame];

// compute offset of sprite in video buffer

offset = ((sprite->y) << 8) + ((sprite->y) << 6) + sprite->x;

for (y=0; y<SPRITE_HEIGHT; y++)
    {
    // copy the next row into the screen buffer using memcpy for speed

    for (x=0; x<SPRITE_WIDTH; x++)
	{

	// test for transparent pixel i.e. 0, if not transparent then draw
      if(work_sprite[work_offset+x])

	 //data==(unsigned char)work_sprite[work_offset+x];
// if(data)
//	     video_buffer[offset+x] =data;
     video_buffer[offset+x]=work_sprite[work_offset+x];
	} // end for x

    // move to next line in video buffer and in sprite bitmap buffer

    offset      += SCREEN_WIDTH;
    work_offset += SPRITE_WIDTH;

    } // end for y

} // end Draw_Sprite


void Behind_Sprite(sprite_ptr sprite)
{

// 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


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;
}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -