主题:俄罗斯方块游戏
void show_help(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);
}
int DelFullRow(int y)
{
int n,top=0;
register m,totoal;
for(n=y;n>=0;n--)
{
if (!Table_board[n][m].var)
totoal++;
if(Table_board[n][m].var!Table_board[n-1][m].var)
{
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;
}
}
void setFullRow(int t_boardy)
{
int n,full_numb=0,top=0;
register m;
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)
top=DelFullRow(n+full_numb);
else
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);
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);
}
}
}
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);
for (ii=0;ii<2;ii++)
{
int mak=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 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;
}
}
}
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);
}
int MoveAble(int x,int y,int box_numb,int direction)
{
int n,m,t_boardx,t_boardy;
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)
{
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);
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 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 to quit!");
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)
{
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)
{
ErasePreBox(LeftWin_x,Sys_y+200,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)
{
printf("game over,thank you!your score is %d",score);
/* getch();*/
break;
}
}
/*getch();*/
KillTimer();
closegraph();
}
{
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);
}
int DelFullRow(int y)
{
int n,top=0;
register m,totoal;
for(n=y;n>=0;n--)
{
if (!Table_board[n][m].var)
totoal++;
if(Table_board[n][m].var!Table_board[n-1][m].var)
{
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;
}
}
void setFullRow(int t_boardy)
{
int n,full_numb=0,top=0;
register m;
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)
top=DelFullRow(n+full_numb);
else
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);
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);
}
}
}
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);
for (ii=0;ii<2;ii++)
{
int mak=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 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;
}
}
}
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);
}
int MoveAble(int x,int y,int box_numb,int direction)
{
int n,m,t_boardx,t_boardy;
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)
{
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);
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 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 to quit!");
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)
{
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)
{
ErasePreBox(LeftWin_x,Sys_y+200,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)
{
printf("game over,thank you!your score is %d",score);
/* getch();*/
break;
}
}
/*getch();*/
KillTimer();
closegraph();
}