Tetris under Win-TC

Source: Internet
Author: User

# Include <graphics. h> <br/> # include <stdio. h> <br/> # include <dos. h> <br/> # include <conio. h> <br/> # include <time. h> <br/> # include <math. h> <br/> # include <stdlib. h> <br/>/* define the position on the screen in the upper left corner */<br/> # define mapxoft 9 <br/> # define mapyoft 5 <br/> /* position displayed in the next square */<br/> # define mapxoft1 13 <br/> # define mapyoft1-2 </P> <p> # define left 0x4b00 <br/> # define right 0x4d00 <br/> # define down 0x5000/* This key is the acceleration key */<br/> # def INE up 0x4800/* This key is the deformation key */<br/> # define ESC 0x011b/* This key is the return key */<br/> # define enter 0x1c0d </ p> <p> # define timer 0x1c/* indicates the interrupt Number of the clock interrupt */<br/>/* indicates that the interrupt processing function is slightly different in C and C ++. <Br/> If _ cplusplus is defined, it indicates that it is in the C ++ environment; otherwise, it is in the C environment. */<Br/> # ifdef _ cplusplus <br/> # DEFINE _ cppargs... <br/> # else <br/> # DEFINE _ cppargs <br/> # endif <br/> int timercounter = 0;/* The timing variable increases by 18 per second. */<Br/>/* pointer to the interrupt processing function (handle) at the entry point of the original clock interrupt processing process */<br/> void interrupt (* oldhandler) (_ cppargs); <br/>/* new clock interrupt processing function */<br/> void interrupt newhandler (_ cppargs) <br/> {<br/>/* increase the Global Counter */<br/> timercounter ++; </P> <p>/* call the old routine */<br/> oldhandler (); <br/>}< br/>/* set a new clock interrupt processing process */<br/> void settimer (void interrupt (* intproc) (_ cppargs )) <br/>{< br/> oldhandler = getvect (Time R); <br/> disable ();/* disable all interrupts when a new clock interrupt processing process is set */<br/> setvect (timer, intproc ); <br/> enable (); /* enable interrupt */<br/>}< br/>/* recover the original clock interrupt processing process */<br/> void killtimer () <br/>{< br/> disable (); <br/> setvect (timer, oldhandler); <br/> enable (); <br/>}</P> <p> struct shape <br/>{< br/> int XY [8], next; <br/> }; <br/> struct shape shapes [19] = <br/> {<br/>/* X1, Y1, X2, Y2, X3, Y3, X4, y4 refers to the relative coordinates of the four small blocks, and next refers to the small block with the deformation of the square <br/> {X1, Y1, X2, Y2, X3, Y3, X4, Y4, next} */<br/> {0,-2, 0,-1, 0, 0, 1, 0, 1 },< br/> {-1, 0, 0, 0, 1,-1, 1, 0, 2}, <br/> {0, -2, 1,-2, 1,-1, 1, 0, 3}, <br/> {-1,-1,-1, 0, 0, -1, 1,-1, 0}, <br/> {0,-2, 0,-1, 0, 0, 1,-2, 5 }, <br/> {-1,-1, 0,-1, 1,-1, 1, 0, 6}, <br/> {0, 0, 1, -2, 1,-1, 1, 0, 7}, <br/> {-1,-1,-1, 0, 0, 0, 1, 0, 4 },< br/> {-1, 0, 0,-1, 0, 0, 1, 0, 9 },< br/> {0,-2, 0,-1, 0, 0, 1,-1, 10}, <br/> {-1,-1, 0,-1, 1,-1, 0,}, <br/> {0,-1, 1,-2, 1, -1, 1, 0, 8}, <br/> {-1, 0, 0,-1, 0, 0, 1,-1,13 }, <br/> {0,-2, 0,-1, 1,-1, 1, 0, 12}, <br/> {-1,-1, 0, -1, 0, 0, 1,}, <br/> {0,-1, 0, 0, 1,-2, 1 }, <br/> {0,-3, 0,-2, 0,-1, 0,}, <br/> {-1, 0, 0, 0, 1, 0, 2,}, <br/> {0,-1, 0, 0, 1,-1, 1,} <br/> }; <br/> int Board [10] [20] = {0};/* defines that the game board is initialized to 0 */<br/> char SP [] = "0 ", le [] = "0", SC [] = "00000"; <br/> int speed, speed0, level, score; <br/> int sign, flag; <br/> int style, style1; /* style indicates the type of the current square and style1 indicates the type of the square to be output */<br/> void draw_block (int x, int y, int style, int way ); <br/> void draw_little_block (int x, int y); <br/> void Init (); <br/> void initialize (); <br/> void speed_change (void); <br/> void score_change (INT); <br/> void kill_line (INT y ); <br/> void fill_board (int x, int y, int style); <br/> I NT change (int * I, int * j, int key); <br/> void renovate (void); <br/> void ajustment (void ); <br/> void level_change (void); </P> <p> main () <br/>{< br/> int I, j, key, X0 = 5, y0 = 1; <br/> randomize (); <br/> while (! Flag)/* 0 indicates that the game is restarted */<br/> {<br/> level = score = speed = 0; <br/> strcpy (Le, "0"); <br/> strcpy (SP, "0"); <br/> strcpy (SC, "00000 "); <br/> for (I = 0; I <10; I ++) <br/> for (j = 0; j <20; j ++) <br/> Board [I] [J] = 0;/* initialize some variables as 0 */<br/> initialize (); /* initialize to the graphics mode */<br/> Init ();/* initialize the game board splitter */<br/> settimer (newhandler ); /* modify the clock interruption */<br/> ajustment ();/* adjust the speed and height before starting the game */</P> <p> If (level> 0) <br/> level_change ();/* varies with height Confirm whether the Square exists */</P> <p> style = random (19);/* randomly determine the block type */</P> <p> while (1) <br/>{< br/> I = x0, j = y0; </P> <p> style1 = random (19 ); /* randomly determine the expected block type */<br/> setcolor (white); <br/> Sign = 1; <br/> draw_block (mapxoft1, mapyoft1, style1, 1); <br/>/* draw the forthcoming square */</P> <p> for (j = y0; j <= 20; j ++) /* Drop the square */<br/> {</P> <p> If (! Check_block (I, j, style) <br/> break; <br/> draw_block (I, j, style, 1 ); </P> <p> while (1) <br/> {<br/> If (speed0 = 0) /* processing when the acceleration key is not pressed */</P> <p >{< br/> If (timercounter> 18/(speed + 1 )) <br/> {<br/>/* restore the timing variable */<br/> timercounter = 0; <br/> break; <br/>}< br/> else if (timercounter> 18/(9 + 1 )) /* processing when the acceleration key is pressed */<br/> {<br/>/* restoring the timing variable */<br/> timercounter = 0; <br/> speed0 = 0; <br/> break; <br/>}< br/> If (biosk Ey (1) <br/>{< br/> key = bioskey (0); <br/> If (Change (& I, & J, key )) /* adjust the value based on the key values */<br/> {<br/> flag = 1; <br/> goto end; <br/>}</P> <p> draw_block (I, j, style, 0 ); <br/> renovate ();/* refresh screen */</P> <p >}</P> <p> If (j = y0) <br/> break; <br/> j --; </P> <p> draw_block (I, j, style, 1 ); </P> <p> fill_board (I, j, style); </P> <p> Sign = 1; <br/> draw_block (mapxoft1, mapyoft1, style1, 0); <br/> style = style1; <br/> K Ill_line (j);/* delete function, delete several rows and change the score and speed */</P> <p> while (bioskey (1 )) /* clear the keys in the memory */<br/> key = bioskey (0); <br/>}< br/> setcolor (Cyan ); <br/> settextstyle (0, 0, 2); <br/> timercounter = 0; </P> <p> while (1) <br/> If (timercounter> 54) <br/>{< br/> timercounter = 0; <br/> break; <br/>}< br/> while (bioskey (1 )) /* clear the keys in the memory */<br/> key = bioskey (0); <br/> outtextxy (400,340, "Game over! "); <Br/> outtextxy (360,360," enter to replay. "); <br/> outtextxy (360,380," ESC to quit. "); <br/> while (bioskey (1) = 0); <br/> key = bioskey (0); </P> <p> end :; <br/> closegraph (); <br/> killtimer (); <br/> If (Key = ESC | flag) <br/> break; <br/>}</P> <p> void initialize () /* initialize the image mode */<br/> {<br/> int gdriver = VGA, gmode = vgahi, errorcode; </P> <p>/* initialize graphics mode */</P> <p> initgraph (& gdrive R, & gmode, "C: // turboc2"); </P> <p>/* read result of initialization */<br/> errorcode = graphresult (); </P> <p> If (errorcode! = Grok)/* an error occurred */</P> <p >{< br/> printf ("graphics error: % s/n", grapherrormsg (errorcode )); <br/> printf ("press any key to halt:"); <br/> getch (); <br/> exit (1 ); /* return with error code */<br/>}< br/> void Init (void) /* initialize the game board splitter */<br/> {<br/> int x1; <br/> X1 = 5 + mapxoft; <br/> setcolor (green ); <br/> circle (X1 + 0.5) * 16, (MAPYOFT-2) * 16, 3*16); <br/> setcolor (white); <br/> line (x 1-0.6) * 16, (MAPYOFT-3.2) * 16, X1 * 16, (MAPYOFT-3.2) * 16); <br/> line (X1 + 1.4) * 16, (MAPYOFT-3.2) * 16, (X1 + 2.0) * 16, (MAPYOFT-3.2) * 16); <br/> line (X1 + 0.5) * 16, (MAPYOFT-2) * 16, (X1 + 0.5) * 16, (MAPYOFT-1.7) * 16); <br/> circle (X1 + 0.5) * 16, (MAPYOFT-0.9) * 16, 0.3*16); <br/> setcolor (Cyan); <br/> line (mapxoft + 3) * 16, (mapyoft + 21) * 16, mapxoft * 16, (mapyoft + 23.5) * 16); <br/> line (mapxoft + 5.5) * 16, (mapyoft + 21) * 16, mapxoft * 16, (mapyof T + 26) * 16); <br/> line (mapxoft + 9) * 16, (mapyoft + 21) * 16, (mapxoft + 12) * 16, (mapyoft + 23.5) * 16); <br/> line (mapxoft + 6.5) * 16, (mapyoft + 21) * 16, (mapxoft + 12) * 16, (mapyoft + 26) * 16); <br/> setcolor (magenta); <br/> ellipse (mapxoft + 1) * 16, (mapyoft + 11, ,); <br/> ellipse (mapxoft + 11) * 16, (mapyoft + 11) * 16,-90, 90 ); <br/> circle (mapxoft + 24) * 16, (mapyoft + 6) * 16, 6*16); <br/> setcolor (white); <br/> Rectangle (x1-0.5) * 16, (MAPYOFT-2.9) * 16, (x1-0.1) * 16, (MAPYOFT-2.5) * 16); <br/> rectangle (X1 + 1.5) * 16, (MAPYOFT-2.9) * 16, (X1 + 1.9) * 16, (MAPYOFT-2.5) * 16); <br/> setcolor (yellow ); <br/> rectangle (10*16, 6*16, 20*16, 26*16); <br/> settextstyle (0, 0, 2); <br/> outtextxy (5*16, 13.5*16, "level"); <br/> outtextxy (20.4*16, 13.5*16, "Speed"); <br/> outtextxy (mapxoft + 22) * 16, (mapyoft + 3) * 16, "score"); <br/> settextstyle (0, 0, 4); <br/> outtextxy (6.5*16, 15*16, "0"); <br/> outtextxy (21.9*16, 15*16, "0 "); <br/> settextstyle (0, 0, 3); <br/> outtextxy (mapxoft + 21) * 16, (mapyoft + 6) * 16, SC ); <br/>}</P> <p> void ajustment (void)/* adjust the speed and height before starting the game */<br/> {<br/> int key, boo = 1, Left = 1; <br/> setcolor (yellow); <br/> settextstyle (500,340, 2); <br/> outtextxy, "Press"); <br/> outtextxy (500,360, "enter"); <br/> outtextxy (500,380, "to"); <br/> ou Ttextxy (500,400, "Start"); <br/> while (1) <br/>{< br/> If (timercounter> 8) <br/> {<br/> timercounter = 0; <br/> boo * =-1; <br/> If (BOO =-1) <br/> setcolor (black); <br/> else <br/> setcolor (yellow); <br/> If (Left = 1) <br/>{< br/> line (6*16, 17*16, 8.5*16, 17*16); <br/> setcolor (black ); <br/> line (21.4*16, 17*16, 23.9*16, 17*16 ); <br/>}</P> <p> else <br/> {<br/> line (21.4*16, 17*16, 23.9*16, 17*16 ); <br/> set Color (black); <br/> line (6*16, 17*16, 8.5*16, 17*16 ); <br/>}< br/> If (bioskey (1) <br/>{< br/> key = bioskey (0 ); <br/> If (Key = up | key = down) <br/> {<br/> setcolor (black); <br/> settextstyle (0, 0, 4); <br/> outtextxy (6.5*16, 15*16, le); <br/> outtextxy (21.9*16, 15*16, SP ); <br/>}< br/> If (Key = enter) <br/> break; <br/> else if (Key = up) <br/>{< br/> If (Left = 1) <br/>{< br/> level --; <br/> If (El =-1) <br/> level = 9; <br/>}< br/> else <br/>{< br/> speed --; <br/> If (speed =-1) <br/> speed = 9; <br/>}< br/> else if (Key = down) <br/>{< br/> If (Left = 1) <br/> {<br/> level ++; <br/> If (Level = 10) <br/> level = 0; </P> <p >}< br/> else <br/>{< br/> speed ++; <br/> If (speed = 10) <br/> speed = 0; <br/>}</P> <p> else if (Key = left | right) <br/> left * =-1; <br/> setcolor (yellow); <br/> sp [0] = speed + '0'; <br/> le [0] = level + '0'; <br/> settextstyle (0, 0, 4 ); <br/> outtextxy (6.5*16, 15*16, le); <br/> outtextxy (21.9*16, 15*16, SP ); <br/>}< br/> setcolor (yellow); <br/> SP [0] = speed + '0 '; <br/> le [0] = level + '0'; <br/> settextstyle (6.5, 4); <br/> outtextxy (* 16, 15*16, le); <br/> outtextxy (21.9*16, 15*16, SP); <br/> setcolor (black); <br/> line (21.4*16, 17*16, 23.9*16, 17*16); <br/> line (6*16, 17*1 6, 8.5*16, 17*16); <br/> settextstyle (500,340, 2); <br/> outtextxy (, "press "); <br/> outtextxy (500,360, "enter"); <br/> outtextxy (500,380, "to"); <br/> outtextxy (500,400, "Start "); <br/>}</P> <p> void level_change (void)/* determine whether the Square exists randomly based on the height */<br/> {<br/> int I, j; <br/> setcolor (white); <br/> for (I = 1; I <= 10; I ++) <br/> for (j = 1; j <= level; j ++) <br/> If (random (2) <br/>{< br/> Board [I-1] [20-j] = 1; </P> <p> draw_l Ittle_block (I + mapxoft, 21-j + mapyoft); <br/>}</P> <p> void draw_block (INT X, int y, int style, int way) <br/>/* This X, Y is the virtual coordinate, which is one of the 19 forms: 0 ~ In 18, a <br/> X and Y are the coordinates of 10*20 squares, and the subscript starts from 1 <br/> way is filled in table 1, clear */<br/> {<br/> int X1 = x + mapxoft, Y1 = Y + mapyoft; <br/> int I; <br/> If (way = 1) <br/> setcolor (white); <br/> else <br/> setcolor (black ); <br/> for (I = 0; I <= 6; I + = 2) <br/> draw_little_block (X1 + shapes [style]. XY [I], Y1 + shapes [style]. XY [I + 1]); <br/> If (Sign = 1) <br/> Sign = 0; <br/>}</P> <p> void draw_little_block (int x, int y)/* Only draws small blocks in the 10*20 game version, no. */<Br/> {<br/> If (x> = 10 & x <20 & Y> = 6 & Y <26) | sign) <br/> {<br/> rectangle (x * 16, y * 16, x * 16 + 16, y * 16 + 16 ); <br/> circle (x * 16 + 8, y * 16 + 8, 4 ); <br/>}</P> <p> int check_block (int x, int y, int style)/* Check In (x, y) if yes, 1 is returned. Otherwise, 0 */<br/>{< br/> int X1 = x + mapxoft, Y1 = Y + mapyoft is returned; <br/> int X2, Y2, I; <br/> for (I = 0; I <= 6; I + = 2) <br/> {<br/> X2 = X1 + shapes [style]. XY [I]; <br/> Y2 = Y1 + shapes [style]. XY [I + 1]; <br/> If (X2> = 10 & X2 <20 & Y2 <26 & (Y2 <6 | Board [x2-10] [y2-6] = 0 )) <br/> continue; <br/> else <br/> break; <br/>}< br/> if (I = 8) <br/> return 1; <br/> else <br/> return 0; <br/>}< br/> void speed_change (void)/* This is a variable speed function. When the score exceeds the first level, that is, add one. */<Br/> {<br/> If (score> = (SP [0]-'0') * 100 & (SP [0]-'0 ') <9) <br/>{< br/> settextstyle (21.9, 4); <br/> setcolor (black); <br/> outtextxy (* 16, 15*16, SP); <br/> SP [0] ++; <br/> speed ++; <br/> settextstyle (0, 0, 4 ); <br/> setcolor (yellow); <br/> outtextxy (21.9*16, 15*16, SP ); <br/>}< br/> void score_change (INT count)/* count indicates the number of deleted rows, accordingly, the score value is changed */<br/> {<br/> int score_inc [4] = {1, 3, 7, 13}; <br/> int I; </ P> <p> setcolor (black); <br/> settextstyle (0, 0, 3); <br/> outtextxy (mapxoft + 21) * 16, (mapyoft + 6) * 16, SC);/* place the original score here */</P> <p> score + = score_inc [count-1]; <br/> SC [4] + = score_inc [count-1]; <br/> for (I = 4; I> = 1; I --) <br/> {</P> <p> SC [I-1] + = (SC [I]-'0')/10; <br/> SC [I]-= (SC [I]-'0')/10*10; <br/>}< br/> setcolor (yellow ); <br/> settextstyle (0, 0, 3); <br/> outtextxy (mapxoft + 21) * 16, (mapyoft + 6) * 16, SC ); /* output new score */< BR/>}</P> <p> void kill_line (INT y)/* Elimination function, delete several rows and change the score and speed */<br/> {<br/> int COUNT = 0, I, T = 1, J, K; <br/> for (; y >=1 & T <= 4; y --, t ++) <br/>{< br/> for (I = 1; I <= 10; I ++) <br/> If (! Board [I-1] [Y-1]) <br/> break; <br/> if (I = 11) <br/>{< br/> count ++; <br/> for (k = 1; k <= 10; k ++) <br/>{< br/> setcolor (black ); <br/> draw_little_block (K + mapxoft, Y + mapyoft); <br/>}< br/> for (j = Y-1; j> = 1; j --) <br/> for (k = 1; k <= 10; k ++) <br/> {<br/> Board [k-1] [J] = Board [k-1] [J-1]; <br/> If (Board [k-1] [J]) <br/>{< br/> setcolor (black); <br/> draw_little_block (K + mapxoft, J + mapyoft); <br/> setcolor (white); <br /> Draw_little_block (K + mapxoft, J + 1 + mapyoft) ;}< br/>}< br/> renovate (); <br/> delay (10000 ); <br/> Y ++; <br/>}</P> <p> If (count> 0) <br/>{</P> <p> setcolor (Cyan); <br/> settextstyle (500,370, 3); <br/> outtextxy (, "Good! "); <Br/> timercounter = 0; <br/> while (1) <br/> If (timercounter> 3) <br/>{< br/> timercounter = 0; <br/> break; <br/>}</P> <p> setcolor (black ); <br/> settextstyle (500,370, 3); <br/> outtextxy (, "Good! "); </P> <p> score_change (count); <br/> speed_change (); <br/>}< br/> void fill_board (int x, int y, int style)/* When a block is stopped, change the corresponding value to 1 */<br/>{</P> <p> int X1, Y1, I; <br/> for (I = 0; I <= 6; I + = 2) <br/> {<br/> X1 = x + shapes [style]. XY [I]; <br/> Y1 = Y + shapes [style]. XY [I + 1]; <br/> Board [x1-1] [y1-1] = 1; <br/>}< br/> int change (int * I, int * j, int key) /* function for Square movement and deformation */<br/> {<br/> int key1; <br/> If (Key = up & check_block (* I, * j, shapes [style]. next) <br/>{< br/> draw_block (* I, * j, style, 0); <br/> style = shapes [style]. next; <br/> draw_block (* I, * j, style, 1 ); <br/>}< br/> else if (Key = left & check_block (* I-1, * j, style )) <br/> {<br/> draw_block (* I, * j, style, 0); <br/> (* I )--; <br/> draw_block (* I, * j, style, 1 ); <br/>}< br/> else if (Key = Right & check_block (* I + 1, * j, style )) <br/> {<br/> draw_block (* I, * j, style, 0); <br/> (* I) ++; <br/> draw_block (* I, * j, style, 1); <br/>}< br/> else if (Key = down & check_block (* I, * j + 1, style) <br/>{< br/> draw_block (* I, * j, style, 0); <br/> (* j) ++; <br/> draw_block (* I, * j, style, 1); <br/> speed0 = 9; <br/>}< br/> else if (Key = enter) <br/>{< br/> while (1) <br/>{</P> <p> If (bioskey (1) <br/> key1 = bioskey (0 ); <br/> If (key1 = enter) <br/> break; <br/>}< br/> else if (Key = ESC) <br/> return 1; <br/> else if (Key = 21040) <br/>{< br/> Sign = 1; <br/> draw_block (mapxoft1, mapyoft1, style1, 0); <br/> style1 = 16; <br/> Sign = 1; <br/> draw_block (mapxoft1, mapyoft1, style1, 1 ); <br/>}< br/> return 0; <br/>}</P> <p> void renovate (void) /* refresh the screen */<br/> {<br/> int I, j; <br/> setcolor (white); <br/> for (I = 1; I <= 10; I ++) <br/> for (j = 1; j <= 20; j ++) <br/> If (Board [I-1] [J-1] = 1) <br/> draw_little_block (I + mapxoft, J + mapyoft ); </P> <p> setcolor (yellow); <br/> rectangle (10*16, 6*16, 20*16, 26*16 ); </P> <p >}< br/>

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.