玩法1:
//上下左右键控制
#include<bits/stdc++.h>
#include<windows.h>
int a[24][17],i,j,tim=800,ti=800,shape=0,b,bn,ta[4][4],turn[4][4],nex[4][4],nextshape,add=0,score=0,speed=1,ok=1,mouse=0,best=0;
void Place(const int x, const int y)
{
COORD PlaceCursorHere;
PlaceCursorHere.X = y;
PlaceCursorHere.Y = x;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), PlaceCursorHere);
return;
}
void color(int x)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),x);
}
void appear()
{
for(i=3; i<=22; i++) for(j=1; j<=15; j++) if(a[i][j]!=0)
{
Place(i-2,2*j);
printf("■");
}
}
void disappear()
{
for(i=3; i<=22; i++) for(j=1; j<=15; j++) if(a[i][j]==2)
{
Place(i-2,2*j);
printf(" ");
}
}
int search(int x0,int y0,int x,int y)
{
POINT pt;
HWND h=GetForegroundWindow();
GetCursorPos(&pt);
ScreenToClient(h,&pt);
if(pt.x>=x0&&pt.y>=y0&&pt.x<=x&&pt.y<=y)
{
if(mouse!=0)
{
Sleep(100);
return 2;
}
else return 1;
}
else return 0;
}
void button(int x)
{
int m=x*10;
if(x<3)
{
Place(22,m);
printf("┌┄┄┄┐");
Place(23,m);
{
if(x==0)
{
if(ok!=0) printf("┆开始 ┆");
else printf("┆暂停 ┆");
}
if(x==1) printf("┆重置 ┆");
if(x==2) printf("┆退出 ┆");
}
Place(24,m);
printf("└┄┄┄┘");
}
else
{
Place(9,m-33);
printf("┌┄┄┐");
Place(10,m-33);
{
if(x==4) printf("┆是┆");
else printf("┆否┆");
}
Place(11,m-33);
printf("└┄┄┘");
}
}
void menu(int x)
{
int k,l;
if(x==0)
{
if(ok!=0)
{
ok=0;
return;
}
else while(1)
{
mouse=GetAsyncKeyState(VK_LBUTTON);
Place(9,13);
printf("暂停");
if(search(7,360,71,390)==2) break;
Sleep(50);
}
Place(9,12);
for(j=1; j<=6; j++) printf(" "); //appear();
}
else
{
for(i=1; i<=20; i++) for(j=2; j<=30; j++)
{
Place(i,j);
printf(" ");
}
Place(7,2);
for(i=1; i<=15; i++) printf("┄");
Place(12,2);
for(i=1; i<=15; i++) printf("┄");
Place(8,6);
printf("你想要 ");
if(x==1) printf("重置?");
if(x==2) printf("exit?");
button(4);
button(5);
while(1)
{
mouse=GetAsyncKeyState(VK_LBUTTON);
for(i=0; i<=1; i++)
{
k=search(63+i*80,150,110+i*80,180);
if(k!=2)
{
if(k==1) color(15);
else color(7);
button(i+4);
}
else
{
for(l=7; l<=12; l++) for(j=2; j<=30; j++)
{
Place(l,j);
printf(" ");
}
if(i==0)
{
if(x==1) ok=2;
else exit(0);
}
return;
}
}
Sleep(50);
}
}
}
void click()
{
for(i=0; i<=2; i++)
{
mouse=GetAsyncKeyState(VK_LBUTTON);
int k=search(i*80+7,360,i*80+71,390);
if(k!=2)
{
if(k==1) color(15);
else color(7);
button(i);
color(7);
}
else
{
menu(i);
return;
}
}
Sleep(50);
}
void ss()
{
Place(9,39);
printf("%d",score);
if(score>=speed*100)
{
speed++;
ti=ti-200;
if(ti<0) ti=0;
else
{
for(i=1; i<=4; i++)
{
Place(9,12);
printf("加速!");
Sleep(70);
Place(9,12);
for(j=1; j<=9; j++) printf(" ");
Sleep(70);
}
Sleep(200);
}
}
if(best<score) best=score;
appear();
Place(12,39);
printf("%d",speed);
Place(15,39);
printf("%d",best);
}
void replace()
{
for(i=1; i<=20; i++) for(j=2; j<=30; j++)
{
Place(i,j);
printf(" ");
}
for(i=0; i<=22; i++) for(j=1; j<=15; j++) a[i][j]=0;
for(i=1; i<=15; i++) a[23][i]=1;
for(i=1; i<=23; i++)
{
a[i][0]=1;
a[i][16]=1;
}
ss();
tim=800;
ti=800;
shape=0;
add=0;
score=0;
speed=1;
}
void change(int x)
{
int q=0,l;
for(l=1; l<=x; l++)
{
for(j=0; j<=2+add; j++)
{
for(i=2+add; i>=0; i--)
{
turn[j][q]=ta[i][j];
q++;
if(q>2+add) q=0;
}
}
for(i=0; i<=3; i++) for(j=0; j<=3; j++) ta[i][j]=turn[i][j];
}
}
void born()
{
int x,q=0;
srand(time(NULL));
bn=b;
shape=nextshape;
x=rand()%12+1;
if(b==5&&x>1)
{
x--;
add=1;
}
for(i=0; i<=2+add; i++) for(j=x; j<=x+2+add; j++)
{
a[i][j]=nex[i][q];
q++;
if(q>2+add) q=0;
}
add=0;
}
void next()
{
srand(time(NULL));
b=rand()%7;
nextshape=rand()%4;
add=0;
for(i=3; i<=6; i++) for(j=37; j<=45; j++)
{
Place(i,j);
printf(" ");
}
for(i=0; i<=3; i++) for(j=0; j<=3; j++) nex[i][j]=turn[i][j]=ta[i][j]=0;
ta[1][1]=2;
if(b==0) ta[1][2]=ta[1][0]=ta[0][1]=2;
if(b==1) ta[1][2]=ta[1][0]=ta[0][0]=2;
if(b==2) ta[1][2]=ta[1][0]=ta[0][2]=2;
if(b==3) ta[1][0]=ta[0][1]=ta[0][2]=2;
if(b==4) ta[0][0]=ta[0][1]=ta[1][2]=2;
if(b==5)
{
ta[1][0]=ta[1][2]=ta[1][3]=2;
add=1;
}
if(b==6) ta[0][0]=ta[0][1]=ta[1][0]=2;
if(nextshape>0&&b!=6)
{
change(nextshape);
for(i=0; i<=3; i++) for(j=0; j<=3; j++)
{
nex[i][j]=turn[i][j];
if(nex[i][j]==2)
{
Place(i+3,j*2+37);
printf("■");
}
}
}
else
{
for(i=0; i<=3; i++) for(j=0; j<=3; j++)
{
nex[i][j]=ta[i][j];
if(nex[i][j]==2)
{
Place(i+3,j*2+37);
printf("■");
}
}
}
}
void clear()
{
int c=0,f=0,l[23],s=0,k;
for(i=3; i<=22; i++)
{
l[i]=0;
for(j=1; j<=15; j++) c=a[i][j]+c;
if(c==15)
{
for(j=1; j<=15; j++) a[i][j]=0;
for(k=i-1; k>=2; k--) for(j=1; j<=15; j++) a[k+1][j]=a[k][j];
f++;
l[i]=1;
s=5;
}
c=0;
}
score=score+f*10;
while(f>1)
{
score=score+f*5;
f--;
}
f=0;
while(s>0)
{
for(i=22; i>=3; i--) if(l[i]==1)
{
Place(i-2,2);
for(j=1; j<=15; j++) printf("■");
}
Sleep(70);
for(i=22; i>=3; i--) if(l[i]==1)
{
Place(i-2,2);
for(j=1; j<=30; j++) printf(" ");
}
Sleep(70);
s--;
}
for(i=3; i<=22; i++) for(j=1; j<=15; j++)
{
Place(i-2,2*j);
printf(" ");
}
appear();
}
void control()
{
int up,down,right,left,c=0,d=0,x,y,no=0,k,l=0,q=0;
k=shape;
add=0;
up=GetAsyncKeyState(VK_UP);
down=GetAsyncKeyState(VK_DOWN);
right=GetAsyncKeyState(VK_RIGHT);
left=GetAsyncKeyState(VK_LEFT);
if(down!=0)
{
tim=0;
}
if(left!=0||right!=0)
{
Sleep(100);
disappear();
for(i=0; i<=22; i++) for(j=1; j<=15; j++)
{
if(a[i][j]==2&&a[i][j-1]!=1) c++;
if(a[i][j]==2&&a[i][j+1]!=1) d++;
}
for(i=0; i<=22; i++) for(j=1; j<=15; j++) if(left!=0&&c==4&&a[i][j]==2)
{
a[i][j-1]=a[i][j];
a[i][j]=0;
}
for(i=0; i<=22; i++) for(j=15; j>=1; j--) if(right!=0&&d==4&&a[i][j]==2)
{
a[i][j+1]=a[i][j];
a[i][j]=0;
}
appear();
}
if(up!=0&&bn!=6)
{
Sleep(150);
disappear();
for(i=0; i<=22; i++)
{
for(j=1; j<=15; j++) if(a[i][j]==2)
{
x=i;
no=1;
break;
}
if(no==1) break;
}
no=0;
for(j=1; j<=15; j++)
{
for(i=0; i<=22; i++) if(a[i][j]==2)
{
y=j;
no=1;
break;
}
if(no==1) break;
}
no=0;
if(k==1) y--;
if(k==2) x--;
add=0;
if(bn==5)
{
add=1;
if(k==0||k==2) x--;
if(k==1||k==3) y--;
}
for(i=0; i<=3; i++) for(j=0; j<=3; j++) ta[i][j]=a[x+i][y+j];
if(bn>=0&&bn<5)
{
if(k==0) ta[2][0]=0;
if(k==1) ta[0][0]=0;
if(k==2) ta[0][2]=0;
if(k==3) ta[2][2]=0;
}
if(bn==5)
{
if(k==0) ta[0][3]=ta[2][0]=ta[2][1]=ta[3][1]=ta[3][0]=0;
if(k==1) ta[0][0]=ta[1][0]=ta[0][1]=ta[1][1]=ta[3][3]=0;
if(k==2) ta[3][0]=ta[0][2]=ta[0][3]=ta[1][2]=ta[1][3]=0;
if(k==3) ta[0][0]=ta[2][2]=ta[2][3]=ta[3][2]=ta[3][3]=0;
}
for(i=0; i<=2+add; i++) for(j=0; j<=2+add; j++) if(ta[i][j]!=1) l++;
if(l==9+add*7)
{
change(1);
for(i=0; i<=22; i++) for(j=1; j<=15; j++) if(a[i][j]==2) a[i][j]=0;
for(i=0; i<=2+add; i++) for(j=0; j<=2+add; j++) if(turn[i][j]==2) a[x+i][y+j]=turn[i][j];
shape++;
}
if(shape>3) shape=0;
appear();
}
}
int main()
{
int k,start,finish,d;
printf("┌");
for(i=1; i<=15; i++) printf("┄");
printf("┐\n");
for(i=1; i<=20; i++) printf("┆\n");
printf("└");
for(i=1; i<=15; i++) printf("┄");
printf("┘");
for(i=1; i<=20; i++)
{
Place(i,32);
printf("┆\n");
}
Place(2,37);
printf("下一个");
Place(8,37);
printf("得分");
Place(14,37);
printf("最高分");
Place(11,37);
printf("速度");
for(i=0; i<=2; i++) button(i);
while(ok==1) click();
while(ok!=1)
{
replace();
next();
while(1)
{
born();
next();
while(1)
{
appear();
k=0;
start=clock();
while(1)
{
finish=clock();
d=finish-start;
if(d>=tim) break;
control();
click();
if(ok==2) break;
}
if(ok==2) break;
disappear();
for(i=22; i>=0; i--) for(j=1; j<=15; j++) if(a[i][j]==2&&a[i+1][j]!=1) k++;
if(k==4)
{
for(i=22; i>=0; i--) for(j=1; j<=15; j++) if(a[i][j]==2)
{
a[i+1][j]=2;
a[i][j]=0;
}
}
else
{
for(i=22; i>=0; i--) for(j=1; j<=15; j++) if(a[i][j]==2) a[i][j]=1;
break;
}
tim=ti;
appear();
}
if(ok==2) break;
appear();
clear();
ss();
for(i=1; i<=15; i++) if(a[2][i]==1)
{
ok=1;
break;
}
}
if(ok==2)
{
ok=0;
continue;
}
for(i=1; i<=20; i++) for(j=2; j<=30; j++)
{
Place(i,j);
printf(" ");
}
Place(9,12);
printf("游戏结束!");
while(ok==1) click();
}
return 0;
}
玩法2:
//J,L左移右移;D变换方向
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <windows.h>
#include <ctime>
#include <conio.h>
#include <iostream>
using namespace std;
#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define inf 2147483647
struct 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, 1, 0}, {0, 0, 0, 0}},
{{8, 0, 0, 0}, {0, 1, 1, 0}, {0, 0, 1, 1}, {0, 0, 0, 0}},
{{9, 0, 0, 0}, {1, 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 = 1;
int fraction = 0;
int positionX, positionY;
bool locked;
int keytime = 100000000;
bool keytimeflag;
int sleeptime = 0;
bool holdflag = 1;
int passcondition = 200;
int addlinetime = 10000, addlineflag;
int locktime = 1000;
int keydownflag;
int xray = 0;
int firstwin = 1;
int exfraction = 0;
int exgamestarttime;
int blind = 0;
int lockdelay = 300;
int 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 >= 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; 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 <= 25; ++i)
puts (" ");
gotoxy (0, 0);
}
void reset ()
{
srand (time (0));
SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
puts ("");
puts (" ■■■■■■■■■■■■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■■■■■■■■■■■■");
nextA = roundblock (); nextB = roundblock (); nextC = roundblock ();
for (int i = 34; i <= 34 + nextA.size; ++i)
{
gotoxy (i, 4);
for (int j = 4; j <= 4 + nextA.size; ++j)
printf (" ");
}
printblock (nextA, 34, 4);
for (int i = 38 + nextA.size; i <= 38 + nextA.size + nextB.size; ++i)
{
gotoxy (i, 4);
for (int j = 4; j <= 4 + nextB.size; ++j)
printf (" ");
}
printblock (nextB, 38 + nextA.size, 4);
for (int i = 42 + nextA.size + nextB.size; i <= 42 + nextA.size + nextB.size + nextC.size; ++i)
{
gotoxy (i, 4);
for (int j = 4; j <= 4 + nextC.size; ++j)
printf (" ");
}
printblock (nextC, 42 + nextA.size + nextB.size, 4);
for (int i = 1; i <= 20; ++i)
map[i][0] = map[i][11] = 1;
CONSOLE_CURSOR_INFO CursorInfo;
GetConsoleCursorInfo (hOut, &CursorInfo);
CursorInfo.bVisible = 0;
SetConsoleCursorInfo (hOut, &CursorInfo);
SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
gotoxy (56, 10);
printf ("%4d", fraction);
gotoxy (57, 11);
printf ("---");
gotoxy (56, 12);
if (fraction)
printf ("%4d", fraction % 100 ? (fraction / 100 + 1) * 100 : fraction);
else
printf (" 100");
SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
gotoxy (45, 20);
printf ("00:00:00");
}
void choosedifficulty ()
{
while (1)
{
gotoxy (0, 0);
switch (mode)
{
case 1:
SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
puts ("");
puts (" ■■■■■■■■■■■■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■■■■■■■■■■■■");
gotoxy (18, 9);
SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
puts (" EASY ");
gotoxy (10, 16);
printf ("慢速的经典俄罗斯");
gotoxy (10, 17);
printf ("方块,适合初学者.");
break;
case 2:
SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_INTENSITY);
puts ("");
puts (" ■■■■■■■■■■■■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■■■■■■■■■■■■");
gotoxy (17, 9);
SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
puts ("MASTER ");
gotoxy (10, 16);
printf ("中速的街机俄罗斯");
gotoxy (10, 17);
printf ("方块,适合中级者.");
break;
case 3:
SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_INTENSITY);
puts ("");
puts (" ■■■■■■■■■■■■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■ ■");
puts (" ■■■■■■■■■■■■");
gotoxy (16, 9);
SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
puts ("SHIRASE");
gotoxy (10, 16);
printf ("高速俄罗斯方块,");
gotoxy (10, 17);
printf ("挑战速度的极限.");
break;
}
char key = _getch ();
if (key == 'i' && mode != 1)
--mode;
if (key == 'k' && mode != 3)
++mode;
gotoxy (10, 16);
printf (" ");
gotoxy (10, 17);
printf (" ");
if (key == ' ')
break;
}
gotoxy (16, 9);
printf (" ");
}
void ready_go ()
{
SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
Sleep (1000);
gotoxy (18, 8);
puts ("READY");
Sleep (800);
gotoxy (18, 8);
puts (" GO! ");
Sleep (800);
gotoxy (18, 8);
puts (" ");
}
void updatedata ()
{
switch (mode)
{
case 1:
addlinetime = inf; lockdelay = 300;
if (fraction >= 0)
{
sleeptime = 2000 - fraction * 5;
locktime = 800;
}
else if (fraction >= 100)
{
sleeptime = 1000 - fraction * 5 / 2;
locktime = 700;
}
if (fraction >= 200)
win ();
break;
case 2:
addlinetime = inf; lockdelay = 300;
if (fraction >= 0)
{
sleeptime = 1000 - fraction * 3;
locktime = 700;
}
if (fraction >= 100)
{
sleeptime = 1000 - fraction * 3;
locktime = 600;
}
if (fraction >= 200)
{
sleeptime = 1000 - fraction * 3;
locktime = 500;
if (fraction >= 300)
locktime = 700;
else if (fraction >= 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 ("%02d:%02d:%02d", clockms / 60000 % 100, clockms / 1000 % 60, clockms % 1000 / 10);
}
type_block roundblock ()
{
type_block c;
int kind = rand () % 7;
while (kind < 2 && fraction <= 6)
kind = rand () % 7;
c.size = 4; c.color = kind + 1;
if (xray) c.color = -2;
for (int i = 0; i < c.size; ++i)
for (int j = 0; j < c.size; ++j)
c.a[i][j] = sblock[kind][i][j];
return c;
}
void printblock (type_block m_block, int x, int y)
{
switch (m_block.color)
{
case 1:
SetConsoleTextAttribute (hOut, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
break;
case 2:
SetConsoleTextAttribute (hOut, FOREGROUND_RED | FOREGROUND_INTENSITY);
break;
case 3:
SetConsoleTextAttribute (hOut, FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
break;
case 4:
SetConsoleTextAttribute (hOut, FOREGROUND_GREEN | FOREGROUND_RED);
break;
case 5:
SetConsoleTextAttribute (hOut, FOREGROUND_BLUE | FOREGROUND_INTENSITY);
break;
case 6:
SetConsoleTextAttribute (hOut, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
break;
case 7:
SetConsoleTextAttribute (hOut, FOREGROUND_RED | FOREGROUND_BLUE);
break;
case -1:
SetConsoleTextAttribute (hOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED);
break;
case -2:
SetConsoleTextAttribute (hOut, FOREGROUND_GREEN);
break;
default:
SetConsoleTextAttribute (hOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
break;
}
for (int i = 0; i < m_block.size; ++i)
{
for (int j = 0 + !i; j < m_block.size; ++j)
if (m_block.a[i][j])
{
gotoxy (x + j * 2, y + i);
if (xray)
puts ("[]");
else
puts ("■");
}
}
}
void clearblock (type_block m_block, int x, int y)
{
for (int i = 0; i < m_block.size; ++i)
{
for (int j = 0 + !i; j < m_block.size; ++j)
if (m_block.a[i][j])
{
gotoxy (x + j * 2, y + i);
puts (" ");
}
}
}
int checkblock (type_block m_block, int x, int y)
{
for (int i = 0; i < m_block.size; ++i)
for (int j = 0; j < m_block.size; ++j)
{
if (!(i + j))
continue;
if (m_block.a[i][j] && (map[x + i][y + j] || x + i > 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 <= 3; ++t)
c = myup (c);
return c;
}
void checkkey ()
{
if (checkblock (now, positionX + 1, positionY) && keydownflag < (sleeptime > 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 <= 20; ++i)
if (checkblock (now, positionX + 1, positionY))
++positionX;
locked = 1;
}
}
if (clock() - keytime > locktime && !checkblock (now, positionX + 1, positionY))
locked = 1;
}
void checkline ()
{
bool tf = 0;
for (int i = 1; i <= 20; ++i)
{
int x = 0;
for (int j = 1; j <= 10; ++j)
if (map[i][j])
++x;
if (x == 10)
{
tf = 1;
for (int k = i; k >= 1; --k)
for (int l = 1; l <= 10; ++l)
map[k][l] = map[k - 1][l];
SetConsoleTextAttribute (hOut, FOREGROUND_RED);
gotoxy (10, i + 1);
for (int k = 1; k <= 10; ++k)
{
map[1][k] = 0;
printf ("..");
}
if (firstwin)
++fraction;
}
}
if (tf)
{
Sleep (100);
if (xray)
SetConsoleTextAttribute (hOut, FOREGROUND_GREEN);
else
SetConsoleTextAttribute (hOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
for (int i = 1; i <= 20; ++i)
{
gotoxy (10, i + 1);
for (int j = 1; j <= 10; ++j)
if (map[i][j] && !blind)
if (xray)
printf ("[]");
else
printf ("■");
else
printf (" ");
}
}
}
void addline ()
{
for (int i = 1; i <= 19; ++i)
for (int j = 1; j <= 10; ++j)
map[i][j] = map[i + 1][j];
for (int i = 1; i <= 10; ++i)
map[20][i] = map[19][i];
if (xray)
SetConsoleTextAttribute (hOut, FOREGROUND_GREEN);
else
SetConsoleTextAttribute (hOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
for (int i = 1; i <= 20; ++i)
{
gotoxy (10, i + 1);
for (int j = 1; j <= 10; ++j)
if (map[i][j] && !(i != 1 && blind))
if (xray)
printf ("[]");
else
printf ("■");
else
printf (" ");
}
if (blind)
{
Sleep (200);
gotoxy (10, 2);
printf (" ");
}
}
void gameover ()
{
clockms = clock () - stclockms;
blockpersecond = (double) blocknum / clockms * 1000; blockperminute = blockpersecond * 60.0;
for (int i = 20; i >= 1; --i)
{
for (int j = 1; j <= 10; ++j)
{
gotoxy (j * 2 + 8, i + 1);
puts (" ");
}
Sleep (150);
}
Sleep (2000);
SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY);
gotoxy (14, 10);
puts (" 暂定段位");
gotoxy (13, 19);
printf ("BPS %lf", blockpersecond);
gotoxy (13, 20);
printf ("BPM %lf", blockperminute);
for (int i = 1; i <= 29; ++i)
{
if (i % 2)
SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
else
SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED);
gotoxy (19, 13);
switch (mode)
{
case 1:
gotoxy (18, 13);
printf ("H%d", fraction + exfraction * 10 + rand () % 10);
break;
case 2:
if (fraction >= 999)
printf ("S13");
else if (fraction >= 950)
printf ("S12");
else if (fraction >= 900)
printf ("S11");
else if (fraction >= 850)
printf ("S10");
else if (fraction >= 800)
printf ("S9");
else if (fraction >= 750)
printf ("S8");
else if (fraction >= 700)
printf ("S7");
else if (fraction >= 650)
printf ("S6");
else if (fraction >= 600)
printf ("S5");
else if (fraction >= 550)
printf ("S4");
else if (fraction >= 500)
printf ("S3");
else if (fraction >= 450)
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 >= 900)
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 (120);
}
Sleep (2000);
system ("pause>nul");
exit (0);
}
void win ()
{
if (firstwin)
{
for (int i = 20; i >= 1; --i)
{
for (int j = 1; j <= 10; ++j)
{
gotoxy (j * 2 + 8, i + 1);
puts (" ");
map[i][j] = 0;
}
Sleep (150);
}
Sleep (1000);
firstwin = 0;
switch (mode)
{
case 1:
easy_extra_game ();
break;
case 2:
master_extra_game ();
break;
case 3:
shirase_extra_game ();
break;
default:
break;
}
return;
}
gotoxy (15, 9);
switch (mode)
{
case 1:
SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
puts (" EASY MODE ");
break;
case 2:
SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_INTENSITY);
puts (" MASTER MODE");
break;
case 3:
SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_INTENSITY);
puts ("SHIRASE MODE");
break;
}
gotoxy (15, 10);
puts (" ALL CLEAR ");
Sleep (5000);
gotoxy (15, 9);
puts (" ");
gotoxy (15, 10);
puts (" ");
gameover ();
}
void easy_extra_game ()
{
sleeptime = 0; exgamestarttime = clock (); locktime = 300; keytime = clock ();
if (mode == 3)
locktime = 200;
while (clock () - exgamestarttime < 40000)
{
if (locked)
{
++exfraction;
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);
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;
if (mode == 3 && !rand () % 4)
xray = 0;
nextC = roundblock();
xray = 1;
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 ();
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 >= 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 <= 20; ++i)
{
gotoxy (10, i + 1);
for (int j = 1; j <= 10; ++j)
if (map[i][j])
if (xray)
printf ("[]");
else
printf ("■");
else
printf (" ");
}
win ();
}
void master_extra_game ()
{
blind = 1;
easy_extra_game ();
}
void shirase_extra_game ()
{
blind = 1;
easy_extra_game ();
}
玩法3:
//俄罗斯方块3(生命无限)
//BUG:清除一行后上方的方块会重力下落
#include <iostream>
#include <windows.h>
#include <vector>
#include <mmsystem.h>
#pragma comment(lib, "winmm.lib")
using namespace std;
#define GameW 10
#define GameH 20
const int CtrlLeft = GameW*2+4 + 3;
struct Point {
Point(){}
Point(int x, int y) {_x = x, _y = y;}
int _x, _y;
};
HANDLE g_hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
HANDLE g_hInput = GetStdHandle(STD_INPUT_HANDLE);
Point g_ptCursor(0,0);
BOOL isChecking = FALSE;
BOOL g_bGameOver = FALSE;
int g_nGameBack[GameH][GameW], Case;
int nowKeyInfo = -1;
int g_nDiff = 1;
int g_nLife = 2;
int g_nScore = 0;
void SetCursor(COORD cd) {
SetConsoleCursorPosition(g_hOutput, cd);
}
void SetCursor(int x, int y){
COORD cd = {x, y};
SetCursor(cd);
}
void SetBlockCursor(int x, int y){
COORD cd = {2*x + 2, y + 1};
SetCursor(cd);
}
void SetBack(int x, int y, BOOL bk) {
SetBlockCursor(x, y);
if (bk)
printf("%s", "■");
else
printf(" ");
}
bool Out(int x, int y) {
return x < 0 || y < 0 || x >= GameW || y >= GameH;
}
struct xBlock {
public:
int len;
int nowRotateID;
BOOL mask[4][4][4];
static vector <xBlock> List;
xBlock() { len = 0; }
xBlock(int l, char *str) {
int i, j, k;
len = l;
memset(mask, FALSE, sizeof(mask));
for(i = 0; i < l; i++) {
for(j = 0; j < l; j++) {
mask[0][i][j] = str[i*l + j] - '0';
}
}
for(k = 1; k < 4; k++) {
for(i = 0; i < len; i++) {
for(j = 0; j < len; j++) {
mask[k][i][j] = mask[k-1][j][len-1-i];
}
}
}
nowRotateID = rand() % 4;
}
void rotate() {
nowRotateID ++;
if (nowRotateID >= 4)
nowRotateID = 0;
}
BOOL getUnit(int x, int y, int roID) {
if (roID == -1) {
roID = nowRotateID;
}
return mask[roID][y][x];
}
};
vector <xBlock> xBlock::List;
class Block {
public:
int x, y;
int ID;
xBlock bk;
void reset(xBlock *pbk) {
bk = *pbk;
x = 4, y = 0;
ID = ++ Case;
if(collide(0,0)) {
lifeDown();
}
draw();
*pbk = xBlock::List[rand() % xBlock::List.size()];
}
void lifeDown() {
int i, j;
for(i = 0; i < GameH; i++) {
for(j = 0; j < GameW; j++) {
SetBack(j, i, TRUE);
Sleep(10);
}
}
if(g_nLife) {
g_nLife --;
for(i = g_nLife; i < 6; i++) {
SetCursor(CtrlLeft + i, 15);
printf("%c", ' ');
}
for(i = GameH-1; i >= 0; i--) {
for(j = GameW-1; j >= 0; j--) {
SetBack(j, i, FALSE);
Sleep(10);
g_nGameBack[i][j] = 0;
}
}
}else {
g_bGameOver = TRUE;
}
}
void erase() {
int i, j;
for(i = 0; i < bk.len; i++) {
for(j = 0; j < bk.len; j++) {
if (bk.getUnit(j, i, -1)) {
if(!Out(j+x, i+y) && g_nGameBack[i+y][j+x]) {
SetBack(j+x, i+y, FALSE);
g_nGameBack[i+y][j+x] = 0;
}
}
}
}
}
void draw() {
int i, j;
for(i = 0; i < bk.len; i++) {
for(j = 0; j < bk.len; j++) {
if (bk.getUnit(j, i, -1)) {
if(!Out(j+x, i+y) && !g_nGameBack[i+y][j+x]) {
SetBack(j+x, i+y, TRUE);
g_nGameBack[i+y][j+x] = ID;
}
}
}
}
}
void draw(int x, int y) {
int i, j;
for(i = 0; i < 4; i++) {
for(j = 0; j < 4; j++) {
SetCursor(x + 2*j, y + i);
if (bk.getUnit(j, i, -1)) {
printf("%s", "■");
}else
printf(" ");
}
}
}
bool collide(int dx, int dy, int roID = -1) {
int i, j;
for(i = 0; i < bk.len; i++) {
for(j = 0; j < bk.len; j++) {
if (bk.getUnit(j, i, roID)) {
Point ptPos(j + x + dx, i + y + dy);
if(Out(ptPos._x, ptPos._y)
|| g_nGameBack[ptPos._y][ptPos._x] && ID != g_nGameBack[ptPos._y][ptPos._x]) {
return TRUE;
}
}
}
}
return FALSE;
}
void rotate(int nTimes = 1) {
int nextro = (bk.nowRotateID + nTimes) % 4;
if(collide(0, 0, nextro)) {
return ;
}
Beep(12000, 50);
erase();
bk.nowRotateID = nextro;
draw();
}
BOOL changepos(int dx, int dy) {
if(collide(dx, dy)) {
return FALSE;
}
erase();
x += dx;
y += dy;
draw();
return TRUE;
}
};
void GameInit() {
CONSOLE_CURSOR_INFO cursor_info;
cursor_info.bVisible = FALSE;
cursor_info.dwSize = 100;
SetConsoleCursorInfo(g_hOutput, &cursor_info);
xBlock::List.push_back(xBlock(3, "010111000"));
xBlock::List.push_back(xBlock(3, "110110000"));
xBlock::List.push_back(xBlock(3, "111001000"));
xBlock::List.push_back(xBlock(3, "111100000"));
xBlock::List.push_back(xBlock(3, "110011000"));
xBlock::List.push_back(xBlock(3, "011110000"));
xBlock::List.push_back(xBlock(4, "1000100010001000"));
}
void DrawFrame(int x, int y, int nWidth, int nHeight) {
int i;
for(i = 0; i < nWidth; i++) {
SetCursor(x + 2*i + 2, y);
printf("%s", "一");
SetCursor(x + 2*i + 2, y + nHeight+1);
printf("%s", "┄");
}
for(i = 0; i < nHeight; i++) {
SetCursor(x, y + i + 1);
printf("%s", "┆");
SetCursor(x + nWidth*2+2, y + i + 1);
printf("%s", "┆");
}
SetCursor(x, y);
printf("%s", "┌");
SetCursor(x, y + nHeight+1);
printf("%s", "└");
SetCursor(x + nWidth*2+2, y);
printf("%s", "┐");
SetCursor(x + nWidth*2+2, y + nHeight+1);
printf("%s", "┘");
}
void MissionInit() {
memset(g_nGameBack, FALSE, sizeof(g_nGameBack));
Case = 1;
int i;
DrawFrame(0, 0, GameW, GameH);
DrawFrame(GameW*2+4, 0, 4, GameH);
SetCursor(CtrlLeft, 2);
printf("Next");
SetCursor(CtrlLeft, 8);
printf("Speed");
for(i = 0; i < g_nDiff; i++) {
SetCursor(CtrlLeft + i, 9);
printf("%c", 1);
}
SetCursor(CtrlLeft, 11);
printf("Score");
SetCursor(CtrlLeft, 12);
printf("%d", g_nScore);
SetCursor(CtrlLeft, 14);
printf("Life");
for(i = 0; i < g_nLife; i++) {
SetCursor(CtrlLeft + i, 15);
printf("%c", 3);
}
SetCursor(CtrlLeft-1, 19);
printf("@Metaphu");
SetCursor(CtrlLeft-1, 20);
printf("Revised");
}
void Check() {
isChecking = TRUE;
int i, j, k;
vector <int> line;
for(i = 0; i < GameH; i++) {
for(j = 0; j < GameW; j++) {
if(!g_nGameBack[i][j])
break;
}
if(j == GameW) {
line.push_back(i);
}
}
if(line.size()) {
int nCount = 7;
while(nCount --) {
for(i = 0; i < line.size(); i++) {
for(j = 0; j < GameW; j++) {
SetBack(j, line[i], nCount&1);
}
}
Sleep(70);
}
for(i = 0; i < line.size(); i++) {
for(j = 0; j < GameW; j++) {
g_nGameBack[line[i]][j] = 0;
}
}
for(i = 0; i < GameW; i++) {
int next = GameH-1;
for(j = GameH-1; j >= 0; j--) {
for(k = next; k >= 0; k--) {
if(g_nGameBack[k][i])
break;
}
next = k - 1;
BOOL is = (k >= 0);
SetBack(i, j, is);
g_nGameBack[j][i] = is;
}
}
g_nScore += 2*line.size()-1;
SetCursor(CtrlLeft, 12);
printf("%d", g_nScore);
if( g_nScore >= g_nDiff * g_nDiff * 10) {
if(g_nDiff <= 6)
g_nDiff ++;
}
if( g_nScore >= 50 * (g_nLife+1)) {
if(g_nLife <= 6)
g_nLife ++;
}
}
isChecking = FALSE;
}
int main() {
Block* obj = new Block();
Block* buf = new Block();
BOOL bCreateNew = FALSE;
int nTimer = GetTickCount();
int LastKeyDownTime = GetTickCount();
GameInit();
MissionInit();
buf->bk = xBlock::List[rand() % xBlock::List.size()];
while(1) {
if(!bCreateNew) {
bCreateNew = TRUE;
obj->reset(&buf->bk);
if(g_bGameOver)
break;
buf->draw(CtrlLeft - 1, 4);
}
if (GetTickCount() - nTimer >= 1000 / g_nDiff) {
nTimer = GetTickCount();
if (!obj->collide(0, 1))
obj->changepos(0, 1);
else {
Check();
bCreateNew = FALSE;
}
}
if (GetTickCount() - LastKeyDownTime >= 100) {
if(FALSE == isChecking) {
LastKeyDownTime = GetTickCount();
if (GetAsyncKeyState(VK_UP)) {
obj->rotate();
}
if (GetAsyncKeyState(VK_LEFT)) {
obj->changepos(-1, 0);
}
if (GetAsyncKeyState(VK_RIGHT)) {
obj->changepos(1, 0);
}
if (GetAsyncKeyState(VK_DOWN)) {
if( FALSE == obj->changepos(0, 2) )
obj->changepos(0, 1);
}
}
}
}
SetCursor(8, 10);
printf("Game Over");
SetCursor(0, GameH+3);
printf("按ESC键退出游戏");
while(1) {
if (GetAsyncKeyState(VK_ESCAPE))
break;
}
return 0;
}