Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the six modes of Tetris in C language

Shulou Source: shulou.com Published: 2022-06-01 11:54:48 10月04日 Update

This article mainly introduces how to realize the six modes of Tetris in C language, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let the editor take you to know it.

Final effect

Home page

Mode selection

Press 1 to enter single player mode

Press 2 to enter two-person mode

Standard mode: in addition to moving left and right, direct fall and pause have been added, and level determination has been added.

Hell mode: in the standard mode under the premise of faster falling speed, more exciting!

Cheating mode: add dazzling mode, deformation, acceleration and deceleration

Double standard race: an algorithm is designed to determine the outcome of the draw.

Double Hell Survival match: too exciting!

Double time trial: set a timer and reset the algorithm to determine the draw. Those with high scores win within a specified period of time.

End animation

Code part

Main.c

# include # include # include "game.h # include" mywindows.h "# include # include # pragma comment (lib," winmm.lib ") void chooseMode () {if (kbhit ()) {mciSendString (" close g ", NULL,0,NULL); switch (getch ()) {case 49: case 97: gameInit (1); break; case 50: case 98: gameInit (2); break Case 51: case 99: gameInit (3); break;} void chooseMode2 () {if (kbhit ()) {mciSendString ("close g", NULL,0,NULL); switch (getch ()) {case 49: case 97: gameInit1 (1); break; case 50: case 98: gameInit1 (2); break Case 51: case 99: gameInit1 (3); break;}} int main () {/ initialization handle, which must be placed at the beginning of initHandle (); / / start animation mciSendString ("open Tetris enters music .mp3 alias g", NULL,0,NULL); mciSendString ("play g repeat", NULL,0,NULL); printAnimation () If (kbhit ()) {getch (); chooseWindow ();} if (kbhit ()) {switch (getch ()) {case 49: case 97: chooseWindow2 (); chooseMode (); break; case 50: case 98: chooseWindow3 (); chooseMode2 (); break;}} return 0;}

Mywindows.c

# include "mywindows.h" / / angle brackets are often used to introduce system header files, and double quotation marks are often used to introduce self-defined header files with different HANDLE handle; / / function definition void initHandle () {handle = GetStdHandle (STD_OUTPUT_HANDLE); hideCursor (); / / hide the cursor position after the game starts / /} void setColor (int color) {SetConsoleTextAttribute (handle,color) } void setPos (int xreint y) {COORD coord = {Xerox 2jiny}; / / letter abcd: one character, Chinese character: two characters SetConsoleCursorPosition (handle,coord); / / set handle position} void hideCursor () {CONSOLE_CURSOR_INFO info; / / the structural variable names of the system are capitalized info.bVisible = FALSE; / / set whether the cursor is visible or not info.dwSize = 1 / / set cursor width (1-100) SetConsoleCursorInfo (handle,&info); / / pointer address character}

Game.c key code

Single-player mode form printing

Void printGradeLevel1 (int num) {switch (num) {case 1: grade1+=10; break; case 2: grade1+=30; break; case 3: grade1+=50; break; case 4: grade1+=80; break;} if (grade1

< 100){ level1 = 1; } else if(grade1 >

& & grade1

< 300){ level1 =2; } setColor(0x0c); setPos(4,8); printf("分数:%d",grade1); setPos(4,9); printf("等级:%d",level1);} void printGradeLevel2(int num){ switch(num){ case 1: grade2+=10; break; case 2: grade2+=30; break; case 3: grade2+=50; break; case 4: grade2+=80; break; } if(grade2 < 100){ level2 = 1; } else if(grade2 >

{level2 = 2;} setColor (0x09); setPos (51 grade2 8); printf ("score:% d", grade2); setPos (51 grade2 9); printf ("Rank:% d", level2);}

Two-person mode form printing

Void windowPrint2 (int x pencil int y) {int iMagin j; / / is used to traverse the two-dimensional array for.

Tags: Mode square detection collision time standard article win or lose Russia code location cursor score animation handle hell keys file level algorithm Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Huawei Microsoft macOS Shulou Information