Implement Tetris In the TC graphic interface

Source: Internet
Author: User

Recently, in order to review graph programming in TC, I wrote a program for Tetris, which can implement the functions of the common Tetris program. Now I will post the source code for everyone to share, due to my limited level, you are welcome to raise any questions. In addition, the decrease speed of the square in this program cannot be changed. If you have any good solutions, please contact me. My QQ is: 496271185.

 

# Include <graphics. h>
# Include <time. h>

# Define cnt_r 30/* Number of Container Lines */
# Define cnt_c 14/* Number of container columns */

# Define CS 10/* block size */
# Define BX 10/* x coordinate in the upper left corner of the container */
# Define by 10/* Y coordinate in the upper left corner of the container */
# Define BSR 30*10/* container size */
# Define BSC 14*10/* container size */
# Define init_x 80/* Square initialization X coordinate */
# Define init_y 10/* Square initialization y coordinate */

/* Define the buttons to respond */
# Define down 0x5000/* keys */
# Define up 0x4800/* keys */
# Define left 0x4b00/* keys */
# Define right 0x4d00/* → key */
# Define ESC 0x11b/* return key */

# Define lscore 100/* eliminate the scores of a row */
# Define SCX 200/* shows the position of the score, X coordinate */
# Define SCY 100/* shows the position of the score, Y coordinate */

/* Defines the square. There are 15 types */
Int B [] [4] [4] = {1, 1, 0, 0,
1, 1, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,

1, 1, 0, 0,
1, 1, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,

1, 1, 0, 0,
1, 1, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,

1, 1, 0, 0,
1, 1, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,

1, 1, 1, 1,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,

1, 0, 0, 0,
1, 0, 0, 0,
1, 0, 0, 0,
1, 0, 0, 0,

1, 1, 1, 1,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,

1, 0, 0, 0,
1, 0, 0, 0,
1, 0, 0, 0,
1, 0, 0, 0,

1, 0, 0, 0,
1, 1, 0, 0,
0, 1, 0, 0,
0, 0, 0, 0,

0, 1, 1, 0,
1, 1, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,

1, 0, 0, 0,
1, 1, 0, 0,
0, 1, 0, 0,
0, 0, 0, 0,

0, 1, 1, 0,
1, 1, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,

0, 1, 0, 0,
1, 1, 0, 0,
1, 0, 0, 0,
0, 0, 0, 0,

1, 1, 0, 0,
0, 1, 1, 0,
0, 0, 0, 0,
0, 0, 0, 0,

0, 1, 0, 0,
1, 1, 0, 0,
1, 0, 0, 0,
0, 0, 0, 0,

1, 1, 0, 0,
0, 1, 1, 0,
0, 0, 0, 0,
0, 0, 0, 0,

1, 1, 0, 0,
0, 1, 0, 0,
0, 1, 0, 0,
0, 0, 0, 0,

0, 0, 1, 0,
1, 1, 1, 0,
0, 0, 0, 0,
0, 0, 0, 0,

1, 0, 0, 0,
1, 0, 0, 0,
1, 1, 0, 0,
0, 0, 0, 0,

1, 1, 1, 0,
1, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,

1, 1, 0, 0,
1, 0, 0, 0,
1, 0, 0, 0,
0, 0, 0, 0,

1, 1, 1, 0,
0, 0, 1, 0,
0, 0, 0, 0,
0, 0, 0, 0,

0, 1, 0, 0,
0, 1, 0, 0,
1, 1, 0, 0,
0, 0, 0, 0,

1, 0, 0, 0,
1, 1, 1, 0,
0, 0, 0, 0,
0, 0, 0, 0 };

Int XY [24] [2] = {2, 2,
2, 2,
2, 2,
2, 2,

4, 1,
1, 4,
4, 1,
1, 4,

2, 3,
3, 2,
2, 3,
3, 2,

2, 3,
3, 2,
2, 3,
3, 2,

2, 3,
3, 2,
2, 3,
3, 2,

2, 3,
3, 2,
2, 3,
3, 2 };

Int box [cnt_r] [cnt_c];/* define container */

Int x = init_x, y = init_y;/* Current Position of the square, global variable */
Int key;/* Save the key message, global variable */
Int K;/* Current square Code */
Time_t oldtm, newtm;/* save time */
Int COUNT = 0;/* counter, record the number of rows that have been eliminated */
Char score [100];

Int showcube (int x, int y, int tag);/* tested */
Int initbox ();/* tested */
Int creatcube ();/* tested */
Int isok (int x, int y);/* tested */
Int input ();/* tested */
Int movedown ();/* tested */
Int moveleft ();/* tested */
Int moveright ();/* tested */
Int changeup ();/* tested */
Int run ();/* tested */
Int Showbox ();/* tested */
Int clearbar (int x, int y, int B [4] [4]);/* tested */
Int bar (int x, int y, int B [4] [4]);/* tested */
Int timeisok ();/* tested */
Int delline ();/* remove rows */
Int printscore ();/* display score */
Int sound ();/* voice */
Int msgbox ();/* message box */
Int help ();/* help information */

/* Initialize the container */
Int initbox ()
{
Int I, J;
For (I = 0; I <cnt_r; ++ I)
For (j = 0; j <cnt_c; ++ J)
Box [I] [J] = 0;
Printscore ();
Help ();
Showbox ();
Return 0;
}

/* Output score */
Int printscore ()
{
Sound ();
Setcolor (black );
Setfillstyle (1, black );
Bar (SCX-5, SCY-5, SCX + 100, SCY + 15 );
Setcolor (4 );
Sprintf (score, "score: % d", count * lscore );
Outtextxy (SCX, SCY, score );
Return 0;
}

Int help ()
{
MoveTo (SCX-5, SCY + 20 );
Outtext ("you can press ESC key to exit the game .");
MoveTo (SCX-5, SCY + 40 );
Outtext ("you can press left key to move the cube to the left .");
MoveTo (SCX-5, SCY + 60 );
Outtext ("you can press right key to move the cube to the right .");
MoveTo (SCX-5, SCY + 80 );
Outtext ("you can press down key to move the cube to the bottom .");
MoveTo (SCX-5, SCY + 100 );
Outtext ("you can press up key to change the shape of cube .");
Return 0;
}

/** Check whether the current square triggers the row elimination action. If yes, the response row is eliminated */
Int delline ()
{
Int end, R, C, I, J;
R = (Y-BY)/CS;/* Start detection from the current row of the square */
End = (R + 3) <cnt_r? R + 3: CNT_R-1;/* end points to the end row */
For (; r <= end; ++ R) {/* control row */
For (C = 0; C <cnt_c; ++ c) {/* control column */
If (Box [r] [c] = 0) break;/* If a line contains spaces, go to the next line */
}
If (C> = cnt_c) {/* The current row should be eliminated */
/* Move the row from the current row to the initial row downward */
For (I = R-1; I> = 0; -- I)/* Copy row I to row I + 1 */
For (j = 0; j <cnt_c; ++ J)
Box [I + 1] [J] = Box [I] [J];
For (j = 0; j <cnt_c; ++ J)
Box [0] [J] = 0;/* remove the first line */
Count ++;
Printscore ();
Showbox ();
}
 
}
Return 0;
}

/* Generate a square and display it */
Int creatcube ()
{
Time_t t = time (0 );
Srand (t );
K = rand () % 24;/* code of the randomly generated square */
If (isok (init_x, init_y )){
Bar (init_x, init_y, B [k]);/* display the generated square */
Oldtm = time (0);/* obtain the initial time */
X = init_x;
Y = init_y;
} Else {
Msgbox ();
Getch ();
Exit (0 );
}
Return 0;
}

/* Message box */
Int msgbox ()
{
Char * MSG = "game over! ";
Setcolor (3 );
Setfillstyle (1, 3 );
Bar (120,140,520,340 );
Setcolor (4 );
Outtextxy( 300,200, MSG );
MoveTo (270,220 );
Sprintf (MSG, "your score is: % d", lscore * count );
Outtext (MSG );
MoveTo (180,240 );
Outtext ("Please press any key to exit this game! ");
Return 0;
}

/* Determine whether the square can arrive */
Int isok (int x, int y)
{
Int I, J;
If (x <BX | x> (bx + BSC-CS * XY [k] [0]) Return-1;/* beyond the left and right boundary, this position cannot reach */
For (I = 0; I <XY [k] [1]; ++ I)
For (j = 0; j <XY [k] [0]; ++ J)
If (Box [(Y-by)/CS) + I] [(X-bx)/CS) + J] + B [k] [I] [J]) = 2 | Y> (bx + BSR-CS * XY [k] [1]) {/* if it exceeds the bottom boundary or overlaps all or part of the existing square, it cannot
Up */

Return 0;
}
Return 1;/* this location can be reached */
}

/* Write the useful information of the current square to the box */
Int input ()
{
Int I, J;
For (I = 0; I <XY [k] [1]; ++ I)
For (j = 0; j <XY [k] [0]; ++ J)
Box [(Y-BY)/CS) + I] [(X-BX)/CS) + J] | = B [k] [I] [J]; /* update the box */
Return 0;
}

/* Move the square downward */
Int movedown ()
{
If (isok (X, Y + CS) = 1 ){
Clearbar (X, Y, B [k]);
Y + = cs;
Bar (X, Y, B [k]);
} Else if (isok (X, Y + CS) = 0) {/* blocks cannot move down */
Input ();/* write B [k] data to the box */
Showbox ();
Delline ();/* remove rows */
Creatcube ();/* generate a new square */
}
Return 0;
}

/* Move the square to the left */
Int moveleft ()
{
If (isok (X-CS, y) = 1 ){
Clearbar (X, Y, B [k]);
X-= cs;
Bar (X, Y, B [k]);}
Return 0;
}

/* Move the square to the right */
Int moveright ()
{
If (isok (x + CS, y) = 1 ){
Clearbar (X, Y, B [k]);
X + = cs;
Bar (X, Y, B [k]);}
Return 0;
}

/* Block deformation */
Int changeup ()
{
Int oldk;
Oldk = K;
++ K;
K = K % 4? (K/4) * 4 + K % 4: K-4;
 
If (isok (x, y) = 1 ){
Clearbar (X, Y, B [oldk]);
Bar (X, Y, B [k]);
}
Else K = oldk;
 
Return 0;
}

/* Automatically move the square downward */
Int run ()
{
If (isok (X, Y + CS) = 1) {/* If the next location can reach */
Clearbar (X, Y, B [k]);
Y + = cs;
Bar (X, Y, B [k]);
} Else if (isok (X, Y + CS) =-1 | isok (X, Y + CS) = 0) {/* the next location cannot be reached */
Input ();/* write B [k] data to the box */
Showbox ();/**/
Delline ();/* remove rows */
Creatcube ();/* generate a new square */
}
Return 0;
}

/* Show small squares */
Int showcube (int x, int y, int tag)
{Int color, bkcolor;
If (TAG = 0 ){
Color = 8;
Bkcolor = white;
} Else if (TAG = 1 ){
Color = 4;
Bkcolor = 2;
}
Setcolor (bkcolor );
Setfillstyle (1, bkcolor );
Bar (X, Y, x + CS, Y + CS );
Setcolor (color );
MoveTo (x, y );
Lineto (x + CS, y );
Lineto (x + CS, Y + CS );
Lineto (X, Y + CS );
Lineto (x, y );
Return 0;
}

/* Display container */
Int Showbox ()
{
Int I, J;
Int x = Bx, y =;
For (I = 0; I <cnt_r; ++ I ){
For (j = 0; j <cnt_c; ++ J ){
If (Box [I] [J] = 0) showcube (X, Y, 0 );
Else if (Box [I] [J] = 1) showcube (X, Y, 1 );
X + = cs;
}
X = Bx;
Y + = cs;
}
 
Return 0;
}

/* Remove blocks */
Int clearbar (int x, int y, int B [4] [4])
{
Int I, J;
MoveTo (x, y );
For (I = 0; I <4; ++ I)
For (j = 0; j <4; ++ J ){
If (B [I] [J] = 0 );
Else if (B [I] [J] = 1 ){
Showcube (x + CS * j, Y + CS * I, 0 );
}
}
Return 0;
}

/* Draw a square */
Int bar (int x, int y, int B [4] [4])
{
Int I, J;
MoveTo (x, y );
For (I = 0; I <4; ++ I)
For (j = 0; j <4; ++ J ){
If (B [I] [J] = 0 );
Else if (B [I] [J] = 1 ){
Showcube (x + CS * j, Y + CS * I, 1 );
}
}
Return 0;
}

Int timeisok ()
{
Newtm = time (0 );
If (newtm-oldtm> = 1 ){
Oldtm = newtm;
Return 1;
}
Else return 0;
}

Int sound ()
{
Sound (1, 587 );
Delay (10000 );
Sound (1, 659 );
Delay (10000 );
Sound (1, 698 );
Delay (10000 );
Sound (1, 880 );
Delay (100000 );
Sound (1, 523 );
Delay (10000 );
Sound (1, 980 );
Delay (100000 );
Sound (1, 784 );
Delay (1000 );
Sound (1, 523 );
Delay (1000 );
Nosound ();
Return 0;
}

Int main ()
{
Int GD = detect, GM;

Registerbgidriver (egavga_driver );
Initgraph (& GD, & GM ,"");

Initbox ();

While (1 ){
If (! Kbhit () & timeisok ()){
Run ();

} Else if (kbhit ()){
If (Key = bioskey (0 ))! = ESC ){
Switch (key ){
Case down: movedown (); break;
Case up: changeup (); break;
Case left: moveleft (); break;
Case right: moveright (); break;
}
}
Else {
Msgbox ();
Getch ();
Exit (0 );
}
}
}
Getch ();
Closegraph ();
Return 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.