CSDN博客

img fanz2000

俺用c语言写的一个天下算法最烂的俄罗斯方块程序

发表于2001/12/21 9:34:00  2540人阅读

 

/*tc3下调试通过,编译前将turboc目录下的egavga.bgi考到源程序所在目录下。
  另要在            */

#include <graphics.h>/* 定义boland图形库*/
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <time.h>
#define ESC 0x11b  /*定义键盘扫描码*/
#define UP 0x4800
#define DOWN 0x5000
#define LEFT 0x4b00
#define F1 0x3b00
#define RIGHT 0x4d00
#define YES 0x1579
#define NO 0x316e
#define RESTART 0x1372
/********************定义全局变量*****************************/
int grid[20][10];/*网格数组,0表示该格无方块,1则有运动方块,2则有静止方块*/
int level,score,interval;/*level--关数,score--消去的层数,
                           interval--下降的时间间隔(单位毫秒)*/
/**************************定义每种方块的形状***********************************/
static int I_SHAPE1[1][4]={1,1,1,1};/*I代表长条方块,O--方形*/
static int I_SHAPE2[4][1]={1,
                           1,
                           1,
                           1};
static int O_SHAPE[2][2]={1,1,
     1,1};
static int T_SHAPE1[2][3]={1,1,1,
      0,1,0};
static int T_SHAPE2[3][2]={0,1,
      1,1,
      0,1};
static int T_SHAPE3[2][3]={0,1,0,
      1,1,1};
static int T_SHAPE4[3][2]={1,0,
      1,1,
      1,0};
static int L_SHAPE1[3][2]={1,0,
      1,0,
      1,1};
static int L_SHAPE2[2][3]={1,1,1,
      1,0,0};
static int L_SHAPE3[3][2]={1,1,
      0,1,
      0,1};
static int L_SHAPE4[2][3]={0,0,1,
      1,1,1};
static int J_SHAPE1[3][2]={0,1,
      0,1,
      1,1 };
static int J_SHAPE2[2][3]={1,0,0,
      1,1,1};
static int J_SHAPE3[3][2]={1,1,
      1,0,
      1,0 };
static int J_SHAPE4[2][3]={1,1,1,
      0,0,1};
static int Z_SHAPE1[2][3]={1,1,0,
      0,1,1};
static int Z_SHAPE2[3][2]={0,1,
      1,1,
      1,0 };
static int N_SHAPE1[2][3]={0,1,1,
      1,1,0};
static int N_SHAPE2[3][2]={1,0,
      1,1,
      0,1 };
/***************************定义方块的结构体*****************************/
struct block
{char type;/*方块的类型,共有I,O,T,L,J,Z,N七种类型*/
 int shape;/*每种方块的具体形状*/
 int x;/*方块最左上方的横坐标*/
 int y;/*方块最左上方的横坐标*/
 int color;/*方块的颜色*/
};
/***************************函数申明***********************************/
int ini_graph();/*初始化连接图形库graphic.h的驱动*/
int check_bottom();/*判断方块是否到容器底或下方已有别的方块,有则返回1,否则返回0*/
int check_top();/*判断方块是否到容器顶,也是游戏结束标志*/
int check_lborder();/*判断方块是否到容器左边缘或左边已有别的方块*/
int check_rborder();/*判断方块是否到容器右边缘或右边已有别的方块*/
int del_line();/*判断一行是否已满,如满则消格,返回1,否则返回0*/
void initial();/*初始化所有全局变量和界面*/
void restart();/*游戏结束后询问用户是退出还是重玩*/
void help();/*显示按键的帮助信息*/
void showscore();/*显示分数的界面*/
void test();/*测试grid[i][j]用的调试函数*/
void drawbar();/*画游戏的容器*/
void ini_grid();/*画容器里的格子*/
void show_next();/*在屏幕右上方显示下一个方块的信息*/
void drawblock();/*画方块*/
void clear_block();/*清除方块*/
void printscore();/*打印分数和关数*/
struct block create();/*随机产生一个新的方块*/
struct block change();/*旋转变换函数*/
/***************************主函数**********************************/
void main()
{int key;/* 用户按键*/
struct  block  nextblock,mblock;/*nextblock--下一个方块,mblock--现在的方块*/
initial();
help(420,200);
showscore();
printscore();
nextblock=create();
loop: if(check_top(mblock)) restart();/*如果方块没到容器顶,进行loop循环*/
  printscore() ;
      mblock=nextblock;
      nextblock=create();
      show_next(nextblock);
while(1)
 { if(bioskey(1)!=0) key=bioskey(0);
      else key=0;
     switch(key)
     { case DOWN:{ if(check_bottom(mblock)==1)
        {  if(! del_line(mblock.y))
  { mblock.color=DARKGRAY;
   drawblock(mblock);}goto loop;}
    clear_block(mblock);
     mblock.y=mblock.y+20;
     drawblock(mblock);
   }
    break;
       case UP: mblock=change(mblock);break;
       case LEFT: if(!check_lborder(mblock)&&!check_bottom(mblock))
       {clear_block(mblock);mblock.x-=20;}  break;
       case RIGHT:if(!check_rborder(mblock)&&!check_bottom(mblock))
      {clear_block(mblock);mblock.x+=20;}
    break;
       case F1:test();break;
       case ESC: setfillstyle(1, DARKGRAY);
   bar(0,465,640,480);
   outtextxy(5,469,"Are you sure to exit (Y/N)...");
   while(1)
     { key=bioskey(0);
       if (key==NO)
                {initial();help(420,200);showscore();printscore();goto loop;}
                if(key==YES)
    {closegraph();exit(1);}
      }               break;
       default:{ if(check_bottom(mblock)==1)
   {  if(! del_line(mblock.y))
    { mblock.color=DARKGRAY;
    drawblock(mblock);
    }
    goto loop;
   }
    clear_block(mblock);
     mblock.y=mblock.y+20;
     drawblock(mblock);
      delay(interval);
        }
      }/*switch*/
  }/*while*/
}/*main*/
/*初始化所有全局变量和界面*/
void initial()
{int i,j;
 score=0;
 interval=500;
 for(i=0;i<20;i++)
  for(j=0;j<10;j++)
grid[i][j]=0;
ini_graph();
drawbar();
ini_grid();
}
/*画游戏的容器*/
void drawbar()
{int left,top,right,bottom;
left=200;
top= 40;
right=400;
bottom=440;
setfillstyle(1,LIGHTBLUE);
bar(left,top,right,bottom);
bar(480,40,560,120);
}
/*在容器画 20*10 的格子 */
void ini_grid()
{int i,j;
for(i=0;i<10;i++)
for(j=0;j<20;j++)
{rectangle(200+i*20,40+j*20,200+i*20+20,40+j*20+20);/*画主容器的格子*/
 }
for(i=0;i<4;i++)
 for(j=0;j<4;j++)
 rectangle(480+i*20,40+j*20,480+i*20+20,40+j*20+20);/*画显示下一个方块的格子*/
}
/*随机产生一个新的方块*/
struct block create()
{struct block newblock ;
 int r;
 r=random(7);
 switch(r)
 {case(0):newblock.type='I';newblock.color=5; break;
  case(1):newblock.type='O';newblock.color=BLUE; break;
  case(2):newblock.type='T';newblock.color=BROWN; break;
  case(3):newblock.type='L';newblock.color=YELLOW; break;
  case(4):newblock.type='J';newblock.color=LIGHTGREEN; break;
  case(5):newblock.type='Z';newblock.color=GREEN; break;
  case(6):newblock.type='N';newblock.color=RED; break;
 }
 newblock.shape=1;
 newblock.x=300;
 newblock.y=40;
 return newblock;
}
/*消格函数,传递参数为方块的左上方的纵坐标值,如有消格,返回0,否则返回1*/
int del_line(int ty)
{int i,j ,b,i0,k,ret;
ret=0;
if (ty>=440) return 0;
i0=(ty-40)/20;
if(i0==19||i0==18||i0==17)b=19;
else b=i0+3;
for(k=i0;k<=b;k++)
 { for(j=0;j<10;j++)
   if(!grid[k][j])  break;
   if(j==10)  {score++;
        ret=1;
        for(i=k;i>0;i--)
        for(j=0;j<10;j++)
  { grid[i][j]=grid[i-1][j];
      if(!grid[i][j])
     {setfillstyle(1,LIGHTBLUE);
      setcolor(WHITE);
      rectangle(200+j*20,40+i*20,200+j*20+20,40+i*20+20);
      floodfill(200+j*20+10,40+i*20+10,WHITE);
     }
   else {setfillstyle(9,DARKGRAY);
         setcolor(DARKGRAY);
         rectangle(200+j*20,40+i*20,200+j*20+20,40+i*20+20);
         floodfill(200+j*20+10,40+i*20+10,DARKGRAY);
        }
  }
     }
 }
return ret;
}
/*判断方块是否到容器左边界或左方已有别的方块,有则返回1,否则返回0
传递参数为当前的的block结构实例变量, */
 int check_lborder(struct block cblock)
 {int i0,j0;
  j0=(cblock.x-200)/20;
  i0=(cblock.y-40)/20;
  if(cblock.x<=200) return 1;/*已经到达容器左边界*/
  switch(cblock.type)
   {case('I'):if(grid[i0][j0-1]) return 1; break;/*左边已有其他静止的方格*/
    case('O'):if(grid[i0][j0-1]) return 1; break;
    case('L'): switch(cblock.shape)
        {case(1):if(grid[i0][j0-1]||grid[i0+1][j0-1]||grid[i0+2][j0-1])
    return 1;  break;
        case(2):if(grid[i0][j0-1]||grid[i0+1][j0-1])
    return 1;     break;
        case(3):if(grid[i0][j0-1]||grid[i0+1][j0]||grid[i0+2][j0])
     return 1;   break;
        case(4): if(grid[i0][j0-1]||grid[i0+1][j0-1])
    return 1;break;
        }           break;
    case('J'):switch(cblock.shape)
        {case(1):if(grid[i0][j0]||grid[i0+1][j0]||grid[i0+2][j0-1])
      return 1;  break;
        case(2):if(grid[i0][j0-1]||grid[i0-1][j0-1])
    return 1;     break;
        case(3):if(grid[i0][j0-1]||grid[i0+1][j0-1]||grid[i0+2][j0-1])
     return 1;   break;
        case(4): if(grid[i0][j0-1]||grid[i0+1][j0+1])
     return 1;      break;
        }    break;
    case('T'):break;
    case('Z'):break;
    case('N'):break;
   }


 return 0;
 }
/*判断方块是否到容器右边界或右方已有别的方块,有则返回1,否则返回0
传递参数为当前的的block结构实例变量, */
int check_rborder(struct block cblock)
{int i0,j0;
j0=(cblock.x-200)/20;/*把方块的左上方的坐标x,y换算成相应数组grid[i][j]的下标i0,j0*/
i0=(cblock.y-40)/20;
switch(cblock.type)
  {case('I'):if(cblock.shape==1)
      {if(cblock.x+80>=400||grid[i0][j0+4]) return 1;}/*已经到达容器右边界或左边已有方块*/
       else {if(cblock.x+20>=400||grid[i0][j0+1])return 1;}
  break;
   case('O'): if(cblock.x+40>=400||grid[i0][j0+2]||grid[i0+1][j0+2])
  return 1;   break;
   case('T'): switch(cblock.shape)
        {case(1): if(cblock.x+60>=400||grid[i0][j0+3]||grid[i0+1][j0+2])
       return 1;break;
  case(3): if(cblock.x+60>=400||grid[i0+1][j0+3]||grid[i0][j0+2])
       return 1;break;

  case(2):if(cblock.x+40>=400||grid[i0][j0+2]||grid[i0+1][j0+2]||grid[i0+2][j0+2])
     return 1;break;
  case(4):if(cblock.x+40>=400||grid[i0][j0+1]||grid[i0+2][j0+1]||grid[i0+1][j0+2])
     return 1;break;
        }
    break;
   case('L'):switch(cblock.shape)
        {case(1): if(cblock.x+40>=400||grid[i0+2][j0+2]||grid[i0][j0+1]||grid[i0+1][j0+1])
         return 1;break;
  case(2): if(cblock.x+60>=400||grid[i0][j0+3]||grid[i0+1][j0+3])
     return 1;break;
  case(3): if(cblock.x+40>=400||grid[i0][j0+2]||grid[i0+1][j0+2]||grid[i0+2][j0+2])
     return 1;break;
  case(4): if(cblock.x+60>=400||grid[i0][j0+3]||grid[i0+1][j0+3])
         return 1;break;
        } break;
   case('J'):switch(cblock.shape)
       {case(1): if(cblock.x+40>=400||grid[i0][j0+2]||grid[i0+1][j0+2]||grid[i0+2][j0+2])
         return 1;break;
  case(2):if(cblock.x+60>=400||grid[i0][j0+1]||grid[i0+1][j0+3])
     return 1;break;
  case(3): if(cblock.x+40>=400||grid[i0][j0+2]||grid[i0+1][j0+1]||grid[i0+2][j0+1])
     return 1;break;
  case(4): if(cblock.x+60>=400||grid[i0][j0+3]||grid[i0+1][j0+3])
         return 1;break;
        } break;
   case('Z'): switch(cblock.shape)
        {case(1): if(cblock.x+60>=400||grid[i0][j0+2]||grid[i0+1][j0+3])
       return 1;break;
  case(2):if(cblock.x+40>=400||grid[i0][j0+2]||grid[i0+1][j0+2]||grid[i0+2][j0+1])
     return 1;break;
        }break;
   case('N'): switch(cblock.shape)
        {case(1): if(cblock.x+60>=400||grid[i0][j0+3]||grid[i0+1][j0+2])
       return 1;break;
  case(2):if(cblock.x+40>=400||grid[i0][j0+1]||grid[i0+1][j0+2]||grid[i0+2][j0+2])
     return 1;break;
        } break;
  }
 return 0;
}
/*旋转变换函数,本游戏的核心程序之一,
传递参数为当前的的block结构实例变量,
判断当前位置是否能变换后 ,若能
返回变换后的block实例变量,否则
返回就原来传进来的block实例变量 */
struct block change(struct block cblock)
{int i,j,i0,j0;
j0=(cblock.x-200)/20;/*把方块的左上方的坐标x,y换算成相应数组grid[i][j]的下标i0,j0*/
i0=(cblock.y-40)/20;
 switch(cblock.type)
 {case('I'):switch(cblock.shape)
    { case(1):if (grid[i0+2][j0+1]||grid[i0+3][j0+1])
       return cblock;
       clear_block(cblock); cblock.x+=20; cblock.shape++;break;
      case(2):if (grid[i0][j0-1]||grid[i0][j0+1] || grid[i0][j0+2]||cblock.x>=360||cblock.x<=200)
       return cblock;/*如果变换后的格子上已经有其他方块,
                                     或变换后方块超出则不变换*/
       clear_block(cblock); cblock.x-=20;/*否则清楚原来的方块,计算出变换新的坐标*/
                     cblock.shape--;/*同一类型的方块进入下一个形状*/
                      break;
    }
   break;
  case('T'):switch(cblock.shape)
     {case(1):if(grid[i0-1][j0+1]) return cblock;
        clear_block(cblock);cblock.y-=20;
       cblock.shape++;  break;
      case(2):if(cblock.x>=360||grid[i0+1][j0+2])  return cblock;
        clear_block(cblock);cblock.shape++;break;
      case(3):if(grid[i0-1][j0+1]||grid[i0][j0+2])
       return cblock;
       clear_block(cblock);cblock.x+=20;cblock.y-=20;
     cblock.shape++;break;
      case(4):if(cblock.x<=200||grid[i0+1][j0-1])
       return cblock;
       clear_block(cblock);cblock.y+=20;cblock.x-=20;
       cblock.shape=1;  break;
     }                      break;
  case('L'):switch(cblock.shape)
     {case(1):if(grid[i0+2][j0-1]||grid[i0+1][j0+1]||cblock.x<=200)
       return cblock;
       clear_block(cblock);cblock.y+=20; cblock.x-=20;
       cblock.shape++;  break;
      case(2): if(grid[i0-1][j0]||grid[i0-1][j0+1]||grid[i0+1][j0+1])
        return cblock;
        clear_block(cblock);cblock.y-=20;
        cblock.shape++;break;
      case(3): if(grid[i0+2][j0-1]||grid[i0+2][j0]||cblock.x<=200)
       return cblock;
       clear_block(cblock);cblock.y+=20;cblock.x-=20;
       cblock.shape++;break;
      case(4): if(grid[i0][j0+1]||grid[i0-1][j0+1])
        return cblock;
        clear_block(cblock);cblock.y-=20;cblock.x+=20;
       cblock.shape=1;  break;
     }
     break;
  case('J'): switch(cblock.shape)
     {case(1):if(grid[i0+1][j0]||grid[i0+2][j0+2]||cblock.x>=360)
       return cblock;
       clear_block(cblock);cblock.y+=20;
       cblock.shape++;  break;
      case(2): if(grid[i0-1][j0+1]||grid[i0-1][j0])
       return cblock;
       clear_block(cblock);cblock.y-=20;
       cblock.shape++;break;
      case(3):if(grid[i0+1][j0+1]||grid[i0+2][j0+1]||grid[i0+1][j0-1])
       return cblock;
       clear_block(cblock);cblock.y+=20;cblock.x-=20;
        if(grid[i0-1][j0]||grid[i0-1][j0+1]||cblock.x<=200)
       return cblock;
       cblock.shape++;break;
      case(4): if(grid[i0-1][j0-2]||grid[i0+1][j0+1])
       return cblock;
       clear_block(cblock);cblock.y-=20;cblock.x+=20;
       cblock.shape=1;  break;
     }
        break;
   case('Z'):switch(cblock.shape)
    { case(1):if(grid[i0-1][j0+1]||grid[i0+1][j0])
       return cblock;
       clear_block(cblock);cblock.y-=20;
       cblock.shape++;break;
      case(2): if(grid[i0+2][j0+1]||grid[i0+2][j0+2])
        return cblock;
        clear_block(cblock);cblock.y+=20;
        cblock.shape--;break;
    }break;
   case('N'):switch(cblock.shape)
    { case(1):if(grid[i0][j0]||grid[i0-1][j0])
        return cblock;
       clear_block(cblock); cblock.y-=20;
       cblock.shape++;break;
      case(2):if(grid[i0+2][j0]||grid[i0+1][j0+2]||cblock.x>=360)
       return cblock;
       clear_block(cblock); cblock.y+=20; cblock.shape--;break;
    }break;
 }
return cblock;/*返回变换后的方块*/
}
/*画方块函数,传递参数为当前block实例变量*/
void drawblock(struct block drblock)
{int i0,j0,i,j;
j0=(drblock.x-200)/20;/*把方块的左上方的坐标x,y换算成相应数组grid[i][j]的下标i0,j0*/
i0=(drblock.y-40)/20;
setfillstyle(9,drblock.color);
setcolor(drblock.color);
switch(drblock.type)
 {case('I'):if(drblock.shape==1)
     { for(j=0;j<4;j++)
       { rectangle(drblock.x+j*20,drblock.y,drblock.x+j*20+20,drblock.y+20);/*画方块格子*/
  floodfill(drblock.x+j*20+10,drblock.y+10,drblock.color);/*蔓延填充方块*/
  grid[i0][j0+j]+=I_SHAPE1[0][j];/*将定义的形状与grid数组相加*/
       }                                /*这样有格子的相应grid数组元素就加1*/
     }
    if(drblock.shape==2)
     { for(i=0;i<4;i++)
       { rectangle(drblock.x,drblock.y+20*i,drblock.x+20,drblock.y+20*i+20);
  floodfill(drblock.x+10,drblock.y+20*i+10,drblock.color);
  grid[i0+i][j0]+=I_SHAPE2[i][0];
       }
     }break;
  case('O'):for(i=0;i<2;i++)
     for(j=0;j<2;j++)
      {rectangle(drblock.x+i*20,drblock.y+j*20,drblock.x+i*20+20,drblock.y+j*20+20);
       floodfill(drblock.x+i*20+10,drblock.y+j*20+10,drblock.color);
      grid[i0+i][j0+j]+=O_SHAPE[i][j];
      }
     break;
  case('T'): switch(drblock.shape)
      {case(1):for(i=0;i<2;i++)
        for(j=0;j<3;j++)
        {grid[i0+i][j0+j]+=T_SHAPE1[i][j] ;
         if(T_SHAPE1[i][j]==1)
   {rectangle(drblock.x+j*20,drblock.y+i*20,drblock.x+j*20+20,drblock.y+i*20+20);
    floodfill(drblock.x+j*20+10,drblock.y+i*20+10,drblock.color);
   }
        } break ;
       case(2):for(i=0;i<3;i++)
        for(j=0;j<2;j++)
        {grid[i0+i][j0+j]+=T_SHAPE2[i][j] ;
         if(T_SHAPE2[i][j]==1)
   {rectangle(drblock.x+j*20,drblock.y+i*20,drblock.x+j*20+20,drblock.y+i*20+20);
    floodfill(drblock.x+j*20+10,drblock.y+i*20+10,drblock.color);
   }
        }  break ;
       case(3):for(i=0;i<2;i++)
        for(j=0;j<3;j++)
        {grid[i0+i][j0+j]+=T_SHAPE3[i][j] ;
         if(T_SHAPE3[i][j]==1)
   {rectangle(drblock.x+j*20,drblock.y+i*20,drblock.x+j*20+20,drblock.y+i*20+20);
    floodfill(drblock.x+j*20+10,drblock.y+i*20+10,drblock.color);
   }
        }break ;
       case(4): for(i=0;i<3;i++)
        for(j=0;j<2;j++)
        {grid[i0+i][j0+j]+=T_SHAPE4[i][j] ;
         if(T_SHAPE4[i][j]==1)
   {rectangle(drblock.x+j*20,drblock.y+i*20,drblock.x+j*20+20,drblock.y+i*20+20);
    floodfill(drblock.x+j*20+10,drblock.y+i*20+10,drblock.color);
   }
         } break ;
     } break;
  case('L'):  switch(drblock.shape)
      {case(1):for(i=0;i<3;i++)
        for(j=0;j<2;j++)
        {grid[i0+i][j0+j]+=L_SHAPE1[i][j] ;
         if(L_SHAPE1[i][j]==1)
   {rectangle(drblock.x+j*20,drblock.y+i*20,drblock.x+j*20+20,drblock.y+i*20+20);
    floodfill(drblock.x+j*20+10,drblock.y+i*20+10,drblock.color);
   }
        } break ;
       case(2):for(i=0;i<2;i++)
        for(j=0;j<3;j++)
        {grid[i0+i][j0+j]+=L_SHAPE2[i][j] ;
         if(L_SHAPE2[i][j]==1)
   {rectangle(drblock.x+j*20,drblock.y+i*20,drblock.x+j*20+20,drblock.y+i*20+20);
    floodfill(drblock.x+j*20+10,drblock.y+i*20+10,drblock.color);
   }
        }  break ;
       case(3):for(i=0;i<3;i++)
        for(j=0;j<2;j++)
        {grid[i0+i][j0+j]+=L_SHAPE3[i][j] ;
         if(L_SHAPE3[i][j]==1)
   {rectangle(drblock.x+j*20,drblock.y+i*20,drblock.x+j*20+20,drblock.y+i*20+20);
    floodfill(drblock.x+j*20+10,drblock.y+i*20+10,drblock.color);
   }
        }break ;
       case(4): for(i=0;i<2;i++)
        for(j=0;j<3;j++)
        {grid[i0+i][j0+j]+=L_SHAPE4[i][j] ;
         if(L_SHAPE4[i][j]==1)
   {rectangle(drblock.x+j*20,drblock.y+i*20,drblock.x+j*20+20,drblock.y+i*20+20);
    floodfill(drblock.x+j*20+10,drblock.y+i*20+10,drblock.color);
   }
         }      break ;
     }
        break;
  case('J'): switch(drblock.shape)
      {case(1):for(i=0;i<3;i++)
        for(j=0;j<2;j++)
        {grid[i0+i][j0+j]+=J_SHAPE1[i][j] ;
         if(J_SHAPE1[i][j]==1)
   {rectangle(drblock.x+j*20,drblock.y+i*20,drblock.x+j*20+20,drblock.y+i*20+20);
    floodfill(drblock.x+j*20+10,drblock.y+i*20+10,drblock.color);
   }
        } break ;
       case(2):for(i=0;i<2;i++)
        for(j=0;j<3;j++)
        {grid[i0+i][j0+j]+=J_SHAPE2[i][j] ;
         if(J_SHAPE2[i][j]==1)
   {rectangle(drblock.x+j*20,drblock.y+i*20,drblock.x+j*20+20,drblock.y+i*20+20);
    floodfill(drblock.x+j*20+10,drblock.y+i*20+10,drblock.color);
   }
        }  break ;
       case(3):for(i=0;i<3;i++)
        for(j=0;j<2;j++)
        {grid[i0+i][j0+j]+=J_SHAPE3[i][j] ;
         if(J_SHAPE3[i][j]==1)
   {rectangle(drblock.x+j*20,drblock.y+i*20,drblock.x+j*20+20,drblock.y+i*20+20);
    floodfill(drblock.x+j*20+10,drblock.y+i*20+10,drblock.color);
   }
        } break;
      case(4): for(i=0;i<2;i++)
        for(j=0;j<3;j++)
        {grid[i0+i][j0+j]+=J_SHAPE4[i][j] ;
         if(J_SHAPE4[i][j]==1)
   {rectangle(drblock.x+j*20,drblock.y+i*20,drblock.x+j*20+20,drblock.y+i*20+20);
    floodfill(drblock.x+j*20+10,drblock.y+i*20+10,drblock.color);
   }
         } break ;
     } break;
  case('Z'):switch(drblock.shape)
     { case(1):for(i=0;i<2;i++)
        for(j=0;j<3;j++)
        {grid[i0+i][j0+j]+=Z_SHAPE1[i][j] ;
         if(Z_SHAPE1[i][j]==1)
   {rectangle(drblock.x+j*20,drblock.y+i*20,drblock.x+j*20+20,drblock.y+i*20+20);
    floodfill(drblock.x+j*20+10,drblock.y+i*20+10,drblock.color);
   }
        } break ;
       case(2):for(i=0;i<3;i++)
        for(j=0;j<2;j++)
        {grid[i0+i][j0+j]+=Z_SHAPE2[i][j] ;
         if(Z_SHAPE2[i][j]==1)
   {rectangle(drblock.x+j*20,drblock.y+i*20,drblock.x+j*20+20,drblock.y+i*20+20);
    floodfill(drblock.x+j*20+10,drblock.y+i*20+10,drblock.color);
   }
        }  break ;
      } break;
  case('N'):switch(drblock.shape)
     { case(1):for(i=0;i<2;i++)
        for(j=0;j<3;j++)
        {grid[i0+i][j0+j]+=N_SHAPE1[i][j] ;
         if(N_SHAPE1[i][j]==1)
   {rectangle(drblock.x+j*20,drblock.y+i*20,drblock.x+j*20+20,drblock.y+i*20+20);
    floodfill(drblock.x+j*20+10,drblock.y+i*20+10,drblock.color);
   }
        } break ;
       case(2):for(i=0;i<3;i++)
        for(j=0;j<2;j++)
        {grid[i0+i][j0+j]+=N_SHAPE2[i][j] ;
         if(N_SHAPE2[i][j]==1)
   {rectangle(drblock.x+j*20,drblock.y+i*20,drblock.x+j*20+20,drblock.y+i*20+20);
    floodfill(drblock.x+j*20+10,drblock.y+i*20+10,drblock.color);
   }
        }  break ;
      } break;
 }
}
/*清楚原来的方块,用原来容器的方格填充
传递参数为当前的block实例变量*/
void clear_block(struct block cblock)
{ int i,j,i0,j0;
j0=(cblock.x-200)/20;/*把方块的左上方的坐标x,y换算成相应数组grid[i][j]的下标i0,j0*/
i0=(cblock.y-40)/20;
setfillstyle(1,LIGHTBLUE);/*设置填充色为原容器格子的颜色*/
setcolor(WHITE);/*恢复原来的前景色*/
  switch(cblock.type)
 {case('I'):if(cblock.shape==1)
     { for(j=0;j<4;j++)
       {rectangle(cblock.x+j*20,cblock.y,cblock.x+j*20+20,cblock.y+20);/*画原容器的格子*/
  floodfill(cblock.x+j*20+10,cblock.y+10,WHITE);/*蔓延填充*/
  grid[i0][j0+j]=0;/*清零,即该格已经没有方块*/
       }
     }
    if(cblock.shape==2)
     { for(i=0;i<4;i++)
       { rectangle(cblock.x,cblock.y+20*i,cblock.x+20,cblock.y+20*i+20);
  floodfill(cblock.x+10,cblock.y+20*i+10,WHITE);
  grid[i0+i][j0]=0;
       }
     }break;
  case('O'):for(i=0;i<2;i++)
     for(j=0;j<2;j++)
      {rectangle(cblock.x+i*20,cblock.y+j*20,cblock.x+i*20+20,cblock.y+j*20+20);
       floodfill(cblock.x+i*20+10,cblock.y+j*20+10,WHITE);
      grid[i0+i][j0+j]=0;
      }
     break;
  case('T'): switch(cblock.shape)
      {case(1):for(i=0;i<2;i++)
        for(j=0;j<3;j++)
       { if(T_SHAPE1[i][j]==1)
   {rectangle(cblock.x+j*20,cblock.y+i*20,cblock.x+j*20+20,cblock.y+i*20+20);
    floodfill(cblock.x+j*20+10,cblock.y+i*20+10,WHITE);
         grid[i0+i][j0+j]=0 ; }
        } break ;

       case(2):for(i=0;i<3;i++)
        for(j=0;j<2;j++)
        { if(T_SHAPE2[i][j]==1)
   {rectangle(cblock.x+j*20,cblock.y+i*20,cblock.x+j*20+20,cblock.y+i*20+20);
    floodfill(cblock.x+j*20+10,cblock.y+i*20+10,WHITE);
    grid[i0+i][j0+j]=0 ;
   }
        }  break ;
       case(3):for(i=0;i<2;i++)
        for(j=0;j<3;j++)
        { if(T_SHAPE3[i][j]==1)
   {rectangle(cblock.x+j*20,cblock.y+i*20,cblock.x+j*20+20,cblock.y+i*20+20);
    floodfill(cblock.x+j*20+10,cblock.y+i*20+10,WHITE);
    grid[i0+i][j0+j]=0;
   }
        }break ;
       case(4): for(i=0;i<3;i++)
        for(j=0;j<2;j++)
        { if(T_SHAPE4[i][j]==1)
   {rectangle(cblock.x+j*20,cblock.y+i*20,cblock.x+j*20+20,cblock.y+i*20+20);
    floodfill(cblock.x+j*20+10,cblock.y+i*20+10,WHITE);
         grid[i0+i][j0+j]=0; }
         } break ;
     } break;
 case('L'):  switch(cblock.shape)
      {case(1):for(i=0;i<3;i++)
        for(j=0;j<2;j++)
        {if(L_SHAPE1[i][j]==1)
   {rectangle(cblock.x+j*20,cblock.y+i*20,cblock.x+j*20+20,cblock.y+i*20+20);
    floodfill(cblock.x+j*20+10,cblock.y+i*20+10,WHITE);
    grid[i0+i][j0+j]=0;
   }
        } break ;
       case(2):for(i=0;i<2;i++)
        for(j=0;j<3;j++)
        {if(L_SHAPE2[i][j]==1)
   {rectangle(cblock.x+j*20,cblock.y+i*20,cblock.x+j*20+20,cblock.y+i*20+20);
    floodfill(cblock.x+j*20+10,cblock.y+i*20+10,WHITE);
     grid[i0+i][j0+j]=0;
   }
        }  break ;
       case(3):for(i=0;i<3;i++)
        for(j=0;j<2;j++)
        {if(L_SHAPE3[i][j]==1)
   {rectangle(cblock.x+j*20,cblock.y+i*20,cblock.x+j*20+20,cblock.y+i*20+20);
    floodfill(cblock.x+j*20+10,cblock.y+i*20+10,WHITE);
     grid[i0+i][j0+j]=0;
   }
        }break ;
       case(4): for(i=0;i<2;i++)
        for(j=0;j<3;j++)
        {if(L_SHAPE4[i][j]==1)
   {rectangle(cblock.x+j*20,cblock.y+i*20,cblock.x+j*20+20,cblock.y+i*20+20);
    floodfill(cblock.x+j*20+10,cblock.y+i*20+10,WHITE);
     grid[i0+i][j0+j]=0;
   }
         } break ;
     }
        break;
  case('J'): switch(cblock.shape)
      {case(1):for(i=0;i<3;i++)
        for(j=0;j<2;j++)
        { if(J_SHAPE1[i][j]==1)
   {rectangle(cblock.x+j*20,cblock.y+i*20,cblock.x+j*20+20,cblock.y+i*20+20);
    floodfill(cblock.x+j*20+10,cblock.y+i*20+10,WHITE);
     grid[i0+i][j0+j]=0;
   }
        } break ;
       case(2):for(i=0;i<2;i++)
        for(j=0;j<3;j++)
        { if(J_SHAPE2[i][j]==1)
   {rectangle(cblock.x+j*20,cblock.y+i*20,cblock.x+j*20+20,cblock.y+i*20+20);
   floodfill(cblock.x+j*20+10,cblock.y+i*20+10,WHITE);
    grid[i0+i][j0+j]=0;
   }
        }  break ;
       case(3):for(i=0;i<3;i++)
        for(j=0;j<2;j++)
        {if(J_SHAPE3[i][j]==1)
   {rectangle(cblock.x+j*20,cblock.y+i*20,cblock.x+j*20+20,cblock.y+i*20+20);
    floodfill(cblock.x+j*20+10,cblock.y+i*20+10,WHITE);
    grid[i0+i][j0+j]=0;
   }
        } break;
      case(4): for(i=0;i<2;i++)
        for(j=0;j<3;j++)
        { if(J_SHAPE4[i][j]==1)
   {rectangle(cblock.x+j*20,cblock.y+i*20,cblock.x+j*20+20,cblock.y+i*20+20);
    floodfill(cblock.x+j*20+10,cblock.y+i*20+10,WHITE);
    grid[i0+i][j0+j]=0;
   }
         } break ;
     } break;
  case('Z'):switch(cblock.shape)
     { case(1):for(i=0;i<2;i++)
        for(j=0;j<3;j++)
        { if(Z_SHAPE1[i][j]==1)
   {rectangle(cblock.x+j*20,cblock.y+i*20,cblock.x+j*20+20,cblock.y+i*20+20);
    floodfill(cblock.x+j*20+10,cblock.y+i*20+10,WHITE);
    grid[i0+i][j0+j]=0;
   }
        } break ;
       case(2):for(i=0;i<3;i++)
        for(j=0;j<2;j++)
        {if(Z_SHAPE2[i][j]==1)
   {rectangle(cblock.x+j*20,cblock.y+i*20,cblock.x+j*20+20,cblock.y+i*20+20);
    floodfill(cblock.x+j*20+10,cblock.y+i*20+10,WHITE);
    grid[i0+i][j0+j]=0;
   }
        }  break ;
      } break;
  case('N'):switch(cblock.shape)
     { case(1):for(i=0;i<2;i++)
        for(j=0;j<3;j++)
        { if(N_SHAPE1[i][j]==1)
   {rectangle(cblock.x+j*20,cblock.y+i*20,cblock.x+j*20+20,cblock.y+i*20+20);
    floodfill(cblock.x+j*20+10,cblock.y+i*20+10,WHITE);
    grid[i0+i][j0+j]=0;
   }
        } break ;
       case(2):for(i=0;i<3;i++)
        for(j=0;j<2;j++)
        {  if(N_SHAPE2[i][j]==1)
   {rectangle(cblock.x+j*20,cblock.y+i*20,cblock.x+j*20+20,cblock.y+i*20+20);
    floodfill(cblock.x+j*20+10,cblock.y+i*20+10,WHITE);
    grid[i0+i][j0+j]=0;
   }
        }  break ;
      } break;
 }
}
/*判断方块是否到容器顶,也是游戏结束标志,如到顶则返回1,否则返回0*/
int check_top(struct block tblock)
{ if(tblock.y==40)  return 1;
   else return 0;
}
/*判断方块是否到容器底或下方已有别的方块,有则返回1,否则返回0*/
int check_bottom(struct block nblock)
{ int j,i0,j0;
 j0=(nblock.x-200)/20;
 i0=(nblock.y-40)/20;
  switch(nblock.type)
 {case('I'): if(nblock.shape==1)
        {if(nblock.y==420)  return 1;/*已经到达容器底部*/
  for(j=0;j<4;j++)
  if(grid[i0+1][j0+j]) return 1;/*I型方块下面一格已有静止方块*/
        }
        if(nblock.shape==2)
        {if(nblock.y==360)  return 1;
  if(grid[i0+4][j0]) return 1;
        }
        break;
  case('O'): if(nblock.y==400) return 1;
      for(j=0;j<2;j++)
      if (grid[i0+2][j0+j]) return 1;
      break;
  case('T'):switch(nblock.shape)
      {case(1):if(nblock.y==400)  return 1;
        if(grid[i0+2][j0+1]||grid[i0+1][j0]||grid[i0+1][j0+2])
    return 1;
        break;
       case(2):if(nblock.y==380) return 1;
        if(grid[i0+3][j0+1]||grid[i0+2][j0]) return 1; break;
       case(3):if(nblock.y==400) return 1;
        for(j=0;j<3;j++)
        if(grid[i0+2][j0+j]) return 1;  break;
       case(4):if(nblock.y==380) return 1;
        if(grid[i0+3][j0]||grid[i0+2][j0+1]) return 1;break;
      } break;
  case('L'): switch(nblock.shape)
      {case(1):if (nblock.y>=380) return 1;
        if (grid[i0+3][j0]||grid[i0+3][j0+1])
     return 1;      break;
       case(2):if(nblock.y>=400) return 1;
        if(grid[i0+2][j0]||grid[i0+1][j0+1]||grid[i0+1][j0+2])
        return 1; break;
       case(3):if (nblock.y>=380) return 1;
        if (grid[i0+3][j0+1]||grid[i0+1][j0])
     return 1;      break;
       case(4):if (nblock.y>=400) return 1;
    for(j=0;j<3;j++)
     if(grid[i0+2][j0+j])
     return 1;      break;
      }
      break;
  case('J'):switch(nblock.shape)
      {case(1):if (nblock.y>=380) return 1;
        if (grid[i0+3][j0]||grid[i0+3][j0+1])
     return 1;      break;
       case(2):if (nblock.y>=400) return 1;
     for(j=0;j<3;j++)
     if(grid[i0+2][j0+j])
     return 1;   break;
       case(3):if (nblock.y>=380) return 1;
        if (grid[i0+3][j0]||grid[i0+1][j0+1])
     return 1;      break;

       case(4):if (nblock.y>=400) return 1;
        if (grid[i0+2][j0+2]||grid[i0+1][j0]||grid[i0+1][j0+1])
     return 1;      break;
      }     break;
  case('Z'):if (nblock.shape==1)
       { if(nblock.y==400) return 1;
  if(grid[i0+1][j0]||grid[i0+2][j0+1]||grid[i0+2][j0+2])
      return 1;    }
     if (nblock.shape==2)
       { if(nblock.y==380) return 1;
  if(grid[i0+3][j0]||grid[i0+2][j0+1])
      return 1;    } break;
  case('N'):if(nblock.shape==1)
        { if(nblock.y==400) return 1;
  if(grid[i0+1][j0+2]||grid[i0+2][j0+1]||grid[i0+2][j0])
      return 1;
        }
      if(nblock.shape==2)
        { if(nblock.y==380) return 1;
   if(grid[i0+2][j0]||grid[i0+3][j0+1])
      return 1;
        }
        break;

 }
return 0;
}
/*在屏幕右上方显示下一个方块的信息*/
void show_next(struct block nextblock)
{int i,j,g[4][4];
setfillstyle(1,LIGHTBLUE);
setcolor(WHITE);
for(i=0;i<4;i++)
 for(j=0;j<4;j++)
{ rectangle(480+i*20,40+j*20,480+i*20+20,40+j*20+20);
 floodfill( 480+i*20+10,40+20*j+10,WHITE);
}
setfillstyle(9,nextblock.color);
setcolor(nextblock.color);
 switch (nextblock.type)
  {case('I'):   for(j=0;j<4;j++)
   {rectangle(480+j*20,60,480+j*20+20,60+20);
    floodfill(480+j*20+10,60+10,nextblock.color);
   }
  break;
   case('O'): for(i=0;i<2;i++)
       for(j=0;j<2;j++)
       { rectangle(500+20*i,60+20*j,500+20*i+20,60+20*j+20);
  floodfill(500+20*i+10,60+20*j+10,nextblock.color);
       }break ;
   case('T'):   for(i=0;i<2;i++)
  for(j=0;j<3;j++)
   {g[i][j]=T_SHAPE1[i][j];
    if(g[i][j]==1)
      { rectangle(480+j*20,60+i*20,480+j*20+20,60+i*20+20);
        floodfill(480+j*20+10,60+i*20+10,nextblock.color);
      }
   }
    break;
   case('L'):    for(i=0;i<3;i++)
   for(j=0;j<2;j++)
   {g[i][j]=L_SHAPE1[i][j];
    if(g[i][j]==1)
      { rectangle(500+j*20,40+i*20,500+j*20+20,40+i*20+20);
        floodfill(500+j*20+10,40+i*20+10,nextblock.color);
      }
   }break;

   case('J') :  for(i=0;i<3;i++)
   for(j=0;j<2;j++)
   {g[i][j]=J_SHAPE1[i][j];
    if(g[i][j]==1)
      { rectangle(500+j*20,40+i*20,500+j*20+20,40+i*20+20);
        floodfill(500+j*20+10,40+i*20+10,nextblock.color);
      }
   }break;
   case('Z'):      for(i=0;i<2;i++)
   for(j=0;j<3;j++)
   {g[i][j]=Z_SHAPE1[i][j];
    if(g[i][j]==1)
      { rectangle(500+j*20,60+i*20,500+j*20+20,60+i*20+20);
        floodfill(500+j*20+10,60+i*20+10,nextblock.color);
      }
   }break;

   case('N'):  for(i=0;i<2;i++)
   for(j=0;j<3;j++)
   {g[i][j]=N_SHAPE1[i][j];
    if(g[i][j]==1)
      { rectangle(500+j*20,60+i*20,500+j*20+20,60+i*20+20);
        floodfill(500+j*20+10,60+i*20+10,nextblock.color);
      }
   }break;
 }
}
/*test the  grid array's value*/
void test()
{int i,j;
gotoxy(1,1);
for (i=0;i<20;i++)
{for (j=0;j<10;j++)
printf ("%2d",grid[i][j]);
printf ("/n");
}
}

/*initial the graphic driver*/
int ini_graph(void)
{
   /* request auto detection */
   int gdriver = DETECT, gmode, errorcode;
   int left, top, right, bottom;

   /* initialize graphics and local variables */
   initgraph(&gdriver, &gmode, "");

   /* read result of initialization */
   errorcode = graphresult();
   if (errorcode != grOk)  /* an error occurred */
   {
      printf("Graphics error: %s/n", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
      exit(1); /* terminate with an error code */
   }

   return 0;

}
/*show the game information  on right*/
void help(int xs,int ys){
 setcolor(15);
 rectangle(xs-3,ys-3,xs+200,ys+150);
 line (xs-3,ys+26,xs+200,ys+26);
 line (xs-3,ys+72,xs+200,ys+72);
 line(xs+39,ys+26,xs+39,ys+72);
 setcolor(12);
 outtextxy(xs+60,ys+8,"Welcome!!");
 setcolor(14);
 outtextxy(xs,ys+45,"HELP:");
 outtextxy(xs+55,ys+30,"->-Left ;<--Right");
 outtextxy(xs+55,ys+45,"^-Change");
 outtextxy(xs+55,ys+60,"ESC-Exit;R-Restar");
 outtextxy(xs,ys+75,"Russia Diamonds ");
 outtextxy(xs,ys+90,"CopyRight By FANZ.com");
 outtextxy(xs,ys+105,"Date:15/12/2001");
 outtextxy(xs,ys+120,"E-mail:Fanz@263.net");
}
/*show score and level*/
void showscore(){
 int cur[8];
cur[0]=420;
cur[1]=360;
cur[2]=cur[0] ;
cur[3]=cur[1]+50;
cur[4]=cur[2]+203;
cur[5]=cur[3];
cur[6]=cur[4];
cur[7]=cur[5]-50;
 setcolor(LIGHTBLUE);
  setfillstyle(7,LIGHTBLUE);

fillpoly(4,cur);
setcolor(13);
 outtextxy(480,380,"Score:");
 outtextxy(480,390,"Level:");
}
/*显示并刷新分数和关数*/
void printscore()
{ char sscore[6];
 char slevel[2];
 settextstyle(0,0,0);
 level=score/100+1;
 if (level>=5) level=1;
 interval=550-level*50;
   showscore();
 setcolor(RED);
 sprintf(sscore,"%d",score);
 sprintf(slevel,"%d",level);
 outtextxy (540,390,slevel);
 outtextxy (540,380,sscore);
 }
/*在屏幕右上方显示下一个方块的信息*/
void restart()
{ int key;
   delay(1000);
   setfillstyle(1,10);
   settextstyle(0,0,6);
   setcolor(3);
   outtextxy(102,152,"GAME OVER");
   setcolor(3);
   outtextxy(99,149,"GAME OVER");
   setcolor(10);
   outtextxy(96,146,"GAME OVER");
   delay(1000);
   setcolor(0);
   settextstyle(0,0,2);
   outtextxy(222,242,"R__Again!");
   outtextxy(232,282,"ESC__Exit!");
   setcolor(10);
   outtextxy(220,240,"R__Again!");
   outtextxy(230,280,"ESC__Exit!");
   while(1)
   { key=bioskey(0);
     if (key==RESTART)
      {initial();return;}
     if(key==ESC)
      {closegraph();exit(1);}
   }
}

0 0

相关博文

我的热门文章

img
取 消
img