高手进
c语言吧
全部回复
仅看楼主
level 5
芝芝751 楼主
帮我看看这个程序里都设计到C的什么知识,说重要的,大概给我说说,我在下边学习。多谢谢谢谢谢谢谢谢谢谢谢谢谢谢。#include
#include
#include
#include
#include
#define ESC27
#define BLACK0#
define HEIZHUAN1
#define BAIZHUAN2#
define TREE13
#define TREE24#
define TREE35
#define TREE46#
define BASE7
#define PLAYERSHOT1#
define NPCSHOT0
#define NPC10#
define NPC21
#define NPC32#
define PLAYER3
#define VIABLE1#
define NOVIABLE0
#define Y0#
define X1char MapData[72][84];char MapState[72][84];int *Base;int *Tank[4][4][2];int *Image[7];int *Shot[4];int *BirBg;int Direction[4][2]={-1,0,0,1,1,0,0,-1};int PosFirst[6][2]={1,1,1,41,1,81,70,36,70,40,70,46};int NextBg[4][3][2]={-2,-1,-2,0,-2,1,-1,2,0,2,1,2,2,1,2,0,2,-1,1,-2,0,-2,-1,-2};int TankBg[4][3][2]={1,-1,1,0,1,1,-1,-1,0,-1,1,-1,-1,-1,-1,0,-1,1,-1,1,0,1,1,1};int hei[4][2][2]={0,-1,0,1,-1,0,1,0,0,1,0,-1,1,0,-1,0};struct tank{ int d,hp,attack,define,i,si,interval,ShotInterval,type,CenterY,CenterX,now,ShotSpeed; struct tank *link;};typedef struct tank *PTANK;struct shot{ int y,x,d,attack,interval,type,i; struct shot *link;};typedef struct shot *PSHOT;PTANK NpcHead,PlayerHead;PSHOT NpcShotHead,PlayerShotHead;int *NpcBir,*PlayerBir,BNpcI,BPlayerI,BNpcY,BNpcX,BPlayerY,BPlayerX,BType,BNpcIntval,BPlayerIntval;int NowNpc=0,NowMaxNpc=6,SumNpc=30,SumNpcDead=0,chance=4,GameOver=0;int MaxNpcShot=6,NowNpcShot=0,MaxPlayerShot=4,NowPlayerShot=0;char CH='-1',str[6];FILE *fname,*fdata;void PPutImage(int *p,int l,int t,int r,int b);int *GGetImage(int l,int t,int r,int b);void DrawRetangle(int l,int t,int r,int b,int color);void ChuLiNpc(void);void ChuLiPlayer(void);void ChuLiNpcShot(void);void ChuLiPlayerShot(void);void ChuLiNpcBirth(void);void ChuLiPlayerBirth(void);void PutMap(void);void LoadMap(void);void LoadData(void);void ConstructNpc(int type);void ConstructPlayer(void);void ConstructNpcShot(PTANK p);void ConstructPlayerShot(PTANK p);int NpcBirth(void);void PlayerBirth(void);void AtiveGameOver(void);void init(void);void DrawRetangle(int l,int t,int r,int b,int color){ int x,y; for(y=t;y<=b;y++){ for(x=l;x<=r;x++){ putpixel(x,y,color); } }}int main(){int gm=DETECT,gd,i,y,x,*p,type,d,n;if((fname=fopen("name","r"))==NULL){printf("cannot open map file!\n");getch();exit(0);}printf("open map file success!\n");if((fdata=fopen("data","r"))==NULL){printf("cannot open data file!\n");getch();exit(0);}printf("open data file success!\n");getch();LoadData();initgraph(&gm,&gd,"");while(!GameOver) {LoadMap();init();PutMap();getch();while(!GameOver){ChuLiPlayerBirth();ChuLiNpcBirth();ChuLiPlayerShot();ChuLiNpcShot();ChuLiNpc();ChuLiPlayer();if(chance==0) GameOver=1;if(SumNpcDead==SumNpc) break;if(bioskey(1)) CH=bioskey(0);delay(2);}if(GameOver==1) {getch();break;}
2007年05月12日 08点05分 1
level 5
芝芝751 楼主
else getch();}getch();fclose(fname);fclose(fdata);closegraph();printf("SumNpcDead:%d\nchance:%d\nGameOver:%d\n",SumNpcDead,chance,GameOver);getch();return 0;}void PPutImage(int *p,int l,int t,int r,int b){ int x,y,i=0; for(y=t;y<=b;y++){ for(x=l;x<=r;x++,i++){ putpixel(x,y,*(p+i)); } }}int *GGetImage(int l,int t,int r,int b){ int x,y,i=0,*p; p=(int *)malloc((r-l+1)*(b-t+1)); for(y=t;y<=b;y++){ for(x=l;x<=r;x++,i++){ p[i]=getpixel(x,y); } } return p;}void LoadMap(void){ int data,y,x,yy,xx; char name[11]; fscanf(fname,"%s\n",name); for(y=0;y<18;y++){ for(x=0;x<21;x++){ fscanf(fname,"%d,",&data); if(data==0) { for(yy=0;yy<4;yy++){ for(xx=0;xx<4;xx++){ MapData[y*4+yy][x*4+xx]=HEIZHUAN; MapState[y*4+yy][x*4+xx]=NOVIABLE; } } }if(data==1) {for(yy=0;yy<4;yy++){for(xx=0;xx<4;xx++){MapData[y*4+yy][x*4+xx]=BAIZHUAN;MapState[y*4+yy][x*4+xx]=NOVIABLE;}}}if(data==2) {for(yy=0;yy<4;yy++){for(xx=0;xx<4;xx++){if(xx==0|xx==2) {MapData[y*4+yy][x*4+xx]=BAIZHUAN;MapState[y*4+yy][x*4+xx]=NOVIABLE;}else {MapData[y*4+yy][x*4+xx]=BLACK;MapState[y*4+yy][x*4+xx]=VIABLE;}}}}if(data==3) {for(yy=0;yy<4;yy++){for(xx=0;xx<4;xx++){if(yy==0|yy==2) {MapData[y*4+yy][x*4+xx]=BAIZHUAN;MapState[y*4+yy][x*4+xx]=NOVIABLE;}else {MapData[y*4+yy][x*4+xx]=BLACK;MapState[y*4+yy][x*4+xx]=VIABLE;}}}}if(data==4) {for(yy=0;yy<4;yy++){for(xx=0;xx<4;xx++){if((yy==0||yy==2)&&(xx==0||xx==2)) MapData[y*4+yy][x*4+xx]=TREE1;if((yy==0||yy==2)&&(xx==1||xx==3)) MapData[y*4+yy][x*4+xx]=TREE2;if((yy==1||yy==3)&&(xx==0||xx==2)) MapData[y*4+yy][x*4+xx]=TREE3;if((yy==1||yy==3)&&(xx==1||xx==3)) MapData[y*4+yy][x*4+xx]=TREE4;MapState[y*4+yy][x*4+xx]=VIABLE;}}}if(data==5) { for(yy=0;yy<4;yy++){ for(xx=0;xx<4;xx++){ MapData[y*4+yy][x*4+xx]=BLACK; MapState[y*4+yy][x*4+xx]=VIABLE; } } }fscanf(fname,"\n");} } if(!feof(fname)) ; else rewind(fname);}void LoadData(void){ int *p,type,d,n,y,x,data;char str[10]; for(type=0;type<4;type++){ for(d=0;d<4;d++){ for(n=0;n<2;n++){ Tank[type][d][n]=(int *)malloc(18*18*2); fscanf(fdata,"%s\n",str); p=Tank[type][d][n]; for(y=0;y<18;y++){ for(x=0;x<18;x++){fscanf(fdata,"%d,",&data);*p=data;p++; } fscanf(fdata,"\n"); } } } } for(n=0;n<7;n++){ Image[n]=(int *)malloc(6*6*2); fscanf(fdata,"%s\n",str); p=Image[n]; for(y=0;y<6;y++){ for(x=0;x<6;x++){fscanf(fdata,"%d,",&data);*p=data;p++; } fscanf(fdata,"\n"); } } for(n=0;n<4;n++){ Shot[n]=(int *)malloc(6*6*2); fscanf(fdata,"%s\n",str);
2007年05月12日 08点05分 2
level 5
芝芝751 楼主
PlayerBir=Tank[PLAYER][0][0]; BPlayerY=6*(PosFirst[3][Y]+6);BPlayerX=6*(PosFirst[3][X]
+3
);BPlayerI=0;chance--;BPlayerIntval=0; PlayerHead->link=(PTANK)malloc(sizeof(struct tank));}void ChuLiNpc(void){ PTANK NpcP,p; int yl,xl,yr,xr,yc,xc,i,j,y,x,ima,yt,data,k=1; NpcP=NpcHead->link; p=PlayerHead->link; while(NpcP!=NULL){ if(NpcP->si<=NpcP->ShotInterval) NpcP->si++; else { if(NowNpcShot
si=0; } } if(NpcP->i!=NpcP->interval) { NpcP->i++;NpcP=NpcP->link;continue; } else {NpcP->i=0;yl=NpcP->CenterY+NextBg[NpcP->d][0][Y];xl=NpcP->CenterX+NextBg[NpcP->d][0][X];yc=NpcP->CenterY+NextBg[NpcP->d][1][Y];xc=NpcP->CenterX+NextBg[NpcP->d][1][X];yr=NpcP->CenterY+NextBg[NpcP->d][2][Y];xr=NpcP->CenterX+NextBg[NpcP->d][2][X];if(yl>=0&&yl<=71&&xl>=0&&xl<=83&&yr>=0&&yr<=71&&xr>=0&&xr<=83&&MapState[yl][xl]==VIABLE&&MapState[yr][xr]==VIABLE&&MapState[yc][xc]==VIABLE){for(i=0;i<3;i++){yt=NpcP->CenterY+TankBg[NpcP->d][i][Y];xl=NpcP->CenterX+TankBg[NpcP->d][i][X];data=MapData[yt][xl];MapState[yt][xl]=VIABLE;yt=6*(yt+4);xl=6*(xl+4);PPutImage(Image[data],xl,yt,xl+5,yt+5);}NpcP->CenterY+=Direction[NpcP->d][Y];NpcP->CenterX+=Direction[NpcP->d][X];yl=NpcP->CenterY-1;xl=NpcP->CenterX-1;for(y=yl;y<=yl+2;y++){for(x=xl;x<=xl+2;x++){MapState[y][x]=NOVIABLE;}}yl=6*(yl+4);xl=6*(xl+4);PPutImage(Tank[NpcP->type][NpcP->d][NpcP->now],xl,yl,xl+17,yl+17);if(NpcP->now==0) NpcP->now=1;else NpcP->now=0;for(yl=NpcP->CenterY-1;yl<=NpcP->CenterY+1;yl++){for(xl=NpcP->CenterX-1;xl<=NpcP->CenterX+1;xl++){data=MapData[yl][xl];if(data>=TREE1&&data<=TREE4) {xr=6*(xl+4);yr=6*(yl+4);PPutImage(Image[data],xr,yr,xr+5,yr+5);}}}}else {NpcP->d=random(4);yl=NpcP->CenterY-1;xl=NpcP->CenterX-1;yl=6*(yl+4);xl=6*(xl+4);PPutImage(Tank[NpcP->type][NpcP->d][NpcP->now],xl,yl,xl+17,yl+17);if(NpcP->now==0) NpcP->now=1;else NpcP->now=0;}if(p!=NULL){if(NpcP->CenterY>=p->CenterY-1&&NpcP->CenterY<=p->CenterY+1){if(NpcP->CenterX
CenterX) {i=NpcP->CenterX+1;j=p->CenterX;}else {i=p->CenterX+1;j=NpcP->CenterX;}for(xl=i;xl
CenterY][xl]==VIABLE) ;else {k=0;break;}}if(k==1) {if(NpcP->CenterX
CenterX) NpcP->d=1;else NpcP->d=3;}}else if(NpcP->CenterX>=p->CenterX-1&&NpcP->CenterX<=p->CenterX+1){if(NpcP->CenterY
CenterY) {i=NpcP->CenterY+1;j=p->CenterY;}else {i=p->CenterY+1;j=NpcP->CenterY;}for(yl=i;yl
CenterX]==VIABLE) ;else {k=0;break;}}if(k==1) {if(NpcP->CenterY
CenterY) NpcP->d=2;else NpcP->d=1;}}}NpcP=NpcP->link; } }}void ChuLiPlayer(void){ PTANK p; int data,yl,xl,yc,xc,yr,xr,yt,OldD,i,y,x;char str[6];
2007年05月12日 08点05分 4
level 5
芝芝751 楼主
p=PlayerHead->link; if(p==NULL) return ; if(p->si
ShotInterval) p->si++; OldD=p->d; if(CH=='i') { if(p->si==p->ShotInterval) { if(NowPlayerShot
si=0;CH='-1';return; } else CH='-1'; } return; } else if(CH=='w') {p->d=0;CH='-1';} else if(CH=='s') {p->d=2;CH='-1';} else if(CH=='a') {p->d=3;CH='-1';} else if(CH=='d') {p->d=1;CH='-1';} else if(CH==ESC) {GameOver=1;return ;} else return ; if(OldD!=p->d) { yl=p->CenterY-1;xl=p->CenterX-1; yl=6*(yl+4);xl=6*(xl+4); PPutImage(Tank[3][p->d][p->now],xl,yl,xl+17,yl+17); if(p->now==0) p->now=1;else p->now=0; } else { yl=p->CenterY+NextBg[p->d][0][Y]; xl=p->CenterX+NextBg[p->d][0][X]; yc=p->CenterY+NextBg[p->d][1][Y]; xc=p->CenterX+NextBg[p->d][1][X]; yr=p->CenterY+NextBg[p->d][2][Y]; xr=p->CenterX+NextBg[p->d][2][X]; if(yl>=0&&yl<=71&&xl>=0&&xl<=83&&yr>=0&&yr<=71&&xr>=0&&xr<=83&&MapState[yl][xl]==VIABLE&&MapState[yr][xr]==VIABLE&&MapState[yc][xc]==VIABLE){ for(i=0;i<3;i++){yt=p->CenterY+TankBg[p->d][i][Y];xl=p->CenterX+TankBg[p->d][i][X];data=MapData[yt][xl];MapState[yt][xl]=VIABLE;yt=6*(yt+4);;xl=6*(xl+4);PPutImage(Image[data],xl,yt,xl+5,yt+5);}p->CenterY+=Direction[p->d][Y];p->CenterX+=Direction[p->d][X];yl=p->CenterY-1;xl=p->CenterX-1;for(y=yl;y<=yl+2;y++){for(x=xl;x<=xl+2;x++){MapState[y][x]=NOVIABLE;}}yl=6*(yl+4);xl=6*(xl+4);PPutImage(Tank[3][p->d][p->now],xl,yl,xl+17,yl+17);if(p->now==0) p->now=1;else p->now=0;for(yl=p->CenterY-1;yl<=p->CenterY+1;yl++){for(xl=p->CenterX-1;xl<=p->CenterX+1;xl++){data=MapData[yl][xl];if(data>=TREE1&&data<=TREE4) {xr=6*(xl+4);yr=6*(yl+4);PPutImage(Image[data],xr,yr,xr+5,yr+5);}}} } else { yl=p->CenterY-1;xl=p->CenterX-1; yl=6*(yl+4);xl=6*(xl+4); PPutImage(Tank[3][p->d][p->now],xl,yl,xl+17,yl+17); if(p->now==0) p->now=1;else p->now=0; } }}void ChuLiNpcShot(void){ PTANK p; PSHOT sptr,sp; int y,x,yn,xn,data,DataNext,ynext,xnext,k=0,i; sptr=NpcShotHead; sp=NpcShotHead->link; while(sp!=NULL){ if(sp->i!=sp->interval) {sp->i++;sptr=sp;sp=sp->link;continue;} else sp->i=0; k=0;yn=sp->y+Direction[sp->d][Y];xn=sp->x+Direction[sp->d][X];data=MapData[sp->y][sp->x];DataNext=MapData[yn][xn];y=6*(sp->y+4);x=6*(sp->x+4);ynext=6*(yn+4);xnext=6*(xn+4);if(yn<0||yn>71||xn<0||xn>83) {NowNpcShot--;PPutImage(Image[data],x,y,x+5,y+5);sptr->link=sp->link;free(sp);sp=sptr->link;continue;}else if(DataNext==BLACK) {PPutImage(Image[data],x,y,x+5,y+5);sp->y=yn;sp->x=xn;PPutImage(Shot[sp->d],xnext,ynext,xnext+5,ynext+5);}else if(DataNext==HEIZHUAN) {NowNpcShot--;PPutImage(Image[data],x,y,x+5,y+5);MapData[yn][xn]=BLACK;MapState[yn][xn]=VIABLE;
2007年05月12日 08点05分 5
level 5
芝芝751 楼主
PPutImage(Image[BLACK],xnext,ynext,xnext+5,ynext+5);for(i=0;i<2;i++){y=yn+hei[sp->d][i][Y];x=xn+hei[sp->d][i][X];if(MapData[y][x]==HEIZHUAN) {MapData[y][x]=BLACK;MapState[y][x]=VIABLE;ynext=6*(y+4);xnext=6*(x+4);PPutImage(Image[BLACK],xnext,ynext,xnext+5,ynext+5);}}sptr->link=sp->link;free(sp);sp=sptr->link;continue;}else if(DataNext==BAIZHUAN) {NowNpcShot--;PPutImage(Image[data],x,y,x+5,y+5);sptr->link=sp->link;free(sp);sp=sptr->link;continue;}else if(DataNext>=TREE1&&DataNext<=TREE4) {PPutImage(Image[data],x,y,x+5,y+5);sp->y=yn;sp->x=xn;}else if(DataNext==BASE) {GameOver=1;AtiveGameOver();break;}p=PlayerHead->link;if(p!=NULL) {if(yn>=p->CenterY-1&&yn<=p->CenterY+1&&xn>=p->CenterX-1&&xn<=p->CenterX+1) {for(y=p->CenterY-1;y<=p->CenterY+1;y++){for(x=p->CenterX-1;x<=p->CenterX+1;x++){data=MapData[y][x];MapState[y][x]=VIABLE;PPutImage(Image[data],6*(x+4),6*(y+4),6*(x+4)+5,6*(y+4)+5);}}free(p);k=1;PlayerHead->link=NULL;}}if(k==1) {NowNpcShot--;sptr->link=sp->link;free(sp);sp=sptr->link;}else {sptr=sp;sp=sp->link;} }}void ChuLiPlayerShot(void){PTANK p,ptr; PSHOT sptr,sp,pptr,pp; int y,x,yn,xn,data,DataNext,ynext,xnext,k=0,i;sptr=PlayerShotHead; sp=PlayerShotHead->link;while(sp!=NULL){ if(sp->i!=sp->interval) {sp->i++;sptr=sp;sp=sp->link;continue;} else sp->i=0;k=0;yn=sp->y+Direction[sp->d][Y];xn=sp->x+Direction[sp->d][X];data=MapData[sp->y][sp->x];DataNext=MapData[yn][xn];y=6*(sp->y+4);x=6*(sp->x+4);ynext=6*(yn+4);xnext=6*(xn+4);if(yn<0||yn>71||xn<0||xn>83) {NowPlayerShot--;PPutImage(Image[data],x,y,x+5,y+5);sptr->link=sp->link;free(sp);sp=sptr->link;continue;}else if(DataNext==BLACK) {PPutImage(Image[data],x,y,x+5,y+5);sp->y=yn;sp->x=xn;PPutImage(Shot[sp->d],xnext,ynext,xnext+5,ynext+5);}else if(DataNext==HEIZHUAN) {NowPlayerShot--;PPutImage(Image[data],x,y,x+5,y+5);MapData[yn][xn]=BLACK;MapState[yn][xn]=VIABLE;PPutImage(Image[BLACK],xnext,ynext,xnext+5,ynext+5);for(i=0;i<2;i++){y=yn+hei[sp->d][i][Y];x=xn+hei[sp->d][i][X];if(MapData[y][x]==HEIZHUAN) {MapData[y][x]=BLACK;MapState[y][x]=VIABLE;ynext=6*(y+4);xnext=6*(x+4);PPutImage(Image[BLACK],xnext,ynext,xnext+5,ynext+5);}}sptr->link=sp->link;free(sp);sp=sptr->link;continue;}else if(DataNext==BAIZHUAN) {NowPlayerShot--;PPutImage(Image[data],x,y,x+5,y+5);sptr->link=sp->link;free(sp);sp=sptr->link;continue;}else if(DataNext>=TREE1&&DataNext<=TREE4) {PPutImage(Image[data],x,y,x+5,y+5);sp->y=yn;sp->x=xn;}else if(DataNext==BASE) {GameOver=1;AtiveGameOver();break;}p=NpcHead->link;ptr=NpcHead;while(p!=NULL){if(yn>=p->CenterY-1&&yn<=p->CenterY+1&&xn>=p->CenterX-1&&xn<=p->CenterX+1) {for(y=p->CenterY-1;y<=p->CenterY+1;y++){
2007年05月12日 08点05分 6
level 5
芝芝751 楼主
for(x=p->CenterX-1;x<=p->CenterX+1;x++){data=MapData[y][x];MapState[y][x]=VIABLE;PPutImage(Image[data],6*(x+4),6*(y+4),6*(x+4)+5,6*(y+4)+5);}}SumNpcDead++;NowNpc--;k=1;ptr->link=p->link;free(p);p=ptr->link;break;}else {ptr=p;p=p->link;}}if(k==1) {NowPlayerShot--;sptr->link=sp->link;free(sp);sp=sptr->link;}else {sptr=sp;sp=sp->link;}}}void ChuLiNpcBirth(void){if((SumNpcDead+NowNpc)
link==NULL) PlayerBirth();else if(BPlayerIntval==6000) {BPlayerIntval=0;if(BPlayerI!=4){PPutImage(PlayerBir,BPlayerX,BPlayerY,BPlayerX+17,BPlayerY+17);PPutImage(BirBg,BPlayerX,BPlayerY+18,BPlayerX+17,BPlayerY+23);BPlayerI++;BPlayerY-=6;if(BPlayerI==4) ConstructPlayer();}}else BPlayerIntval++;}void PutMap(void){int YW,XD,YS,PosYW,PosYS,PosXD,DataW,DataS,y,x,i;int pos[11][2]={71,39,70,39,69,39,68,39,68,40,68,41,68,42,68,43,69,43,70,43,71,43};for(y=0;y<480;y++){for(x=0;x<640;x++){putpixel(x,y,6);}}for(YW=35,YS=36;YW>=0;YW--,YS++){for(XD=0;XD<84;XD++){PosYW=6*(YW+4);PosYS=6*(YS+4);PosXD=6*(XD+4);DataW=MapData[YW][XD];DataS=MapData[YS][XD];PPutImage(Image[DataW],PosXD,PosYW,PosXD+5,PosYW+5);PPutImage(Image[DataS],PosXD,PosYS,PosXD+5,PosYS+5);}}y=69;x=40;y=6*(y+4);x=6*(x+4);PPutImage(Base,x,y,x+17,y+17);for(YW=69;YW<=71;YW++){for(XD=40;XD<=41;XD++){MapState[YW][XD]=NOVIABLE;MapData[YW][XD]=BASE;}}for(i=0;i<11;i++){y=pos[i][0];x=pos[i][1];MapState[y][x]=NOVIABLE;MapData[y][x]=HEIZHUAN;y=6*(y+4);x=6*(x+4);PPutImage(Image[1],x,y,x+5,y+5);}}void AtiveGameOver(void){outtextxy(550,250,"gameover");}void init(void){NpcHead=(PTANK)malloc(sizeof(struct tank));PlayerHead=(PTANK)malloc(sizeof(struct tank));NpcHead->link=NULL;PlayerHead->link=NULL;NpcShotHead=(PSHOT)malloc(sizeof(struct shot));PlayerShotHead=(PSHOT)malloc(sizeof(struct shot));NpcShotHead->link=NULL;PlayerShotHead->link=NULL;NowNpc=0,NowMaxNpc=12,SumNpc=40,SumNpcDead=0,chance=4,GameOver=0; MaxNpcShot=24,NowNpcShot=0,MaxPlayerShot=10,NowPlayerShot=0;}
2007年05月12日 08点05分 7
level 0
加我QQ364224493我是作者
2007年05月12日 09点05分 8
level 0
动画原理碰撞检测单链表的添加,删除指针数组
2007年05月12日 09点05分 9
level 0
文本文件读取计时图像输出与擦除
2007年05月12日 09点05分 10
level 0
http://post.baidu.com/f?kz=192276136代码解说链接
2007年05月12日 09点05分 11
1