Posted By

ozkriff on 05/02/09


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

tandouri


tetris.c


 / Published in: C
 

WIP

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #include <SDL/SDL.h>
  6. #include <SDL/SDL_gfx.h>
  7.  
  8. #include "io.c"
  9.  
  10. #define WAIT_TIME 700 //miliseconds
  11. #define PIECE_SIZE 5 //in blocks
  12. #define MAP_HEIGHT 20 // in blocks
  13. #define MAP_WIDTH 10 // in blocks
  14. #define MAP_POSITION 320 //px
  15. #define BLOCK_SIZE 16 //px
  16. #define MAP_LINE_WIDTH 5 //px
  17.  
  18. int current_x = 0, current_y = 0;
  19. int piece [PIECE_SIZE] [PIECE_SIZE] = {0};
  20. int map [MAP_WIDTH] [MAP_HEIGHT] = {0};
  21.  
  22. #define TYPES 2
  23. int pieces [ TYPES ][PIECE_SIZE][PIECE_SIZE] = {
  24. {
  25. {0, 0, 0, 0, 0},
  26. {0, 0, 0, 0, 0},
  27. {0, 1, 2, 1, 0},
  28. {0, 0, 1, 0, 0},
  29. {0, 0, 0, 0, 0}
  30. }, {
  31. {0, 0, 0, 0, 0},
  32. {0, 0, 0, 0, 0},
  33. {0, 1, 2, 1, 0},
  34. {0, 0, 0, 1, 0},
  35. {0, 0, 0, 0, 0}
  36. }
  37. };
  38.  
  39. int i, j; /* VaeRY BAD. */
  40. int screen_height = get_screen_height(); /* BAD. */
  41.  
  42. unsigned long time_1;
  43. unsigned long time_2;
  44.  
  45.  
  46. /* ----------------------- IS_GAME_OVER ---------------------------------- */
  47. /* If the first line has blocks, then, game over. */
  48.  
  49. int is_game_over()
  50. {
  51. for ( i = 0; i < MAP_WIDTH; i++ )
  52. if ( map[i][0] )
  53. return 1;
  54. return 0;
  55. }
  56.  
  57.  
  58. /* ----------------------- STORE_PIECE ----------------------------------- */
  59. /* Store each !=0 block of the piece into the board. */
  60.  
  61. void store_piece ()
  62. {
  63. int i1, i2, j1, j2;
  64. for ( i1 = current_x, i2 = 0; i1 < current_x + PIECE_SIZE; i1++, i2++)
  65. for ( j1 = current_y, j2 = 0; j1 < current_y + PIECE_SIZE; j1++, j2++)
  66. if ( piece[j2][i2] )
  67. map[i1][j1] = 1;
  68. }
  69.  
  70.  
  71.  
  72. /* ----------------------- DELETE_LINE ----------------------------------- */
  73. /* Delete a line of the board by moving all above lines down */
  74. /* pY: Vertical position in blocks of the line to delete */
  75.  
  76. void delete_line (int y)
  77. {
  78. for ( j = y; j > 0; j--)
  79. for ( i = 0; i < MAP_WIDTH; i++)
  80. map[i][j] = map[i][j-1];
  81. }
  82.  
  83.  
  84. /* ----------------------- DELETE_POSSIBLE_LINES ------------------------- */
  85. /* Delete all the lines that should be removed. */
  86.  
  87. void delete_possible_lines ()
  88. {
  89. for ( j = 0; j < MAP_HEIGHT; j++) {
  90. for ( i = 0; i < MAP_WIDTH; i++ ) {
  91. if (map[i][j] != 1)
  92. break;
  93. i++;
  94. }
  95. if ( i == MAP_WIDTH )
  96. DeleteLine(j);
  97. }
  98. }
  99.  
  100.  
  101.  
  102. /* ----------------------- IS_POSSIBLE_MOVEMENT -------------------------- */
  103.  
  104. int is_possible_movement ( )
  105. {
  106. int i1, i2, j1, j2;
  107.  
  108. for ( i1 = current_x, i2 = 0; i1 < current_x + PIECE_SIZE; i1++, i2++)
  109. for ( j1 = current_y, j2 = 0; j1 < current_y + PIECE_SIZE; j1++, j2++) {
  110. /* Check limits */
  111. if ( i1 < 0 || i1 > MAP_WIDTH - 1 || j1 > MAP_HEIGHT - 1 )
  112. if ( piece[j2][i2] )
  113. return 0;
  114.  
  115. /* Check collisions with a block already stored in the map. */
  116. if (j1 >= 0)
  117. if ( piece[j2][i2] && !map[i1][j1] )
  118. return 0;
  119. }
  120. return 1; /* No collision. Movement IS possible. */
  121. }
  122.  
  123.  
  124. /* ------------------------ ROTATE_PIECE --------------------------------- */
  125.  
  126. void rotate_piece ()
  127. {
  128. int piece_2 [PIECE_SIZE] [PIECE_SIZE] = {0};
  129.  
  130. for ( i = 0; i < PIECE_SIZE; i++ )
  131. for ( j = 0; j < PIECE_SIZE; j++ )
  132. piece_2[i][j] = piece[i][j];
  133. for ( i = 0; i < PIECE_SIZE; i++ )
  134. for ( j = 0; j < PIECE_SIZE; j++ )
  135. piece[i][j] = piece_2[j][i];
  136. }
  137.  
  138.  
  139. /* ------------------------ CREATE_NEW_PIECE ----------------------------- */
  140. void create_new_piece()
  141. {
  142. for( i = 0; i < PIECE_SIZE; i++ )
  143. for( j = 0; j < PIECE_SIZE; j++ )
  144. piece[i][j] = pieces [rand(TYPES)] [i] [j];
  145.  
  146. /* ADD function that puts piece in right position. */
  147. current_y = 0; /* TODO */
  148. current_x = MAP_WIDTH / 2;
  149.  
  150. /* may be some rotation...*/
  151. /* + generate next(). later. */
  152. }
  153.  
  154.  
  155. /* ------------------------ DRAW_SHADOW ---------------------------------- */
  156. void draw_shadow()
  157. {
  158. /* TODO. 2 */
  159. }
  160.  
  161.  
  162. /* ------------------------ DRAW_NEXT_PIECE ------------------------------ */
  163. void draw_next_piece()
  164. {
  165. /* TODO. 1 */
  166. }
  167.  
  168.  
  169. /* ------------------------ ADD_RUBBISH_TO_MAP --------------------------- */
  170. void add_rubbish_to_map()
  171. {
  172. /* TODO. later. */
  173. }
  174.  
  175.  
  176. /* ----------------------- DRAW_PIECE ------------------------------------ */
  177.  
  178. void draw_piece ()
  179. {
  180. color piece_color; /* Color of the block. */
  181.  
  182. /* Obtain the position in pixel in the screen of the block we want to draw. */
  183. int mPixelsY = (screen_height - (BLOCK_SIZE * MAP_HEIGHT)) + (current_y * BLOCK_SIZE);
  184. int mPixelsX = ( MAP_POSITION - (BLOCK_SIZE * (MAP_WIDTH / 2)) ) + (current_x * BLOCK_SIZE);
  185.  
  186.  
  187. /* Travel the matrix of blocks of the piece and draw the blocks that are filled. */
  188. for ( i = 0; i < PIECE_SIZE; i++ )
  189. for ( j = 0; j < PIECE_SIZE; j++ ) {
  190. switch ( piece[j][i] ) {
  191. case 1: piece_color = GREEN; break; /* For each block (except the pivot). */
  192. case 2: piece_color = BLUE; break; /* For the pivot. */
  193. default: piece_color = YELLOW;break;
  194. }
  195. if ( piece[j][i] )
  196. draw_rectangle (
  197. mPixelsX + i * BLOCK_SIZE,
  198. mPixelsY + j * BLOCK_SIZE,
  199. (mPixelsX + i * BLOCK_SIZE) + BLOCK_SIZE - 1,
  200. (mPixelsY + j * BLOCK_SIZE) + BLOCK_SIZE - 1,
  201. piece_color );
  202. }
  203. }
  204.  
  205.  
  206. /* ----------------------- DRAW_MAP -------------------------------------- */
  207.  
  208. void draw_map ()
  209. {
  210. /* Calculate the limits of the board in pixels. */
  211. int X1 = MAP_POSITION - (BLOCK_SIZE * (MAP_WIDTH / 2)) - 1;
  212. int X2 = MAP_POSITION + (BLOCK_SIZE * (MAP_WIDTH / 2));
  213. int Y = screen_height - (BLOCK_SIZE * MAP_HEIGHT);
  214.  
  215. /* Rectangles that delimits the board. */
  216. draw_rectangle ( X1 - BOARD_LINE_WIDTH, Y, X1, screen_height - 1, BLUE);
  217. draw_rectangle ( X2, Y, X2 + BOARD_LINE_WIDTH, screen_height - 1, BLUE);
  218.  
  219. /* Drawing the blocks that are already stored in the board. */
  220. X1++;
  221. for ( i = 0; i < MAP_WIDTH; i++ )
  222. for ( j = 0; j < MAP_HEIGHT; j++ )
  223. if ( map(i, j) )
  224. draw_rectangle (
  225. X1 + i * BLOCK_SIZE,
  226. Y + j * BLOCK_SIZE,
  227. (X1 + i * BLOCK_SIZE) + BLOCK_SIZE - 1,
  228. (Y + j * BLOCK_SIZE) + BLOCK_SIZE - 1,
  229. RED );
  230. }
  231.  
  232.  
  233. /* ----------------------- MAIN ------------------------------------------ */
  234.  
  235. int main()
  236. {
  237. add_rubbish_to_map(); /* TODO */
  238.  
  239. create_new_piece(); /* First piece. */
  240. /* Get the actual clock milliseconds. */
  241. time_1 = SDL_GetTicks();
  242.  
  243. /* ----- Main Loop --------------------------------------------------------- */
  244. while ( !is_key_down(SDLK_ESCAPE) ) {
  245. /* ----- Draw -------------------------------------------------------------- */
  246. clear_screen ();
  247.  
  248. draw_map ();
  249. draw_piece ();
  250. draw_next_piece (); /* LATER. */
  251. draw_shadow (); /* TODO */
  252.  
  253. update_screen (); /* Put the graphic context in the screen. */
  254.  
  255. /* ----- Input ------------------------------------------------------------- */
  256. int key = poll_key();
  257. switch (key) {
  258. case (SDLK_RIGHT):
  259. current_x++;
  260. if ( !is_possible_movement() )
  261. current_x--;
  262. break;
  263. case (SDLK_LEFT):
  264. current_x--;
  265. if ( ! )
  266. current_x++;
  267. break;
  268. #if 0
  269. case (SDLK_DOWN):
  270. current_y++;
  271. if ( !is_possible_movement() )
  272. current_y--;
  273. break;
  274. #endif
  275. case (SDLK_DOWN):
  276. /* Check collision from up to down. */
  277. while ( is_possible_movement() )
  278. current_y++;
  279. current_y--;
  280. store_piece ();
  281.  
  282. delete_possible_lines ();
  283. if ( is_game_over()) {
  284. puts( "GAME OVER" );
  285. get_key();
  286. exit(0);
  287. }
  288. create_new_piece();
  289. break;
  290. case (SDLK_UP):
  291. rotate_piece();
  292. if ( !is_possible_movement() ) {
  293. /* Stupid hack... =( */
  294. rotate_piece();
  295. rotate_piece();
  296. rotate_piece();
  297. }
  298. break;
  299. default:
  300. puts( "Suka. <, >, A, V." );
  301. break;
  302. }
  303.  
  304. /* ----- Vertical movement ------------------------------------------------- */
  305. time_2 = SDL_GetTicks();
  306.  
  307. if ( (time_2 - time_1) > WAIT_TIME ) {
  308. current_y++;
  309. if ( !is_game_over() ) {
  310. store_piece ();
  311. delete_possible_lines ();
  312. if ( is_game_over() ) {
  313. puts( "GAME OVER" );
  314. get_key();
  315. exit(0);
  316. }
  317. create_new_piece();
  318. }
  319. time_1 = SDL_GetTicks();
  320. }
  321. } /* while ( !is_key_down(SDLK_ESCAPE) ) */
  322.  
  323. return 0;
  324. }

Report this snippet  

You need to login to post a comment.