Advertisement
erodemobiles

Untitled

May 2nd, 2011
498
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.55 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <unistd.h>
  5. #include <time.h>
  6. #include "primlib.h"
  7. #include "pieces.h"
  8.  
  9. /** width of the playable area, in basic square units (not pixels), it has to be a positive integer multiplication of 5 (5,10,15...) */
  10. #define BOARDWIDTH 10
  11. /** height of the playable area, in basic square units (not pixels), it has to be a positive integer multiplication of 12 (12,24,36,...) */
  12. #define BOARDHEIGHT 24
  13.  
  14.  
  15.  
  16. /*####################################################################################################################################################*/
  17.  
  18. int board[BOARDHEIGHT+2][BOARDWIDTH+2];
  19. int UNIT = 480/BOARDHEIGHT;
  20.  
  21. struct useful
  22. {
  23.   int flag;
  24.   int rotation;
  25.   int type;
  26.   int rot_next;
  27.   int type_next;
  28. } useful;
  29.  
  30. void initialdraw();
  31. void borderdraw();
  32. void movement();
  33. void update_board();
  34. int checkrow();
  35. void shiftall(int);
  36. int spawnbrick();
  37. int random_num (float);
  38. /*####################################################################################################################################################*/
  39.  
  40. int main(int argc, char *argv[])
  41. {
  42. int i,j;
  43. useful.flag = 0;
  44. useful.rot_next = random_num(4);
  45. useful.type_next = random_num(7);
  46. for(i=0;i<BOARDHEIGHT;i++)/* setting the main board array to zero*/
  47. {
  48. for(j=1;j<=BOARDWIDTH;j++)
  49. {
  50. board[i][j]=0;
  51. }
  52. }
  53. for(i=BOARDHEIGHT;i<BOARDHEIGHT+2;i++)/* setting the rows outside to non-zero values, so that the move_down function can refer to them as filled and stop when the ground is reached*/
  54. {
  55. for(j=0;j<BOARDWIDTH + 2;j++)
  56. {
  57. board[i][j]=3;
  58. }
  59. }
  60. for (i = 0; i < BOARDHEIGHT; i++)/* setting the invisible columns to the left and right to 3 */
  61. {
  62.   board [i][0] = 3;
  63.   board [i][BOARDWIDTH + 1] = 3;
  64. }
  65.  
  66. if (initGraph())
  67. exit(3);
  68.  
  69. for(;;)
  70. {
  71. if(isKeyDown(SDLK_ESCAPE) || useful.flag == 1)
  72.       {
  73.       break;
  74.       }
  75. if (spawnbrick())
  76.       initialdraw();
  77. else break;
  78.  
  79. movement();
  80. for (i = 1; i <= BOARDHEIGHT; i++)
  81. {
  82.   j = checkrow();
  83.   if (j) shiftall(j - 1);
  84. }
  85. printf ("\n");
  86. }
  87. sleep(1);
  88. filledRect(0, 0, screenWidth() - 1, screenHeight() - 1, BLACK);
  89. textout (screenWidth()/2, screenHeight()/2, "GAME OVER!", RED);
  90. updateScreen();
  91. getkey();
  92. return 0;
  93. }
  94.  
  95. /*####################################################################################################################################################*/
  96.  
  97.  
  98. /** function drawing the borders */
  99. void borderdraw()
  100. {
  101. int boardshift = (screenWidth()/UNIT-BOARDWIDTH)/2 +BOARDWIDTH % 2;
  102. filledRect(boardshift*UNIT-5,0,boardshift*UNIT,screenHeight()-1,YELLOW);
  103. filledRect(screenWidth()-(boardshift*UNIT),0,screenWidth()-(boardshift*UNIT-5),screenHeight()-1,YELLOW);
  104. }
  105.  
  106.  
  107. /*####################################################################################################################################################*/
  108.  
  109.  
  110. /** function spawning the brick */
  111. void initialdraw()
  112. {
  113. int i,j;
  114. srand(time(NULL));
  115. useful.type = useful.type_next;
  116. useful.rotation = useful.rot_next;
  117. useful.type_next = random_num(7);
  118. useful.rot_next =random_num(4);
  119. borderdraw();
  120. filledRect(0,0,screenWidth()-1,screenHeight()-1,BLACK);
  121. if(((useful.type==2 || useful.type==4 || useful.type==5 || useful.type==6) && (useful.rotation==1)) || (useful.type==1 && useful.rotation%2==0) || (useful.type==3 && useful.rotation==3))/** some of pieces would be displayed lower than they should be - failsafe mechanism.. */
  122. {
  123. for (i=0;i<3;i++)
  124. {
  125. for(j=BOARDWIDTH/2;j<BOARDWIDTH/2+4;j++)
  126. {
  127. board[i][j] = pieces[useful.type][useful.rotation][i+1][j-(BOARDWIDTH/2)];
  128. }
  129. }
  130. }
  131. else
  132. {
  133. for (i=0;i<4;i++)
  134. {
  135. for(j=BOARDWIDTH/2;j<BOARDWIDTH/2+4;j++)
  136. {
  137. board[i][j]+=pieces[useful.type][useful.rotation][i][j-(BOARDWIDTH/2)];
  138. }
  139. }
  140. }
  141. draw_next();
  142. update_board();
  143. for(i=0;i<BOARDHEIGHT + 2;i++)
  144. {
  145. for(j=0;j<=BOARDWIDTH+1;j++)
  146. {
  147. printf("%d   ", board[i][j]);
  148. }
  149. printf("\n");
  150. }
  151. }
  152.  
  153. /*####################################################################################################################################################*/
  154.  
  155.  
  156. /** function moving block down by 1 square */
  157. int move_down()
  158. {
  159. int temp,i,j,possible;
  160. for (i=BOARDHEIGHT;i>=0;i--)
  161. {
  162. for(j=BOARDWIDTH;j>=1;j--)
  163. {
  164. if((board[i][j]==1 || board[i][j]==2) && board[i+1][j]==3)
  165. {
  166. return;
  167. }
  168. }
  169. for(j=1;j<=BOARDWIDTH;j++)
  170. {
  171. if((board[i][j]==1 || board[i][j]==2) && board[i+1][j]==3)
  172. {
  173. return;
  174. }
  175. }
  176. }
  177. for (i = BOARDHEIGHT; i >= 0; i--)
  178. {
  179.   for(j=BOARDWIDTH;j>0;j--)
  180.   {
  181.   if((board[i][j]==1 || board[i][j]==2))
  182.   {
  183.   temp = board[i][j];
  184.   board[i][j]=0;
  185.   board[i+1][j]=temp;
  186.   }
  187.   }
  188. }
  189. update_board();
  190. return ;
  191. }
  192.  
  193.  
  194. /*####################################################################################################################################################*/
  195.  
  196. void move_left()
  197. {
  198. int temp,i,j, possible = 1;
  199. for (i = 0; i < BOARDHEIGHT; i++)
  200. {
  201.   for (j = 0; j < BOARDWIDTH + 1; j++)
  202.   {
  203.     if (board[i][j] == 3 && (board [i][j + 1] == 1 || board[i][j + 1] == 2))
  204.     {
  205.       return;
  206.     }
  207.   }
  208. }
  209. for (i=BOARDHEIGHT;i>=0;i--)
  210. {
  211.  
  212. for(j=1;j<=BOARDWIDTH;j++)
  213. {
  214. if((board[i][j]==1 || board[i][j]==2))
  215. {
  216. temp = board[i][j];
  217. board[i][j]=0;
  218. board[i][j-1]=temp;
  219. }
  220. }
  221.  
  222. }
  223. update_board();
  224. }
  225.  
  226.  
  227.  
  228. /*####################################################################################################################################################*/
  229.  
  230. void move_right()
  231. {
  232. int temp,i,j, possible = 1;
  233. for (i = 0; i < BOARDHEIGHT; i++)
  234. {
  235.   for (j = BOARDWIDTH + 1; j > 0; j--)
  236.   {
  237.     if (board[i][j] == 3 && (board [i][j - 1] == 1 || board[i][j - 1] == 2))
  238.     {
  239.       possible = 0;
  240.       break;
  241.     }
  242.   }
  243. }
  244. for (i=0;i<BOARDHEIGHT;i++)
  245. {
  246. for(j=BOARDWIDTH-1;j>=0;j--)
  247. {
  248. if((board[i][j]==1 || board[i][j]==2) && possible)
  249. {
  250. temp = board[i][j];
  251. board[i][j]=0;
  252. board[i][j+1]=temp;
  253. }
  254. }
  255. }
  256. update_board();
  257. }
  258.  
  259. /*
  260. void rotate(int vert_position, int hori_position, int type, int rotation)
  261. {
  262. int i,j;
  263. for(i=0;i<BOARDHEIGHT;i++)
  264. {
  265. for(j=0;j<BOARDWIDTH;j++)
  266. {
  267. if(board[i][j]==1 || board[i][j]==2)
  268. {
  269. board[i][j]=0;
  270. }
  271. }
  272. }
  273. for(i=vert_position;i<vert_position+4;i++)
  274. {
  275. for(j=hori_position-2;j<hori_position+2;j++)
  276. {
  277. if(rotation<3)
  278. {
  279. board[i][j] = pieces [type][rotation+1][i-vert_position][j-hori_position+2];
  280. }
  281. else if(rotation==3)
  282. {
  283. board[i][j] = pieces [type][0][i-vert_position][j-hori_position+2];
  284. }
  285. }
  286. }
  287. update_board();
  288. }
  289. */
  290.  
  291.  
  292.  
  293. /*####################################################################################################################################################*/
  294.  
  295.  
  296. /** function performing the movement of a block */
  297. void movement()
  298. {
  299. int i, j, k, l, p, key;
  300. int vert_position=0;
  301. key = '0';
  302. while(vert_position<BOARDHEIGHT)
  303. {
  304. for(j=0;j<70000000;j++)
  305. {
  306. key = pollkey();
  307. if(isKeyDown(SDLK_ESCAPE))
  308. {
  309. useful.flag = 1;
  310. return ;
  311. }
  312. if(key == SDLK_RIGHT)
  313. {
  314. move_right();
  315. for(p=j;p<70000000;p++)
  316. {}
  317. move_down();
  318. break;
  319. }
  320. else if(key == SDLK_LEFT)
  321. {
  322. move_left();
  323. for(p=j;p<70000000;p++)
  324. {}
  325. move_down();
  326. break;
  327. }
  328. else if (key == SDLK_DOWN)
  329. {
  330.   for (p = 0; p < BOARDHEIGHT; p++) move_down();
  331.   break;
  332. }
  333. else
  334. {
  335.   for(p=j;p<70000000;p++)
  336. {}
  337. move_down();
  338. break;
  339. }
  340. }
  341.  
  342. for(i=BOARDHEIGHT;i>0;i--)
  343. {
  344. for(j=BOARDWIDTH + 1;j > 0;j--)
  345. {
  346. if((board[i][j]==1 || board[i][j]==2) && board[i+1][j]==3)
  347. {
  348. for(k=0;k<BOARDHEIGHT;k++)
  349. {
  350. for(l=0;l<BOARDWIDTH + 2;l++)
  351. {
  352. if(board[k][l]==1 || board[k][l]==2)
  353. {
  354. board[k][l]=3;
  355. }
  356. }
  357. }
  358. return ;
  359. }
  360. }
  361. }
  362. vert_position++;
  363. update_board();
  364. }
  365.  
  366.  
  367. }
  368.  
  369.  
  370. /*####################################################################################################################################################*/
  371.  
  372.  
  373. /** Function goes through all elements of the main (board) array, checking their values, and drawing squares on the corresponding coordinates. If the array element is equal 0, nothing happens, if 1 - a red square is drawn, if 2 - a yellow (pivot) square is drawn, and if 3 = magenta square is drawn. The magenta squares are those, which have already fallen and are no longer moving. */
  374. void update_board()
  375. {
  376. int i,j,x1,y1,x2,y2;
  377. int boardshift = (screenWidth()/UNIT-BOARDWIDTH)/2 +BOARDWIDTH % 2;/* boardshift - a variable, which determines the horizontal position of the board */
  378. filledRect(0,0,screenWidth()-1,screenHeight()-1,BLACK);
  379. borderdraw();/* drawing the borders */
  380. for(i=0;i<BOARDHEIGHT;i++)
  381. {
  382. for(j=1;j<=BOARDWIDTH;j++)
  383. {
  384. /* setting the coordinates for the square, they are dependent on the position in array - the visible board is a reflection of this array */
  385. x1 = ((j-1)*UNIT)+boardshift*UNIT+2;
  386. y1 = ((i)*UNIT)+2;
  387. x2 = ((j)*UNIT)+boardshift*UNIT-2;
  388. y2 = ((i+1)*UNIT)-2;
  389.  
  390.  
  391. /* setting the appropriate colours */
  392. if(board[i][j]==1)/* normal piece element */
  393. {
  394. filledRect(x1,y1,x2,y2, RED);
  395. }
  396. if(board[i][j]==2)/* pivot piece element */
  397. {
  398. filledRect(x1,y1,x2,y2,YELLOW);
  399. }
  400. if(board[i][j]==3)/* elements which have already fallen (unmovable) */
  401. {
  402. filledRect(x1,y1,x2,y2,WHITE);
  403. }
  404. if(board[i][j]==0)/* the remaining, zero fields, are filled with black (helpful in removing the lines) */
  405. {
  406. filledRect(x1,y1,x2,y2,BLACK);
  407. }
  408. }
  409. }
  410. updateScreen();
  411. }
  412.  
  413.  
  414.  
  415. int checkrow(void)
  416. {
  417.   int i, j, counter = 0;
  418.   for (i = 0; i < BOARDHEIGHT; i ++)
  419.   {
  420.     for (j = 1; j <= BOARDWIDTH; j ++)
  421.     {
  422.       if (board[i][j]) counter++;
  423.       if (counter == BOARDWIDTH) return i + 1;
  424.     }
  425.     counter = 0;
  426.   }
  427.   return 0;
  428. }
  429.  
  430. void shiftall(int row)
  431. {
  432.   int i, j;
  433.   for (i = row; i >= 0; i--)
  434.   {
  435.     for (j = 1; j <= BOARDWIDTH; j ++)
  436.     {
  437.       if (i == 0)
  438.       {
  439. board [i][j] = 0;
  440. continue;
  441.       }
  442.       board[i][j] = board[i - 1][j];
  443.  
  444.     }
  445.   }
  446.   update_
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement