C++俄罗斯方块代码.docx
《C++俄罗斯方块代码.docx》由会员分享,可在线阅读,更多相关《C++俄罗斯方块代码.docx(23页珍藏版)》请在冰豆网上搜索。
![C++俄罗斯方块代码.docx](https://file1.bdocx.com/fileroot1/2022-12/9/cc0d0568-37e5-4239-bacd-2f90d0134093/cc0d0568-37e5-4239-bacd-2f90d01340931.gif)
C++俄罗斯方块代码
#include
#include
#include
#include
#include
#include"colorConsole.h"
voidbegin();
voidframe();
int*getblocks();
voidmove(intline);
voiddrawblocks(intline);
voidclearsquare(intline);//
voidturn(intline);
boolisavailable(intline);//
voidremember(intline);
voiddeleteline(intline);
boolifgameover();
voidend();
#defineup72
#definedown80
//老师的文件
//开始游戏
//边框设定
//方块产生
//移动
//方块显示
方块擦出
//方块旋转
判断是否能下落
//记忆方块位置
//方块满一行消除
//判断是否游戏结束
//游戏结束
#defineleft
75
#defineright
77
#defineesc
27
HANDLEhandle;
inta1[4][4]={{1},{1,1,1}};
inta2[4][4]={{0,1},{1,1,1}};
inta3[4][4]={{1,1},{0,1,1}};
inta4[4][4]={{0,0,1},{1,1,1}};
inta5[4][4]={{0,1,1},{1,1}};
inta6[4][4]={{1,1,1,1}};
inta7[4][4]={{1,1},{1,1}};
introw=0;
intscore=0;
intlevel=0;
int*block1=NULL;
int*block2=NULL;
int*block3=NULL;
intcoordinate[12][18]={0};
计算在内)
intjudge=0;
//七种方块的二维数组
//列数
//坐标数组,边框12*18(最后
,两边边框
intscorex=0;
inttemp[4][4]={0};
voidmain()//主函数
{
intt=1;
handle=initiate();
while(t)
{
t=0;
begin();
sndPlaySound("music.wav",SND_LOOP|SND_ASYNC);
frame();
WORDwColors[1];
wColors[0]=FOREGROUND_GREEN|FOREGROUND_RED|FOREGROUND_INTENSITY
for(intk=1;k<=999999;k++)
{
if(ifgameover())//判断是否结束
{
textout(handle,34,10,wColors,1,"GameOver");
Sleep(800);
end();
}
else
{
if(k==1)
block2=getblocks();
block3=block2;
//block2指向将出现的方块地址
block2=getblocks();
//获取下一个新的方块
block1=block3;
//擦除next的方块
//在next显示下一块方块图形
row=52;clearsquare(16);block1=block2;drawblocks(15);
row=34;
block1=block3;
for(inti=4;i<=7;i++)//所构建的方块图形最多只有占有两排,所以只
用4-7即可对应
{
if(*(block1+i))
textout(handle,26+i*2,4,wColors,1,"■");//方块先露出下面
部分
}
Sleep(500-50*level);
for(intline=4;line<=22;line++)落,方块从第四排开始出现
{
if(isavailable(line))
方块是否碰壁,碰到已落方块
{
clearsquare(line);
露初的下面分
drawblocks(line);
的下落方块
move(line);
}
else
{
remember(line);
这些位置对应的all数组中元素置1
deleteline(line);
分
if(line==4)
judge=1;
//方块自主下
//检验刚产生的
//消除方块先
//产生完整
//落定后将
//消行以及加
break;
}
}
}
}
}
}
voidbegin()
{
inti=1;
WORDwColors[1];
wColors[0]=FOREGROUND_GREEN|FOREGROUND_INTENSITY;
WORDwColors1[2];
wColors1[0]=FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_INTE
NSITY;
wColors1[1]=FOREGROUND_RED|FOREGROUND_INTENSITY;
textout(handle,18,4,wColors,1,"
Jk丄%;
textout(handle,18,5,wColors,1,"
J◎kJ◎k”);
textout(handle,18,6,wColors,1,"
/■■■■■■■Ik
textout(handle,18,7,wColors,1,"
—人、1;
");
while(i)
{
textout(handle,30,8,wColors1,2,"俄罗斯方块");
Sleep(800);
textout(handle,30,8,wColors1,2,"");
Sleep(800);
if(_kbhit())//输入等级
{
textout(handle,18,8,wColors,1,"
textout(handle,18,9,wColors,1,"
textout(handle,18,10,wColors,1,"
textout(handle,18,11,wColors,1,"
textout(handle,18,12,wColors,1,"
textout(handle,18,13,wColors,1,"
textout(handle,18,14,wColors,1,"
textout(handle,18,15,wColors,1,"
switch(_getch())
{
case'1':
{
level=1;
i=0;//跳出循环break;
}
case'2':
{
level=4;
i=0;
break;
}
case'3':
{
level=7;
i=0;
break;
}
}
}
}
system("cls");//清屏
}
voidframe()//边框的设定
{
WORDwColors[1];wColors[0]=FOREGROUND_GREEN|FOREGROUND_BLUE|FOREGROUND_INTE
NSITY;
WORDwColors1[1];wColors1[0]=FOREGROUND_RED|FOREGROUND_INTENSITY;
for(inti=0;i<=11;i++)
coordinate[i][17]=1;//底排边框定义为1
for(intj=0;j<=17;j++)
{
coordinate[0][j]=1;//两边边框定义为1
coordinate[11][j]=1;
}
charstring[5];
textout(handle,59,5,wColors,1,itoa(level,string,10));
textout(handle,52,5,wColors,1,"level:
");
textout(handle,52,9,wColors,1,"score:
0");
textout(handle,52,13,wColors,1,"next:
");
textout(handle,10,6,wColors1,1,"
暂停
SPACE")
textout(handle,10,7,wColors1,1,"
退出
ESC");
textout(handle,10,8,wColors1,1,"
翻转
T);
textout(handle,10,9,wColors1,1,"
向右
t");
textout(handle,10,10,wColors1,1,"
向左
J");
textout(handle,10,11,wColors1,1,"
加速
textout(handle,33,2,wColors,1,"
来~战个痛
");
for(intm=13;m<=24;m++)
{
textout(handle,2*m,3,wColors,1,"
—//上边框
}
for(intn=4;n<=21;n++)
{
textout(handle,26,n,wColors,1,"
II");//左边框
}
for(intk=4;k<=21;k++)
{
textout(handle,48,k,wColors,1,"
II");//右边框
}
for(intl=13;l<=23;l++)
//下边框
{
textout(handle,2*l,21,wColors,1,"
}
textout(handle,26,3,wColors,1,"F');
textout(handle,48,3,wColors,1,"、');
textout(handle,26,21,wColors,1,"k');
textout(handle,48,21,wColors,1,"/);
}
int*getblocks()//随机方块生成
{
int*m=NULL;
srand(time(NULL));
intn=rand()%7;
switch(n)
{
case0:
m=&a1[0][0];break;
case1:
m=&a2[0][0];break;
case2:
m=&a3[0][0];break;
case3:
m=&a4[0][0];break;
case4:
m=&a5[0][0];break;
case5:
m=&a6[0][0];break;
case6:
m=&a7[0][0];break;
}
returnm;
}
voiddrawblocks(intline)//出现方块
{
WORDwColors[1];
wColors[0]=FOREGROUND_GREEN|FOREGROUND_RED|FOREGROUND_INTEN
SITY;
for(intj=0;j<=15;j++)
{
inttemp;
temp=j/4;
if(*(block1+j))textout(handle,row+j*2-temp*8,line+temp,wColors,1,"■");
}
}
voidclearsquare(intline)//方块消失
{
WORDwColors[1];
wColors[0]=FOREGROUND_BLUE|FOREGROUND_INTENSITY;
temp=m/4;//得0-3对应方块数组1-4行
if(*(block1+m))textout(handle,row+m*2-temp*8,line-1+temp,wColors,1,"");
}
}
}
voidmove(intline)//方块的左右移动,加速下落,翻转等
{
intmid=0,speed=100-10*level;
while(mid{
if(_kbhit())
{
switch(_getch())
{
case72:
//翻转
{
turn(line);
break;
}
case75:
//左移
{
row=row+2;
if(isavailable(line))
{
row=row-2;
clearsquare(line+1);
row=row+2;
}
case80:
{
}
case27:
{
}
case32:
{
drawblocks(line);
}
else
row=row-2;
break;
//加速下落,即直接跳除循环
mid=speed;
break;
//终止游戏
end();
break;
//暂停
intflag=1;
while(flag)
{
if(_kbhit())
{
if(_getch()==32)
flag=0;
break;
}
else
Sleep(10);
}
}
default:
break;
}
}
Sleep(8);//使方块延迟mid++;
}
}
voidturn(intline)
//消除原来的图案
{
clearsquare(line+1);
intb[4][4]={0};//保存旋转前的方块
intnum=0,l=0;
for(intm=0;m<=3;m++)
{
for(intn=0;n<=3;n++)
{
b[m][n]=*(block1+m*4+n);
temp[m][n]=0;
}
}
for(inti=3;i>=0;i--)
{
for(intj=0;j<4;j++)
{
if(b[i][j])
{
temp[j][l]=b[i][j];
num=1;
//把b[4][4]全赋值为当前图形数组
//按行从下向上扫描
//按列从左向右扫描
//如果为有效点,则进行90度旋转
}
}
if(num)
{
l++;
num=0;
}
}
block1=&temp[0][0];
if(isavailable(line))
drawblocks(line);
else
{
for(intp=0;p<=3;p++)
{
for(intq=0;q<=3;q++)
temp[p][q]=b[p][q];
}
block1=&temp[0][0];
drawblocks(line);
}
}
//检验,即看方块即将存在位置是否已经有1
boolisavailable(intline)
{
intx,y;
for(intm=0;m<=15;m++)
{
inttemp;
temp=m/4;
x=row/2-13-4*temp+m;
y=line-4+temp;
if(*(block1+m)&&coordinate[x][y])//
return0;
}
}
voidremember(intline)
{
intx,y;
for(intm=0;m<=15;m++)
{
inttemp;
temp=m/4;
x=row/2-13-temp*4+m;
y=line-4+temp;
if(*(block1+m))
置为1
//边框左边已有13个位置
//上面已有4个位置
相与为1则返回0,否则跳出并循环继续
//记忆
//如果当前位置为1,则返回原位置,并设
coordinate[x][y-1]=1;
}
}
voiddeleteline(intl)//消行
{
WORDwColors[1];
wColors[0]=FOREGROUND_GREEN|FOREGROUND_RED|FOREGROUND_INTEN
SITY;
intsnum=0,b=0;
for(intm=0;m<=16;m++)//从上向下消去方块
{
if(coordinate[1][m]==1&&coordinate[2][m]==1&&coordinate[3][m]==1&&coordi
nate[4][m]==1&&coordinate[5][m]==1&&coordinate[6][m]==1&&coordinate[7][
m]==1&&coordinate[8][m]==1&&coordinate[9][m]==1&&coordinate[10][m]==1
)
{
textout(handle,28,m+4,wColors,1,"good”);
Sleep(750);
for(intn=1;n<=m;n++)
{
coordinate[j][m-n+1]=coordinate[j][m-n];
}
snum++;
}
}
for(intn=1;n<=10;n++)
{
for(intd=0;d<=16;d++)
{
intx,y;
x=n*2+26;
y=d+4;
textout(handle,x,y,wColors,1,"");
if(coordinate[n][d])
{
textout(handle,x,y,wColors,1,"■");
}
}
}
score=score+(snum*(snum+1)/2);
if((score-scorex)>=30)//每得到30分自动加速
{
level++;
scorex=score;
}
charstring[5];
textout(handle,59,9,wColors,1,itoa(score,string,10));
textout(handle,59,5,wColors,1,itoa(level,string,10));
}
boolifgameover()//终止游戏
{
if(judge==1)
{
return1;
}
else
return0;
}
voidend()//退出
{
WORDwColors[1];wColors[0]=FOREGROUND_GREEN|FOREGROUND_INTENSITY;textout(handle,28,22,wColors,1,"Pressanykeytoexit");
while
(1)
{
if(_kbhit())
{
exit(EXIT_SUCCESS);
}
}
}