bubble.c
资源名称:泡泡龙源代码.zip [点击查看]
上传用户:junwei_58
上传日期:2007-05-15
资源大小:79k
文件大小:8k
源码类别:
其他智力游戏
开发平台:
Visual C++
- void exitgame(){
- smoothout();
- returnMode();
- printf("nThank you for playing the game!!!nn");
- printf("Programmed by Group. Birdmen 2002.5n");
- printf("n");
- printf("Press any key to return to your system...n");
- getch_Key();
- uninstallKeyFunc();
- uninstallTimer();
- exit(1);
- }
- void seekActions(){
- AGAIN:
- while(!gameover&&keyboard.status==READY){PixelDance();ob->move();}
- if(keyboard.status==PRESS){
- keyboard.status=READY;
- switch(keyboard.recentPressKey){// to pressed key
- case KEY_EXIT: gameover=3;break;//EscMenu.key();break;
- case KEY_UP: ob->turn(MID);break;
- case KEY_LEFT: ob->turn(LEFT);break;
- case KEY_RIGHT:ob->turn(RIGHT);break;
- /* case KEY_w: if(players==1)ob2->turn(MID);break;
- case KEY_a: if(players==1)ob2->turn(LEFT);break;
- case KEY_d: if(players==1)ob2->turn(RIGHT);break;
- case KEY_s: if(players==1)ob2->eject();break;*/
- default:;
- }
- }else
- if(keyboard.status==RELEASE){
- keyboard.status=READY;
- switch(keyboard.recentReleaseKey){
- case KEY_UP:
- case KEY_LEFT:
- case KEY_RIGHT:ob->turn(RELEASE);break;
- case KEY_DOWN: ob->eject();break;
- /* case KEY_w:
- case KEY_a:
- case KEY_d: if(players==1)ob2->turn(RELEASE);break;*/
- default:break;
- }
- }
- if(!gameover)goto AGAIN;
- }
- void isGameEnd(){
- int i,j,c=0;
- // win?
- for(i=0;i<TABLEWIDTH;i++)if(table[i][0]==NONE_B)c++;
- if(c==TABLEWIDTH){
- gameover=1; //win
- return;
- }
- // check color number
- c=0;
- for(i=0;i<16;i++)colortable[i]=0;
- for(j=0;j<TABLEHEIGHT;j++){
- for(i=0;i<TABLEWIDTH;i++){
- if(table[i][j]!=NONE_B&&colortable[table[i][j]]==0){
- colortable[table[i][j]]=1;
- c++;
- if(c==colorNum)goto END2;
- }
- }
- }
- if(c<colorNum)colorNum=c;
- // lost ?
- END2:
- for(i=0;i<TABLEWIDTH;i++)
- if(table[i][16]!=NONE_B){
- gameover=2; //lost
- return;
- }
- return;
- }
- inline int getballx(int i,int j){
- return (i*BALLSIZE+XMAGIN+(j%2+1)*BALLSIZE/2);
- }
- inline int getbally(int j){
- return (j*(BALLSIZE-2)+YMAGIN+BALLSIZE/2);
- }
- inline int getballi(int x,int j){
- int n=(x-XMAGIN-(j%2+1)*BALLSIZE/2)%BALLSIZE;
- int ir=(x-XMAGIN-(j%2+1)*BALLSIZE/2)/BALLSIZE;
- if(n>BALLSIZE/2)ir++;
- return ir;
- }
- inline int getballj(int y){
- int n=(y-YMAGIN-BALLSIZE/2)%(BALLSIZE-2);
- int jr=(y-YMAGIN-BALLSIZE/2)/(BALLSIZE-2);
- if(n>BALLSIZE/2)jr++;
- return jr;
- }
- int extsymble(int i,int j){
- int n;
- if(i>=0&&j>=0&&j<TABLEHEIGHT&&i<TABLEWIDTH&&
- table[i][j]==tempBall&&tableT[i][j]==0){
- tableT[i][j]=1;
- n=1+extsymble(i+1,j)+extsymble(i-1,j);
- if(j%2==1){
- n+=extsymble(i,j-1)+extsymble(i+1,j-1)+
- extsymble(i,j+1)+extsymble(i+1,j+1);
- }else{
- n+=extsymble(i-1,j-1)+extsymble(i,j-1)+
- extsymble(i-1,j+1)+extsymble(i,j+1);
- }
- return n;
- }else return 0;
- }
- //* debug use
- void print(int i,int j,int n){
- char str[3];
- sprintf(str,"%2d",n);
- textxy(i,j,str);
- }// */
- void drawball(int i,int j){
- int c;
- if(i>=0&&j>=0&&j<TABLEHEIGHT&&i<TABLEWIDTH){
- if(table[i][j])c=table[i][j]*10+1;
- else c=0;
- circleB(getballx(i,j),getbally(j),BALLSIZE/2-3,c);
- // print(getballx(i,j)-6,getbally(j),table[i][j]);
- }
- }
- int extsymble2(int i,int j){
- int n;
- if(i>=0&&j>=0&&j<TABLEHEIGHT&&i<TABLEWIDTH&&
- table[i][j]!=NONE_B&&tableT[i][j]==0){
- tableT[i][j]=1;
- n=1+extsymble2(i+1,j)+extsymble2(i-1,j);
- if(j%2==1){
- n+=extsymble2(i,j-1)+extsymble2(i+1,j-1)+
- extsymble2(i,j+1)+extsymble2(i+1,j+1);
- }else{
- n+=extsymble2(i-1,j-1)+extsymble2(i,j-1)+
- extsymble2(i-1,j+1)+extsymble2(i,j+1);
- }
- return n;
- }else return 0;
- }
- int searchFall(){
- int i,j,n=0;
- for(j=0;j<TABLEHEIGHT;j++)
- for(i=0;i<TABLEWIDTH;i++)
- if(table[i][j]!=NONE_B)n++;
- for(i=0;i<TABLEWIDTH;i++)n-=extsymble2(i,0);
- for(i=0;i<TABLEWIDTH;i++)
- for(j=0;j<TABLEHEIGHT;j++)
- if(tableT[i][j]){
- tableT[i][j]=0;
- }else if(table[i][j]!=NONE_B){
- table[i][j]=NONE_B;
- drawball(i,j);
- //delay(200);
- }
- return n;
- }
- void addballline(int n){
- int i,j,m;
- BALL nB;
- while(n--){
- for(j=TABLEHEIGHT-1;j;j--){
- m=0;
- for(i=0;i<TABLEWIDTH;i++){
- if(table[i][j]!=NONE_B)m++;
- table[i][j]=table[i][j-1];
- drawball(i,j);
- }
- if(m)delay(5*m);
- }
- for(i=0;i<TABLEWIDTH;i++){
- do{nB=(BALL)(rand()%15+1);}while(!colortable[nB]);
- table[i][j]=nB;
- drawball(i,0);
- }
- }
- searchFall();
- }
- int flush(int i,int j){
- tempBall=table[i][j];
- int n=extsymble(i,j);
- // print(i,j);
- for(j=0;j<TABLEHEIGHT;j++){
- for(i=0;i<TABLEWIDTH;i++){
- if(tableT[i][j]==1){
- if(n>=3){
- table[i][j]=NONE_B;
- drawball(i,j);
- //delay(200);
- }
- tableT[i][j]=0;
- }
- }
- }
- n+=searchFall();
- return (n>=3)?n:0;
- }
- object::object():score(0){
- sx=400;sy=550;
- ply=0;
- nextBall=RED_B;
- }
- object::~object(){}
- void object::move(){
- if(dd==1&&direct<2800){
- hidePointer();
- direct++;
- showPointer();
- }
- if(dd==-1&&direct>-2800){
- hidePointer();
- direct--;
- showPointer();
- }
- if(dd==2&&direct){
- hidePointer();
- if(direct>0)direct--;
- if(direct<0)direct++;
- showPointer();
- }
- }
- void object::turn(Direction a){
- switch(a){
- case RIGHT:dd=1;break;
- case LEFT: dd=-1;break;
- case MID: dd=2;break;
- case RELEASE:dd=0;break;
- default:;
- }
- }
- void object::hidePointer(){
- int x=sx+100*sin(direct/2000.0),y=sy-100*cos(direct/2000.0);
- WriteMode=XOR_PUT;
- setcolor(ARROWCOLOR);
- line(x,y,sx,sy);
- WriteMode=COPY_PUT;
- }
- void object::showPointer(){
- int x=sx+100*sin(direct/2000.0),y=sy-100*cos(direct/2000.0);
- WriteMode=XOR_PUT;
- setcolor(ARROWCOLOR);
- line(x,y,sx,sy);
- WriteMode=COPY_PUT;
- }
- void object::eject(){
- int s;
- char scr[8];
- keyLock();
- hidePointer();
- BALL nB=nextBall;
- do{nextBall=(BALL)(rand()%15+1);}while(!colortable[nextBall]);
- showball();
- ball ejectBall(sx,sy,direct,nB);
- s=ejectBall.move();
- score+=s;
- sprintf(scr,"%7d",score);
- if(ply==0)textxy(10,1,scr,1);
- else textxy(650,1,scr,1);
- if(!s&&!t--){addballline(1);t=colorNum;}
- sprintf(scr,"%7d",t);
- if(ply==0)textxy(10,20,scr,1);
- else textxy(650,20,scr,1);
- isGameEnd();
- showPointer();
- keyUnlock();
- }
- void object::start(){
- char scr[8];
- BALL nB;
- score=0;
- direct=0;
- sprintf(scr,"%7d",0);
- colorNum=6;
- colortable[0]=0;
- colortable[1]=1;
- colortable[2]=0;
- colortable[3]=0;
- colortable[4]=1;
- colortable[5]=0;
- colortable[6]=0;
- colortable[7]=0;
- colortable[8]=0;
- colortable[9]=0;
- colortable[10]=1;
- colortable[11]=0;
- colortable[12]=0;
- colortable[13]=1;
- colortable[14]=1;
- colortable[15]=1;
- t=colorNum;
- for(int j=0;j<5;j++)for(int i=0;i<TABLEWIDTH;i++){
- do{nB=(BALL)(rand()%15+1);}while(!colortable[nB]);
- table[i][j]=nB;
- tableT[i][j]=0;
- drawball(i,j);
- }
- for(;j<TABLEHEIGHT;j++)for(i=0;i<TABLEWIDTH;i++){
- tableT[i][j]=0;
- table[i][j]=NONE_B;
- }
- showball();
- showPointer();
- if(ply==0)textxy(10,1,scr,1);
- else textxy(650,1,scr,1);
- }
- void object::showball(){
- circleB(50,500,BALLSIZE/2-3,nextBall*10);
- }
- void object::AIcontrol(){}
- int ball::check(){
- int i,j,i1,i2,j1,j2;
- if(x<XMAGIN+BALLSIZE/2+1||x>XMAGIN+TABLEWIDTH*BALLSIZE)bump();
- if(y<YMAGIN+BALLSIZE/2+1)return 0;
- j=getballj(y);
- if(j>0)j1=j-1;else j1=0;
- j2=j;
- for(j=j1;j<=j2;j++){
- i=getballi(x,j);
- if(i>0)i1=i-1;else i1=0;
- if(i<TABLEWIDTH-1-j%2)i2=i+1;else i2=TABLEWIDTH-1;
- for(i=i1;i<=i2;i++){
- if(table[i][j]!=NONE_B){
- if(sqrt(pow(getballx(i,j)-x,2)+
- pow(getbally(j)-y,2) )
- <BALLSIZE-5){
- return 0;
- }
- }
- }
- }
- return 1;
- }
- int ball::move(){
- int tx=x,ty=y;
- unsigned i=BALLSIZE-5;
- while(check()){
- x+=sin(direct/2000.0);
- y-=cos(direct/2000.0);
- if((i%BALLSIZE)==1){
- tx=x;
- ty=y;
- circleB(tx,ty,BALLSIZE/2-3,color*10+7);
- }
- if((i%BALLSIZE)==0){
- circleB(tx,ty,BALLSIZE/2-3,0);
- }
- i++;
- if(i==60000u)i=0;
- PixelDance();
- }
- circleB(tx,ty,BALLSIZE/2-3,0);
- return solid();
- }
- int ball::solid(){
- int i,j;
- j=getballj(y);
- i=getballi(x,j);
- table[i][j]=color;
- drawball(i,j);
- return flush(i,j);
- }