bubble.c
上传用户:junwei_58
上传日期:2007-05-15
资源大小:79k
文件大小:8k
源码类别:

其他智力游戏

开发平台:

Visual C++

  1. void exitgame(){
  2. smoothout();
  3. returnMode();
  4. printf("nThank you for playing the game!!!nn");
  5. printf("Programmed by Group. Birdmen 2002.5n");
  6. printf("n");
  7. printf("Press any key to return to your system...n");
  8. getch_Key();
  9. uninstallKeyFunc();
  10. uninstallTimer();
  11. exit(1);
  12. }
  13. void seekActions(){
  14.  AGAIN:
  15. while(!gameover&&keyboard.status==READY){PixelDance();ob->move();}
  16. if(keyboard.status==PRESS){
  17. keyboard.status=READY;
  18. switch(keyboard.recentPressKey){// to pressed key
  19. case KEY_EXIT: gameover=3;break;//EscMenu.key();break;
  20. case KEY_UP:   ob->turn(MID);break;
  21. case KEY_LEFT: ob->turn(LEFT);break;
  22. case KEY_RIGHT:ob->turn(RIGHT);break;
  23.  /* case KEY_w:    if(players==1)ob2->turn(MID);break;
  24. case KEY_a:    if(players==1)ob2->turn(LEFT);break;
  25. case KEY_d:    if(players==1)ob2->turn(RIGHT);break;
  26. case KEY_s:    if(players==1)ob2->eject();break;*/
  27. default:;
  28. }
  29. }else
  30. if(keyboard.status==RELEASE){
  31. keyboard.status=READY;
  32. switch(keyboard.recentReleaseKey){
  33. case KEY_UP:
  34. case KEY_LEFT:
  35. case KEY_RIGHT:ob->turn(RELEASE);break;
  36. case KEY_DOWN: ob->eject();break;
  37. /* case KEY_w:
  38. case KEY_a:
  39. case KEY_d:    if(players==1)ob2->turn(RELEASE);break;*/
  40. default:break;
  41. }
  42. }
  43. if(!gameover)goto AGAIN;
  44. }
  45. void isGameEnd(){
  46. int i,j,c=0;
  47. // win?
  48. for(i=0;i<TABLEWIDTH;i++)if(table[i][0]==NONE_B)c++;
  49. if(c==TABLEWIDTH){
  50. gameover=1;     //win
  51. return;
  52. }
  53. // check color number
  54. c=0;
  55. for(i=0;i<16;i++)colortable[i]=0;
  56. for(j=0;j<TABLEHEIGHT;j++){
  57. for(i=0;i<TABLEWIDTH;i++){
  58. if(table[i][j]!=NONE_B&&colortable[table[i][j]]==0){
  59. colortable[table[i][j]]=1;
  60. c++;
  61. if(c==colorNum)goto END2;
  62. }
  63. }
  64. }
  65. if(c<colorNum)colorNum=c;
  66. // lost ?
  67.  END2:
  68. for(i=0;i<TABLEWIDTH;i++)
  69. if(table[i][16]!=NONE_B){
  70. gameover=2; //lost
  71. return;
  72. }
  73. return;
  74. }
  75. inline int getballx(int i,int j){
  76. return (i*BALLSIZE+XMAGIN+(j%2+1)*BALLSIZE/2);
  77. }
  78. inline int getbally(int j){
  79. return (j*(BALLSIZE-2)+YMAGIN+BALLSIZE/2);
  80. }
  81. inline int getballi(int x,int j){
  82. int n=(x-XMAGIN-(j%2+1)*BALLSIZE/2)%BALLSIZE;
  83. int ir=(x-XMAGIN-(j%2+1)*BALLSIZE/2)/BALLSIZE;
  84. if(n>BALLSIZE/2)ir++;
  85. return ir;
  86. }
  87. inline int getballj(int y){
  88. int n=(y-YMAGIN-BALLSIZE/2)%(BALLSIZE-2);
  89. int jr=(y-YMAGIN-BALLSIZE/2)/(BALLSIZE-2);
  90. if(n>BALLSIZE/2)jr++;
  91. return jr;
  92. }
  93. int extsymble(int i,int j){
  94. int n;
  95. if(i>=0&&j>=0&&j<TABLEHEIGHT&&i<TABLEWIDTH&&
  96. table[i][j]==tempBall&&tableT[i][j]==0){
  97. tableT[i][j]=1;
  98. n=1+extsymble(i+1,j)+extsymble(i-1,j);
  99. if(j%2==1){
  100. n+=extsymble(i,j-1)+extsymble(i+1,j-1)+
  101.  extsymble(i,j+1)+extsymble(i+1,j+1);
  102. }else{
  103. n+=extsymble(i-1,j-1)+extsymble(i,j-1)+
  104.  extsymble(i-1,j+1)+extsymble(i,j+1);
  105. }
  106. return n;
  107. }else return 0;
  108. }
  109. //* debug use
  110. void print(int i,int j,int n){
  111. char str[3];
  112. sprintf(str,"%2d",n);
  113. textxy(i,j,str);
  114. }// */
  115. void drawball(int i,int j){
  116. int c;
  117. if(i>=0&&j>=0&&j<TABLEHEIGHT&&i<TABLEWIDTH){
  118. if(table[i][j])c=table[i][j]*10+1;
  119. else c=0;
  120. circleB(getballx(i,j),getbally(j),BALLSIZE/2-3,c);
  121. // print(getballx(i,j)-6,getbally(j),table[i][j]);
  122. }
  123. }
  124. int extsymble2(int i,int j){
  125. int n;
  126. if(i>=0&&j>=0&&j<TABLEHEIGHT&&i<TABLEWIDTH&&
  127. table[i][j]!=NONE_B&&tableT[i][j]==0){
  128. tableT[i][j]=1;
  129. n=1+extsymble2(i+1,j)+extsymble2(i-1,j);
  130. if(j%2==1){
  131. n+=extsymble2(i,j-1)+extsymble2(i+1,j-1)+
  132.  extsymble2(i,j+1)+extsymble2(i+1,j+1);
  133. }else{
  134. n+=extsymble2(i-1,j-1)+extsymble2(i,j-1)+
  135.  extsymble2(i-1,j+1)+extsymble2(i,j+1);
  136. }
  137. return n;
  138. }else return 0;
  139. }
  140. int searchFall(){
  141. int i,j,n=0;
  142. for(j=0;j<TABLEHEIGHT;j++)
  143. for(i=0;i<TABLEWIDTH;i++)
  144. if(table[i][j]!=NONE_B)n++;
  145. for(i=0;i<TABLEWIDTH;i++)n-=extsymble2(i,0);
  146. for(i=0;i<TABLEWIDTH;i++)
  147. for(j=0;j<TABLEHEIGHT;j++)
  148. if(tableT[i][j]){
  149. tableT[i][j]=0;
  150. }else if(table[i][j]!=NONE_B){
  151. table[i][j]=NONE_B;
  152. drawball(i,j);
  153. //delay(200);
  154. }
  155. return n;
  156. }
  157. void addballline(int n){
  158. int i,j,m;
  159. BALL nB;
  160. while(n--){
  161. for(j=TABLEHEIGHT-1;j;j--){
  162. m=0;
  163. for(i=0;i<TABLEWIDTH;i++){
  164. if(table[i][j]!=NONE_B)m++;
  165. table[i][j]=table[i][j-1];
  166. drawball(i,j);
  167. }
  168. if(m)delay(5*m);
  169. }
  170. for(i=0;i<TABLEWIDTH;i++){
  171. do{nB=(BALL)(rand()%15+1);}while(!colortable[nB]);
  172. table[i][j]=nB;
  173. drawball(i,0);
  174. }
  175. }
  176. searchFall();
  177. }
  178. int flush(int i,int j){
  179. tempBall=table[i][j];
  180. int n=extsymble(i,j);
  181. // print(i,j);
  182. for(j=0;j<TABLEHEIGHT;j++){
  183. for(i=0;i<TABLEWIDTH;i++){
  184. if(tableT[i][j]==1){
  185. if(n>=3){
  186. table[i][j]=NONE_B;
  187. drawball(i,j);
  188. //delay(200);
  189. }
  190. tableT[i][j]=0;
  191. }
  192. }
  193. }
  194. n+=searchFall();
  195. return (n>=3)?n:0;
  196. }
  197. object::object():score(0){
  198. sx=400;sy=550;
  199. ply=0;
  200. nextBall=RED_B;
  201. }
  202. object::~object(){}
  203. void object::move(){
  204. if(dd==1&&direct<2800){
  205. hidePointer();
  206. direct++;
  207. showPointer();
  208. }
  209. if(dd==-1&&direct>-2800){
  210. hidePointer();
  211. direct--;
  212. showPointer();
  213. }
  214. if(dd==2&&direct){
  215. hidePointer();
  216. if(direct>0)direct--;
  217. if(direct<0)direct++;
  218. showPointer();
  219. }
  220. }
  221. void object::turn(Direction a){
  222. switch(a){
  223. case RIGHT:dd=1;break;
  224. case LEFT: dd=-1;break;
  225. case MID:  dd=2;break;
  226. case RELEASE:dd=0;break;
  227. default:;
  228. }
  229. }
  230. void object::hidePointer(){
  231. int x=sx+100*sin(direct/2000.0),y=sy-100*cos(direct/2000.0);
  232. WriteMode=XOR_PUT;
  233. setcolor(ARROWCOLOR);
  234. line(x,y,sx,sy);
  235. WriteMode=COPY_PUT;
  236. }
  237. void object::showPointer(){
  238. int x=sx+100*sin(direct/2000.0),y=sy-100*cos(direct/2000.0);
  239. WriteMode=XOR_PUT;
  240. setcolor(ARROWCOLOR);
  241. line(x,y,sx,sy);
  242. WriteMode=COPY_PUT;
  243. }
  244. void object::eject(){
  245. int s;
  246. char scr[8];
  247. keyLock();
  248. hidePointer();
  249. BALL nB=nextBall;
  250. do{nextBall=(BALL)(rand()%15+1);}while(!colortable[nextBall]);
  251. showball();
  252. ball ejectBall(sx,sy,direct,nB);
  253. s=ejectBall.move();
  254. score+=s;
  255. sprintf(scr,"%7d",score);
  256. if(ply==0)textxy(10,1,scr,1);
  257. else      textxy(650,1,scr,1);
  258. if(!s&&!t--){addballline(1);t=colorNum;}
  259. sprintf(scr,"%7d",t);
  260. if(ply==0)textxy(10,20,scr,1);
  261. else      textxy(650,20,scr,1);
  262. isGameEnd();
  263. showPointer();
  264. keyUnlock();
  265. }
  266. void object::start(){
  267. char scr[8];
  268. BALL nB;
  269. score=0;
  270. direct=0;
  271. sprintf(scr,"%7d",0);
  272. colorNum=6;
  273. colortable[0]=0;
  274. colortable[1]=1;
  275. colortable[2]=0;
  276. colortable[3]=0;
  277. colortable[4]=1;
  278. colortable[5]=0;
  279. colortable[6]=0;
  280. colortable[7]=0;
  281. colortable[8]=0;
  282. colortable[9]=0;
  283. colortable[10]=1;
  284. colortable[11]=0;
  285. colortable[12]=0;
  286. colortable[13]=1;
  287. colortable[14]=1;
  288. colortable[15]=1;
  289. t=colorNum;
  290. for(int j=0;j<5;j++)for(int i=0;i<TABLEWIDTH;i++){
  291. do{nB=(BALL)(rand()%15+1);}while(!colortable[nB]);
  292. table[i][j]=nB;
  293. tableT[i][j]=0;
  294. drawball(i,j);
  295. }
  296. for(;j<TABLEHEIGHT;j++)for(i=0;i<TABLEWIDTH;i++){
  297. tableT[i][j]=0;
  298. table[i][j]=NONE_B;
  299. }
  300. showball();
  301. showPointer();
  302. if(ply==0)textxy(10,1,scr,1);
  303. else      textxy(650,1,scr,1);
  304. }
  305. void object::showball(){
  306. circleB(50,500,BALLSIZE/2-3,nextBall*10);
  307. }
  308. void object::AIcontrol(){}
  309. int ball::check(){
  310. int i,j,i1,i2,j1,j2;
  311. if(x<XMAGIN+BALLSIZE/2+1||x>XMAGIN+TABLEWIDTH*BALLSIZE)bump();
  312. if(y<YMAGIN+BALLSIZE/2+1)return 0;
  313. j=getballj(y);
  314. if(j>0)j1=j-1;else j1=0;
  315. j2=j;
  316. for(j=j1;j<=j2;j++){
  317. i=getballi(x,j);
  318. if(i>0)i1=i-1;else i1=0;
  319. if(i<TABLEWIDTH-1-j%2)i2=i+1;else i2=TABLEWIDTH-1;
  320. for(i=i1;i<=i2;i++){
  321. if(table[i][j]!=NONE_B){
  322. if(sqrt(pow(getballx(i,j)-x,2)+
  323. pow(getbally(j)-y,2)   )
  324.  <BALLSIZE-5){
  325.  return 0;
  326.  }
  327. }
  328. }
  329. }
  330. return 1;
  331. }
  332. int ball::move(){
  333. int tx=x,ty=y;
  334. unsigned i=BALLSIZE-5;
  335. while(check()){
  336. x+=sin(direct/2000.0);
  337. y-=cos(direct/2000.0);
  338. if((i%BALLSIZE)==1){
  339. tx=x;
  340. ty=y;
  341. circleB(tx,ty,BALLSIZE/2-3,color*10+7);
  342. }
  343. if((i%BALLSIZE)==0){
  344. circleB(tx,ty,BALLSIZE/2-3,0);
  345. }
  346. i++;
  347. if(i==60000u)i=0;
  348. PixelDance();
  349. }
  350. circleB(tx,ty,BALLSIZE/2-3,0);
  351. return solid();
  352. }
  353. int ball::solid(){
  354. int i,j;
  355. j=getballj(y);
  356. i=getballi(x,j);
  357. table[i][j]=color;
  358. drawball(i,j);
  359. return flush(i,j);
  360. }