发一个c语言写的俄罗斯方块的代码 [

 

  1. #include <stdlib.h>
  2. #include <graphics.h>
  3. #include <bios.h>
  4. #define mDRAW     5
  5. #define mLINE     6
  6. #define mADOWN    7
  7. #define mGEN      8
  8. #define mLEFT     75
  9. #define mRIGHT    77
  10. #define mSPACE    57
  11. #define mDOWN     80
  12. #define mESC      1
  13. #define TIMEINT   2
  14. #define MAXX      9
  15. #define MAXY   30
  16. #define BACKCOLOR BLACK
  17. #define WINX      50
  18. #define WINY      470
  19. #define GAP       6
  20. #define AREAX     (WINX+GAP)
  21. #define AREAY     (WINY-GAP)
  22. #define BOXW      15
  23. int oldarea[MAXY+1][MAXX];
  24. int area[MAXY+1][MAXX];
  25. int actW,actH,actX,actY;
  26. int curX,curY,curColor,curW,curH;
  27. int newX,newY,newColor,newW,newH;
  28. int active;
  29. int box[4][4];
  30. int FORCOLOR;
  31. int MESSAGE;
  32. int BOX[7][4][4]={
  33. {
  34.   {1,1,1,1},
  35.   {0,0,0,0},
  36.   {0,0,0,0},
  37.   {0,0,0,0}
  38. },{
  39.   {1,1,1,0},
  40.   {1,0,0,0},
  41.   {0,0,0,0},
  42.   {0,0,0,0}
  43. },{
  44.   {1,1,1,0},
  45.   {0,0,1,0},
  46.   {0,0,0,0},
  47.   {0,0,0,0}
  48. },{
  49.   {1,1,1,0},
  50.   {0,1,0,0},
  51.   {0,0,0,0},
  52.   {0,0,0,0}
  53. },{
  54.   {1,1,0,0},
  55.   {0,1,1,0},
  56.   {0,0,0,0},
  57.   {0,0,0,0}
  58. },{
  59.   {0,1,1,0},
  60.   {1,1,0,0},
  61.   {0,0,0,0},
  62.   {0,0,0,0}
  63. },{
  64.   {1,1,0,0},
  65.   {1,1,0,0},
  66.   {0,0,0,0},
  67.   {0,0,0,0}
  68. }
  69. };

  70. void init();
  71. void draw();
  72. int genBox();
  73. int getKey();
  74. void lineFull();
  75. int moveLeft();
  76. int moveRight();
  77. int moveDown();
  78. int rotate();
  79. int getW();
  80. int getH();
  81. void clearOldBox();
  82. void putNewBox();
  83. int collisionRotate(int box[][4]);
  84. void getMessage();
  85. void dispatchMessage();
  86. int timeCome();
  87. void fallDown();
  88. int gameOver();
  89. main()
  90. {
  91. int i;/*printf("This program is made by xiezhijian");*/
  92. init();
  93. do
  94. {
  95.   getMessage();
  96.   dispatchMessage();
  97. }
  98. while(!gameOver());
  99. getch();
  100. closegraph();
  101. }
  102. void getMessage()
  103. {
  104. if(MESSAGE) return;
  105. if(timeCome())
  106. {
  107.   MESSAGE=mADOWN;
  108.   return;
  109. }
  110. if(bioskey(1))
  111. {
  112.   MESSAGE=bioskey(0)>>8;
  113.   return;
  114. }
  115. }
  116. void dispatchMessage()
  117. {
  118. switch(MESSAGE)
  119. {
  120.   case mLEFT:  moveLeft();break;
  121.   case mRIGHT: moveRight();break;
  122.   case mADOWN: moveDown();break;
  123.   case mSPACE: rotate();break;
  124.   case mDOWN:  fallDown(); break;
  125.   case mDRAW:  draw();break;
  126.   case mLINE:  lineFull();break;
  127.   case mGEN:   genBox();break;
  128.   case mESC:   closegraph(); exit(0);
  129.   default:     MESSAGE=0;
  130. }
  131. }
  132. void fallDown()
  133. {
  134. while(active)
  135. {
  136.   moveDown(); draw();
  137. }
  138. MESSAGE=mLINE;
  139. }
  140. int timeCome()
  141. {
  142. static long tm, old;
  143. tm=biostime(0,tm);
  144. if(tm-old<TIMEINT) return 0;
  145. else
  146. {
  147.   old=tm; return 1;
  148. }
  149. }
  150. void init()
  151. {
  152. int i,j,x1,y1,x2,y2;
  153. int driver=DETECT, mode=0;
  154. randomize();
  155. registerbgidriver(EGAVGA_driver);
  156. initgraph(&driver,&mode,"");
  157. cleardevice();
  158. setfillstyle(SOLID_FILL,BLUE);
  159. bar(0,0,639,479);
  160. x1=AREAX;
  161. y1=AREAY-BOXW*MAXY;
  162. x2=AREAX+MAXX*BOXW;
  163. y2=AREAY;
  164. rectangle(--x1,--y1,++x2,++y2);
  165. setfillstyle(SOLID_FILL,BLACK);
  166. bar(++x1,++y1,--x2,--y2);
  167. y1=AREAY-MAXY*BOXW; y2=AREAY;
  168. setcolor(DARKGRAY);
  169. for(i=0;i<MAXX;i++)
  170. {
  171.   x1=AREAX+i*BOXW;
  172.   line(x1,y1,x1,y2);
  173. }
  174. x1=AREAX; x2=x1+MAXX*BOXW;
  175. for(j=0;j<MAXY;j++)
  176. {
  177.   y1=AREAY-j*BOXW;
  178.   line(x1,y1,x2,y1);
  179. }
  180. for(j=0;j<MAXY;j++)
  181.   for(i=0;i<MAXX;i++)
  182.    area[j][i]=oldarea[j][i]=0;
  183. actX=0; actY=0; actW=MAXX-1; actH=MAXY-1;
  184. draw();
  185. MESSAGE=mGEN;
  186. }
  187. int genBox()
  188. {
  189. int i,j,boxidx;
  190. boxidx=random(7); FORCOLOR=random(7)+1;
  191. for(j=0;j<4;j++)
  192.   for(i=0;i<4;i++)
  193.    box[j][i]=BOX[boxidx][j][i];
  194. curW=getW(); curH=getH();
  195. curX=(MAXX+curW)/2;
  196. if(curX+curW>=MAXX)curX=MAXX-1-curW;
  197. curY=MAXY-1-curH;
  198. newX=curX; newY=curY; actX=curX;actY=curY;
  199. actW=newW=curW; actH=newH=curH;
  200. active=1;
  201. if(collision(box)) return 0;
  202. putNewBox();
  203. draw(); MESSAGE=0;
  204. return 1;
  205. }
  206. void lineFull()
  207. {
  208. int row,col, rowEnd,full,i,j;
  209. rowEnd=newY+newH;
  210. if(rowEnd>=MAXY-1) rowEnd=MAXY-2;
  211. for(row=newY; row<=rowEnd;)
  212. {
  213.   full=1;
  214.   for(col=0;col<MAXX;col++)
  215.    if(!area[row][col]){full=0; break;}
  216.   if(!full){++row; continue;}
  217.   for(j=row; j<MAXY-1;j++)
  218.    for(i=0;i<MAXX;i++)
  219.     area[j][i]=area[j+1][i];
  220.   actX=0;actY=row; actW=MAXX-1; actH=MAXY-1-row;
  221.   draw(); rowEnd--;
  222. }
  223. MESSAGE=mGEN;
  224. }
  225. void draw()
  226. {
  227. int row,col,x1,y1,x2,y2;
  228. for(row=actY;row<=actY+actH;row++)
  229.   for(col=actX;col<=actX+actW;col++)
  230.    if(area[row][col]!=oldarea[row][col])
  231.    {
  232.     if(area[row][col]==0)
  233.      setfillstyle(SOLID_FILL,BACKCOLOR);
  234.     else
  235.      setfillstyle(SOLID_FILL,FORCOLOR);
  236.     x1=AREAX+col*BOXW; x2=x1+BOXW;
  237.     y1=AREAY-(row+1)*BOXW; y2=y1+BOXW;
  238.     bar(++x1,++y1,--x2,--y2);
  239.     oldarea[row][col]=area[row][col];
  240.    }
  241. MESSAGE=0;
  242. }
  243. int moveLeft()
  244. {
  245. newX=curX-1; clearOldBox();
  246. if(collision(box))
  247. {
  248.   newX=curX;
  249.   putNewBox();
  250.   MESSAGE=0;
  251.   return 0;
  252. }
  253. putNewBox();
  254. actW=curW+1; actX=curX=newX;
  255. MESSAGE=mDRAW;
  256. return 1;
  257. }
  258. int moveRight()
  259. {
  260. newX=curX+1; clearOldBox();
  261. if(collision(box))
  262. {
  263.   newX=curX;
  264.   putNewBox();
  265.   MESSAGE=0;
  266.   return 0;
  267. }
  268. putNewBox();
  269. actW=curW+1; actX=curX; curX=newX;
  270. MESSAGE=mDRAW;
  271. return 1;
  272. }
  273. int moveDown()
  274. {
  275. int i,j;
  276. newY=curY-1;
  277. clearOldBox();
  278. if(collision(box))
  279. {
  280.   newY=curY;
  281.   putNewBox();
  282.   active=0;
  283.   MESSAGE=mLINE;
  284.   return 0;
  285. }
  286. putNewBox();
  287. actH=curH+1; actY=newY; curY=newY;
  288. MESSAGE=mDRAW;
  289. return 1;
  290. }
  291. int rotate()
  292. {
  293. int newBox[4][4];
  294. int i,j;
  295. clearOldBox();

  296. for(j=0;j<4;j++)
  297.   for(i=0;i<4;i++)
  298.    newBox[j][i]=0;
  299. for(j=0;j<4;j++)
  300.   for(i=0;i<4;i++)
  301.    newBox[curW-i][j]=box[j][i];
  302. newW=curH; newH=curW;
  303. if(collisionRotate(newBox))
  304. {
  305.   newW=curW; newH=curH; newX=curX; newY=curY;
  306.   putNewBox();
  307.   MESSAGE=0;
  308.   return 0;
  309. }
  310. for(j=0;j<4;j++)
  311.   for(i=0;i<4;i++)
  312.    box[j][i]=newBox[j][i];
  313. putNewBox();
  314. actH=newH>curH? newH:curH;
  315. actW=curX+actH-newX;
  316. actX=newX; actY=newY; curX=newX;
  317. curY=newY; curW=newW; curH=newH;
  318. MESSAGE=mDRAW;
  319. return 1;
  320. }
  321. int getW()
  322. {
  323. int i,j;
  324. for(i=3;i>0;i--)
  325.   for(j=0;j<4;j++)
  326.    if(box[j][i]) return i;
  327. return 0;
  328. }
  329. int getH()
  330. {
  331. int i,j;
  332. for(j=3;j>0;j--)
  333.   for(i=0;i<4;i++)
  334.    if(box[j][i]) return j;
  335. return 0;
  336. }

  337. void clearOldBox()
  338. {
  339. int i,j;
  340. for(j=0;j<=curH; j++)
  341.   for(i=0;i<=curW; i++)
  342.    if(box[j][i])
  343.     area[curY+j][curX+i]=0;
  344. }
  345. void putNewBox()
  346. {
  347. int i,j;
  348. for(j=0;j<=newH;j++)
  349.   for(i=0;i<=newW;i++)
  350.    if(box[j][i])
  351.     area[newY+j][newX+i]=FORCOLOR;
  352. }
  353. int collision(int cbox[][4])
  354. {
  355. int i,j;
  356. if(newX<0) return 1;
  357. if(newX+newW>=MAXX) return 1;
  358. if(newY<0) return 1;
  359. for(j=0;j<=newH;j++)
  360.   for(i=0;i<=newW;i++)
  361.    if(area[newY+j][newX+i]&&cbox[j][i]) return 1;
  362. return 0;
  363. }
  364. int collisionRotate(int cbox[][4])
  365. {
  366. int i,j;
  367. if(newX+newW>=MAXX) newX=MAXX-1-newW;
  368. if(newY+newH>=MAXY) newY=MAXY-1-newH;
  369. if(collision(cbox)) return 1;
  370. for(i=0;i<=newW;i++)
  371.   for(j=0;j<=newH;j++)
  372.    if(area[newY+j][newX+i])
  373.    {
  374.     newX-=newW-i+1; goto L;
  375.    }
  376. L: return collision(cbox);
  377. }
  378. int gameOver()
  379. {
  380. if(!active &&(curY+curH>MAXY-3)) return 1;
  381. else return 0;
  382. }
复制代码
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值