Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.21 KB | None | 0 0
  1. /*
  2. * tetris.c
  3. *
  4. * compile: gcc -o tetris -lncurses tetris.c
  5. *
  6. * tetrimino types
  7. * 1: #### 2: ## 3: ## 4: ##
  8. * ## ## ##
  9. *
  10. * 5: # 6: # 7: #
  11. * ### ### ###
  12. *
  13. * How to play
  14. * 'h': move a tetrimino to left
  15. * 'j': drop a tetrimino shortly
  16. * 'k': rotate a tetrimino anticlockwise
  17. * 'l': move a tetrimino to right
  18. * 'q': quit a game
  19. */
  20. #include <stdlib.h>
  21. #include <ncurses.h>
  22. #include <unistd.h>
  23. #include <time.h>
  24. #define WIDTH (10)
  25. #define HEIGHT (20)
  26. #define COUNT (40)
  27.  
  28. typedef struct tetrimino {
  29. int type; // 1~7
  30. int x, y;
  31. int array[4][2];
  32. } tetrimino;
  33.  
  34. int tet_type[7][4][2] = {{{0,-1},{0,0},{0,1},{0,2}}, // type 1
  35. {{0,0},{0,1},{1,0},{1,1}}, // type 2
  36. {{0,0},{0,1},{1,-1},{1,0}}, // type 3
  37. {{0,0},{1,0},{-1,1},{0,1}}, // type 4
  38. {{-1,0},{-1,1},{0,1},{1,1}}, // type 5
  39. {{1,-1},{-1,0},{0,0},{1,0}}, // type 6
  40. {{0,-1},{-1,0},{0,0},{1,0}}}; // type 7
  41.  
  42.  
  43. int board[WIDTH+2][HEIGHT+2] = {};
  44.  
  45. void init() {
  46. srand((unsigned)time(NULL)); // set random seed
  47.  
  48. initscr();
  49. leaveok(stdscr, true); //no cursor
  50. curs_set(0); // no cursor (again)
  51. noecho();
  52. start_color();
  53. init_pair(1, COLOR_WHITE, COLOR_BLACK);
  54. init_pair(2, COLOR_RED, COLOR_RED); // type 1
  55. init_pair(3, COLOR_YELLOW, COLOR_YELLOW); // type 2
  56. init_pair(4, COLOR_MAGENTA, COLOR_MAGENTA); // type 3
  57. init_pair(5, COLOR_GREEN, COLOR_GREEN); // type 4
  58. init_pair(6, COLOR_BLUE, COLOR_BLUE); // type 5
  59. init_pair(7, COLOR_CYAN, COLOR_CYAN); // type 6
  60. init_pair(8, COLOR_WHITE, COLOR_WHITE); // type 7
  61. bkgd(COLOR_PAIR(1));
  62. erase();
  63. }
  64.  
  65. void init_board() {
  66. for (int i=0; i<=HEIGHT+1; i++) {
  67. board[0][i] = 7; // left side wall
  68. board[WIDTH+1][i] = 7; // right side wall
  69. }
  70. for (int i=0; i<=WIDTH+1; i++)
  71. board[i][HEIGHT+1] = 7; //ground
  72. }
  73.  
  74. void finalize() {
  75. endwin();
  76. }
  77.  
  78. void gameover() {
  79. attron(COLOR_PAIR(1));
  80. move(10,4);
  81. addstr("G A M E O V E R");
  82. timeout(-1);
  83. getch();
  84. }
  85.  
  86. void display_board() {
  87. for (int j=0; j<=HEIGHT+1; j++) {
  88. for (int i=0; i<=WIDTH+1; i++) {
  89. attron(COLOR_PAIR(board[i][j]+1));
  90. move(j, i*2);
  91. addstr(" ");
  92. }
  93. }
  94. }
  95.  
  96. void display_tetrimino(tetrimino *t) {
  97. for (int i=0; i<4; i++) {
  98. attron(COLOR_PAIR(t->type+1));
  99. move( t->y + t->array[i][1],
  100. (t->x + t->array[i][0]) * 2 );
  101. addstr(" ");
  102. }
  103. }
  104.  
  105. int check_movable(tetrimino *t) {
  106. for (int i=0; i<4; i++) {
  107. if ( board[ t->x + t->array[i][0] ]
  108. [ t->y + t->array[i][1] ] )
  109. return 0;
  110. }
  111. return 1;
  112. }
  113.  
  114. int drop(tetrimino *t) {
  115. t->y++;
  116. if(!check_movable(t)) {
  117. t->y--;
  118. return 1;
  119. }
  120. return 0;
  121. }
  122.  
  123. void move_left(tetrimino *t) {
  124. t->x--;
  125. if (!check_movable(t))
  126. t->x++;
  127. }
  128. void move_right(tetrimino *t) {
  129. t->x++;
  130. if (!check_movable(t))
  131. t->x--;
  132. }
  133.  
  134. void rotate(tetrimino *t) {
  135. int swap[4][2] = {};
  136. for (int i=0; i<4; i++)
  137. for (int j=0; j<2; j++)
  138. swap[i][j] = t->array[i][j];
  139. for (int i=0; i<4; i++) {
  140. t->array[i][0] = swap[i][1];
  141. t->array[i][1] = -swap[i][0];
  142. }
  143. if (!check_movable(t))
  144. for (int i=0; i<4; i++)
  145. for (int j=0; j<2; j++)
  146. t->array[i][j] = swap[i][j];
  147. }
  148.  
  149. int new_tetrimino(tetrimino *t) {
  150. t -> type = rand()%7 + 1;
  151. t -> x = WIDTH/2;
  152. t -> y = 1;
  153. for (int i=0; i<4; i++)
  154. for (int j=0; j<2; j++)
  155. t -> array[i][j] = tet_type[t->type-1][i][j];
  156. return check_movable(t);
  157. }
  158.  
  159. void anchor(tetrimino *t) {
  160. for (int i=0; i<4; i++) {
  161. board[ t->x + t->array[i][0] ][ t->y + t->array[i][1] ]
  162. = t->type;
  163. }
  164. }
  165.  
  166. int check_row(int r) {
  167. for (int i=1; i<=WIDTH; i++)
  168. if (!board[i][r])
  169. return 0;
  170. return 1;
  171. }
  172.  
  173. void delete_row(int r) {
  174. for (int i=r; i>0; i--)
  175. for (int j=1; j<=WIDTH; j++)
  176. board[j][i] = board[j][i-1];
  177. }
  178.  
  179. int main() {
  180. int cnt = 0;
  181. tetrimino tet;
  182.  
  183. init();
  184. init_board();
  185. clear();
  186. new_tetrimino(&tet);
  187. while (1) {
  188. timeout(5);
  189. switch(getch()) {
  190. case 'q':
  191. goto END;
  192. break;
  193. case 'h':
  194. move_left(&tet);
  195. break;
  196. case 'j':
  197. drop(&tet);
  198. break;
  199. case 'k':
  200. rotate(&tet);
  201. break;
  202. case 'l':
  203. move_right(&tet);
  204. }
  205. if (cnt == COUNT) {
  206. if (drop(&tet)) {
  207. anchor(&tet);
  208. if (!new_tetrimino(&tet))
  209. goto GAME_OVER;
  210. for (int i=HEIGHT; i>0; i--)
  211. if (check_row(i)) {
  212. delete_row(i);
  213. i++;
  214. }
  215. }
  216. cnt = 0;
  217. }
  218. display_board();
  219. display_tetrimino(&tet);
  220. refresh();
  221. cnt++;
  222. }
  223. GAME_OVER:
  224. gameover();
  225. END:
  226. finalize();
  227. return 0;
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement