我写了一个俄罗斯方块,用easyx图形化的 所以测试的时候要windows系统和easyx图形库 以下代码:
#include <bits/stdc++.h>
#include <easyx.h>
#include <windows.h>
#include <graphics.h>
#include <conio.h>
const int __w = 14, __h = 28;
using namespace std;
void __init_paint();
void __start_paint();
void __paint_block();
void __sommon();
void __change_block(int mod);
void __get_key();
void __tetris_system();
void __remove_line(int line);
void __check_line();
void __pause();
void __next_paint();
void __restart();
void __score_paint();
int __randint(int l, int r);
int __rotate_block(int sta);
bool __check(char c);
int __block_map[110][110], __col, __sta; // MAP OF THE TETRIS, RANDOM BLOCK COLOR, STATUS(SHAPE) OF THE NEW BLOCK
int __plx, __ply, __interval = 1000, __score; // PLACE OF A BLOCK X, PLACE OF A BLOCK Y, END - START (TIME DIFFERENCE), SCORE
int __nplx, __nply, __ncol, __nsta; // NEXT BLOCK
int __block[20][10] =
{
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 1, 0, 2, 0, 3}, // 1. ——
{0, 0, 1, 0, 2, 0, 3, 0}, // 2. |
{0, 0, 1, 0, 1, 1, 2, 1}, // 3. <-> REVERSE "RIGHT ROTATE Z"
{0, 1, 0, 2, 1, 0, 1, 1}, // 4. <-> REVERSE Z
{0, 1, 1, 0, 1, 1, 2, 0}, // 5. RIGHT ROTATE Z
{0, 0, 0, 1, 1, 1, 1, 2}, // 6. Z
{0, 0, 0, 1, 1, 0, 1, 1}, // 7. ■
{0, 0, 1, 0, 1, 1, 1, 2}, // 8. |___
{0, 0, 0, 1, 0, 2, 1, 2}, // 9. ▔▔|
{0, 0, 0, 1, 1, 0, 2, 0}, // 10. ∨-∧ REVERSE L
{0, 1, 1, 1, 2, 0, 2, 1}, // 11. <-> REVERSE L
{0, 2, 1, 0, 1, 1, 1, 2}, // 12. ___|
{0, 0, 0, 1, 0, 2, 1, 0}, // 13. |▔▔
{0, 0, 0, 1, 1, 1, 2, 1}, // 14. 7
{0, 0, 1, 0, 2, 0, 2, 1}, // 15. L
{0, 1, 1, 0, 1, 1, 1, 2}, // 16. _|_
{0, 0, 0, 1, 0, 2, 1, 1}, // 17. ▔|▔
{0, 1, 1, 0, 1, 1, 2, 1}, // 18. -|
{0, 0, 1, 0, 1, 1, 2, 0} // 19. |-
}; // BLOCK SHAPE (XY OFFSET)
bool __is_full, __can_sommon = 1, __is_start = 0; // PILED TO THE TOP, IT'S TIME TO SOMMON, IS START
default_random_engine __randome{random_device{}()}; // A GOOD RANDOM FUNCTION
clock_t __sommon_start, __sommon_end;
int __randint(int l, int r)
{
return __randome()%(r-l+1)+l;
}
void __init_paint() // START INTERFACE
{
initgraph(481, 601, 1);
cleardevice(), settextcolor(WHITE), settextstyle(30, 0, _T("consolas"));
outtextxy(180, 150, _T("TETRIS")), settextstyle(20, 0, _T("consolas"));
outtextxy(60, 240, _T("↑:RORATE BLOCK ↓:ACCELERATE DOWN"));
outtextxy(60, 280, _T("←:MOVE LEFT →:MOVE RIGHT SPACE:PAUSE"));
outtextxy(60, 350, _T("0:EXIT ENTER:START"));
setlinestyle(PS_ENDCAP_SQUARE, 10);
line(10, 10, 470, 10), line(10, 590, 470, 590);
line(10, 10, 10, 590), line(470, 10, 470, 590);
while(1)
{
int __key =_getch();
if(__key == '0') exit(0);
else if(__key == 13)
{
cleardevice(), __start_paint();
return;
}
}
}
void __start_paint() // GAME INTERFACE
{
setlinestyle(PS_ENDCAP_SQUARE, 10), setlinecolor(WHITE);
line(10, 10, 470, 10), line(10, 590, 470, 590);
line(10, 10, 10, 590), line(470, 10, 470, 590);
line(10, 10, 470, 10), line(10, 590, 470, 590);
line(10, 10, 10, 590), line(470, 10, 470, 590);
line(310, 10, 310, 590), line(310, 320, 470, 320);
outtextxy(330, 40, _T("NEXT BLOCK")), outtextxy(330, 350, _T("NOW SCORE"));
setlinestyle(PS_ENDCAP_SQUARE, 1);
for(int i = 1; i <= __h+1; i++) line(20, i*20, __w*20+20, i*20); // GRIDDING
for(int i = 1; i <= __w+1; i++) line(i*20, 20, i*20, __h*20+20);
}
void __set_color(int num)
{
switch(num)
{
case 1: {setfillcolor(LIGHTRED), setlinecolor(LIGHTRED); break;}
case 2: {setfillcolor(RED), setlinecolor(RED); break;}
case 3: {setfillcolor(YELLOW), setlinecolor(YELLOW); break;}
case 4: {setfillcolor(LIGHTGREEN), setlinecolor(LIGHTGREEN); break;}
case 5: {setfillcolor(GREEN), setlinecolor(GREEN); break;}
case 6: {setfillcolor(LIGHTCYAN), setlinecolor(LIGHTCYAN); break;}
case 7: {setfillcolor(CYAN), setlinecolor(CYAN); break;}
case 8: {setfillcolor(LIGHTBLUE), setlinecolor(LIGHTBLUE); break;}
case 9: {setfillcolor(BLUE), setlinecolor(BLUE); break;}
case 10: {setfillcolor(LIGHTMAGENTA), setlinecolor(LIGHTMAGENTA); break;}
case 11: {setfillcolor(MAGENTA), setlinecolor(MAGENTA); break;}
}
}
void __restart()
{
cleardevice(), settextstyle(30, 0, _T("consolas"));
outtextxy(180, 150, _T("TETRIS")), outtextxy(120, 190, _T("!!!GAME OVER!!!"));
settextstyle(20, 0, _T("consolas"));
char s[10];
sprintf(s, "%d", __score);
outtextxy(140, 240, _T("SCORE: "));
outtextxy(200, 240, s);
setlinecolor(WHITE), setlinestyle(PS_ENDCAP_SQUARE, 10);
line(10, 10, 470, 10), line(10, 590, 470, 590);
line(10, 10, 10, 590), line(470, 10, 470, 590);
outtextxy(120, 350, _T("0:EXIT ENTER:START"));
while(1)
{
int __key =_getch();
if(__key == '0') exit(0);
else if(__key == 13)
{
cleardevice(), __start_paint();
return;
}
}
}
void __score_paint()
{
char s[10];
sprintf(s, "%d", __score);
outtextxy(330, 380, s);
}
void __paint_block()
{
for(int i = 1; i <= __w; i++)
{
for(int j = 1; j <= __h; j++)
if(__block_map[j][i])
{
__set_color(__block_map[j][i]);
fillrectangle(i*20+2, j*20+2, i*20+18, j*20+18);
}
else
{
setfillcolor(BLACK), setlinecolor(BLACK);
fillrectangle(i*20+1, j*20+1, i*20+19, j*20+19);
}
}
}
void __sommon()
{
__ply = __nply, __sta = __nsta, __col = __ncol;
__sommon_start = clock();
__is_full = 0, __plx = 1;
for(int i = 0; i < 4; i++)
{
if(__block_map[__block[__sta][i*2]+1][__ply+__block[__sta][i*2+1]]) __is_full = 1;
__block_map[__block[__sta][i*2]+1][__ply+__block[__sta][i*2+1]] = __col;
}
__nply = __randint(1, __w-4), __nsta = __randint(1, 19), __ncol = __randint(1, 11);
__paint_block();
}
void __next_paint()
{
setfillcolor(BLACK), setlinecolor(BLACK);
fillrectangle(320, 80, 440, 200);
setlinecolor(WHITE);
for(int i = 5; i <= 9; i++) line(330, i*20, 430, i*20);
for(int i = 17; i <= 21; i++) line(i*20, 90, i*20, 190);
__set_color(__ncol);
for(int i = 0; i < 4; i++)
{
fillrectangle(__block[__nsta][i*2+1]*20+342, __block[__nsta][i*2]*20+102,
__block[__nsta][i*2+1]*20+358, __block[__nsta][i*2]*20+118);
}
}
bool __check(char c)
{
if(c == 'D') // DOWN
{
for(int i = 0; i < 4; i++)
{
int nx = __plx+__block[__sta][i*2], ny = __ply+__block[__sta][i*2+1];
if(nx+1 > __h) {__can_sommon = 1; return 0;}
if(__block_map[nx+1][ny])
{
bool tf = true;
for(int j = 0; j < 4; j++)
if(__plx+__block[__sta][j*2] == nx+1 && __ply+__block[__sta][j*2+1] == ny)
{tf = false; break;}
if(tf) {__can_sommon = 1; return 0;}
}
}
__can_sommon = 0;
return 1;
}
if(c == 'O') // ROTATE
{
int rotate_id = __rotate_block(__sta);
for(int i = 0; i < 4; i++)
{
int xx = __plx+__block[rotate_id][i*2], yy = __ply+__block[rotate_id][i*2+1];
if(xx >= __h || yy >= __w) return 0;
if(__block_map[xx][yy])
{
bool tf = true;
for(int j = 0; j < 4; j++)
if(__plx+__block[__sta][j*2] == xx && __ply+__block[__sta][j*2+1] == yy)
{tf = false; break;}
if(tf) return 0;
}
}
return 1;
}
if(c == 'L') // LEFT
{ // THE GREATEST CODES OFTEN REQUIRE ONLY THE EASIEST DUPLICATION!
for(int i = 0; i < 4; i++) // CTRL C + CTRL V, NICE~
{
int nx = __plx+__block[__sta][i*2], ny = __ply+__block[__sta][i*2+1];
if(ny-1 <= 0) return 0;
if(__block_map[nx][ny-1])
{
bool tf = true;
for(int j = 0; j < 4; j++)
if(__plx+__block[__sta][j*2] == nx && __ply+__block[__sta][j*2+1] == ny-1)
{tf = false; break;}
if(tf) return 0;
}
}
return 1;
}
if(c == 'R') // RIGHT
{
for(int i = 0; i < 4; i++)
{
int nx = __plx+__block[__sta][i*2], ny = __ply+__block[__sta][i*2+1];
if(ny+1 > __w) return 0;
if(__block_map[nx][ny+1])
{
bool tf = true;
for(int j = 0; j < 4; j++)
if(__plx+__block[__sta][j*2] == nx && __ply+__block[__sta][j*2+1] == ny+1)
{tf = false; break;}
if(tf) return 0;
}
}
return 1;
}
return 0;
}
void __change_block(int mod)
{
if(mod == 1 && __check('D'))
{
for(int i = 3; i >= 0; i--)
{
__block_map[__plx+__block[__sta][i*2]][__ply+__block[__sta][i*2+1]] = 0;
__block_map[__plx+__block[__sta][i*2]+1][__ply+__block[__sta][i*2+1]] = __col;
}
__plx++;
}
if(mod == 2 && __check('L'))
{
bool vi[5];
for(int i = 0; i < 4; i++)
{
int mx, mxv = 100;
for(int j = 0; j < 4; j++)
if(!vi[j] && __block[__sta][j*2+1] < mxv) mxv = __block[__sta][j*2+1], mx = j;
vi[mx] = 1;
__block_map[__plx+__block[__sta][mx*2]][__ply+__block[__sta][mx*2+1]] = 0;
__block_map[__plx+__block[__sta][mx*2]][__ply+__block[__sta][mx*2+1]-1] = __col;
}
__ply--;
}
if(mod == 3 && __check('R'))
{
bool vi[5];
for(int i = 0; i < 4; i++)
{
int mx, mxv = -1;
for(int j = 0; j < 4; j++)
if(!vi[j] && __block[__sta][j*2+1] > mxv) mxv = __block[__sta][j*2+1], mx = j;
vi[mx] = 1;
__block_map[__plx+__block[__sta][mx*2]][__ply+__block[__sta][mx*2+1]] = 0;
__block_map[__plx+__block[__sta][mx*2]][__ply+__block[__sta][mx*2+1]+1] = __col;
}
__ply++;
}
if(mod == 4 && __check('O'))
{
int rotate_id = __rotate_block(__sta);
for(int i = 0; i < 4; i++)
__block_map[__plx+__block[__sta][i*2]][__ply+__block[__sta][i*2+1]] = 0;
__sta = rotate_id;
for(int i = 0; i < 4; i++)
__block_map[__plx+__block[__sta][i*2]][__ply+__block[__sta][i*2+1]] = __col;
}
__paint_block();
}
void __get_key()
{
if(!_kbhit()) return;
char __now_key = _getch();
if(__now_key == 'S' || __now_key == 's') __change_block(1), Sleep(50);
else if(__now_key == 'A' || __now_key == 'a') __change_block(2), Sleep(50);
else if(__now_key == 'D' || __now_key == 'd') __change_block(3), Sleep(50);
else if(__now_key == 'W' || __now_key == 'w') __change_block(4), Sleep(50);
else if(__now_key == '0') exit(0);
else if(__now_key == ' ') __pause();
}
void __remove_line(int line)
{
for(int i = line; i >= 1; i--)
for(int j = 1; j <= __w; j++)
__block_map[i][j] = __block_map[i-1][j];
}
void __check_line()
{
int sum = 0;
for(int i = 1; i <= __h; i++)
{
bool line_fail = 0;
for(int j = 1; j <= __w; j++)
if(!__block_map[i][j]) {line_fail = 1; break;}
if(!line_fail) __remove_line(i), sum++;
}
__score += sum*100;
if(__score != 0 && __score%500 == 0 && __interval > 700) __interval -= 50;
__paint_block();
}
int __rotate_block(int sta)
{
switch(sta)
{
case 1: return 2;
case 2: return 1;
case 3: return 4;
case 4: return 3;
case 5: return 6;
case 6: return 5;
case 7: return 7;
case 8: return 10;
case 9: return 11;
case 10: return 9;
case 11: return 8;
case 12: return 15;
case 13: return 14;
case 14: return 12;
case 15: return 13;
case 16: return 19;
case 17: return 18;
case 18: return 16;
case 19: return 17;
}
return 0;
}
void __tetris_system()
{
__nply = __randint(1, __w-4), __nsta = __randint(1, 19), __ncol = __randint(1, 11);
__init_paint();
while(1)
{
__sommon_end = clock();
if(__can_sommon)
{
__check_line(), __score_paint();
__sommon_end = clock();
if(__sommon_end-__sommon_start > 700) __sommon(), __can_sommon = 0, __next_paint();
}
if(__is_full) __restart();
if(__sommon_end-__sommon_start > __interval)
__sommon_start = clock(), __change_block(1);
__get_key();
}
}
void __pause()
{
cleardevice(), setlinestyle(PS_ENDCAP_SQUARE, 10), setlinecolor(WHITE);
line(10, 10, 470, 10), line(10, 590, 470, 590);
line(10, 10, 10, 590), line(470, 10, 470, 590);
settextstyle(30, 0, _T("consolas")), outtextxy(180, 150, _T("TETRIS"));
outtextxy(187, 190, _T("PAUSE")), outtextxy(120, 350, _T("0:EXIT ENTER:GO ON"));
while(1)
{
int __key =_getch();
if(__key == '0') exit(0);
else if(__key == 13)
{
cleardevice(), __start_paint();
return;
}
}
}
int main()
{
__tetris_system();
return 0;
}
求万能的谷民们用各种方式hack它,让它不能正常运行,谢谢