一个c++小游戏(tc30运行通过)
c++吧
全部回复
仅看楼主
level 1
主文件 game.cpp#include
// main fuction.void main(){me *myself;int gdriver=DETECT,gmode,d,i,size,x,y,number,next,kill=0,jkl=0,asd=0,nandu=0;object **ob;ob =new object*[100];printf("please tell me your level:");scanf("%d",&nandu);int baop = nandu*5;for(int g=0;g<=99;++g)ob[g]=NULL;next = 0;double j;void **baoimage,*azabao,*alifebao,*baoblack;baoimage = new void*[2];long int key;initgraph(&gdriver,&gmode,"d:\\tc30\\bgi");int baosize = imagesize(10,10,35,35);azabao = malloc(baosize);alifebao = malloc(baosize);baoblack = malloc(baosize);getimage(10,10,35,35,baoblack);setcolor(5);rectangle(10,10,35,35);setcolor(10);settextstyle(1,0,4);outtextxy(10,0,"B");getimage(10,10,35,35,azabao);setcolor(0);outtextxy(10,0,"B");setcolor(4);outtextxy(10,0,"L");getimage(10,10,35,35,alifebao);baoimage[0] = azabao;baoimage[1] = alifebao;cleardevice();setcolor(4);void *buff,*buffe,*black,*temp;black = malloc(imagesize(100,400,120,430));getimage(300,600,320,630,black);bar(20,20,40,40);setlinestyle(0,0,3);line(30,20,30,10);size=imagesize(20,10,40,40);buff = malloc(size);buffe = malloc(size);getimage(20,10,40,40,buff);myself = new me(buff,black,3,3,ob);cleardevice();bar(25,20,35,40);line(30,40,30,50);line(20,20,40,20);getimage(20,20,40,50,buffe);cleardevice();settextstyle(3,0,8);setcolor(10);cleardevice();outtextxy(100,100,"STAR WAR");setcolor(4);settextstyle(0,0,2);outtextxy(100,200,"press any key");getch();cleardevice();putimage(myself->x,myself->y,buff,XOR_PUT);ob[0]=myself;clock_t st,fir,dead;fir = clock();ob[2]=new enemy(buffe,100,0,kill/2+random(20),30,20,13,kill/20+random(4),black);int numb=0,lifeb = 3,zab;char *num;while(1){settextstyle(0,0,1);setcolor(0);sprintf(num,"you killed %5d enemy",numb);outtextxy(10,10,num);sprintf(num,"life:%3d,bomb:%3d",lifeb,zab);outtextxy(10,20,num);setcolor(13);sprintf(num,"you killed %5d enemy",kill);outtextxy(10,10,num);sprintf(num,"life:%3d,bomb:%3d",myself->life,myself->zanum);outtextxy(10,20,num);numb = kill;zab = myself->zanum;lifeb = myself->life;st = clock();key = 0;if(bioskey(1)!=0)key=bioskey(0);if(myself->isdestroyed!=1){switch(key){case 4471:myself->mup();break;case 8051:myself->mdown();break;case 7777:myself->mleft();break;case 8292:myself->mright();break;case 8807://fireif((clock()-fir)<4)break;myself->fire();fir = clock();break;case 9064:myself->diuza();break;case 4209:cleardevice();settextstyle(1,0,4);setcolor(15);outtextxy(200,300,"really exit?(y/n)");if(getch()=='y')goto end;cleardevice();}}for(i = 0;i<=99;++i){if(ob[i]!=NULL){ob[i]->move(ob,&kill);if(ob[i]->isdestroyed == 1){ // if(ob[i]->hight==30&&random(baop)==0){ // for(jkl = 1;jkl<=99&&ob[jkl]!=NULL;++jkl); // bao asd = 1+random(2); // ob[jkl]= new bao(random(640),0,25,25,3,(bproperty)asd,baoblack,baoimage[asd-1],myself,ob); // }object *k=ob[i]; //ob[i]->destroy(&ob[i]);if(i!=0)delete k;if(i==0&&ob[i]->hight>0&&ob[i]->isdestroyed==1){myself->buffer=black;};};};};if(myself->isdestroyed!=1)myself->redraw();if(ob[0]==NULL)break;if(random(130)<=kill*nandu/20){for(i=1;ob[i]!=NULL;++i);ob[i]=new enemy(buffe,random(640),0,kill*nandu/40+random(10),30,20,1+random(14),kill/50+random(4),black);}while((clock()-st)<1);}settextstyle(4,0,7);setcolor(15);outtextxy(140,100,"GAME OVER");getch();end: cleardevice();char *end;int r = int(kill/50);switch(r){case 0:end = "more carefully next time!";break;case 1:end = "good!";break;case 2:end = "very good!";break;default:end = "perfect!!";}settextstyle(1,0,5);sprintf(num,"you killed %d enemy",kill);outtextxy(10,100,num);setcolor(15);settextstyle(0,0,1);outtextxy(10,300,end);outtext("press 'q' to exit.");while(getch()!='q');}
2005年10月20日 10点10分 1
level 1
头文件 planegam.h,要放在include下#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
// class // object is a base class //zidan,enemy is son classes. //move() contains the basic operations leave to done.enum bproperty{aza=1,alife=2};enum bool{false,true};class object{public:virtual void move(object **obj,int *num){}virtual void destroy(object** o){};object(){}int hight,width,x,y,isdestroyed;clock_t time;void *black;bool ishit(object* o){bool xhit=false,yhit=false;if((o->x-x
x
width)){xhit = true;}if((o->y-y
y
hight)){yhit = true;}return xhit&&yhit;};};class me;class za;class enemy;class zidan;class des;class bao;class me:public object{public:int life,zanum;virtual void move(object **obj,int *num);me(void *buf,void *b,int l,int zan,object **obj){buffer = buf;life = l;zanum=zan;o=obj;black = b;putimage(220,400,buffer,XOR_PUT);time = clock();bbuffer = buffer;x = 300;y = 440;hight = 30;width = 20;isdestroyed=0;c = 4;};virtual void destroy(object** o);void redraw(){putimage(x,y,buffer,COPY_PUT);}void mup();void relife(){time = clock();buffer = bbuffer;x = 310;y = 450;hight =0;width = 0;++zanum;diuza();}void mdown();void mright();void mleft();void fire();void diuza();int c;object **o;void *buffer,*bbuffer;};class bao:public object{public:bproperty property;void *buffer;me *myplane;object **obj;int speed,hi,wi;bao(int px,int py,int h,int w,int spd,bproperty p,void *blk,void *buf,me *myp,object **o);virtual void move(object **o,int *num);virtual void destroy(object **o);};bao::bao(int px,int py,int h,int w,int spd,bproperty p,void *blk,void *buf,me *myp,object **o){x = px;y = py;property = p;speed = spd;obj = o;black = blk;buffer = buf;myplane = myp;isdestroyed = 0;hight = 0;width = 0;hi = h;wi = w;putimage(x,y,buffer,COPY_PUT);};void bao::move(object **o,int *num){int dx = speed-random(2*speed);int dy = speed - random(2*speed)+2;putimage(x,y,black,COPY_PUT);x+=dx;y+=dy;putimage(x,y,buffer,XOR_PUT);hight =hi;width = wi;if(ishit(o[0])){switch(property){case aza:++(myplane->zanum);break;case alife:++(myplane->life);break;}isdestroyed = 1;};if(y>480)isdestroyed = 1;hight = 0;width = 0;};void bao::destroy(object **o){putimage(x,y,black,COPY_PUT);*o = NULL;};//zidanclass des:public object{public:me *sd;des(int px,int py,int m,me *asd){sd = asd;x = px;y=py;hight = 0;width = 0;isdestroyed = 0;max =m;r = 0;};virtual void move(object **ob,int *num){setcolor(0);setfillstyle(1,0);fillellipse(x,y,r,r);if(r>=max){isdestroyed = 1;return;};r+=5;setcolor(r/5);setfillstyle(1,r/5+1);fillellipse(x,y,r,r);}virtual void destroy(object** o){*o = NULL;
2005年10月20日 10点10分 2
level 1
if(sd!=NULL){if(sd->life>=0){*o = sd;sd->relife();sd->isdestroyed = 0;}}}protected:int r,max;};class za:public object{public:za(int px,int py,void *bu,void *b){x = px;black = b;y = py;buffer = bu;hight = 0;width = 0;isdestroyed = 0;r = 0;}virtual void move(object **o,int *num){putimage(x,y,black,COPY_PUT);int mx,my;if(y<240){r+= 10;setfillstyle(1,14);fillellipse(x+10,y+15,r*2,r);hight=2*r;width=4*r;mx = x;my = y;x = x-2*r+10;y = y-r+15;for(int g=1;g<=99;++g)if(ishit(o[g])&&o[g]!=this&&o[g]->hight>0){o[g]->isdestroyed = 1;if(o[g]->hight>10)++(*num);}if(r >= 200)isdestroyed = 1;hight = width=0;x = mx;y = my;return;}y-=10;putimage(x,y,buffer,COPY_PUT);}virtual void destroy(object **o){setcolor(0);setfillstyle(1,0);fillellipse(x+10,y+15,2*r,r);*o = NULL;}protected:int r;void *buffer;};class zidan:public object{ //lenth is 3public:virtual void move(object **obj,int *num);virtual void destroy(object **o){setcolor(0);line(x,y,x,y+3);*o=new des(x,y-2,10,NULL);}zidan(int px,int py,int spd,int h,int w,int co,int p){ //h is hight,w is widthx = px;y = py;c= co;speed = spd;hight = h;width=w;point = p;sound(300);for(int as=0;as<7000;++as);nosound();isdestroyed = 0;}int c;~zidan(){setcolor(0);line(x,y,x,y-3);}protected:int speed,point;}; //mevoid me::move(object **obj,int *num){hight = 0;width = 0;if((clock()-time)>=100){hight = 30;width = 40;}}void me::destroy(object** o){putimage(x,y,black,COPY_PUT);*o = new des(x+width/2,y+hight/2,50,this);--life;};void me::mup(){putimage(x,y,black,COPY_PUT);y=y-5;putimage(x,y,buffer,XOR_PUT);};void me::mdown(){putimage(x,y,black,COPY_PUT);y=y+5;putimage(x,y,buffer,XOR_PUT);};void me::mright(){putimage(x,y,black,COPY_PUT);x=x+5;putimage(x,y,buffer,XOR_PUT);};void me::mleft(){putimage(x,y,black,COPY_PUT);x=x-5;putimage(x,y,buffer,XOR_PUT);};void me::fire(){int nex=-1;while(o[++nex]!=NULL);o[nex]=new zidan(x+10,y-4,4,3,3,c,1);};void me::diuza(){for(int w = 1;w<=99&&o[w]!=NULL;++w);if(zanum<=0)return;o[w]=new za(x,y-40,buffer,black);--zanum;}//enemyclass enemy:public object{public:enemy(void* buf,int px,int py,int fp,int h,int w,int co,int s,void *b); //fp is the possibilityof fire~enemy();void fire(object **o);virtual void move(object **obj,int *num);virtual void destroy(object **o);protected:int firep,c,sp;void *eimage;}; //the fuction in the class // fuctionvoid enemy::move(object **obj,int *num){int d = 0;if(x!=obj[0]->x)d = (obj[0]->x-x)/abs(obj[0]->x-x);//if d is bigger than 0,he should go rightif(d==0&&obj[0]->y-y>100)d = 1-random(2);if(d!=0&&obj[0]->y-y>100&&abs(x+width-obj[0]->x-obj[0]->width)<(width+obj[0]->width)/2+50-random(100))d = -d;putimage(x,y,black,COPY_PUT);x+=d*random(8);y+=3;if(ishit(obj[0])){isdestroyed = 1;obj[0]->isdestroyed = 1;return;}putimage(x,y,eimage,XOR_PUT);if(y>480){isdestroyed = 1;return;}if(random(100)<=firep)fire(obj);}void enemy::enemy(void *buf,int px,int py,int pf,int h,int w,int co,int s,void *b){eimage = buf;x = px;black = b;y = py;firep = pf;width = w;c = co;sp = s;hight = h;isdestroyed = 0;putimage(x,y,eimage,XOR_PUT);};void enemy::destroy(object **o){putimage(x,y,black,COPY_PUT);*o= new des(x+width/2,y+hight/2,50,NULL);}void enemy::fire(object **o){int d =0;if(x!=o[0]->x){d = (o[0]->x-x)/abs(o[0]->x-x);putimage(x,y,eimage,XOR_PUT);x+=d*random(8);putimage(x,y,eimage,XOR_PUT);}for(int mv=1;o[mv]!=NULL;++mv);o[mv] = new zidan(x+10,y+40,-sp-3,3,3,c,0);}void zidan::move(object **obj,int *num){object **o=obj;setcolor(0);line(x,y,x,y+3);setcolor(c);y = y-speed;line(x,y,x,y+3);for(int n=0;n<=99;++n){if(o[n] !=NULL){if(ishit(o[n])&&o[n]!=this&&o[n]->hight!=0){if(o[n]->hight>10)*num+=point;o[n]->isdestroyed = 1;isdestroyed = 1;};if((y<0)||(y>640))isdestroyed = 1;};};}
2005年10月20日 10点10分 3
level 1
tc30路径须为d:/tc30按q退出.运行时键盘小写.
2005年10月20日 10点10分 4
level 1
注释不清楚,大家凑合着看吧!
2005年10月20日 10点10分 5
level 0
大哥,你的注释不是不清楚,我根本就没看到几个,
2005年10月20日 12点10分 6
level 1
好长啊,没有注释,简直就是漫无边际啊。
2005年10月20日 14点10分 7
level 0
可否先将游戏的运行效果大致说一下?
2005年10月21日 01点10分 8
1