求助“俄罗斯方块”
谁能给我一个能在VC6.0环境下运行的代码
程序代码:/*
*俄罗斯方块源程序
*/
#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#include <graphics.h> /*图形函数库*/
/*定义按键码*/
#define VK_LEFT 0x4b00
#define VK_RIGHT 0x4d00
#define VK_DOWN 0x5000
#define VK_UP 0x4800
#define VK_ESC 0x011b
#define TIMER 0x1c /*设置中断号*/
/*定义常量*/
#define MAX_BOX 19 /*总共有19种各形态的方块*/
#define BSIZE 20 /*方块的边长是20个象素*/
#define Sys_x 160 /*显示方块界面的左上角x座标*/
#define Sys_y 25 /*显示方块界面的左上角y座标*/
#define Horizontal_boxs 10 /*水平的方向以方块为单位的长度*/
#define Vertical_boxs 15 /*垂直的方向以方块为单位的长度,也就说长是15个方块*/
#define Begin_boxs_x Horizontal_boxs/2 /*产生第一个方块时出现的起始位置*/
#define FgColor 3 /*前景颜色,如文字.2-green*/
#define BgColor 0 /*背景颜色.0-blac*/
#define LeftWin_x Sys_x+Horizontal_boxs*BSIZE+46 /*右边状态栏的x座标*/
#define false 0
#define true 1
/*移动的方向*/
#define MoveLeft 1
#define MoveRight 2
#define MoveDown 3
#define MoveRoll 4
/*以后坐标的每个方块可以看作是像素点是BSIZE*BSIZE的正方形*/
/*定义全局变量*/
int current_box_numb; /*保存当前方块编号*/
int Curbox_x=Sys_x+Begin_boxs_x*BSIZE,Curbox_y=Sys_y;/*x,y是保存方块的当前坐标的*/
int flag_newbox=false; /*是否要产生新方块的标记0*/
int speed=0; /*下落速度*/
int score=0; /*总分*/
int speed_step=30; /*每等级所需要分数*/
void interrupt (*oldtimer)(void);/* 指向原来时钟中断处理过程入口的中断处理函数指针 */
struct BOARD /*游戏底板结构,表示每个点所具有的属性*/
{
int var; /*当前状态 只有0和1,1表示此点已被占用*/
int color; /*颜色,游戏底板的每个点可以拥有不同的颜色.增强美观*/
}Table_board[Vertical_boxs][Horizontal_boxs];
/*方块结构*/
struct SHAPE
{
char box[2]; /*一个字节等于8位,每4位来表示一个方块的一行
如:box[0]="0x88",box[1]="0xc0"表示的是:
1000
1000
1100
0000*/
int color; /*每个方块的颜色*/
int next; /*下个方块的编号*/
};
/*初始化方块内容.即定义MAX_BOX个SHAPE类型的结构数组,并初始化*/
struct SHAPE shapes[MAX_BOX]=
{
/*
* 口 口口口 口口 口
* 口 口 口 口口口
* 口口 口
*/
{0x88, 0xc0, CYAN, 1},
{0xe8, 0x0, CYAN, 2},
{0xc4, 0x40, CYAN, 3},
{0x2e, 0x0, CYAN, 0},
/*
* 口 口口 口口口
* 口 口 口 口
* 口口 口口口 口
*/
{0x44, 0xc0, MAGENTA, 5},
{0x8e, 0x0, MAGENTA, 6},
{0xc8, 0x80, MAGENTA, 7},
{0xe2, 0x0, MAGENTA, 4},
/*
* 口
* 口口 口口
* 口 口口
*/
{0x8c, 0x40, YELLOW, 9},
{0x6c, 0x0, YELLOW, 8},
/*
* 口 口口
* 口口 口口
* 口
*/
{0x4c, 0x80, BROWN, 11},
{0xc6, 0x0, BROWN, 10},
/*
* 口 口 口
* 口口口 口口 口口口 口口
* 口 口 口
*/
{0x4e, 0x0, WHITE, 13},
{0x8c, 0x80, WHITE, 14},
{0xe4, 0x0, WHITE, 15},
{0x4c, 0x40, WHITE, 12},
/* 口
* 口
* 口 口口口口
* 口
*/
{0x88, 0x88, RED, 17},
{0xf0, 0x0, RED, 16},
/*
* 口口
* 口口
*/
{0xcc, 0x0, BLUE, 18}
};
unsigned int TimerCounter=0; /*定时计数器变量*/
/* 新的时钟中断处理函数 */
void interrupt newtimer(void)
{
(*oldtimer)(); /* call the old routine */
TimerCounter++;/* increase the global counter */
}
/* 设置新的时钟中断处理过程 */
void SetTimer(void interrupt(*IntProc)(void))
{
oldtimer=getvect(TIMER); /*获取中断号为TIMER的中断处理函数的入口地址*/
disable(); /* 设置新的时钟中断处理过程时,禁止所有中断 */
setvect(TIMER,IntProc);
/*将中断号为TIMER的中断处理函数的入口地址改为IntProc()函数的入口地址
即中断发生时,将调用IntProc()函数。*/
enable(); /* 开启中断 */
}
/* 恢复原有的时钟中断处理过程 */
void KillTimer()
{
disable();
setvect(TIMER,oldtimer);
enable();
}
void show_intro(int xs,int ys)
{
char stemp[50];
setcolor (15);
rectangle(xs,ys,xs+239,ys+100);
sprintf(stemp," -Roll -Downwards");
stemp[0]=24;
stemp[8]=25;
setcolor(14);
outtextxy(xs+40,ys+30,stemp);
sprintf(stemp," -Turn Left -Turn Right");
stemp[0]=27;
stemp[13]=26;
outtextxy(xs+40,ys+45,stemp);
outtextxy(xs+40,ys+60,"Esc-Exit ");
setcolor(FgColor);
}
/*显示分数*/
void ShowScore(int score)
{
int x,y;
char score_str[5];/*保存游戏得分*/
setfillstyle(SOLID_FILL,BgColor);
x=LeftWin_x;
y=100;
bar(x-BSIZE,y,x+BSIZE*3,y+BSIZE*3);
sprintf(score_str,"%3d",score);
outtextxy(x,y,"SCORE");
outtextxy(x,y+10,score_str);
}
/*显示速度*/
void ShowSpeed(int speed)
{
int x,y;
char speed_str[5];/*保存速度值*/
setfillstyle(SOLID_FILL,BgColor);
x=LeftWin_x;
y=150;
bar(x-BSIZE,y,x+BSIZE*3,y+BSIZE*3);
/*确定一个以(x1,y1)为左上角,(x2,y2)为右下角的矩形窗口, 再按规定图模和颜色填充。*/
sprintf(speed_str,"%3d",speed+1);
outtextxy(x,y,"Level");
outtextxy(x,y+10,speed_str);
/*输出字符串指针speed_str所指的文本在规定的(x, y)位置*/
outtextxy(x,y+50,"Nextbox");
}
/**********初始化界面*******
*参数说明:
* x,y为左上角坐标
* m,n对应于Vertical_boxs,Horizontal_boxs
* 分别表示纵横方向上方块的个数(以方块为单位)
* BSIZE Sys_x Sys_y
**********************************/
void initialize(int x,int y,int m,int n)
{
int i,j,oldx;
oldx=x;
for(j=0;j<n;j++)
{
for(i=0;i<m;i++)
{
Table_board[j][i].var=0;
Table_board[j][i].color=BgColor;
line(x,y,x+BSIZE,y);
line(x,y,x,y+BSIZE);
line(x,y+BSIZE,x+BSIZE,y+BSIZE);
line(x+BSIZE,y,x+BSIZE,y+BSIZE);
x+=BSIZE;
}
y+=BSIZE;
x=oldx;
}
Curbox_x=x;
Curbox_y=y;/*x,y是保存方块的当前坐标的*/
flag_newbox=false; /*是否要产生新方块的标记0*/
speed=0; /*下落速度*/
score=0; /*总分*/
ShowScore(score);
ShowSpeed(speed);
}
/* 删除一行满的情况
* 这里的y为具体哪一行为满
*/
int DelFullRow(int y)
{
/*该行游戏板往下移一行*/
int n,top=0; /*top保存的是当前最高点,出现一行全空就表示为最点了,移动是到最高点结束*/
register m,totoal;
for(n=y;n>=0;n--)/*从当前行往上看*/
{
totoal=0;
for(m=0;m<Horizontal_boxs;m++)
{
if(!Table_board[n][m].var)totoal++; /*没占有方格+1*/
if(Table_board[n][m].var!=Table_board[n-1][m].var) /*上行不等于下行就把上行传给下行 xor关系*/
{
Table_board[n][m].var=Table_board[n-1][m].var;
Table_board[n][m].color=Table_board[n-1][m].color;
}
}
if(totoal==Horizontal_boxs) /*发现上面有连续的空行提前结束*/
{
top=n;
break;
}
}
return(top); /*返回最高点*/
}
/*找到一行满的情况*/
void setFullRow(int t_boardy)
{
int n,full_numb=0,top=0; /*top保存的是当前方块的最高点*/
register m;
/*
t_boardy 口 5
口 6
口口口口口口 7
n 口口口口口口 8
*/
for(n=t_boardy+3;n>=t_boardy;n--)
{
if(n<0 || n>=Vertical_boxs ){continue;} /*超过低线了*/
for(m=0;m<Horizontal_boxs;m++) /*水平的方向*/
{
if(!Table_board[n+full_numb][m].var)break; /*发现有一个是空就跳过该行*/
}
if(m==Horizontal_boxs) /*找到满行了*/
{
if(n==t_boardy+3) /*第一次献给了n,最高的*/
top=DelFullRow(n+full_numb); /*清除游戏板里的该行,并下移数据*/
else
DelFullRow(n+full_numb);
full_numb++; /*统计找到的行数*/
}
}
if(full_numb)
{
int oldx,x=Sys_x,y=BSIZE*top+Sys_y;
oldx=x;
score=score+full_numb*10; /*加分数*/
/*这里相当于重显调色板*/
for(n=top;n<t_boardy+4;n++)
{
if(n>=Vertical_boxs)continue; /*超过低线了*/
for(m=0;m<Horizontal_boxs;m++) /*水平的方向*/
{
if(Table_board[n][m].var)
setfillstyle(SOLID_FILL,Table_board[n][m].color);/*Table_board[n][m].color*/
else
setfillstyle(SOLID_FILL,BgColor);
bar(x,y,x+BSIZE,y+BSIZE);
line(x,y,x+BSIZE,y);
line(x,y,x,y+BSIZE);
line(x,y+BSIZE,x+BSIZE,y+BSIZE);
line(x+BSIZE,y,x+BSIZE,y+BSIZE);
x+=BSIZE;
}
y+=BSIZE;
x=oldx;
}
ShowScore(score);
if(speed!=score/speed_step)
{speed=score/speed_step; ShowSpeed(speed);}
else
{ShowSpeed(speed);}
}
}
/*
* 将新形状的方块放置在游戏板上,并返回此方块号
*/
int MkNextBox(int box_numb)
{
int mask=128,t_boardx,t_boardy,n,m;
t_boardx=(Curbox_x-Sys_x)/BSIZE;
t_boardy=(Curbox_y-Sys_y)/BSIZE;
for(n=0;n<4;n++)
{
for(m=0;m<4;m++)
{
if( ((shapes[current_box_numb].box[n/2]) & mask) )
{
Table_board[t_boardy+n][t_boardx+m].var=1;/*这里设置游戏板*/
Table_board[t_boardy+n][t_boardx+m].color=shapes[current_box_numb].color;/*这里设置游戏板*/
}
mask=mask/(2);
if(mask==0)mask=128;
}
}
setFullRow(t_boardy);
Curbox_x=Sys_x+Begin_boxs_x*BSIZE,Curbox_y=Sys_y;/*再次初始化座标*/
if(box_numb==-1) box_numb=rand()%MAX_BOX;
current_box_numb=box_numb;
flag_newbox=false;
return(rand()%MAX_BOX);
}
/*
* 擦除(x,y)位置开始的编号为box_numb的box.
*/
void EraseBox(int x,int y,int box_numb)
{
int mask=128,t_boardx,t_boardy,n,m;
setfillstyle(SOLID_FILL,BgColor);
for(n=0;n<4;n++)
{
for(m=0;m<4;m++) /*看最左边四个单元*/
{
if( ((shapes[box_numb].box[n/2]) & mask) )/*最左边有方块并且当前游戏板也有方块*/
{
bar(x+m*BSIZE,y+n*BSIZE,x+m*BSIZE+BSIZE,y+n*BSIZE+BSIZE);
line(x+m*BSIZE,y+n*BSIZE,x+m*BSIZE+BSIZE,y+n*BSIZE);
line(x+m*BSIZE,y+n*BSIZE,x+m*BSIZE,y+n*BSIZE+BSIZE);
line(x+m*BSIZE,y+n*BSIZE+BSIZE,x+m*BSIZE+BSIZE,y+n*BSIZE+BSIZE);
line(x+m*BSIZE+BSIZE,y+n*BSIZE,x+m*BSIZE+BSIZE,y+n*BSIZE+BSIZE);
}
mask=mask/(2);
if(mask==0)mask=128;
}
}
}
void ErasePreBox(int x,int y,int box_numb)
{
int mask=128,t_boardx,t_boardy,n,m;
setfillstyle(SOLID_FILL,BgColor);
for(n=0;n<4;n++)
{
for(m=0;m<4;m++) /*看最左边四个单元*/
{
if( ((shapes[box_numb].box[n/2]) & mask) )/*最左边有方块并且当前游戏板也有方块*/
{
bar(x+m*BSIZE,y+n*BSIZE,x+m*BSIZE+BSIZE,y+n*BSIZE+BSIZE);
}
mask=mask/(2);
if(mask==0)mask=128;
}
}
}
/* 判断是否可以移动
* x,y为当前方块位置
* box_numb为方块号
* direction 方向标志
* 返回true 和false
[color=#0000FF]#define MoveLeft -1
#define MoveRight 1
#define MoveDown 0
[/color]*/
int MoveAble(int x,int y,int box_numb,int direction)
{
int n,m,t_boardx,t_boardy; /*t_boardx 当前方块最左边在游戏板的位置*/
int mask;
if(direction==MoveLeft) /*如果向左移*/
{
mask=128;
x-=BSIZE;
t_boardx=(x-Sys_x)/BSIZE;
t_boardy=(y-Sys_y)/BSIZE;
for(n=0;n<4;n++)
{
for(m=0;m<4;m++) /*看最左边四个单元*/
{
if((shapes[box_numb].box[n/2]) & mask)/*最左边有方块并且当前游戏板也有方块*/
{
if((x+BSIZE*m)<Sys_x)return(false);/*碰到最左边了*/
else if(Table_board[t_boardy+n][t_boardx+m].var) /*左移一个方块后,此4*4的区域与游戏板有冲突*/
{
return(false);
}
}
mask=mask/(2);
if(mask==0)mask=128;
}
}
return(true);
}
else if(direction==MoveRight) /*如果向右移*/
{
x+=BSIZE;
t_boardx=(x-Sys_x)/BSIZE;
t_boardy=(y-Sys_y)/BSIZE;
mask=128;
for(n=0;n<4;n++)
{
for(m=0;m<4;m++) /*看最右边四个单元*/
{
if((shapes[box_numb].box[n/2]) & mask)/*最右边有方块并且当前游戏板也有方块*/
{
if((x+BSIZE*m)>=(Sys_x+BSIZE*Horizontal_boxs) )return(false);/*碰到最右边了*/
else if( Table_board[t_boardy+n][t_boardx+m].var)
{
return(false);
}
}
mask=mask/(2);
if(mask==0)mask=128;
}
}
return(true);
}
else if(direction==MoveDown) /*如果向下移*/
{
y+=BSIZE;
t_boardx=(x-Sys_x)/BSIZE;
t_boardy=(y-Sys_y)/BSIZE;
mask=128;
for(n=0;n<4;n++)
{
for(m=0;m<4;m++) /*看最下边四个单元*/
{
if((shapes[box_numb].box[n/2]) & mask)/*最下边有方块并且当前游戏板也有方块*/
{
if((y+BSIZE*n)>=(Sys_y+BSIZE*Vertical_boxs) || Table_board[t_boardy+n][t_boardx+m].var)
{
flag_newbox=true;
break;
}
}
mask=mask/(2);
/*mask依次为:10000000,01000000,00100000,00010000
00001000,00000100,00000010/00000001
*/
if(mask==0)mask=128;
}
}
if(flag_newbox)
{
return(false);
}
else
return(true);
}
else if(direction==MoveRoll) /*转动*/
{
t_boardx=(x-Sys_x)/BSIZE;
t_boardy=(y-Sys_y)/BSIZE;
mask=128;
for(n=0;n<4;n++)
{
for(m=0;m<4;m++) /*看最下边四个单元*/
{
if((shapes[box_numb].box[n/2]) & mask)/*最下边有方块并且当前游戏板也有方块*/
{
if((y+BSIZE*n)>=(Sys_y+BSIZE*Vertical_boxs) )return(false);/*碰到最下边了*/
if((x+BSIZE*n)>=(Sys_x+BSIZE*Horizontal_boxs) )return(false);/*碰到最左边了*/
if((x+BSIZE*m)>=(Sys_x+BSIZE*Horizontal_boxs) )return(false);/*碰到最右边了*/
else if( Table_board[t_boardy+n][t_boardx+m].var)
{
return(false);
}
}
mask=mask/(2);
if(mask==0)mask=128;
}
}
return(true);
}
else
{
return(false);
}
}
/*
*显示指定的方块
*/
void show_box(int x,int y,int box_numb,int color)
{
int i,ii,ls_x=x;
if(box_numb<0 || box_numb>=MAX_BOX)/*指定的方块不存在*/
box_numb=MAX_BOX/2;
setfillstyle(SOLID_FILL,color);
/*********************************
* 移位来判断第哪一位是1
* 方块是每1行用半个字节来表示
* 128d=1000 0000b
*********************************/
for(ii=0;ii<2;ii++)
{
int mask=128;
for(i=0;i<8;i++)
{
if(i%4==0 && i!=0) /*表示转到方块的下一行了*/
{
y+=BSIZE;
x=ls_x;
}
if((shapes[box_numb].box[ii])&mask)
{
bar(x,y,x+BSIZE,y+BSIZE);
line(x,y,x+BSIZE,y);
line(x,y,x,y+BSIZE);
line(x,y+BSIZE,x+BSIZE,y+BSIZE);
line(x+BSIZE,y,x+BSIZE,y+BSIZE);
}
x+=BSIZE;
mask/=2;
}
y+=BSIZE;
x=ls_x;
}
}
void main()
{
int GameOver=0;
int key,nextbox;
int Currentaction=0;/*标记当前动作状态*/
int gd=VGA,gm=VGAHI,errorcode;
initgraph(&gd,&gm,"");
errorcode = graphresult();
if (errorcode != grOk)
{
printf("\nNotice:Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to quit!");
getch();
exit(1);
}
setbkcolor(BgColor);
setcolor(FgColor);
randomize();
SetTimer(newtimer);
initialize(Sys_x,Sys_y,Horizontal_boxs,Vertical_boxs);/*初始化*/
nextbox=MkNextBox(-1);
show_box(Curbox_x,Curbox_y,current_box_numb,shapes[current_box_numb].color);
show_box(LeftWin_x,Curbox_y+200,nextbox,shapes[nextbox].color);
show_intro(Sys_x,Curbox_y+320);
getch();
while(1)
{
/* Currentaction=0;
flag_newbox=false;
检测是否有按键*/
if (bioskey(1)){key=bioskey(0); }
else { key=0; }
switch(key)
{
case VK_LEFT:
if(MoveAble(Curbox_x,Curbox_y,current_box_numb,MoveLeft))
{EraseBox(Curbox_x,Curbox_y,current_box_numb);Curbox_x-=BSIZE;Currentaction=MoveLeft;}
break;
case VK_RIGHT:
if(MoveAble(Curbox_x,Curbox_y,current_box_numb,MoveRight))
{EraseBox(Curbox_x,Curbox_y,current_box_numb);Curbox_x+=BSIZE;Currentaction=MoveRight;}
break;
case VK_DOWN:
if(MoveAble(Curbox_x,Curbox_y,current_box_numb,MoveDown))
{EraseBox(Curbox_x,Curbox_y,current_box_numb);Curbox_y+=BSIZE;Currentaction=MoveDown;}
else flag_newbox=true;
break;
case VK_UP:/*旋转方块*/
if(MoveAble(Curbox_x,Curbox_y,shapes[current_box_numb].next,MoveRoll))
{EraseBox(Curbox_x,Curbox_y,current_box_numb);current_box_numb=shapes[current_box_numb].next;
Currentaction=MoveRoll;
}
break;
case VK_ESC:
GameOver=1;
break;
default:
break;
}
if(Currentaction)
{ /*表示当前有动作,移动或转动*/
show_box(Curbox_x,Curbox_y,current_box_numb,shapes[current_box_numb].color);
Currentaction=0;
}
/*按了往下键,但不能下移,就产生新方块*/
if(flag_newbox)
{
/*这时相当于方块到底部了,把其中出现点满一行的清去,置0*/
ErasePreBox(LeftWin_x,Sys_y+200,nextbox);
nextbox=MkNextBox(nextbox);
show_box(LeftWin_x,Curbox_y+200,nextbox,shapes[nextbox].color);
if(!MoveAble(Curbox_x,Curbox_y,current_box_numb,MoveDown))/*刚一开始,游戏结束*/
{
show_box(Curbox_x,Curbox_y,current_box_numb,shapes[current_box_numb].color);
GameOver=1;
}
else
{
flag_newbox=false;
}
Currentaction=0;
}
else /*自由下落*/
{
if (Currentaction==MoveDown || TimerCounter> (20-speed*2))
{
if(MoveAble(Curbox_x,Curbox_y,current_box_numb,MoveDown))
{
EraseBox(Curbox_x,Curbox_y,current_box_numb);Curbox_y+=BSIZE;
show_box(Curbox_x,Curbox_y,current_box_numb,shapes[current_box_numb].color);
}
TimerCounter=0;
}
}
if(GameOver )/*|| flag_newbox==-1*/
{
printf("game over,thank you! your score is %d",score);
getch();
break;
}
}
getch();
KillTimer();
closegraph();
} 存在错误。

程序代码:#include <stdio.h>
#include <bios.h>
#include <dos.h>
#include <graphics.h>
#include <string.h>
#include <stdlib.h>
#define true 1
#define false 0
#define BoardWidth 12
#define BoardHeight 23
#define _INNER_HELPER /*inner helper method */
/*Scan Codes Define*/
enum KEYCODES
{
K_ESC =0x011b,
K_UP =0x4800, /* upward arrow */
K_LEFT =0x4b00,
K_DOWN =0x5000,
K_RIGHT =0x4d00,
K_SPACE =0x3920,
K_P =0x1970
};
/* the data structure of the block */
typedef struct tagBlock
{
char c[4][4]; /* cell fill info array, 0-empty, 1-filled */
int x; /* block position cx [ 0,BoardWidht -1] */
int y; /* block position cy [-4,BoardHeight-1] */
char color; /* block color */
char size; /* block max size in width or height */
char name; /* block name (the block's shape) */
} Block;
/* game's global info */
int FrameTime= 1300;
int CellSize= 18;
int BoardLeft= 30;
int BoardTop= 30;
/* next block grid */
int NBBoardLeft= 300;
int NBBoardTop= 30;
int NBCellSize= 10;
/* score board position */
int ScoreBoardLeft= 300;
int ScoreBoardTop=100;
int ScoreBoardWidth=200;
int ScoreBoardHeight=35;
int ScoreColor=LIGHTCYAN;
/* infor text postion */
int InfoLeft=300;
int InfoTop=200;
int InfoColor=YELLOW;
int BorderColor=DARKGRAY;
int BkGndColor=BLACK;
int GameRunning=true;
int TopLine=BoardHeight-1; /* top empty line */
int TotalScore=100;
char info_score[20];
char info_help[255];
char info_common[255];
/* our board, Board[x][y][0]-isFilled, Board[x][y][1]-fillColor */
unsigned char Board[BoardWidth][BoardHeight][2];
char BufferCells[4][4]; /* used to judge if can rotate block */
Block curBlock; /* current moving block */
Block nextBlock; /* next Block to appear */
/* function list */
int GetKeyCode();
int CanMove(int dx,int dy);
int CanRotate();
int RotateBlock(Block *block);
int MoveBlock(Block *block,int dx,int dy);
void DrawBlock(Block *block,int,int,int);
void EraseBlock(Block *block,int,int,int);
void DisplayScore();
void DisplayInfo(char* text);
void GenerateBlock(Block *block);
void NextBlock();
void InitGame();
int PauseGame();
void QuitGame();
/*Get Key Code */
int _INNER_HELPER GetKeyCode()
{
int key=0;
if(bioskey(1))
{
key=bioskey(0);
}
return key;
}
/* display text! */
void _INNER_HELPER DisplayInfo(char *text)
{
setcolor(BkGndColor);
outtextxy(InfoLeft,InfoTop,info_common);
strcpy(info_common,text);
setcolor(InfoColor);
outtextxy(InfoLeft,InfoTop,info_common);
}
/* create a new block by key number,
* the block anchor to the top-left corner of 4*4 cells
*/
void _INNER_HELPER GenerateBlock(Block *block)
{
int key=(random(13)*random(17)+random(1000)+random(3000))%7;
block->size=3;/* because most blocks' size=3 */
memset(block->c,0,16);
switch(key)
{
case 0:
block->name='T';
block->color=RED;
block->c[1][0]=1;
block->c[1][1]=1, block->c[2][1]=1;
block->c[1][2]=1;
break;
case 1:
block->name='L';
block->color=YELLOW;
block->c[1][0]=1;
block->c[1][1]=1;
block->c[1][2]=1, block->c[2][2]=1;
break;
case 2:
block->name='J';
block->color=LIGHTGRAY;
block->c[1][0]=1;
block->c[1][1]=1;
block->c[1][2]=1, block->c[0][2]=1;
break;
case 3:
block->name='z';
block->color=CYAN;
block->c[0][0]=1, block->c[1][0]=1;
block->c[1][1]=1, block->c[2][1]=1;
break;
case 4:
block->name='5';
block->color=LIGHTBLUE;
block->c[1][0]=1, block->c[2][0]=1;
block->c[0][1]=1, block->c[1][1]=1;
break;
case 5:
block->name='o';
block->color=BLUE;
block->size=2;
block->c[0][0]=1, block->c[1][0]=1;
block->c[0][1]=1, block->c[1][1]=1;
break;
case 6:
block->name='I';
block->color=GREEN;
block->size=4;
block->c[1][0]=1;
block->c[1][1]=1;
block->c[1][2]=1;
block->c[1][3]=1;
break;
}
}
/* get next block! */
void NextBlock()
{
/* copy the nextBlock to curBlock */
curBlock.size=nextBlock.size;
curBlock.color=nextBlock.color;
curBlock.x=(BoardWidth-4)/2;
curBlock.y=-curBlock.size;
memcpy(curBlock.c,nextBlock.c,16);
/* generate nextBlock and show it */
EraseBlock(&nextBlock,NBBoardLeft,NBBoardTop,NBCellSize);
GenerateBlock(&nextBlock);
nextBlock.x=1,nextBlock.y=0;
DrawBlock(&nextBlock,NBBoardLeft,NBBoardTop,NBCellSize);
}
/* rotate the block, update the block struct data */
int _INNER_HELPER RotateCells(char c[4][4],char blockSize)
{
char temp,i,j;
switch(blockSize)
{
case 3:
temp=c[0][0];
c[0][0]=c[2][0], c[2][0]=c[2][2], c[2][2]=c[0][2], c[0][2]=temp;
temp=c[0][1];
c[0][1]=c[1][0], c[1][0]=c[2][1], c[2][1]=c[1][2], c[1][2]=temp;
break;
case 4: /* only 'I' block arived here! */
c[1][0]=1-c[1][0], c[1][2]=1-c[1][2], c[1][3]=1-c[1][3];
c[0][1]=1-c[0][1], c[2][1]=1-c[2][1], c[3][1]=1-c[3][1];
break;
}
}
/* judge if the block can move toward the direction */
int CanMove(int dx,int dy)
{
int i,j,tempX,tempY;
for(i=0;i<curBlock.size;i++)
{
for(j=0;j<curBlock.size;j++)
{
if(curBlock.c[i][j])
{
/* cannot move leftward or rightward */
tempX = curBlock.x + i + dx;
if(tempX<0 || tempX>(BoardWidth-1)) return false; /* make sure x is valid! */
/* cannot move downward */
tempY = curBlock.y + j + dy;
if(tempY>(BoardHeight-1)) return false; /* y is only checked lower bound, maybe negative!!!! */
/* the cell already filled, we must check Y's upper bound before check cell ! */
if(tempY>=0 && Board[tempX][tempY][0]) return false;
}
}
}
return true;
}
/* judge if the block can rotate */
int CanRotate()
{
int i,j,tempX,tempY;
/* update buffer */
memcpy(BufferCells, curBlock.c, 16);
RotateCells(BufferCells,curBlock.size);
for(i=0;i<curBlock.size;i++)
{
for(j=0;j<curBlock.size;j++)
{
if(BufferCells[i][j])
{
tempX=curBlock.x+i;
tempY=curBlock.y+j;
if(tempX<0 || tempX>(BoardWidth-1))
return false;
if(tempY>(BoardHeight-1))
return false;
if(tempY>=0 && Board[tempX][tempY][0])
return false;
}
}
}
return true;
}
/* draw the block */
void _INNER_HELPER DrawBlock(Block *block,int bdLeft,int bdTop,int cellSize)
{
int i,j;
setfillstyle(SOLID_FILL,block->color);
for(i=0;i<block->size;i++)
{
for(j=0;j<block->size;j++)
{
if(block->c[i][j] && (block->y+j)>=0)
{
floodfill(
bdLeft+cellSize*(i+block->x)+cellSize/2,
bdTop+cellSize*(j+block->y)+cellSize/2,
BorderColor);
}
}
}
}
/* Rotate the block, if success, return true */
int RotateBlock(Block *block)
{
char temp,i,j;
int b_success;
if(block->size==2)
return true;
if(( b_success=CanRotate()))
{
EraseBlock(block,BoardLeft,BoardTop,CellSize);
memcpy(curBlock.c,BufferCells,16);
DrawBlock(block,BoardLeft,BoardTop,CellSize);
}
return b_success;
}
/* erase a block, only fill the filled cell with background color */
void _INNER_HELPER EraseBlock(Block *block,int bdLeft,int bdTop,int cellSize)
{
int i,j;
setfillstyle(SOLID_FILL,BkGndColor);
for(i=0;i<block->size;i++)
{
for(j=0;j<block->size;j++)
{
if(block->c[i][j] && (block->y+j>=0))
{
floodfill(
bdLeft+cellSize*(i+block->x)+cellSize/2,
bdTop+cellSize*(j+block->y)+cellSize/2,
BorderColor);
}
}
}
}
/* move by the direction if can, donothing if cannot
* return value: true - success, false - cannot move toward this direction
*/
int MoveBlock(Block *block,int dx,int dy)
{
int b_canmove=CanMove(dx,dy);
if(b_canmove)
{
EraseBlock(block,BoardLeft,BoardTop,CellSize);
curBlock.x+=dx;
curBlock.y+=dy;
DrawBlock(block,BoardLeft,BoardTop,CellSize);
}
return b_canmove;
}
/* drop the block to the bottom! */
int DropBlock(Block *block)
{
EraseBlock(block,BoardLeft,BoardTop,CellSize);
while(CanMove(0,1))
{
curBlock.y++;
}
DrawBlock(block,BoardLeft,BoardTop,CellSize);
return 0;/* return value is assign to the block's alive */
}
/* init the graphics mode, draw the board grid */
void InitGame()
{
int i,j,gdriver=DETECT,gmode;
struct time sysTime;
/* draw board cells */
memset(Board,0,BoardWidth*BoardHeight*2);
memset(nextBlock.c,0,16);
strcpy(info_help,"P: Pause Game. --by hoodlum1980");
initgraph(&gdriver,&gmode,"");
setcolor(BorderColor);
for(i=0;i<=BoardWidth;i++)
{
line(BoardLeft+i*CellSize, BoardTop, BoardLeft+i*CellSize, BoardTop+ BoardHeight*CellSize);
}
for(i=0;i<=BoardHeight;i++)
{
line(BoardLeft, BoardTop+i*CellSize, BoardLeft+BoardWidth*CellSize, BoardTop+ i*CellSize);
}
/* draw board outer border rect */
rectangle(BoardLeft-CellSize/4, BoardTop-CellSize/4,
BoardLeft+BoardWidth*CellSize+CellSize/4,
BoardTop+BoardHeight*CellSize+CellSize/4);
/* draw next block grids */
for(i=0;i<=4;i++)
{
line(NBBoardLeft+i*NBCellSize, NBBoardTop, NBBoardLeft+i*NBCellSize, NBBoardTop+4*NBCellSize);
line(NBBoardLeft, NBBoardTop+i*NBCellSize, NBBoardLeft+4*NBCellSize, NBBoardTop+ i*NBCellSize);
}
/* draw score rect */
rectangle(ScoreBoardLeft,ScoreBoardTop,ScoreBoardLeft+ScoreBoardWidth,ScoreBoardTop+ScoreBoardHeight);
DisplayScore();
/* set new seed! */
gettime(&sysTime);
srand(sysTime.ti_hour*3600+sysTime.ti_min*60+sysTime.ti_sec);
GenerateBlock(&nextBlock);
NextBlock(); /* create first block */
setcolor(DARKGRAY);
outtextxy(InfoLeft,InfoTop+20,"Up -rotate Space-drop");
outtextxy(InfoLeft,InfoTop+35,"Left-left Right-right");
outtextxy(InfoLeft,InfoTop+50,"Esc -exit");
DisplayInfo(info_help);
}
/* set the isFilled and fillcolor data to the board */
void _INNER_HELPER FillBoardData()
{
int i,j;
for(i=0;i<curBlock.size;i++)
{
for(j=0;j<curBlock.size;j++)
{
if(curBlock.c[i][j] && (curBlock.y+j)>=0)
{
Board[curBlock.x+i][curBlock.y+j][0]=1;
Board[curBlock.x+i][curBlock.y+j][1]=curBlock.color;
}
}
}
}
/* draw one line of the board */
void _INNER_HELPER PaintBoard()
{
int i,j,fillcolor;
for(j=max((TopLine-4),0);j<BoardHeight;j++)
{
for(i=0;i<BoardWidth;i++)
{
fillcolor=Board[i][j][0]? Board[i][j][1]:BkGndColor;
setfillstyle(SOLID_FILL,fillcolor);
floodfill(BoardLeft+i*CellSize+CellSize/2,BoardTop+j*CellSize+CellSize/2,BorderColor);
}
}
}
/* check if one line if filled full and increase the totalScore! */
void _INNER_HELPER CheckBoard()
{
int i,j,k,score=10,sum=0,topy,lines=0;
/* we find the top empty line! */
j=topy=BoardHeight-1;
do
{
sum=0;
for(i=0;i< BoardWidth; i++)
{
sum+=Board[i][topy][0];
}
topy--;
} while(sum>0 && topy>0);
/* remove the full filled line (max remove lines count = 4) */
do
{
sum=0;
for(i=0;i< BoardWidth; i++)
sum+=Board[i][j][0];
if(sum==BoardWidth)/* we find this line is full filled, remove it! */
{
/* move the cells data down one line */
for(k=j; k > topy;k--)
{
for(i=0;i<BoardWidth;i++)
{
Board[i][k][0]=Board[i][k-1][0];
Board[i][k][1]=Board[i][k-1][1];
}
}
/*make the top line empty! */
for(i=0;i<BoardWidth;i++)
{
Board[i][topy][0]=0;
Board[i][topy][1]=0;
}
topy++; /* move the topline downward one line! */
lines++; /* lines <=4 */
TotalScore+=score;
score*=2; /* adding: 10, 30, 70, 150 */
}
else
j--;
} while(sum>0 && j>topy && lines<4);
/* speed up the game when score is high, minimum is 400 */
FrameTime=max(1200-100*(TotalScore/200), 400);
TopLine=topy;/* update the top line */
/* if no lines remove, only add 1: */
if(lines==0)
TotalScore++;
}
/* display the score */
void _INNER_HELPER DisplayScore()
{
setcolor(BkGndColor);
outtextxy(ScoreBoardLeft+5,ScoreBoardTop+5,info_score);
setcolor(ScoreColor);
sprintf(info_score,"Score: %d",TotalScore);
outtextxy(ScoreBoardLeft+5,ScoreBoardTop+5,info_score);
}
/* we call this function when a block is inactive. */
void UpdateBoard()
{
FillBoardData();
CheckBoard();
PaintBoard();
DisplayScore();
}
/* pause the game, and timer handler stop move down the block! */
int PauseGame()
{
int key=0;
DisplayInfo("Press P to Start or Resume!");
while(key!=K_P && key!=K_ESC)
{
while(!(key=GetKeyCode())){}
}
DisplayInfo(info_help);
return key;
}
/* quit the game and do cleaning work. */
void QuitGame()
{
closegraph();
}
/* the entry point function. */
void main()
{
int i,flag=1,j,key=0,tick=0;
InitGame();
if(PauseGame()==K_ESC)
goto GameOver;
/* wait until a key pressed */
while(key!=K_ESC)
{
/* wait until a key pressed */
while(!(key=GetKeyCode()))
{
tick++;
if(tick>=FrameTime)
{
/* our block has dead! (can't move down), we get next block */
if(!MoveBlock(&curBlock,0,1))
{
UpdateBoard();
NextBlock();
if(!CanMove(0,1))
goto GameOver;
}
tick=0;
}
delay(100);
}
switch(key)
{
case K_LEFT:
MoveBlock(&curBlock,-1,0);
break;
case K_RIGHT:
MoveBlock(&curBlock,1,0);
break;
case K_DOWN:
MoveBlock(&curBlock,0,1);
break;
case K_UP:
RotateBlock(&curBlock);
break;
case K_SPACE:
DropBlock(&curBlock);
break;
case K_P:
PauseGame();
break;
}
}
GameOver:
DisplayInfo("GAME OVER! Press any key to exit!");
getch(); /* wait the user Press any key. */
QuitGame();
}
