//吴德盛
#include <conio.h>
#include <stdlib.h>
#include <windows.h>
#include "colorConsole.h" //老师的模板
#define SQUARE_COLOR FOREGROUND_RED| FOREGROUND_GREEN|FOREGROUND_INTENSITY //方块的颜色
#define up 72
#define down 80
#define left 75
#define right 77
#define esc 27
#define MAPW 12 //地图的宽度
#define MAPH 20 //地图的高度
BOOL isavailable(int a[],int x,int y,int w,int h); //判定是否能放下
void turn(int a[][4],int w,int h,int *x,int y); //转动
int * create(); //创建方块
void init(); //初始化工作
void drawblocks(int a[],int w,int h,int x,int y,WORD wColors[],int nColors);
void clearcache(); //清除键盘缓冲区
void end();
void clearsquare(int *a,int w,int h,int x,int y);
void gameover();
void deletemap(int m[][MAPW],int row,int w,int h); //消除一行
int dx=30,dy=5; //屏幕上的偏移量
int score=0,level=0;
int map[MAPH][MAPW];
int a1[4][4]={{1},{1,1,1}};
int a2[4][4]={{0,1},{1,1,1}};
int a3[4][4]={{1,1},{0,1,1}};
int a4[4][4]={{0,0,1},{1,1,1}};
int a5[4][4]={{0,1,1},{1,1}};
int a6[4][4]={{1,1,1,1}};
int a7[4][4]={{1,1},{1,1}};
int a[4][4];
int main()
{
init();
int *b=NULL;
b=create(); //预创建方块
int sign,blank,x,y;
while(1)
{
for(int i=0;i<4;i++) //复制方块
for(int j=0;j<4;j++)
if(a[i][j]=*(b+i*4+j)) blank=i;
y=1-blank;x=4;
clearsquare(&a[0][0],4,4,13,13);
b=create();
HANDLE handle;
handle=initiate();
WORD wColors[1]={FOREGROUND_RED| FOREGROUND_GREEN|FOREGROUND_INTENSITY };
drawblocks(b,4,4,13,13,wColors,1);
wColors[0]=SQUARE_COLOR;
drawblocks(&a[0][0],4,4,x,y,wColors,1);
clearcache();
char string[5];
wColors[0]=FOREGROUND_RED| FOREGROUND_GREEN|FOREGROUND_INTENSITY;
textout(handle,26+dx,5+dy,wColors,1,itoa(score,string,10));
textout(handle,26+dx,9+dy,wColors,1,itoa(level,string,10));
sign=1;
while(sign)
{
int delay=0,max_delay=100-10*level; //延迟量
while(delay<max_delay)
{
if(_kbhit()) //用if避免按住键使方块卡住
{
int draw=0;
int key=_getch();
switch (key)
{
case up:
clearsquare(&a[0][0],4,4,x,y);
turn(a,4,4,&x,y);
draw=1;
break;
case down:
delay=max_delay;
break;
case left:
if(isavailable(&a[0][0],x-1,y,4,4))
{
clearsquare(&a[0][0],4,4,x,y);
x--;
draw=1;
}
break;
case right:
if(isavailable(&a[0][0],x+1,y,4,4))
{
clearsquare(&a[0][0],4,4,x,y);
x++;
draw=1;
}
break;
case esc:
end();
break;
}
if(draw)
{
HANDLE handle;
handle=initiate();
WORD wColors[1]={SQUARE_COLOR};
drawblocks(&a[0][0],4,4,x,y,wColors,1);
draw=0;
}
}
_sleep(8);delay++;
}
if(isavailable(&a[0][0],x,y+1,4,4)) //是否能下移
{
clearsquare(&a[0][0],4,4,x,y);
y++;
HANDLE handle;
handle=initiate();
WORD wColors[1]={SQUARE_COLOR};
drawblocks(&a[0][0],4,4,x,y,wColors,1);
}
else
{
sign=0; //标记,使跳出 while(sign) 循环,产生新方块
if(y<=1) gameover(); //是否结束
for(int i=0;i<4;i++) //放下方块
for(int j=0;j<4;j++)
if(a[i][j]&&((i+y)<MAPH-1)&&((j+x)<MAPW-1))
map[i+y][j+x]=a[i][j];
int full,k=0;
for(i=y;i<min(y+4,MAPH-1);i++)
{
full=1;
for(int j=1;j<11;j++)
if(!map[i][j]) full=0;
if(full) //消掉一行
{
deletemap(map,i,MAPW,MAPH);
k++;
score=score+k;
level=min(score/30,9);
}
}
}
}
}
return EXIT_SUCCESS;
}
BOOL isavailable(int a[],int x,int y,int w,int h)
{
for(int i=max(y,1);i<y+h;i++)
for(int j=x;j<x+w;j++)
if(map[i][j]&&a[w*(i-y)+j-x])
return 0;
return 1;
}
int * create()
{
int * a=NULL;
int c=rand()%7;
switch(c)
{
case 0:
a=&a1[0][0];break;
case 1:
a=&a2[0][0];break;
case 2:
a=&a3[0][0];break;
case 3:
a=&a4[0][0];break;
case 4:
a=&a5[0][0];break;
case 5:
a=&a6[0][0];break;
case 6:
a=&a7[0][0];break;
}
return a;
}
void init()
{
for(int i=0;i<20;i++)
{
map[i][0]=-2;
map[i][11]=-2;
}
for(i=0;i<12;i++)
{
map[0][i]=-1;
map[19][i]=-1;
}
map[0][0]=-3;
map[0][11]=-3;
map[19][0]=-3;
map[19][11]=-3;
HANDLE handle;
handle=initiate();
WORD wColors[1]={FOREGROUND_RED| FOREGROUND_GREEN|FOREGROUND_INTENSITY};
textout(handle,26+dx,3+dy,wColors,1,"SCORE");
textout(handle,26+dx,7+dy,wColors,1,"LEVEL");
textout(handle,26+dx,11+dy,wColors,1,"NEXT");
wColors[0]=FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_INTENSITY;
drawblocks(&map[0][0],12,20,0,0,wColors,1);
textout(handle,dx,dy,wColors,1,"◇══════════◇");
wColors[0]=FOREGROUND_RED| FOREGROUND_GREEN|FOREGROUND_INTENSITY;
textout(handle,dx-16,dy,wColors,1,"按任意键开始");
wColors[0]=FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_INTENSITY ;
textout(handle,dx-15,dy+3,wColors,1,"制作者");
wColors[0]=FOREGROUND_RED| FOREGROUND_GREEN|FOREGROUND_INTENSITY ;
textout(handle,dx-15,dy+5,wColors,1,"吴德盛");
textout(handle,dx-15,dy+7,wColors,1,"王山风");
textout(handle,dx-15,dy+9,wColors,1,"张悦");
textout(handle,dx-15,dy+11,wColors,1,"罗晓斌");
textout(handle,dx-15,dy+13,wColors,1,"刘亚辉");
wColors[0]=FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_INTENSITY ;
textout(handle,dx-15,dy+15,wColors,1,"测试者");
wColors[0]=FOREGROUND_RED| FOREGROUND_GREEN|FOREGROUND_INTENSITY ;
textout(handle,dx-15,dy+17,wColors,1,"唐程毅");
int x=_getch();
srand(x);
textout(handle,dx-16,dy,wColors,1," ");
}
void drawblocks(int a[],int w,int h,int x,int y,WORD wColors[],int nColors)
{
HANDLE handle;
handle = initiate();
int temp;
for(int i=0;i<h;i++)
for(int j=0;j<w;j++)
if((temp=a[i*w+j])&&y+i>0)
{
if(temp==-3)
textout(handle,2*(x+j)+dx,y+i+dy,wColors,nColors,"◆");
else if(temp==-2)
textout(handle,2*(x+j)+dx,y+i+dy,wColors,nColors,"║");
else if(temp==-1)
textout(handle,2*(x+j)+dx,y+i+dy,wColors,nColors,"═");
else if(temp==1)
textout(handle,2*(x+j)+dx,y+i+dy,wColors,nColors,"■");
}
}
void clearcache()
{
while(_kbhit())
{
_getch();
}
}
void end()
{
exit(EXIT_SUCCESS);
}
void turn(int a[][4],int w,int h,int *x,int y)
{
int b[4][4]={{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0}};
int sign=0,line=0;
for(int i=h-1;i>=0;i--)
{
for(int j=0;j<w;j++)
if(a[i][j])
{
b[j][line]=a[i][j];
sign=1;
}
if(sign)
{
line++;
sign=0;
}
}
for(i=0;i<4;i++)
if(isavailable(&b[0][0],*x-i,y,w,h))
{
*x-=i;
for(int k=0;k<h;k++)
for(int j=0;j<w;j++)
a[k][j]=b[k][j];
break;
}
}
void clearsquare(int *a,int w,int h,int x,int y)
{
HANDLE handle;
handle=initiate();
WORD wColors[1]={SQUARE_COLOR};
for(int i=0;i<h;i++)
for(int j=0;j<w;j++)
if(a[i*w+j]&&i+y>0)
textout(handle,2*(x+j)+dx,y+i