Tetris (c)

Source: Internet
Author: User

My mind is old-fashioned. I used erasure and matrix tabulation to create tables. They are all the stuff of our predecessors. Here we don't have to make a shift, but worship and worship ......

However, you can only experience the hardships and difficulties of your own hands ......

In the future, I will encounter a Brain Bug when playing games. I 'd like to leave it easy for developers ......

In addition, I found that the imagination of the masses is amazing. Various idea, various abnormal blocks, creative and spoof blocks are emerging one after another. I searched the internet and found that only what you couldn't think of was possible, no one has done it yet. Alas, creativity will always be the soul of the game. Continue to work hard.

Effect:

Code:

PS: the code is ugly. Sorry, there are still a lot of bugs. Welcome to the bricks, and despise them ......

# Include <stdio. h> # include <stdlib. h> # include <windows. h> # include <time. h> # include <conio. h> # define mod 28 # define size_n 19 # define size_m 12int cur_x, cur_y; int score, Mark, next, map [size_n] [size_m], gamespeed = 300; int shape [28] [6] = {// This is a relatively lazy method, that is, 7 blocks, plus 28 rotation types {0, 0 },{,}, {-,}, {0,-1,-,-}, {0, 1,-}, {,-}, {, 0,-, 1}, {,-, 0,-1 }, {-,}, {0, -, 1}, {-, 0,-,-1}, {-1,-1, }, {, 1,-, 0}, {-,}, {, 1,-, 0}, {-, 0, -,-2}, {-,-,}, {,}, {0,-, 0}, {0,-, 0, ,}, {0,-,-1}, {-, 0,-1,-1,-1, }, {,}, {,}, {0,-3, -3,0 }}; void gotoxy (int x, int y) {// gotoxy is in system. A letter in the H library file, coord C; C. X = X-1; C. y = Y-1; setconsolec Ursorposition (getstdhandle (std_output_handle), c);} void gameover () {// judge the end of the game int I, j, flag = 0; For (j = 1; j <SIZE_M-1; j ++) {If (Map [1] [J]! = 0) {flag = 1; break;} If (flag = 1) {for (I = 1; I <SIZE_N-1; I ++) {gotoxy (2, I + 1); For (j = 1; j <SIZE_M-1; j ++) {printf ("□");} puts (" ") ;}gotoxy ); printf ("game over! "); Gotoxy (1, size_n + 1); exit (0) ;}} void showmap (int id) {int I, j; gotoxy (1,1); If (ID! =-1) {for (I = 0; I <size_n; I ++) {for (j = 0; j <size_m; j ++) {if (I = 0 & J = 0 | I = 0 & J = SIZE_M-1 | j = 0 & I = SIZE_N-1 | j = SIZE_M-1 & I = SIZE_N-1) printf (""); else if (I = 0 | I = SIZE_N-1) printf ("--"); else if (j = 0 | j = SIZE_M-1) printf ("|"); else if (Map [I] [J] = 2) printf ("■ "); else if (I = cur_x + shape [ID] [0] & J = cur_y + shape [ID] [1] | I = cur_x + shape [ID] [2] & J = cur_y + shape [ID] [3] | I = cur_x + shape [ID] [4] & J = cur_y + S HAPE [ID] [5] | I = cur_x & J = cur_y) printf ("■ "); else if (Map [I] [J] = 0) printf ("");} if (I = 1) printf ("Next :"); if (I = 11) printf ("score: % d", score); if (I = 14) printf ("Speed: % d ", score/100 + 1); puts ("") ;}} else {mark = 1; for (I = 0; I <size_n; I ++) {for (j = 0; j <size_m; j ++) {if (I = 0 & J = 0 | I = 0 & J = SIZE_M-1 | j = 0 & I = SIZE_N-1 | j = SIZE_M-1 & I = SIZE_N-1) printf (""); else if (I = 0 | I = SIZE_N-1) printf ("--"); else if (J = 0 | j = SIZE_M-1) printf ("|"); else if (Map [I] [J] = 2) printf ("■ "); else if (Map [I] [J] = 0) printf ("");} if (I = 1) printf ("Next :"); if (I = 11) printf ("score: % d", score); if (I = 14) printf ("Speed: % d ", score/100 + 1); puts ("") ;}/ * for processing the next square, erase it first and then draw a picture */gotoxy (); printf (""); for (I = 0; I <6; I = I + 2) {gotoxy (30 + 2 * shape [ID] [I + 1], 6 + shape [ID] [I]); printf ("") ;}gotoxy (30,6); printf ("■"); for (I = 0; I <6; I = I + 2) {gotoxy (30 + 2 * Shape [next] [I + 1], 6 + shape [next] [I]); printf ("■");} Sleep (gamespeed );} void Init (int id) {// initialization function, cur_x, cur_y is a global variable, marking the location of the moving square memset (MAP, 0, sizeof (MAP )); for (INT I = 0; I <size_n; I ++) {for (Int J = 0; j <size_m; j ++) if (I = SIZE_N-1 | j = 0 | j = SIZE_M-1) map [I] [J] =-1;} cur_x = 0; cur_y = 5; showmap (ID);} int judge_in (int x, int y, int ID) {// determine whether the field exists, or whether it is legal if (Map [x] [Y]! = 0) return 0; For (INT I = 0; I <6; I = I + 2) {If (Map [x + shape [ID] [I] [Y + shape [ID] [I + 1]! = 0) return 0;} return 1;} void fun_score () {// score, erase the flicker of the row, and translate the graph downward for (INT I = 1; I <SIZE_N-1; I ++) {int flag = 0; For (Int J = 1; j <SIZE_M-1; j ++) {If (Map [I] [J]! = 2) {flag = 1; break;} If (flag = 0) {int K = 3; while (k --) {gotoxy (2, I + 1 ); for (int ii = 1; II <SIZE_M-1; II ++) {If (Map [I] [II] = 2) {If (K % 2 = 1) printf (""); else printf ("■") ;}} sleep (100) ;}for (int ii = I; II> 1; II --) {for (int jj = 1; JJ <SIZE_M-1; JJ ++) map [II] [JJ] = map [II-1] [JJ];} showmap (-1 ); score + = 10; If (score % 100 = 0 & score! = 0) gamespeed-= 50 ;}} void main () {int I, ID, set = 1; srand (Time (null); Id = rand () % MOD; id = (ID + mod) % MOD; next = rand () % MOD; next = (next + mod) % MOD; Init (ID); While (1) {here: Mark = 0; If (set = 0) {id = next; next = rand () % MOD; next = (next + mod) % MOD; cur_x = 0; cur_y = 5; set = 1;} while (! Kbhit () {gameover (); If (judge_in (cur_x + 1, cur_y, ID) = 1) cur_x ++; else {map [cur_x] [cur_y] = 2; for (I = 0; I <6; I = I + 2) map [cur_x + shape [ID] [I] [cur_y + shape [ID] [I + 1] = 2; fun_score (); Set = 0 ;} if (mark! = 1) showmap (ID); goto here;} // end of while (! Kbhit () Char key; Key = getch (); If (Key = 72) {int TMP = ID; Id ++; if (ID % 4 = 0 & ID! = 0) id = ID-4; If (judge_in (cur_x, cur_y, ID )! = 1) id = TMP;} else if (Key = 80 & judge_in (cur_x + 1, cur_y, ID) = 1) cur_x ++; else if (Key = 75 & judge_in (cur_x, cur_y-1, ID) = 1) cur_y --; else if (Key = 77 & judge_in (cur_x, cur_y + 1, ID) = 1) cur_y ++; else if (Key = 27) {gotoxy (1, size_n + 1); exit (0 );}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.