How to realize C++ in Tetris Mini Game
This article mainly shows you "how C++ realizes Mini Game of Tetris". The content is simple and clear. I hope it can help you solve your doubts. Next, let the editor lead you to study and learn the article "how C++ realizes Mini Game of Tetris".
The details are as follows
Operation instructions:
D F: rotate left and right
J L: move left and right
E (a bunch of keys): speed up the fall
Space: start the game and fall to the end
Get the code!
# include # include using namespace std;#define KEY_DOWN (vk_code) ((GetAsyncKeyState (vk_code) & 0x8000)? 1: 0) # define inf 2147483647struct type_block {int a [10] [10]; int color; int size;} now, nextA, nextB, nextC, hold Int sblock [20] [10] [10] = {{7,0,0,0}, {0,0,1,1}, {0,1,0}, {0,0,0,0}, {8,0,0,0,0}, {0,0,1,0}, {0,0,0,0}, {9,0,0,0}, {1,1,1}, {0,0} 0, 0}, {0, 0, 0, 0}}, {{10, 0, 0, 0}, {0, 0, 0, 1}, {0, 1, 1, 1}, {0, 0, 0, 0}}, {{13, 0, 0, 0}, {0, 1, 0, 0}, {0, 1, 1, 1}, {0, 0, 0, 0}}, {{5, 0, 0, 0}, {0, 1, 1, 0} {0, 1, 1, 0}, {0, 0, 0, 0}}, {{16, 0, 0, 0}, {0, 0, 1, 0}, {0, 1, 1, 1}, {0, 0, 0, 0}}, {{0, 0, 0, 0}, {0, 1, 0, 0}, {0, 1, 1, 0}, {0, 0, 1, 0}}, {{1, 0, 0, 0}, {0, 0 1, 0}, {0, 1, 1, 0}, {0, 1, 0, 0}}, {{2, 1, 0, 0}, {0, 1, 0, 0}, {0, 1, 0, 0}, {0, 1, 0, 0}}, {{11, 0, 1, 0}, {0, 0, 1, 0}, {0, 0, 1, 1}, {0, 0, 0, 0}}, {{12, 0, 0, 0} {0, 1, 1, 1}, {0, 1, 0, 0}, {0, 0, 0, 0}}, / / 11 {{3, 0, 1, 1}, {0, 0, 0, 1}, {0, 0, 0, 1}, {0, 0, 0, 0}}, {{14, 1, 1, 0}, {0, 1, 0, 0}, {0, 1, 0, 0}, {0, 0, 0, 0}}, {{15 1, 1, 1}, {0, 0, 0, 1}, {0, 0, 0, 0}, {0, 0, 0, 0}}, {{4, 0, 0, 1}, {0, 0, 0, 1}, {0, 0, 1, 1}, {0, 0, 0, 0}}, {{17, 0, 0, 0}, {0, 0, 1, 0}, {0, 0, 1, 1}, {0, 0, 1, 0}} {{18, 0, 0, 0}, {0, 0, 0, 0}, {0, 1, 1, 1}, {0, 0, 1, 0}}, / / 17 {{6, 0, 0, 0}, {0, 0, 1, 0}, {0, 1, 1, 0}, {0, 0, 1, 0} HANDLE hOut = GetStdHandle (STD_OUTPUT_HANDLE); int map [22] [12]; int mode = 1nint fraction = 0tint positionX, positionY;bool locked;int keytime = 100000000 Bool keytimeflag;int sleeptime = 0holdflag holdflag = 1holdflag = 200int addlinetime = 10000, addlineflag;int locktime = 1000transint keydownflag;int xray = 0tint firstwin = 1tint exfraction = 0tint exgamestarttime;int blind = 0fliint lockdelay = 300int clockms, stclockms;double blockpersecond, blockperminute;int blocknum;void gotoxy (int x, int y) Void welcomepage (); void reset (); void choosedifficulty (); void ready_go (); void updatedata (); void updatetime (); type_block roundblock (); void printblock (type_block m_block, int x, int y); void clearblock (type_block m_block, int x, int y); int checkblock (type_block m_block, int x, int y); type_block myup (type_block m_block); type_block mydown (type_block m_block); void checkkey (); void checkline (); void addline () Void gameover (); void win (); void easy_extra_game (); void master_extra_game (); void shirase_extra_game (); int main () {welcomepage (); reset (); choosedifficulty (); ready_go (); clearblock (nextA, 34,4); clearblock (nextB, 38 + nextA.size, 4); clearblock (nextC, 42 + nextA.size + nextB.size, 4); now = nextA; nextA = nextB; nextB = nextC; nextC = roundblock () Printblock (nextA, 34,4); printblock (nextB, 38 + nextA.size, 4); printblock (nextC, 42 + nextA.size + nextB.size, 4); positionX = 0; positionY = 4; locked = 0; keytime = clock (); keytimeflag = 1; addlineflag = clock (); stclockms = clock (); while (1) {updatedata (); updatetime () If (locked) {+ + blocknum; SetConsoleTextAttribute (hOut, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY); now.color =-xray * 2; printblock (now, positionY * 2 + 8, positionX + 1); for (int I = 0; I
< 4; ++i) { for (int j = 0; j < 4; ++j) { if (!(i + j)) continue; if (now.a[i][j]) map[positionX + i][positionY + j] = 1; } } checkline (); Sleep (lockdelay); updatetime (); if (blind) clearblock (now, positionY * 2 + 8, positionX + 1); locked = 0; clearblock (nextA, 34, 4); clearblock (nextB, 38 + nextA.size, 4); clearblock (nextC, 42 + nextA.size + nextB.size, 4); now = nextA; nextA = nextB; nextB = nextC; nextC = roundblock(); printblock (nextA, 34, 4); printblock (nextB, 38 + nextA.size, 4); printblock (nextC, 42 + nextA.size + nextB.size, 4); positionX = 0; positionY = 4; if (!checkblock (now, positionX, positionY)) gameover (); if (fraction % 100 != 99 && fraction != passcondition) ++fraction; SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY); gotoxy (56, 10); printf ("%4d", fraction); gotoxy (57, 11); printf ("---"); gotoxy (56, 12); int lssc = fraction % 100 ? (fraction / 100 + 1) * 100 : fraction; if (mode == 1 && lssc >Lssc = 200; if (mode = = 2 & & lssc > = 999) lssc = 900; if (mode = = 3 & & lssc > = 1300) lssc = 1300; printf ("% 4d", lssc); keytime = clock (); holdflag = 1 If (clock ()-addlineflag > addlinetime) {addlineflag = clock (); addline ();} keydownflag = 0 } if (checkblock (now, positionX + 1, positionY)) while (checkblock (now, positionX + 1, positionY)) {+ + positionX; updatetime (); if (sleeptime) {printblock (now, positionY * 2 + 8, positionX + 1); int sttime = clock (), timeover While ((timeover = (clock ()-sttime)
< sleeptime)) && !kbhit()); clearblock (now, positionY * 2 + 8, positionX + 1); if (timeover) checkkey (); } checkkey (); } printblock (now, positionY * 2 + 8, positionX + 1); Sleep (100); clearblock (now, positionY * 2 + 8, positionX + 1); checkkey (); }} void gotoxy (int x, int y){ COORD pos; pos.X = x; pos.Y = y; SetConsoleCursorPosition (hOut, pos);}void welcomepage (){ puts (" "); puts (" ■■■■■■■■■■ "); puts (" ■■■■■■■■■■ "); puts (" ■■ "); puts (" ■■ "); puts (" ■■ "); puts (" ■■ ■■■■ ■ ■ ■ ■■■ "); puts (" ■■ ■ ■ ■■■■ ■ ■■■ ■ ■ "); puts (" ■■ ■■■■ ■ ■■ ■ ■ "); puts (" ■■ ■ ■ ■ ■ ■■■ "); puts (" ■■ ■■■■ ■ ■ ■ ■ "); puts (" ■ "); puts (" ■■■■ "); puts (" "); puts (" ------------------------------------------------------------------ "); puts (" CHAMPION "); puts (" "); puts (" "); puts (" "); puts (" "); puts (" "); SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY); int modnum = 0; while (1) { gotoxy (22, 20); if (modnum) puts ("PUSH START BUTTOM"); else puts (" "); int sttime = clock (), timeover; while ((timeover = (clock () - sttime < 700)) && !kbhit()); if (timeover) { if (_getch () == ' ') break; } modnum ^= 1; } gotoxy (0, 0); for (int i = 1; i = 400) locktime = 600; } if (fraction >= 999) win (); break; case 3: addlinetime = inf; lockdelay = 300-fraction / 400 * 50; sleeptime = 0; locktime = 240-fraction / 300 * 40; if (fraction > = 600) addlinetime = 10000; if (fraction > = 800) addlinetime = 7000 If (fraction > = 1000) {addlinetime = inf; xray = 1;} if (fraction > = 1300) win ();}} void updatetime () {SetConsoleTextAttribute (hOut, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY); gotoxy (45,20); clockms = clock ()-stclockms Printf ("d:d:d", clockms / 60000, clockms / 1000 60, clockms% 1000 / 10);} type_block roundblock () {type_block c; int kind = rand () 7; while (kind)
< 2 && fraction 20 || y + j < 1 || y + j >10) return 0;} return 1;} type_block myup (type_block m_block) {type_block c; int kind = m_block.a [0] [0]; for (int I = 0; I
< m_block.size; ++i) for (int j = 0; j < m_block.size; ++j) c.a[i][j] = sblock[kind][i][j]; c.size = m_block.size; c.color = m_block.color; return c;}type_block mydown (type_block m_block){ type_block c = m_block; for (int t = 1; t 100 ? 0 : 4)) { ++keydownflag; if (KEY_DOWN('J')) { if (checkblock (now, positionX, positionY - 1)) --positionY; } if (KEY_DOWN('L')) { if (checkblock (now, positionX, positionY + 1)) ++positionY; } } if (kbhit()) { keytime = clock (); char key = _getch(); if (key == 'j') { if (checkblock (now, positionX, positionY - 1)) --positionY; } if (key == 'l') { if (checkblock (now, positionX, positionY + 1)) ++positionY; } if (key == 's') { if (!holdflag) return; if (hold.a[0][0]) { clearblock (hold, 60, 5); type_block t = now; now = hold; hold = t; printblock (hold, 60, 5); positionX = 0; positionY = 4; keytime = clock(); holdflag = 0; } else { SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_INTENSITY); gotoxy (60, 4); puts ("Hold"); clearblock (hold, 60, 5); hold = now; printblock (hold, 60, 5); clearblock (nextA, 34, 4); clearblock (nextB, 38 + nextA.size, 4); clearblock (nextC, 42 + nextA.size + nextB.size, 4); now = nextA; nextA = nextB; nextB = nextC; nextC = roundblock(); printblock (nextA, 34, 4); printblock (nextB, 38 + nextA.size, 4); printblock (nextC, 42 + nextA.size + nextB.size, 4); positionX = 0; positionY = 4; keytime = clock(); } } if (key == 'f') { type_block newnow = myup (now); if (checkblock (newnow, positionX, positionY)) now = newnow; else if (checkblock (newnow, positionX - 1, positionY)) { now = newnow; --positionX; } else if (checkblock (newnow, positionX - 2, positionY)) { now = newnow; positionX -= 2; } else if (checkblock (newnow, positionX, positionY - 1)) { now = newnow; --positionY; } else if (checkblock (newnow, positionX, positionY + 1)) { now = newnow; ++positionY; } } if (key == 'd') { type_block newnow = mydown (now); if (checkblock (newnow, positionX, positionY)) now = newnow; else if (checkblock (newnow, positionX - 1, positionY)) { now = newnow; --positionX; } else if (checkblock (newnow, positionX - 2, positionY)) { now = newnow; positionX -= 2; } else if (checkblock (newnow, positionX, positionY - 1)) { now = newnow; --positionY; } else if (checkblock (newnow, positionX, positionY + 1)) { now = newnow; ++positionY; } } if (KEY_DOWN(' ')) { for (int i = 1; i locktime && !checkblock (now, positionX + 1, positionY)) locked = 1;}void checkline (){ bool tf = 0; for (int i = 1; i = 500) printf ("S3"); else if (fraction >Printf ("S2"); else if (fraction > = 400) printf ("S1"); else if (fraction > = 350) printf ("1"); else if (fraction > = 300) printf ("2") Else if (fraction > = 250) printf ("3"); else if (fraction > = 200) printf ("4"); else if (fraction > = 150) printf ("5"); else if (fraction > = 100) printf ("6") Else if (fraction > = 50) printf ("7"); else printf ("8"); break; case 3: if (fraction > = 1300) printf ("S13"); else if (fraction > = 1200) printf ("S12") Else if (fraction > = 1100) printf ("S11"); else if (fraction > = 1000) printf ("S10"); else if (fraction > = 1000) printf ("S9"); else if (fraction > = 800) printf ("S8") Else if (fraction > = 700) printf ("S7"); else if (fraction > = 600) printf ("S6"); else if (fraction > = 500) printf ("S5"); else if (fraction > = 400) printf ("S4") Else if (fraction > = 300) printf ("S3"); else if (fraction > = 200) printf ("S2"); else if (fraction > = 100) printf ("S1"); else {gotoxy (15,13) Printf ("Let's go"); gotoxy (15,14); printf ("better"); gotoxy (15,15); printf ("next time!");} break;} Sleep } Sleep (2000); system ("pause > nul"); exit (0);} void win () {if (firstwin) {for (int I = 20; I > = 1;-- I) {for (int j = 1; j = 200) lssc = 200; if (mode = 2 & lssc > = 999) lssc = 900 If (mode = = 3 & & lssc > = 1300) lssc = 1300; printf ("% 4d", lssc); keytime = clock (); holdflag = 1; if (clock ()-addlineflag > addlinetime) {addlineflag = clock (); addline () } keydownflag = 0;} if (checkblock (now, positionX + 1, positionY)) while (checkblock (now, positionX + 1, positionY)) {+ + positionX; if (sleeptime) {printblock (now, positionY * 2 + 8, positionX + 1) Int sttime = clock (), timeover; while ((timeover = (clock ()-sttime < sleeptime)) & &! kbhit (); clearblock (now, positionY * 2 + 8, positionX + 1); if (timeover) checkkey ();} checkkey () } printblock (now, positionY * 2 + 8, positionX + 1); Sleep (100); clearblock (now, positionY * 2 + 8, positionX + 1); checkkey ();} if (xray) SetConsoleTextAttribute (hOut, FOREGROUND_GREEN); else SetConsoleTextAttribute (hOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY); for (int I = 1) I