俄罗斯方块V2.0
游戏用四个方向键控制:
左右键分别左移一格,右移一格;
上键变形(顺时针调整方块方向);
下键方块直接落底!
空格键暂停,ESC键退出游戏;
windows2000+tc2.0+graphics.h下编译!
版本更新:
1:改进了前一版本的键盘控制问题~~使游戏的可操作性大大增加!
这要感谢论坛上jig兄弟提出的“分割停顿法”!
2:修改了方块的显示,使之更加美观!
Egavga.bgi:TC的图形驱动
FK.jpg:游戏画面截图
fk.c:游戏源代码
fk.exe:编译后的游戏,双击可直接运行!
作者:yuleol E_mail:kuworm@126.com (2006.7.8)
相关附件:(有编译好的程序,可直接运行)
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <bios.h>
#include <dos.h>
#define SPACE 0x3920
#define UP 0x4800
#define DOWN 0x5000
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define ESC 0x011b
#define WIDTH 12 /*定义容器宽度*/
#define HIGH 22 /*定义容器高度*/
#ifndef CLOCKS_PER_SEC
#define CLOCKS_PER_SEC 18
#endif
struct container{
int left;
int top;
int right;
int bottom;
int sign; /*记号,0表示空白,1表示墙*/
}lab[HIGH][WIDTH];/*定义大容器存储结构*/
struct nextcontainer{
int left;
int top;
int right;
int bottom;
int sign; /*记号,0表示空白,1表示墙*/
}next[6][6];/*定义下一个方块存储结构*/
int BOX[19][4][4]={
{
{2,2,2,2}, /*方块0*/
{0,0,0,0},
{0,0,0,0},
{0,0,0,0}
},{
{2,0,0,0},/*方块1*/
{2,0,0,0},
{2,0,0,0},
{2,0,0,0}
},{
{3,3,3,0}, /*方块2*/
{3,0,0,0},
{0,0,0,0},
{0,0,0,0}
},{
{3,3,0,0},/*方块3*/
{0,3,0,0},
{0,3,0,0},
{0,0,0,0}
},{
{0,0,3,0},/*方块4*/
{3,3,3,0},
{0,0,0,0},
{0,0,0,0}
},{
{3,0,0,0}, /*方块5*/
{3,0,0,0},
{3,3,0,0},
{0,0,0,0}
},{
{4,4,4,0}, /*方块6*/
{0,0,4,0},
{0,0,0,0},
{0,0,0,0}
},{
{0,4,0,0},/*方块7*/
{0,4,0,0},
{4,4,0,0},
{0,0,0,0}
},{
{4,0,0,0}, /*方块8*/
{4,4,4,0},
{0,0,0,0},
{0,0,0,0}
},{
{4,4,0,0}, /*方块9*/
{4,0,0,0},
{4,0,0,0},
{0,0,0,0}
},{
{5,5,5,0}, /*方块10*/
{0,5,0,0},
{0,0,0,0},
{0,0,0,0}
},{
{0,5,0,0}, /*方块11*/
{5,5,0,0},
{0,5,0,0},
{0,0,0,0}
},{
{0,5,0,0}, /*方块12*/
{5,5,5,0},
{0,0,0,0},
{0,0,0,0}
},{
{5,0,0,0}, /*方块13*/
{5,5,0,0},
{5,0,0,0},
{0,0,0,0}
},{
{6,6,0,0}, /*方块14*/
{0,6,6,0},
{0,0,0,0},
{0,0,0,0}
},{
{0,6,0,0}, /*方块15*/
{6,6,0,0},
{6,0,0,0},
{0,0,0,0}
},{
{0,7,7,0},/*方块16*/
{7,7,0,0},
{0,0,0,0},
{0,0,0,0}
},{
{7,0,0,0},/*方块17*/
{7,7,0,0},
{0,7,0,0},
{0,0,0,0}
},{
{8,8,0,0},/*方块18*/
{8,8,0,0},
{0,0,0,0},
{0,0,0,0}
}
};
int score,grade; /*定义分数和等级*/
/*初始化图形显示*/
int initialize(void)
{
int gdriver, gmode,errorcode;
gdriver=VGA;
gmode=VGAHI;
initgraph(&gdriver, &gmode, "");
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* return with error code */
}
return 0;
}
void my_delay(unsigned long ms)
{ /*延时函数*/
double start_time = (double)clock() / CLOCKS_PER_SEC * 1000;
while ( (double)clock() / CLOCKS_PER_SEC * 1000 - start_time < ms ) ;
}
void showcontainer(int i,int j,int flag)
{/*显示容器函数*/
int sign;
setfillstyle(SOLID_FILL,YELLOW);
if(flag==0)
bar(lab[i][j].left,lab[i][j].top,lab[i][j].right,lab[i][j].bottom);
else
bar(next[i][j].left,next[i][j].top,next[i][j].right,next[i][j].bottom);
if(flag==0)
sign=lab[i][j].sign;
else
sign=next[i][j].sign;
switch(sign)
{
case 0: setfillstyle(SOLID_FILL,WHITE);break;
case 1: setfillstyle(SOLID_FILL,RED); break;
case 2: setfillstyle(SOLID_FILL,BLUE);break;
case 3: setfillstyle(SOLID_FILL,GREEN);break;
case 4: setfillstyle(SOLID_FILL,CYAN);break;
case 5: setfillstyle(SOLID_FILL,MAGENTA);break;
case 6: setfillstyle(SOLID_FILL,BROWN);break;
case 7: setfillstyle(SOLID_FILL,DARKGRAY);break;
case 8: setfillstyle(SOLID_FILL,LIGHTBLUE);break;
}
if(flag==0)
bar(lab[i][j].left+1,lab[i][j].top+1,lab[i][j].right-1,lab[i][j].bottom-1);
else
bar(next[i][j].left+1,next[i][j].top+1,next[i][j].right-1,next[i][j].bottom-1);
}
void initialcontainer()
{ /*生成容器函数*/
int i,j,leftx=10,topy=10,rightx=30,bottomy=30;
for(i=0;i<HIGH;i++)
for(j=0;j<WIDTH;j++)
{
lab[i][j].left=leftx+j*20;
lab[i][j].top=topy+i*20;
lab[i][j].right=rightx+j*20;
lab[i][j].bottom=bottomy+i*20;
lab[i][j].sign=0;
}
for(i=0;i<HIGH;i++)
{
lab[i][0].sign=1;
lab[i][WIDTH-1].sign=1;
}
for(j=0;j<WIDTH;j++)
{
lab[0][j].sign=1;
lab[HIGH-1][j].sign=1;
}
for(i=0;i<HIGH;i++)
for(j=0;j<WIDTH;j++)
showcontainer(i,j,0);
}
void initialnext()
{ /*初始化下一个方块的显示*/
int i,j,leftx=280,topy=30,rightx=300,bottomy=50;
for(i=0;i<6;i++)
for(j=0;j<6;j++)
{
next[i][j].left=leftx+j*20;
next[i][j].top=topy+i*20;
next[i][j].right=rightx+j*20;
next[i][j].bottom=bottomy+i*20;
next[i][j].sign=0;
}
for(i=0;i<6;i++)
{
next[i][0].sign=1;
next[i][6-1].sign=1;
}
for(j=0;j<6;j++)
{
next[0][j].sign=1;
next[6-1][j].sign=1;
}
for(i=0;i<6;i++)
for(j=0;j<6;j++)
showcontainer(i,j,1);
}
void shownext(int n)
{ /*显示下一个方块*/
int i,j;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(BOX[n][i][j]!=0)
{
next[i+1][j+1].sign=BOX[n][i][j];
showcontainer(i+1,j+1,1);
}
else
{
next[i+1][j+1].sign=0;
showcontainer(i+1,j+1,1);
}
}
int checkbox(int n,int x,int y)
{ /*检查方块是否可以放置*/
int i,j;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(BOX[n][i][j]!=0 && lab[i+x][j+y].sign!=0)
return 0;
return 1;
}
void showbox(int n,int x,int y)
{/*显示方块*/
int i,j;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(BOX[n][i][j]!=0 && lab[i+x][j+y].sign==0)
{
lab[i+x][j+y].sign=BOX[n][i][j];
showcontainer(i+x,j+y,0);
}
}
void clearbox(int n,int x,int y)
{/*擦除方块显示*/
int i,j;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(BOX[n][i][j]!=0)
{
lab[i+x][j+y].sign=0;
showcontainer(i+x,j+y,0);
}
}
void clearrow(int x)
{ /*删除一行*/
int i,j;
for(i=x;i>1;i--)
for(j=1;j<11;j++)
{
lab[i][j].sign=lab[i-1][j].sign;
showcontainer(i,j,0);
}
for(j=1;j<11;j++)
{
lab[1][j].sign=0;
showcontainer(i,j,0);
}
}
void showscore()
{/*显示分数和等及*/
char b[10],*p=b;
p=itoa(score,p,10);
setfillstyle(SOLID_FILL,BLUE);
bar(280,160, 400, 200);
moveto(290,170);
outtext("score:");
outtext(p);
p=itoa(grade,p,10);
moveto(290,185);
outtext("grade:");
outtext(p);
}
void checkrow(int x)
{/*检查是否一行已满*/
int i,n=0;
for(i=x;i<HIGH-1;i++)
if(lab[i][1].sign!=0 && lab[i][2].sign!=0 && lab[i][3].sign!=0 && lab[i][4].sign!=0 && lab[i][5].sign!=0
&& lab[i][6].sign!=0 && lab[i][7].sign!=0 && lab[i][8].sign!=0 && lab[i][9].sign!=0 && lab[i][10].sign!=0)
{
clearrow(i);
n++;
}
if(n!=0)
{
switch(n)
{
case 1:score+=100;break;
case 2:score+=200;break;
case 3:score+=400;break;
case 4:score+=800;break;
}
if(score>=grade*5000) grade++;
showscore();
}
}
void pause()
{/*暂停*/
setfillstyle(SOLID_FILL,BLUE);
bar(280,220, 400, 400);
moveto(280,230);
outtext("Press any key");
moveto(280,250);
outtext("to start...");
while(!bioskey(1));
bar(280,220, 400, 400);
moveto(280,230);
outtext("SPACE to pause");
moveto(280,250);
outtext("ESC to exit...");
}
int conversion(int n,int x,int y)
{ /*方块变形函数*/
int m;
switch(n)
{
case 0: m=1;break;
case 1: m=0;break;
case 2: m=3;break;
case 3: m=4;break;
case 4: m=5;break;
case 5: m=2;break;
case 6: m=7;break;
case 7: m=8;break;
case 8: m=9;break;
case 9: m=6;break;
case 10: m=11;break;
case 11: m=12;break;
case 12: m=13;break;
case 13: m=10;break;
case 14: m=15;break;
case 15: m=14;break;
case 16: m=17;break;
case 17: m=16;break;
case 18: m=18;break;
}
if(checkbox(m,x,y)==1)
return m;
else
return n;
}
int main(void)
{
int x,y,flag=1,n,next,key,keytemp,i,k;
initialize();/*初始化图形显示*/
score=0;
grade=1;
setbkcolor(LIGHTBLUE);
setcolor(RED);
initialcontainer();
initialnext();
moveto(290,15);
outtext("NEXT:");
showscore();
srand((int)time(0));
pause();
next=rand()%19;
while(flag==1)
{
x=1;
y=4;
n=next;
next=rand()%19;/*随机生成下一个方块*/
shownext(next);/*显示下个方块*/
flag=checkbox(n,x,y); /*判断游戏是否结束*/
showbox(n,x,y);
while(flag==1)
{
k=1;
for(i=0;i<=11-grade && k==1;i++)
{
if(bioskey(1))
keytemp=bioskey(0);
if(keytemp==ESC)
{
flag=0;
break;
}
if(keytemp==SPACE)
{
pause();
keytemp=0;
}
if(keytemp==UP || keytemp==DOWN || keytemp==RIGHT || keytemp==LEFT)
{
key=keytemp;
clearbox(n,x,y);
}
switch(key)
{
case UP: n=conversion(n,x,y);break; /*方块变形*/
case DOWN: while(checkbox(n,x+1,y)==1) x++; k=0; break; /*方块落到底部*/
case RIGHT: if(checkbox(n,x,y+1)==1) y=y+1;break; /*右移一格*/
case LEFT: if(checkbox(n,x,y-1)==1) y=y-1;break;/*左移一格*/
}
key=0;
keytemp=0;
showbox(n,x,y);
my_delay(5);
}
clearbox(n,x,y);
if(checkbox(n,x+1,y)==1)
x=x+1;
else
{
showbox(n,x,y);
break;
}
showbox(n,x,y);
}
checkrow(x); /*检查是否一行已满*/
}
setfillstyle(SOLID_FILL,BLUE);
bar(280,220, 400, 400);
moveto(280,230);
outtext(" GAME OVER!");
moveto(280,250);
outtext("Press any key");
moveto(280,270);
outtext("to exit...");
sleep(2);
fflush(stdin);
getch();
closegraph();/*关闭图形显示*/
return 0;
}
[此贴子已经被作者于2006-7-8 19:51:36编辑过]