C++俄罗斯方块程序
# include & ltstdio.h & gt
# include & ltdos.h & gt
# include & ltconio.h & gt
# include & ltgraphics.h & gt
# include & ltstdlib.h & gt
#ifdef __cplusplus
#define __CPPARGS...
#否则
#define __CPPARGS
#endif
#define MINBOXSIZE 15 /*最小方形尺寸*/
#定义背景颜色7 /*背景颜色*/
#定义GX 200
#定义GY 10
#define SJNUM 10000 /*玩家每达到10000级,就会增加一级速度*/(最高等级达到9级时停止增加速度)。
/*键码*/
#定义VK _左0x4b00
#定义VK _右0x4d00
#定义VK_DOWN 0x5000
#定义VK_UP 0x4800
#定义VK_HOME 0x4700
#定义VK_END 0x4f00
#定义VK空间0x3920
#定义VK_ESC 0x011b
#定义VK _输入0x1c0d
/*定义俄罗斯方块的方向*/
#定义F_DONG 0
#定义F_NAN 1
#定义F_XI 2
#定义F_BEI 3
#define NEXTCOL 20 /*要打印的下一个框的纵坐标*/
#define NEXTROW 12 /*要绘制的下一个框的水平标签*/
#define MAXROW 14 /*游戏屏幕大小*/
#定义MAXCOL 20
#定义SCCOL 100 /*在游戏屏幕大监视器上的相对位置*/
#定义SCROW 60
int gril[22][16];/*游戏屏幕坐标*/
int col=1,row = 7;/*当前框的水平和垂直坐标*/
int boxfx=0,box GS = 0;/*当前神庙块的形状和方向*/
int nextboxfx=0,nextboxgs=0,maxcol = 22/*下一个方块的形状和方向*/
int minboxcolor=6,nextminboxcolor = 6;
int num = 0;/*游戏分数*/
int dj=0,gamedj[10]={18,16,14,12,10,8,6,4,2,1 };/*游戏关卡*/(最大速度18次下落)
/*使用三维数组记录盒子的初始形状和方向*/
int boxstr[7][4][16]={
{
{1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0},
{0,1,0,0,1,1,0,0,1,0,0,0,0,0,0,0},
{1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0},
{0,1,0,0,1,1,0,0,1,0,0,0,0,0,0,0}
},
{
{0,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0},
{1,0,0,0,1,1,0,0,0,1,0,0,0,0,0,0},
{0,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0},
{1,0,0,0,1,1,0,0,0,1,0,0,0,0,0,0}
},
{
{1,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0},
{1,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0},
{1,0,0,0,1,0,0,0,1,1,0,0,0,0,0,0},
{0,0,1,0,1,1,1,0,0,0,0,0,0,0,0,0}
},
{
{1,1,0,0,1,0,0,0,1,0,0,0,0,0,0,0},
{1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0},
{0,1,0,0,0,1,0,0,1,1,0,0,0,0,0,0},
{1,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0}
},
{
{0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0},
{0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0},
{0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0},
{0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0}
},
{
{1,1,0,0,1,1,0,0,0,0,0,0.0,0,0,0},
{1,1,0,0,1,1,0,0,0,0,0,0.0,0,0,0},
{1,1,0,0,1,1,0,0,0,0,0,0.0,0,0,0},
{1,1,0,0,1,1,0,0,0,0,0,0.0,0,0,0}
},
{
{0,0,0,0,1,1,1,0,0,1,0,0,0,0,0,0},
{1,0,0,0,1,1,0,0,1,0,0,0,0,0,0,0},
{0,1,0,0,1,1,1,0,0,0,0,0.0,0,0,0},
{0,1,0,0,1,1,0,0,0,1,0,0,0,0,0,0}
}
};
/*随机获取当前方块和下一个方块的形状和方向*/
void boxrad(){
minboxcolor = nextminboxcolor
boxgs = nextboxgs
boxfx = nextboxfx
nextminboxcolor = random(14)+1;
if(nextminboxcolor = = 4 | | nextminboxcolor = = 7 | | nextminboxcolor = = 8)
nextminboxcolor = 9;
nextboxfx = F _ DONG
nextbox GS = random(7);
}
/*初始化图形模块测试*/
void init(int gdrive,int gmode){
int错误代码;
init graph(& amp;g drive & amp;gmode," D:\ \ TC \ \ ";
error code = graph result();
if(errorcode!=grOk){
printf("的错误:%s ",grapherrormsg(error code));
退出(1);
}
}
/*在图形模式下清除屏幕*/
void cls()
{
setfillstyle(SOLID_FILL,0);
set color(0);
巴(0,0,640,480);
}
/*图形模式下的高级屏幕清除*/
void clscr(int a,int b,int c,int d,int color){
setfillstyle(SOLID_FILL,color);
setcolor(颜色);
酒吧(甲、乙、丙、丁);
}
/*最小正方形的绘制*/
void minbox(int asc,int bsc,int color,int bdcolor){
int a=0,b = 0;
a = sc col+ASC;
b = SCROW+BSC;
clscr(a+1,b+1,a-1+MINBOXSIZE,b-1+MINBOXSIZE,color);
如果(色!=BGCOLOR){
set color(BD color);
line(a+1,b+1,a-1+MINBOXSIZE,b+1);
line(a+1,b+1,a+1,b-1+MINBOXSIZE);
line(a-1+MINBOXSIZE,b+1,a-1+MINBOXSIZE,b-1+MINBOXSIZE);
line(a+1,b-1+MINBOXSIZE,a-1+MINBOXSIZE,b-1+MINBOXSIZE);
}
}
/*游戏中出现的文本*/
void txt(int a,int b,char *txt,int font,int color){
setcolor(颜色);
settextstyle(0,0,font);
outtextxy(a,b,txt);
}
/*windows绘图*/
void win(int a,int b,int c,int d,int bgcolor,int bordercolor){
clscr(a,b,c,d,bgcolor);
set color(border color);
线(a,b,c,b);
线(a,b,a,d);
线(a,d,c,d);
线(丙,乙,丙,丁);
}
/*当前框的绘制*/
void funbox(int a,int b,int color,int bdcolor){
int i,j;
int boxz[4][4];
for(I = 0;我& lt16;i++)
boxz[I/4][I % 4]= boxstr[boxgs][boxfx][I];
for(I = 0;我& lt4;i++)
for(j = 0;j & lt4;j++)
if(boxz[i][j]==1)
minbox((j+row+a)*MINBOXSIZE,(i+col+b)*MINBOXSIZE,color,BD color);
}
/*绘制下一个框*/
void nextfunbox(int a,int b,int color,int bdcolor){
int i,j;
int boxz[4][4];
for(I = 0;我& lt16;i++)
boxz[I/4][I % 4]= boxstr[nextboxgs][nextboxfx][I];
for(I = 0;我& lt4;i++)
for(j = 0;j & lt4;j++)
if(boxz[i][j]==1)
minbox((j+a)*MINBOXSIZE,(i+b)*MINBOXSIZE,color,BD color);
}
/*时间中断的定义*/
#定义定时器0x1c
int timer counter = 0;
void中断(* old handler)(_ _ CPPARGS);
void中断newhandler(__CPPARGS){
timer counter++;
old handler();
}
void SetTimer(void interrupt(* int proc)(_ _ CPPARGS)){
old handler = get vect(TIMER);
disable();
setvect(TIMER,int proc);
enable();
}
/*因为游戏规则,消除方块最小的线*/
void delcol(int a){
int i,j;
for(I = a;我& gt1;我-)
for(j = 1;j & lt15;j++){
minbox(j*MINBOXSIZE,i*MINBOXSIZE,BGCOLOR,BGCOLOR);
gril[I][j]= gril[I-1][j];
if(gril[i][j]==1)
minbox(j*MINBOXSIZE,i*MINBOXSIZE,minboxcolor,0);
}
}
/*用最小的正方形消除所有行*/
void delete(){
int i,j,zero,delgx = 0;
char * nm = " 00000
for(I = 1;我& lt21;i++){
零= 0;
for(j = 1;j & lt15;j++)
if(gril[i][j]==0)
零= 1;
if(零==0){
德尔科尔(一);
delgx++;
}
}
num = num+delgx * delgx * 10;
DJ = num/10000;
sprintf(nm," %d ",num);
clscr(456,173,500,200,4);
txt(456,173,"编号:",1,15);
txt(456,193,nm,1,15);
}
/*中断结束时间*/
void KillTimer(){
disable();
setvect(TIMER,old handler);
enable();
}
/*测试当前方块是否可以向下下落*/
int downok(){
int i,j,k=1,a[4][4];
for(I = 0;我& lt16;i++)
a[I/4][I % 4]= boxstr[boxgs][boxfx][I];
for(I = 0;我& lt4;i++)
for(j = 0;j & lt4;j++)
如果(a[I][j]& amp;& ampgril[col+i+1][row+j])
k = 0;
返回(k);
}
/*测试当前方块是否可以向左*/
int leftok(){
int i,j,k=1,a[4][4];
for(I = 0;我& lt16;i++)
a[I/4][I % 4]= boxstr[boxgs][boxfx][I];
for(I = 0;我& lt4;i++)
for(j = 0;j & lt4;j++)
如果(a[I][j]& amp;& ampgril[col+i][row+j-1])
k = 0;
返回(k);
}
/*测试当前方块是否可以向右对齐*/
int rightok(){
int i,j,k=1,a[4][4];
for(I = 0;我& lt16;i++)
a[I/4][I % 4]= boxstr[boxgs][boxfx][I];
for(I = 0;我& lt4;i++)
for(j = 0;j & lt4;j++)
如果(a[I][j]& amp;& ampgril[col+i][row+j+1])
k = 0;
返回(k);
}
/*测试当前方块是否可以变形*/
int upok(){
int i,j,k=1,a[4][4];
for(I = 0;我& lt4;i++)
for(I = 0;我& lt16;i++)
a[I/4][I % 4]= boxstr[boxgs][boxfx+1][I];
for(I = 3;我& gt=0;我-)
for(j = 3;j & gt=0;j -)
如果(a[I][j]& amp;& ampgril[col+i][row+j])
k = 0;
返回(k);
}
/*当前框落下后,标记屏幕坐标*/
void setgril(){
int i,j,a[4][4];
funbox(0,0,minboxcolor,0);
for(I = 0;我& lt16;i++)
a[I/4][I % 4]= boxstr[boxgs][boxfx][I];
for(I = 0;我& lt4;i++)
for(j = 0;j & lt4;j++)
if(a[i][j])
gril[col+I][row+j]= 1;
col = 1;row = 7;
}
/*游戏结束*/
void gameover(){
int i,j;
for(I = 20;我& gt0;我-)
for(j = 1;j & lt15;j++)
minbox(j*MINBOXSIZE,i*MINBOXSIZE,2,0);
txt(103,203,《游戏结束》,3,10);
}
/*键的设置*/
void call_key(int keyx){
开关(keyx){
案例VK向下:{/*向下箭头,在横坐标上加一。*/
if(downok()){
col++;
funbox(0,0,minboxcolor,0);}
否则{
funbox(0,0,minboxcolor,0);
setgril();
nextfunbox(NEXTCOL,NEXTROW,4,4);
boxrad();
nextfunbox(NEXTCOL,NEXTROW,nextminboxcolor,0);
delete();
}
打破;
}
大小写VK _向上:{/*向上箭头键,方向形状旋转90度*/
if(upok())
box fx++;
if(box FX & gt;3)
box FX = 0;
funbox(0,0,minboxcolor,0);
打破;
}
大小写VK _左:{ /*左箭头键,在纵坐标上减一*/
if(leftok())
行-;
funbox(0,0,minboxcolor,0);
打破;
}
大小写VK _右:{ /*右箭头键,加一*/
if(rightok())
row++;
funbox(0,0,minboxcolor,0);
打破;
}
案例VK_SPACE: /*空格键,直接下降到最后一个可以下降的地方*/
while(downok())
col++;
funbox(0,0,minboxcolor,0);
setgril();
nextfunbox(NEXTCOL,NEXTROW,4,4);
boxrad();
nextfunbox(NEXTCOL,NEXTROW,nextminboxcolor,0);
delete();
打破;
默认值:
{
txt(423,53," worng key!",1,4);
txt(428,80,"请输入一个唯一的密钥AG!",1,4);
getch();
clscr(420,50,622,97,BGCOLOR);
}
}
}
/*中断开始时间*/(顺序关系格式)
void timezd(void){
int键;
SetTimer(new handler);
boxrad();
nextfunbox(NEXTCOL,NEXTROW,nextminboxcolor,0);
for(;;){
if(bioskey(1)){
key = BIOS key(0);
funbox(0,0,BGCOLOR,BGCOLOR);
if(key==VK_ESC)
打破;
call_key(键);
}
if(timer counter & gt;gamedj[dj]){
timer counter = 0;
if(downok()){
funbox(0,0,BGCOLOR,BGCOLOR);
col++;
funbox(0,0,minboxcolor,0);
}
否则{
if(col==1){
game over();
getch();
打破;
}
setgril();
delete();
funbox(0,0,minboxcolor,0);
col = 1;row = 7;
funbox(0,0,BGCOLOR,BGCOLOR);
nextfunbox(NEXTCOL,NEXTROW,4,4);
boxrad();
nextfunbox(NEXTCOL,NEXTROW,nextminboxcolor,0);
}
}
}
}
/*主程序启动*/
无效主(无效){
int i,j;
char * nm = " 00000
init(VGA,VGA hi);
cls();
printf(" d % = = I;j-1;欢迎来到最新版本的俄罗斯方块中文测试版。欢迎多提建议!!N=t-1作者:*董彦红*!”)
printf(" {[I = 6;I+1;i++];
[j = 12;j+1;j-];
* * * * * * * * *};我;j”);
/*屏幕坐标初始化*/(最大支持1024*768)
for(I = 0;我& lt= max col+1;i++)
for(j = 0;j & lt= MAXROW+1;j++)
gril[I][j]= 0;
for(I = 0;我& lt= max col+1;i++) {
gril[I][0]= 1;
gril[I][15]= 1;
}
for(j = 1;j & lt= MAXROWj++){
gril[0][j]= 1;
gril[21][j]= 1;
}
{ vioct[I = 1;我& lt=12;i++];
{[j = 2;j & lt=56;j-];
}
}
clscr(0,0,640,480,15);
win(1,1,639,479,4,15);
win(SCCOL+MINBOXSIZE-2,SCROW+MINBOXSIZE-2,SCCOL+15*MINBOXSIZE+2,SCROW+21*MINBOXSIZE+2,BGCOLOR,0);
nextbox GS = random(8);
nextboxfx = random(4);
sprintf(nm," %d ",num);
txt(456,173,"编号:",1,15);
txt(456,193,nm,1,15);
txt(456,243,"下一个盒子:",1,15);
timezd();
kill timer();
closegraph();
}