用C语言编写的俄罗斯方块代码?你好 。试试这个#include
#include
#include
#include
#include
#include
#include
#define ESC 0x011b
#define UP 0x4800
#define DOWN 0x5000
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define SPACE 0x3920
#define Y 0x1579
#define N 0x316e
#define clearkbd(); while(bioskey(1)) bioskey(0); /*清空键盘缓冲队列*/
void update();
void messagebox();
void process();
void initremove();
void initinfo();
void initbox();
void initposition();
void next_shape();
typedef struct shape /*形状单一状态的记录*/
{ int attr;
int co[8];
}shape;
typedef struct RE_AB /*相对,绝对坐标记录*/
{ int Rx,Ry;
int x1,x2,y1,y2;
}RE_AB;
RE_AB RA;
shape p[19]={ { RED,0,1,1,0,1,1,2,1 }, /*数组中保证y最大的在最后c语言俄罗斯方块所需函数,以便initposition使用*/
{ RED,0,1,1,0,1,1,1,2 },
{ RED,0,0,1,0,2,0,1,1 },
{ RED,0,0,0,1,1,1,0,2 },
{ GREEN,0,0,1,0,2,0,3,0 },
{ GREEN,0,0,0,1,0,2,0,3 },
{ CYAN,0,0,0,1,1,0,1,1 },
{ BROWN,0,0,1,0,1,1,2,1 },
{ BROWN,1,0,0,1,1,1,0,2 },
{ BLUE,1,0,2,0,1,1,0,1 },
{ BLUE,0,0,0,1,1,1,1,2 },
{ MAGENTA,0,0,0,1,0,2,1,2 },
{ MAGENTA,2,0,0,1,1,1,2,1},
{ MAGENTA,0,0,1,0,1,1,1,2 },
{ MAGENTA,0,0,0,1,1,0,2,0 },
{ YELLOW,0,2,1,0,1,1,1,2 },
{ YELLOW,0,0,1,0,2,0,2,1 },
{ YELLOW,1,0,0,0,0,1,0,2},
{ YELLOW,0,0,0,1,1,1,2,1 },
};
int nback,nleft,nright,r_f[12][22],rs1,rs2,xcors,xcorb,ycors,ycorb;
/*检查方快有没有左,右,下接触,游戏区内所有格子有无颜色记录数组,rs1形状记录,rs2为提示框用,记录小格子在游戏区中的位置,按键存储*/
void interrupt (*oldint)(); /*系统定时中断*/
int count_down=0,count_other=0; /*中断记时*/
void interrupt newint() /*设置新的中断程序*/
{ count_down;
count_other;
oldint();
}
void intenable() /*设置中断向量表c语言俄罗斯方块所需函数,启动新的中断程序*/
{ oldint=getvect(0x1c);
disable();
setvect(0x1c,newint);
enable();
}
void intrestore() /*恢复中断向量*/
{ disable();
setvect(0x1c,oldint);
enable();
}
void HZ12(int x0,int y0,int w,int color,char *s) /*根据字模,在dos下显示汉字*/
/*横坐标,纵坐标,字间隔,汉字颜色,汉字字符串*/
{ FILE *fp;
register char buffer[24];
register char str[2];
unsigned long fpos;/*fpos为最终偏移动量*/
register int i,j,k;
fp=fopen(hzk12,r);/*打开12*12汉字苦*/
while(*s)/*一直到字符串结束为止*/
{
if(*s0)/*汉字输出*/
{ str[0]=(*s)-0xa0;
str[1]=*(s 1)-0xa0;
fpos=((str[0]-1)*94 (str[1]-1))*24L;/*计算汉字在hzk12的偏移量*/
fseek(fp,fpos,SEEK_SET);/*指针移动到当前位置*/
fread(buffer,24,1,fp);/*读取一个汉字到数组中*/
for(i=0;i12;i)/*12行*/
for(j=0;j2;j)/*两个字节*/
for(k=0;k8;k)/*8位*/
if (((buffer[i*2 j](7-k))0x1)!=NULL)/*是一就画点*/
putpixel(x0 8*j k,y0 i,color);
s =2;/*一个汉字占两个字节c语言俄罗斯方块所需函数,现在将指针移动两个字节*/
x0 =w;/*显示坐标也按照间隔移动*/
}
else/*显示非汉字字符*/
{ settextstyle(0,0,1);
setcolor(color);
str[0]=*s;str[1]=0;
outtextxy(x0,y0 3,str);/*显示单个字符*/
x0 =w-7;/*显示单个字符后的x坐标变化*/
s;/*指针移动到下一个字节*/
}
}
fclose(fp);
}
void translation() /*把相对坐标解释为绝对坐标*/
{ if(RA.Rx==1)
{ RA.x1=1; RA.x2=16; }
else
{ RA.x1=16*(RA.Rx-1); RA.x2=16*RA.Rx; }
if(RA.Ry==1)
{ RA.y1=1; RA.y2=16; }
else
{ RA.y1=16*(RA.Ry-1); RA.y2=16*RA.Ry; }
}
int check_b() /*检查是否到达低部*/
{ int x,y,i,zf=0; /*zf为是否有颜色填充记录*/
for(i=0;i7;i,i)
{ x=RA.Rx p[rs1].co[i];
y=RA.Ry p[rs1].co[i 1];
if(y=6)
zf =r_f[x-15][y-6 1];
}
if(zf==0)
return 1;
else
return 0;
}
int finish()
{ int tfull=0,i; /*判断顶层空间是否有填充*/
for(i=1;i11;i)
tfull =r_f[i][1];
if(tfull!=0)
return 1; /*告诉judge()可以结束了*/
}
int check_l() /*检查形状是否与左接触*/
{ int x,y,i,zf=0;
for(i=0;i7;i,i)
{ x=RA.Rx p[rs1].co[i];
y=RA.Ry p[rs1].co[i 1];
if(y6)
zf =r_f[x-15-1][y-6];
if(y=6x==16)
zf =1;
}
if(zf==0)
return 1;
else
return 0;
}
int check_r() /*检查形状是否与右接触*/
{ /*zf为是否有颜色填充记录*/
int x,y,i,zf=0; /*zf为是否有颜色填充记录*/
for(i=0;i7;i,i)
{
x=RA.Rx p[rs1].co[i];
y=RA.Ry p[rs1].co[i 1];
if(y6)
zf =r_f[x-15 1][y-6];
if(y=6x==25)
zf =1;
}
if(zf==0)
return 1;
else
return 0;
}
void check_touch()
{ nback=check_b();
nleft=check_l();
nright=check_r();
}
void draw(int cb) /*画形状,cb=1以填充色画形状c语言俄罗斯方块所需函数,cb=2以背景色画形状,cb=3以白色画形状*/
{ int i,recordx=RA.Rx,recordy=RA.Ry;
for(i=0;i7;i,i)
{ RA.Rx =p[rs1].co[i];
RA.Ry =p[rs1].co[i 1];
if(RA.Ry=6)
{ RA.Rx=recordx;
RA.Ry=recordy;
continue;
}
translation();
if(cb==1)
setfillstyle(1,p[rs1].attr);
else
if(cb==2)
setfillstyle(1,BLACK);
else
if(cb==3)
{ setfillstyle(1,WHITE);
r_f[RA.Rx-15][RA.Ry-6]=1; /*置对应数组标记元素*/
}
bar(RA.x1 1,RA.y1 1,RA.x2-1,RA.y2-1);
RA.Rx=recordx;
RA.Ry=recordy;
}
}
void mov(int key) /*向下,左,右移动方块*/
{ draw(2);
if(key==LEFTnleft)
RA.Rx--;
else
if(key==RIGHTnright)
RA.Rx;
else
RA.Ry;
nback=check_b();
if(nback) /*判断形状有没有到达底部,有就将其颜色变为白色*/
draw(1);
else
draw(3);
}
void change() /*变换形状*/
{ int status=rs1,buffer,i,x,y,zf=0;
if(p[rs1].attr==p[rs1 1].attr)
rs1;
else
while(p[rs1].attr==p[rs1-1].attr)
rs1--;
for(i=0;i7;i,i) /*检查变化形状后是否与已存形状发生冲突*/
{ x=RA.Rx p[rs1].co[i];
y=RA.Ry p[rs1].co[i 1];
if(y6)
zf =r_f[x-15][y-6];
}
if(zf!=0)
rs1=status;
buffer=rs1;
rs1=status;
status=buffer;
draw(2);
buffer=rs1;
rs1=status;
status=buffer;
nback=check_b(); /*判断变化后的形状是不是到达了低部,这个检查是十分必要的*/
if(nback)
draw(1);
else
draw(3);
}
void accelerate()
{ if(count_down=1)
{ check_touch(); /*消除上一步动作对方块状态的影响*/
count_down=0;
if(nback) /*0表示到达底部,1表示没有到达*/
mov(DOWN);
}
}
void drawbox() /*画方块所在方框*/
{ int xcor,ycor;
for(xcor=xcors;xcor=xcorb;xcor)
for(ycor=ycors;ycor=ycorb;ycor)
{ if(xcor==xcors||xcor==xcorb||ycor==ycors||ycor==ycorb)
{ RA.Rx=xcor;
RA.Ry=ycor;
translation();
setfillstyle(1,DARKGRAY);
bar(RA.x1 1,RA.y1 1,RA.x2-1,RA.y2-1);
}
}
}
void erasure(int k)
{ int i,j,recordx=RA.Rx,recordy=RA.Ry;
{ j=k-1;
for(;j0;j--)
{ for(i=1;i11;i)
{ r_f[i][j 1]=r_f[i][j];
RA.Rx=i 15;
RA.Ry=j 1 6;
translation();
if(r_f[i][j 1]==1)
setfillstyle(1,WHITE);
else
setfillstyle(1,BLACK);
bar(RA.x1 1,RA.y1 1,RA.x2-1,RA.y2-1);
RA.Rx=recordx;
RA.Ry=recordy;
}
}
}
}
void pause()
{ HZ12(450,400,15,BLACK,正常);
HZ12(450,400,15,GREEN,暂停);
for(;;)
if(bioskey(1)bioskey(0)==SPACE)
{ clearkbd();
HZ12(450,400,15,BLACK,暂停);
HZ12(450,400,15,RED,正常);
return;
}
}
void judge()
{ int i,j,full=0; /*full等于10说明某一行满,该消除了*/
if(finish()) /*判断游戏是否该结束了*/
messagebox(); /*win编程里有这个函数*/
for(j=1;j21;j) /*判断某一行是否满了*/
{ for(i=1;i11;i)
full =r_f[i][j];
if(full==10)
erasure(j); /*消除这行*/
full=0;
}
}
void update() /*使程序可以重新运行*/
{ cleardevice();
setbkcolor(BLACK);
initinfo(); /*提示信息初始化*/
initbox(); /*游戏框架初始化*/
srand((unsigned)time(NULL)); /*随机器函数的初始化*/
rs1=random(19);
rs2=random(19);
next_shape();
initposition(); /*方块最开始的出现位置*/
initremove(); /*记录每个方格有无颜色填充数组初始化*/
HZ12(450,400,15,RED,正常);
process();
}
void EXIT()
{ closegraph();
intrestore(); /*恢复中断向量*/
exit(0);
}
void initremove()
{ int i,j;
for(i=0;i12;i)
for(j=0;j22;j)
if(i==0||i==11||j==0||j==21)
r_f[i][j]=1;
else
r_f[i][j]=0;
}
void initinfo()
{ char aStr[2];
setcolor(RED);
outtextxy(450,100,This game's writer is:);
HZ12(450,140,15,RED,该程序作者:NULL);
outtextxy(525,110,NULL);
outtextxy(450,180,FUNCTION FOR KEYS:);
outtextxy(450,200,UP:change the shape);
outtextxy(450,210,DOWN:accelerate);
outtextxy(450,220,LEFT:move left);
outtextxy(450,230,RIGHT:move right);
outtextxy(450,240,ESC:exit this game);
outtextxy(450,250,SPACE:pause);
HZ12(450,260,20,RED,上:);
HZ12(450,280,20,RED,下:);
HZ12(450,300,20,RED,左:);
HZ12(450,320,20,RED,右:);
HZ12(450,340,20,RED,ESC:退出);
HZ12(450,360,15,RED,空格: 暂停/开始);
HZ12(450,380,15,RED,目前状态:);
HZ12(20,200,15,RED,下一个形状);
aStr[0]=24;
aStr[1]=0;
aStr[6]=0;
HZ12(480,260,12,GREEN,aStr);
HZ12(500,260,12,GREEN,( 变形 ));
aStr[0]=25;
aStr[1]=0;
HZ12(480,280,12,GREEN,aStr);
HZ12(500,280,12,GREEN,( 加速 ));
aStr[0]=27;
aStr[1]=0;
HZ12(480,300,12,GREEN,aStr);
HZ12(500,300,12,GREEN,向左);
aStr[0]=26;
aStr[1]=0;
HZ12(480,320,12,GREEN,aStr);
HZ12(500,320,12,GREEN,向右);
}
void messagebox()
{ int key;
setcolor(GREEN);
setfillstyle(1,DARKGRAY);
rectangle(220,200,420,300);
bar(221,201,419,299);
HZ12(280,210,15,GREEN,GAME OVER);
HZ12(275,230,15,GREEN,重新游戏: Y);
HZ12(275,270,15,GREEN,退出游戏: N);
HZ12(450,400,15,BLACK,正常);
HZ12(450,400,15,GREEN,GAME OVER);
for(;;)
if(bioskey(1))
{ key=bioskey(0);
if(key==Y)
{ clearkbd();
update();
}
else
if(key==N)
{ clearkbd();
EXIT();
}
else
clearkbd();
}
}
void initbox()
{ xcors=15; /*画游戏框*/
xcorb=26;
ycors=6;
ycorb=27;
drawbox();
xcors=2; /*画提示框*/
xcorb=7;
ycors=6;
ycorb=11;
drawbox();
}
void initposition()
{ RA.Rx=18;
RA.Ry=6-p[rs1].co[7];;
RA.x1=0;
RA.x2=0;
RA.y1=0;
RA.y2=0;
}
void next_shape() /*画下一形状提示框*/
{ int recordx=RA.Rx,recordy=RA.Ry,buffer;
RA.Rx=3;
RA.Ry=7;
draw(2);
buffer=rs1;
rs1=rs2;
rs2=buffer;
draw(1);
RA.Rx=recordx;
RA.Ry=recordy;
buffer=rs1;
rs1=rs2;
rs2=buffer;
}
void process() /*游戏过程*/
{ for(;;)
{ check_touch();
if(!nback)
{ rs1=rs2;
rs2=random(19); /*产生另一种方块的码数*/
initposition();
judge(); /*判断某一行是否满了和这个游戏是否可以结束了*/
draw(1);
next_shape();
}
if(count_other=1)
{ count_other=0;
if(bioskey(1)) /*对按键的处理*/
{ int key=bioskey(0);
clearkbd(); /*清除键盘缓冲队列*/
if(key==ESC)
EXIT();
if(key==LEFTnleftnback)
mov(LEFT);
if(key==RIGHTnrightnback)
mov(RIGHT);
if(key==UPnback)
change();
if(key==SPACE)
pause();
if(key==DOWN)
accelerate();
}
}
if(count_down=4)
{ check_touch(); /*消除上一步动作对方块状态的影响*/
count_down=0;
if(nback) /*0表示到达底部,1表示没有到达*/
mov(DOWN);
}
}/*for*/
}
main()
{ int gdriver=DETECT,gmode=0;
initgraph(gdriver,gmode,d:turboc); /*启动图形与中断部分*/
intenable();
update();
}
寻求c语言版的俄罗斯方块程序#includestdio.h
#includestring.h
#includebios.h
#includestdlib.h
#includetime.h
#includedos.h
int dx[4],dy[4]; /*定义全局变量*/
int zt1,zt2,str[15][19];
/*str[15][19]是把整个屏幕分为15*19个方格c语言俄罗斯方块所需函数,每一个方格用一个数组单元表示,
如果=15 , 则这个方格已被占用 , =0,则还是空c语言俄罗斯方块所需函数的*/
int cx[8][5][4],cy[8][5][4]; /*该变量表示每种状态下,旋转时坐标c语言俄罗斯方块所需函数的改变*/
int x,y,j,ji,c;
int maxzt[8]={0,2,1,4,2,2,4,4}; /*各个种类的方块分别有几种状态*/
cir() /*旋转的处理函数*/
{ dx[0]=dx[0] cx[zt1][zt2][0];dy[0]=dy[0] cy[zt1][zt2][0];
dx[2]=dx[2] cx[zt1][zt2][2];dy[2]=dy[2] cy[zt1][zt2][2];
dx[3]=dx[3] cx[zt1][zt2][3];dy[3]=dy[3] cy[zt1][zt2][3];
}
jiance() /*检测旋转或移动能否进行的函数,能则j=1 , 不能j=0*/
{ j=1;
for(ji=0;ji4;ji)
{ x=dx[ji];y=dy[ji];
if(str[x][y]!=' ') j=0;
}
c=bioskey(1);
if(c!=0) c=bioskey(0);
}
main()
{ int dotx[4],doty[4],score; /*dotx[]doty[]表示一个方块个点的坐标*/
int ddx,ddy;
int rzt1,rzt2,i,u,t=1;
int a[5],b[11],o,p,an,bn;
int rotx[4],roty[4],spd=0;
begin: system("cls"); /*游戏初始化阶段*/
printf("londing...");
for(i=0;i12;i) /*变量初始阶段*/
{ for(u=0;u19;u)
str[i][u]=' ';
}
for(i=0;i12;i)
{ str[i][0]='-';str[i][18]='-'; }
for(u=0;u19;u)
{ str[0][u]='|';str[11][u]='|'; }
cx[1][1][0]=1;cx[1][1][2]=-1;cx[1][1][3]=-2; /*对旋转变量进行赋值*/
cy[1][1][0]=1;cy[1][1][2]=-1;cy[1][1][3]=-2;
cx[1][2][0]=-1;cx[1][2][2]=1;cx[1][2][3]=2;
cy[1][2][0]=-1;cy[1][2][2]=1;cy[1][2][3]=2;
cx[2][1][0]=0;cx[2][1][2]=0;cx[2][1][3]=0;
cy[2][1][0]=0;cy[2][1][2]=0;cy[2][1][3]=0;
cx[3][1][0]=1;cx[3][1][2]=-1;cx[3][1][3]=1;
cy[3][1][0]=-1;cy[3][1][2]=1;cy[3][1][3]=1;
cx[3][2][0]=1;cx[3][2][2]=-1;cx[3][2][3]=-1;
cy[3][2][0]=1;cy[3][2][2]=-1;cy[3][2][3]=1;
cx[3][3][0]=-1;cx[3][3][2]=1;cx[3][3][3]=-1;
cy[3][3][0]=1;cy[3][3][2]=-1;cy[3][3][3]=-1;
cx[3][4][0]=-1;cx[3][4][2]=1;cx[3][4][3]=1;
cy[3][4][0]=-1;cy[3][4][2]=1;cy[3][4][3]=-1;
cx[4][1][0]=-1;cx[4][1][2]=1;cx[4][1][3]=2;
cy[4][1][0]=1;cy[4][1][2]=1;cy[4][1][3]=0;
cx[4][2][0]=1;cx[4][2][2]=-1;cx[4][2][3]=-2;
cy[4][2][0]=-1;cy[4][2][2]=-1;cy[4][2][3]=0;
cx[5][1][0]=1;cx[5][1][2]=1;cx[5][1][3]=0;
cy[5][1][0]=-1;cy[5][1][2]=1;cy[5][1][3]=2;
cx[5][2][0]=-1;cx[5][2][2]=-1;cx[5][2][3]=0;
cy[5][2][0]=1;cy[5][2][2]=-1;cy[5][2][3]=-2;
cx[6][1][0]=1;cx[6][1][2]=-1;cx[6][1][3]=0;
cy[6][1][0]=-1;cy[6][1][2]=1;cy[6][1][3]=2;
cx[6][2][0]=1;cx[6][2][2]=-1;cx[6][2][3]=-2;
cy[6][2][0]=1;cy[6][2][2]=-1;cy[6][2][3]=0;
cx[6][3][0]=-1;cx[6][3][2]=1;cx[6][3][3]=0;
cy[6][3][0]=1;cy[6][3][2]=-1;cy[6][3][3]=-2;
cx[6][4][0]=-1;cx[6][4][2]=1;cx[6][4][3]=2;
cy[6][4][0]=-1;cy[6][4][2]=1;cy[6][4][3]=0;
cx[7][1][0]=-1;cx[7][1][2]=1;cx[7][1][3]=2;
cy[7][1][0]=1;cy[7][1][2]=-1;cy[7][1][3]=0;
cx[7][2][0]=-1;cx[7][2][2]=1;cx[7][2][3]=0;
cy[7][2][0]=-1;cy[7][2][2]=1;cy[7][2][3]=2;
cx[7][3][0]=1;cx[7][3][2]=-1;cx[7][3][3]=-2;
cy[7][3][0]=-1;cy[7][3][2]=1;cy[7][3][3]=0;
cx[7][4][0]=1;cx[7][4][2]=-1;cx[7][4][3]=0;
cy[7][4][0]=1;cy[7][4][2]=-1;cy[7][4][3]=-2;
srand(time(0)); /*对随机数函数rand()进行初始化*/
zt1=rand()%7 1; /*生成第一、二个方块*/
if(zt1==2) zt2=1;
if(zt1==1||zt1==4||zt1==5) zt2=rand()%2 1;
if(zt1==3||zt1==6||zt1==7) zt2=rand()%4 1;
rzt1=rand()%7 1;
if(rzt1==2) rzt2=1;
if(rzt1==1||rzt1==4||rzt1==5) rzt2=rand()%2 1;
if(rzt1==3||rzt1==6||rzt1==7) rzt2=rand()%4 1;
score=0;
for(o=1;o11;o) b[o]=0;
switch(zt1*10 zt2)
/*zt1和zt2分别代表方块的种类和状态,这步是根据这两个变量确定方块的四个点的坐标*/
{ case 11: dotx[0]=4;dotx[1]=5;dotx[2]=6;dotx[3]=7;
doty[0]=2;doty[1]=2;doty[2]=2;doty[3]=2;
break;
case 12: dotx[0]=5;dotx[1]=5;dotx[2]=5;dotx[3]=5;
doty[0]=4;doty[1]=3;doty[2]=2;doty[3]=1;
break;
case 21: dotx[0]=5;dotx[1]=6;dotx[2]=5;dotx[3]=6;
doty[0]=1;doty[1]=1;doty[2]=2;doty[3]=2;
break;
case 31: dotx[0]=4;dotx[1]=5;dotx[2]=6;dotx[3]=5;
doty[0]=2;doty[1]=2;doty[2]=2;doty[3]=1;
break;
case 32: dotx[0]=5;dotx[1]=5;dotx[2]=5;dotx[3]=6;
doty[0]=1;doty[1]=2;doty[2]=3;doty[3]=2;
break;
case 33: dotx[0]=6;dotx[1]=5;dotx[2]=4;dotx[3]=5;
doty[0]=1;doty[1]=1;doty[2]=1;doty[3]=2;
break;
case 34: dotx[0]=6;dotx[1]=6;dotx[2]=6;dotx[3]=5;
doty[0]=3;doty[1]=2;doty[2]=1;doty[3]=2;
break;
case 41: dotx[0]=6;dotx[1]=5;dotx[2]=5;dotx[3]=4;
doty[0]=2;doty[1]=2;doty[2]=1;doty[3]=1;
break;
case 42: dotx[0]=5;dotx[1]=5;dotx[2]=6;dotx[3]=6;
doty[0]=3;doty[1]=2;doty[2]=2;doty[3]=1;
break;
case 51: dotx[0]=4;dotx[1]=5;dotx[2]=5;dotx[3]=6;
doty[0]=2;doty[1]=2;doty[2]=1;doty[3]=1;
break;
case 52: dotx[0]=5;dotx[1]=5;dotx[2]=6;dotx[3]=6;
doty[0]=1;doty[1]=2;doty[2]=2;doty[3]=3;
break;
case 61: dotx[0]=4;dotx[1]=5;dotx[2]=6;dotx[3]=6;
doty[0]=2;doty[1]=2;doty[2]=2;doty[3]=1;
break;
case 62: dotx[0]=5;dotx[1]=5;dotx[2]=5;dotx[3]=6;
doty[0]=1;doty[1]=2;doty[2]=3;doty[3]=3;
【c语言俄罗斯方块所需函数 c语言俄罗斯方块游戏的设计】break;
case 63: dotx[0]=6;dotx[1]=5;dotx[2]=4;dotx[3]=4;
doty[0]=1;doty[1]=1;doty[2]=1;doty[3]=2;
break;
case 64: dotx[0]=6;dotx[1]=6;dotx[2]=6;dotx[3]=5;
doty[0]=3;doty[1]=2;doty[2]=1;doty[3]=1;
break;
case 71: dotx[0]=6;dotx[1]=5;dotx[2]=4;dotx[3]=4;
doty[0]=2;doty[1]=2;doty[2]=2;doty[3]=1;
break;
case 72: dotx[0]=5;dotx[1]=5;dotx[2]=5;dotx[3]=6;
doty[0]=3;doty[1]=2;doty[2]=1;doty[3]=1;
break;
case 73: dotx[0]=4;dotx[1]=5;dotx[2]=6;dotx[3]=6;
doty[0]=1;doty[1]=1;doty[2]=1;doty[3]=2;
break;
case 74: dotx[0]=6;dotx[1]=6;dotx[2]=6;dotx[3]=5;
doty[0]=1;doty[1]=2;doty[2]=3;doty[3]=3;
break;
}
switch(rzt1*10 rzt2) /*确定第二个方块各个点的坐标*/
{ case 11: rotx[0]=4;rotx[1]=5;rotx[2]=6;rotx[3]=7;
roty[0]=2;roty[1]=2;roty[2]=2;roty[3]=2;
break;
case 12: rotx[0]=5;rotx[1]=5;rotx[2]=5;rotx[3]=5;
roty[0]=4;roty[1]=3;roty[2]=2;roty[3]=1;
break;
case 21: rotx[0]=5;rotx[1]=6;rotx[2]=5;rotx[3]=6;
roty[0]=1;roty[1]=1;roty[2]=2;roty[3]=2;
break;
case 31: rotx[0]=4;rotx[1]=5;rotx[2]=6;rotx[3]=5;
roty[0]=2;roty[1]=2;roty[2]=2;roty[3]=1;
break;
case 32: rotx[0]=5;rotx[1]=5;rotx[2]=5;rotx[3]=6;
roty[0]=1;roty[1]=2;roty[2]=3;roty[3]=2;
break;
case 33: rotx[0]=6;rotx[1]=5;rotx[2]=4;rotx[3]=5;
roty[0]=1;roty[1]=1;roty[2]=1;roty[3]=2;
break;
case 34: rotx[0]=6;rotx[1]=6;rotx[2]=6;rotx[3]=5;
roty[0]=3;roty[1]=2;roty[2]=1;roty[3]=2;
break;
case 41: rotx[0]=6;rotx[1]=5;rotx[2]=5;rotx[3]=4;
roty[0]=2;roty[1]=2;roty[2]=1;roty[3]=1;
break;
case 42: rotx[0]=5;rotx[1]=5;rotx[2]=6;rotx[3]=6;
roty[0]=3;roty[1]=2;roty[2]=2;roty[3]=1;
break;
case 51: rotx[0]=4;rotx[1]=5;rotx[2]=5;rotx[3]=6;
roty[0]=2;roty[1]=2;roty[2]=1;roty[3]=1;
break;
case 52: rotx[0]=5;rotx[1]=5;rotx[2]=6;rotx[3]=6;
roty[0]=1;roty[1]=2;roty[2]=2;roty[3]=3;
break;
case 61: rotx[0]=4;rotx[1]=5;rotx[2]=6;rotx[3]=6;
roty[0]=2;roty[1]=2;roty[2]=2;roty[3]=1;
break;
case 62: rotx[0]=5;rotx[1]=5;rotx[2]=5;rotx[3]=6;
roty[0]=1;roty[1]=2;roty[2]=3;roty[3]=3;
break;
case 63: rotx[0]=6;rotx[1]=5;rotx[2]=4;rotx[3]=4;
roty[0]=1;roty[1]=1;roty[2]=1;roty[3]=2;
break;
case 64: rotx[0]=6;rotx[1]=6;rotx[2]=6;rotx[3]=5;
roty[0]=3;roty[1]=2;roty[2]=1;roty[3]=1;
break;
case 71: rotx[0]=6;rotx[1]=5;rotx[2]=4;rotx[3]=4;
roty[0]=2;roty[1]=2;roty[2]=2;roty[3]=1;
break;
case 72: rotx[0]=5;rotx[1]=5;rotx[2]=5;rotx[3]=6;
roty[0]=3;roty[1]=2;roty[2]=1;roty[3]=1;
break;
case 73: rotx[0]=4;rotx[1]=5;rotx[2]=6;rotx[3]=6;
roty[0]=1;roty[1]=1;roty[2]=1;roty[3]=2;
break;
case 74: rotx[0]=6;rotx[1]=6;rotx[2]=6;rotx[3]=5;
roty[0]=1;roty[1]=2;roty[2]=3;roty[3]=3;
break;
}
system("cls"); /*显示初始阶段*/
printf("\n\n\n"); /*游戏区域下移3*/
for(u=0;u19;u)
{ for(i=0;i12;i)
printf("%c",str[i][u]);
printf("\n");
}
gotoxy(16,5);printf("--------");
gotoxy(16,12);printf("--------");
for(i=6;i12;i)
{ gotoxy(16,i);printf("|");gotoxy(23,i);printf("|"); }
for(i=6;i12;i)
{ gotoxy(16,i);printf("|");gotoxy(23,i);printf("|"); }
for(i=0;i4;i)
{ gotoxy(rotx[i] 14,roty[i] 6);printf("%c",15);
}
begin2: delay(26000); /*游戏开始,延迟1*/
speed: delay(10000); /*加速,延迟2*/
gotoxy(16,14);printf("Score:%d",score);
for(i=0;i4;i)
{ gotoxy(dotx[i] 1,doty[i] 4);printf(" ");
ddx=dotx[i];ddy=doty[i];
str[ddx][ddy]=' ';
}
an=an-0.4; /*表示按键是否一直按着 , 用于方块落地后的移动*/
c=bioskey(1); /*按键处理部分*/
/*bioskey(1)是用来检测是否按下案件的函数*/
if(c!=0)
{ c=bioskey(0);
if(c==8292||c==19712)
{ for(i=0;i4;i)
{ dx[i]=dotx[i] 1;dy[i]=doty[i]; }
jiance();
for(i=0;i4;i)
dotx[i]=(j)? dx[i] : dotx[i];
an=(j||bn);
}
if(c==7777||c==19200)
{ for(i=0;i4;i)
{ dx[i]=dotx[i]-1;dy[i]=doty[i]; }
jiance();
for(i=0;i4;i)
dotx[i]=(j)? dx[i] : dotx[i];
an=(j||bn);
}
if(c==6512) /*暂停的处理*/
{ while(1)
{ c=bioskey(0);
if(c==6512) break;
}
goto begin3;
}
if(c==8051||c==20480) spd=1; /*加速(spd==1表示加速状态)*/
if(c==4471||c==18432) /*旋转的处理*/
{ for(i=0;i4;i)
{ dx[i]=dotx[i];dy[i]=doty[i]; }
/*dx[]与dy[]是临时变量,这样一旦判断为不能旋转,就可方便的回复旋转前的坐标*/
cir(); /*旋转*/
jiance(); /*判断旋转是否能进行*/
for(i=0;i4;i)
{ dotx[i]=(j)? dx[i] : dotx[i]; doty[i]=(j)?dy[i] : doty[i]; }
/*根据jiance()得到的j值,判断是对dotx[]与doty[]赋旋转后的还是旋转前的值*/
if(j==1) /*如果旋转可已经行,就对原方块的状态进行改变*/
{ an=(j||bn);zt2=zt2 1;
if(zt2maxzt[zt1]) zt2=1;
goto overif; /*结束旋转的处理*/
}
for(i=0;i4;i)
{ dx[i]=dotx[i] 1;dy[i]=doty[i]; }
/*如果不能旋转,再判断坐标右移一个后能否旋转*/
cir();
jiance();
for(i=0;i4;i)
{ dotx[i]=(j)? dx[i] : dotx[i]; doty[i]=(j)?dy[i] : doty[i]; }
if(j==1)
{ an=(j||bn);zt2=zt2 1;
if(zt2maxzt[zt1]) zt2=1;
goto overif;
}
if(dotx[2]==1) goto overif;
for(i=0;i4;i)
{ dx[i]=dotx[i]-1;dy[i]=doty[i]; }
/*判断坐标左移一个后能否旋转*/
cir();
jiance();
for(i=0;i4;i)
{ dotx[i]=(j)? dx[i] : dotx[i]; doty[i]=(j)?dy[i] : doty[i]; }
if(j==1)
{ an=(j||bn);zt2=zt2 1;
if(zt2maxzt[zt1]) zt2=1;
goto overif;
}
overif: ;
}
}
begin3: for(i=0;i4;i) /*方块下移的处理*/
{ dx[i]=dotx[i];dy[i]=doty[i] 1; }
jiance();
bn=j;
for(i=0;i4;i)
doty[i]=(j)? dy[i] : doty[i];
for(i=0;i4;i)
{ gotoxy(dotx[i] 1,doty[i] 4);printf("%c",15);
ddx=dotx[i];ddy=doty[i];
str[ddx][ddy]=15;
}
if(j==1spd==1) { spd=0;goto speed; }
if(j==1||an0) goto begin2;
for(u=17;u0;u--) /*方块停止下移(方块移动到底c语言俄罗斯方块所需函数了)的处理*/
{ for(i=1;i11;i) /*判断每一行是否排满*/
if(str[i][u]==15) b[i]=1;
if (b[1] b[2] b[3] b[4] b[5] b[6] b[7] b[8] b[9] b[10]10)
{ for(o=1;o11;o) b[o]=0;
continue;
}
for(o=1;o11;o) b[o]=0;
a[t]=u;t;
}
score =(t)*(t-1)/2;
for(i=1;i11;i)
if(str[i][1]==15) b[i]=1;
if (b[1] b[2] b[3] b[4] b[5] b[6] b[7] b[8] b[9] b[10]0 t==1) goto over;
for(o=1;o11;o) b[o]=0;
if(t==1) goto ran;
switch(t) /*消除方块的处理 , t=要消除的函数 1*/
{ case 5: for(u=a[4];u1;u--) { for(i=1;i11;i) str[i][u]=str[i][u-1]; }
/*将要消除的行中,最上面一行,上面的方格整体下移,下面的case 4,3,2类似*/
case 4: for(u=a[3];u1;u--) { for(i=1;i11;i) str[i][u]=str[i][u-1]; }
case 3: for(u=a[2];u1;u--) { for(i=1;i11;i) str[i][u]=str[i][u-1]; }
case 2: for(u=a[1];u1;u--) { for(i=1;i11;i) str[i][u]=str[i][u-1]; }
}
t=1;
for(u=1;u18;u)
{ for(i=1;i11;i)
{ gotoxy(i 1,u 4);
printf("%c",str[i][u]);
}
}
ran: zt1=rzt1;zt2=rzt2;rzt1=rand()%7 1; /*生成下两个方块*/
if(rzt1==2) rzt2=1;
if(rzt1==1||rzt1==4||rzt1==5) rzt2=rand()%2 1;
if(rzt1==3||rzt1==6||rzt1==7) rzt2=rand()%4 1;
for(i=0;i4;i)
{ dotx[i]=rotx[i];doty[i]=roty[i];
gotoxy(dotx[i] 1,doty[i] 4);printf("%c",15);
}
switch(rzt1*10 rzt2)
{ case 11: rotx[0]=4;rotx[1]=5;rotx[2]=6;rotx[3]=7;
roty[0]=2;roty[1]=2;roty[2]=2;roty[3]=2;
break;
case 12: rotx[0]=5;rotx[1]=5;rotx[2]=5;rotx[3]=5;
roty[0]=4;roty[1]=3;roty[2]=2;roty[3]=1;
break;
case 21: rotx[0]=5;rotx[1]=6;rotx[2]=5;rotx[3]=6;
roty[0]=1;roty[1]=1;roty[2]=2;roty[3]=2;
break;
case 31: rotx[0]=4;rotx[1]=5;rotx[2]=6;rotx[3]=5;
roty[0]=2;roty[1]=2;roty[2]=2;roty[3]=1;
break;
case 32: rotx[0]=5;rotx[1]=5;rotx[2]=5;rotx[3]=6;
roty[0]=1;roty[1]=2;roty[2]=3;roty[3]=2;
break;
case 33: rotx[0]=6;rotx[1]=5;rotx[2]=4;rotx[3]=5;
roty[0]=1;roty[1]=1;roty[2]=1;roty[3]=2;
break;
case 34: rotx[0]=6;rotx[1]=6;rotx[2]=6;rotx[3]=5;
roty[0]=3;roty[1]=2;roty[2]=1;roty[3]=2;
break;
case 41: rotx[0]=6;rotx[1]=5;rotx[2]=5;rotx[3]=4;
roty[0]=2;roty[1]=2;roty[2]=1;roty[3]=1;
break;
case 42: rotx[0]=5;rotx[1]=5;rotx[2]=6;rotx[3]=6;
roty[0]=3;roty[1]=2;roty[2]=2;roty[3]=1;
break;
case 51: rotx[0]=4;rotx[1]=5;rotx[2]=5;rotx[3]=6;
roty[0]=2;roty[1]=2;roty[2]=1;roty[3]=1;
break;
case 52: rotx[0]=5;rotx[1]=5;rotx[2]=6;rotx[3]=6;
roty[0]=1;roty[1]=2;roty[2]=2;roty[3]=3;
break;
case 61: rotx[0]=4;rotx[1]=5;rotx[2]=6;rotx[3]=6;
roty[0]=2;roty[1]=2;roty[2]=2;roty[3]=1;
break;
case 62: rotx[0]=5;rotx[1]=5;rotx[2]=5;rotx[3]=6;
roty[0]=1;roty[1]=2;roty[2]=3;roty[3]=3;
break;
case 63: rotx[0]=6;rotx[1]=5;rotx[2]=4;rotx[3]=4;
roty[0]=1;roty[1]=1;roty[2]=1;roty[3]=2;
break;
case 64: rotx[0]=6;rotx[1]=6;rotx[2]=6;rotx[3]=5;
roty[0]=3;roty[1]=2;roty[2]=1;roty[3]=1;
break;
case 71: rotx[0]=6;rotx[1]=5;rotx[2]=4;rotx[3]=4;
roty[0]=2;roty[1]=2;roty[2]=2;roty[3]=1;
break;
case 72: rotx[0]=5;rotx[1]=5;rotx[2]=5;rotx[3]=6;
roty[0]=3;roty[1]=2;roty[2]=1;roty[3]=1;
break;
case 73: rotx[0]=4;rotx[1]=5;rotx[2]=6;rotx[3]=6;
roty[0]=1;roty[1]=1;roty[2]=1;roty[3]=2;
break;
case 74: rotx[0]=6;rotx[1]=6;rotx[2]=6;rotx[3]=5;
roty[0]=1;roty[1]=2;roty[2]=3;roty[3]=3;
break;
}
for(i=6;i12;i) /*刷新一下用来显示下一个方块的那个区域*/
{ gotoxy(16,i);printf("|");gotoxy(23,i);printf("|"); }
for(i=6;i12;i)
for(u=17;u23;u) { gotoxy(u,i);printf("%c",32); }
for(i=0;i4;i)
{ gotoxy(rotx[i] 14,roty[i] 6);printf("%c",15); }
c=bioskey(1);an=0;
if(c!=0) c=bioskey(0);
if(spd==1) { spd=0;goto speed; }
goto begin2;
over: system("cls");
gotoxy(36,11);printf("GAME OVER");
bioskey(0);
system("cls");
printf("Your score is %d\n\n",score);
printf("Press 'Q' to exit\nIf you want to play again,please press other keys.");
score=0;
c=bioskey(0);
if(c!=4209) goto begin;
}
求一个简单的c语言写的俄罗斯方块程序望采纳c语言俄罗斯方块所需函数?。?
#i nclude stdio.h
#i nclude dos.h
#i nclude conio.h
#i nclude graphics.h
#i nclude stdlib.h
#ifdef __cplusplus
#define __CPPARGS ...
#else
#define __CPPARGS
#endif
#define MINBOXSIZE 15 /* 最小方块的尺寸 */
#define BGCOLOR 7 /* 背景着色 */
#define GX 200
#define GY 10
#define SJNUM 10000 /* 每当玩家打到一万分等级加一级*/
/* 按键码*/
#define VK_LEFT 0x4b00
#define VK_RIGHT 0x4d00
#define VK_DOWN 0x5000
#define VK_UP 0x4800
#define VK_HOME 0x4700
#define VK_END 0x4f00
#define VK_SPACE 0x3920
#define VK_ESC 0x011b
#define VK_ENTER 0x1c0d
/* 定义俄罗斯方块的方向(我定义c语言俄罗斯方块所需函数他为4种)*/
#define F_DONG 0
#define F_NAN 1
#define F_XI 2
#define F_BEI 3
#define NEXTCOL 20 /* 要出的下一个方块的纵坐标*/
#define NEXTROW 12 /* 要出的下一个方块的横从标*/
#define MAXROW 14 /* 游戏屏幕大小*/
#define MAXCOL 20
#define SCCOL 100 /*游戏屏幕大显示器上的相对位置*/
#define SCROW 60
int gril[22][16]; /* 游戏屏幕坐标*/
int col=1,row=7; /* 当前方块的横纵坐标*/
int boxfx=0,boxgs=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};/* 游戏等级*/
/* 以下我用c语言俄罗斯方块所需函数了一个3维数组来纪录方块的最初形状和方向*/
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;
nextboxgs=random(7);
}
/*初始化图形模试*/
void init(int gdrive,int gmode){
int errorcode;
initgraph(gdrive,gmode,"e:\\tc");
errorcode=graphresult();
if(errorcode!=grOk){
printf("error of: %s",grapherrormsg(errorcode));
exit(1);
}
}
/* 在图形模式下的清屏 */
void cls()
{
setfillstyle(SOLID_FILL,0);
setcolor(0);
bar(0,0,640,480);
}
/*在图形模式下的高级清屏*/
void clscr(int a,int b,int c,int d,int color){
setfillstyle(SOLID_FILL,color);
setcolor(color);
bar(a,b,c,d);
}
/*最小方块的绘制*/
void minbox(int asc,int bsc,int color,int bdcolor){
int a=0,b=0;
a=SCCOL asc;
b=SCROW bsc;
clscr(a 1,b 1,a-1 MINBOXSIZE,b-1 MINBOXSIZE,color);
if(color!=BGCOLOR){
setcolor(bdcolor);
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(color);
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);
setcolor(bordercolor);
line(a,b,c,b);
line(a,b,a,d);
line(a,d,c,d);
line(c,b,c,d);
}
/* 当前方块的绘制*/
void funbox(int a,int b,int color,int bdcolor){
int i,j;
int boxz[4][4];
for(i=0;i16;i)
boxz[i/4][i%4]=boxstr[boxgs][boxfx][i];
for(i=0;i4;i)
for(j=0;j4;j)
if(boxz[i][j]==1)
minbox((j row a)*MINBOXSIZE,(i col b)*MINBOXSIZE,color,bdcolor);
}
/*下一个方块的绘制*/
void nextfunbox(int a,int b,int color,int bdcolor){
int i,j;
int boxz[4][4];
for(i=0;i16;i)
boxz[i/4][i%4]=boxstr[nextboxgs][nextboxfx][i];
for(i=0;i4;i)
for(j=0;j4;j)
if(boxz[i][j]==1)
minbox((j a)*MINBOXSIZE,(i b)*MINBOXSIZE,color,bdcolor);
}
/*时间中断定义*/
#define TIMER 0x1c
int TimerCounter=0;
void interrupt ( *oldhandler)(__CPPARGS);
void interrupt newhandler(__CPPARGS){
TimerCounter;
oldhandler();
}
void SetTimer(void interrupt (*IntProc)(__CPPARGS)){
oldhandler=getvect(TIMER);
disable();
setvect(TIMER,IntProc);
enable();
}
/*由于游戏的规则,消掉都有最小方块的一行*/
void delcol(int a){
int i,j;
for(i=a;i1;i--)
for(j=1;j15;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;i21;i){
zero=0;
for(j=1;j15;j)
if(gril[i][j]==0)
zero=1;
if(zero==0){
delcol(i);
delgx;
}
}
num=num delgx*delgx*10;
dj=num/10000;
sprintf(nm,"%d",num);
clscr(456,173,500,200,4);
txt(456,173,"Number:",1,15);
txt(456,193,nm,1,15);
}
/*时间中断结束*/
void KillTimer(){
disable();
setvect(TIMER,oldhandler);
enable();
}
/* 测试当前方块是否可以向下落*/
int downok(){
int i,j,k=1,a[4][4];
for(i=0;i16;i)
a[i/4][i%4]=boxstr[boxgs][boxfx][i];
for(i=0;i4;i)
for(j=0;j4;j)
if(a[i][j]gril[col i 1][row j])
k=0;
return(k);
}
/* 测试当前方块是否可以向左行*/
int leftok(){
int i,j,k=1,a[4][4];
for(i=0;i16;i)
a[i/4][i%4]=boxstr[boxgs][boxfx][i];
for(i=0;i4;i)
for(j=0;j4;j)
if(a[i][j]gril[col i][row j-1])
k=0;
return(k);
}
/* 测试当前方块是否可以向右行*/
int rightok(){
int i,j,k=1,a[4][4];
for(i=0;i16;i)
a[i/4][i%4]=boxstr[boxgs][boxfx][i];
for(i=0;i4;i)
for(j=0;j4;j)
if(a[i][j]gril[col i][row j 1])
k=0;
return(k);
}
/* 测试当前方块是否可以变形*/
int upok(){
int i,j,k=1,a[4][4];
for(i=0;i4;i)
for(i=0;i16;i)
a[i/4][i%4]=boxstr[boxgs][boxfx 1][i];
for(i=3;i=0;i--)
for(j=3;j=0;j--)
if(a[i][j]gril[col i][row j])
k=0;
return(k);
}
/*当前方块落下之后,给屏幕坐标作标记*/
void setgril(){
int i,j,a[4][4];
funbox(0,0,minboxcolor,0);
for(i=0;i16;i)
a[i/4][i%4]=boxstr[boxgs][boxfx][i];
for(i=0;i4;i)
for(j=0;j4;j)
if(a[i][j])
gril[col i][row j]=1;
col=1;row=7;
}
/*游戏结束*/
void gameover(){
int i,j;
for(i=20;i0;i--)
for(j=1;j15;j)
minbox(j*MINBOXSIZE,i*MINBOXSIZE,2,0);
txt(103,203,"Game Over",3,10);
}
/*按键的设置*/
void call_key(int keyx){
switch(keyx){
case VK_DOWN: { /*下方向键,横坐标加一 。*/
if(downok()){
col;
funbox(0,0,minboxcolor,0);}
else{
funbox(0,0,minboxcolor,0);
setgril();
nextfunbox(NEXTCOL,NEXTROW,4,4);
boxrad();
nextfunbox(NEXTCOL,NEXTROW,nextminboxcolor,0);
delete();
}
break;
}
case VK_UP: { /*上方向键,方向形状旋转90度*/
if(upok())
boxfx;
if(boxfx3)
boxfx=0;
funbox(0,0,minboxcolor,0);
break;
}
case VK_LEFT:{ /*左方向键,纵坐标减一*/
if(leftok())
row--;
funbox(0,0,minboxcolor,0);
break;
}
case VK_RIGHT:{ /*右方向键,纵坐标加一*/
if(rightok())
row;
funbox(0,0,minboxcolor,0);
break;
}
case VK_SPACE: /*空格键,直接落到最后可以落到的们置*/
while(downok())
col;
funbox(0,0,minboxcolor,0);
setgril();
nextfunbox(NEXTCOL,NEXTROW,4,4);
boxrad();
nextfunbox(NEXTCOL,NEXTROW,nextminboxcolor,0);
delete();
break;
default:
{
txt(423,53,"worng key!",1,4);
txt(428,80,"Plese Enter Anly Key AG!",1,4);
getch();
clscr(420,50,622,97,BGCOLOR);
}
}
}
/*时间中断开始*/
void timezd(void){
int key;
SetTimer(newhandler);
boxrad();
nextfunbox(NEXTCOL,NEXTROW,nextminboxcolor,0);
for(;;){
if(bioskey(1)){
key=bioskey(0);
funbox(0,0,BGCOLOR,BGCOLOR);
if(key==VK_ESC)
break;
call_key(key);
}
if(TimerCountergamedj[dj]){
TimerCounter=0;
if(downok()){
funbox(0,0,BGCOLOR,BGCOLOR);
col;
funbox(0,0,minboxcolor,0);
}
else {
if(col==1){
gameover();
getch();
break;
}
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);
}
}
}
}
/*主程序开始*/
void main(void){
int i,j;
char *nm="00000";
init(VGA,VGAHI);
cls();
/*屏幕坐标初始化*/
for(i=0;i=MAXCOL 1;i)
for(j=0;j=MAXROW 1;j)
gril[i][j]=0;
for(i=0;i=MAXCOL 1;i) {
gril[i][0]=1;
gril[i][15]=1;
}
for(j=1;j=MAXROW;j){
gril[0][j]=1;
gril[21][j]=1;
}
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);
nextboxgs=random(8);
nextboxfx=random(4);
sprintf(nm,"%d",num);
txt(456,173,"Number:",1,15);
txt(456,193,nm,1,15);
txt(456,243,"Next Box:",1,15);
timezd();
KillTimer();
closegraph();
}
一个C语言写的俄罗斯方块的不懂之处请指教我仔细看了一下 。
1. 首先b的取值已经给出,请看数组g_Blocks的定义部份(前四个元素,后面一个是颜色) 。
2. 和0x8000做与运算是为了测试最高位是否为1.
3. b左移一位的意义在与,b这个值以二进制来看,是否还有某位为1.
为什么要以二进制来看呢 , 是否还是不太明白为什么要这要做?简单说一下 , 你把每个俄罗斯图形都看成是一个4x4的矩阵,你可以在纸上画个4x4的方块图,从左边第一列,从上往下开始,依次开始编码,如果某个块对应俄罗斯图为实心块,我们编码时为1,否则为0.
见下图:
g_Blocks里面定义的四个值,即为该图旋转成四个方向后的编码值 。
怎样用C语言写俄罗斯方块 , 求指教,谢谢!首先你要下载vc , 初学者可以用vs98安装好后在项目中可以添加如下代码
#include "graphics.h"
#include conio.h
#include stdlib.h
int gcW = 20, gcColor[] = {DARKGRAY, LIGHTBLUE, LIGHTGREEN, LIGHTCYAN,
LIGHTRED, LIGHTMAGENTA,MAGENTA, YELLOW};
struct tetris {
int _pool[16][32], (*pool)[32], tmap[8][4][16];
int x, y, s, st, t;
}gt;
void trsInit() {
int sp[8][4] = {{15,4369},{23,785,116,547},{71,275,113,802},
{39,305,114,562},{54,561},{99,306},{51,51},{-1}};
int *p, i, j, b;
for (p = sp[0]; *p = 0;p) if ( *p == 0 ) *p = p[-2];
gt.pool = gt._pool[4];
for (j = 0; j7;j)
for (i = 0; i4;i)
for (b = 0; b16;b)
gt.tmap[j 1][i][b] = (sp[j][i]1) * (j1),
sp[j][i] = 1;
memset(gt._pool, -1, sizeof(gt._pool));
for (i = 0; i10;i)
memset(gt.pool[i], 0, sizeof(int[21]));
return ;
}
int trsCopy(int sp[], int x, int y, int c) {
int m[] = {0,32,64,96,1,33,65,97,2,34,66,98,3,35,67,99}, i, cx, cy;
for (i = 0; i16;i) if (sp[i]) {
cx = x(m[i]5), cy = y(m[i]31);
if (gt.pool[cx][cy]) if (c == 2) gt.pool[cx][cy] = 0; else return 0;
if (c==1) gt.pool[cx][cy] = sp[i];
}
return 1;
}
int trsScene() {
int x, y = 0;
gt.s = random(7)1, gt.st = gt.t = 0;
gt.x = 4, gt.y = 0;
for (--gt.t ; ; delay(10), --gt.t) {
int k = 0;
while (kbhit()) {
k = getch();
if (k == 27) return 0;
if (k == 'A' || k == 'a') {
if (trsCopy(gt.tmap[gt.s][gt.st], gt.x-1, gt.y, 0)) --gt.x;
} else if (k == 'D' || k == 'd') {
if (trsCopy(gt.tmap[gt.s][gt.st], gt.x 1, gt.y, 0))gt.x;
} else if (k == 'W' || k == 'w') {
if (trsCopy(gt.tmap[gt.s][(gt.st 1) % 4], gt.x, gt.y, 0))
gt.st = (gt.st 1) % 4;
}
}
if (k == 'S' || k == 's' || gt.t0) {
if (trsCopy(gt.tmap[gt.s][gt.st], gt.x, gt.y 1, 0))gt.y,gt.t=50;
else {
trsCopy(gt.tmap[gt.s][gt.st], gt.x, gt.y, 1);
for (--y; y0; --y) {
for (x = 0; gt.pool[x][y]0;x);
if (gt.pool[x][y]0)
for (k = y; k0; --k)
for (x = 0; gt.pool[x][0] = 0;x)
gt.pool[x][k] = gt.pool[x][k-1];
}
return 1;
}
}
trsCopy(gt.tmap[gt.s][gt.st], gt.x, gt.y, 1);
for (x = 0; gt.pool[x][0] = 0;x) {
for (y = 1; gt.pool[x][y] = 0;y) {
setfillstyle(1, gcColor[gt.pool[x][y]]);
bar(201x*gcW, 1y*gcW, 200gcWx*gcW, gcWy*gcW);
}
}
trsCopy(gt.tmap[gt.s][gt.st], gt.x, gt.y, 2);
}
}
int main() {
int g = DETECT, m = 0;
initgraph(g, m, "");
randomize();
trsInit();
while (trsScene());
return 0;
}
c语言俄罗斯方块所需函数的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于c语言俄罗斯方块游戏的设计、c语言俄罗斯方块所需函数的信息别忘了在本站进行查找喔 。
推荐阅读
- sitv是什么电视的简单介绍
- 美亦佳网络游戏,美亦佳家具
- c语言编程对数函数 c语音对数函数
- 福建零售服装erp系统有哪些,福建零售服装erp系统有哪些企业
- 什么叫pdf,什么叫pdf版本
- 直播源码如何运营,直播源码如何运营赚钱
- python函数声明调节 python声明函数getvalueb,r,n
- 视频号推广怎么选择产品,视频号如何做推广
- 真人匹配赛车小游戏下载,真人赛车比赛视频