C实现的俄罗斯方块

        刚开始学习C的时候写的一个东西,好像只有在一些老旧的机器上能跑,今天有机会接触到了这样的一台机器,试了一下,还是可以跑得动的,好激动(虽然程序在内存释放方面是有问题的)附上代码:

#include "stdio.h"
#include "dos.h"
#include "conio.h"
#include "graphics.h"
#include "stdlib.h"
/***********************按键码***************************/
#define VK_LEFT   0x4b00
#define VK_RIGHT  0x4d00
#define VK_DOWN   0x5000
#define VK_UP     0x4800
#define VK_ESC    0x011b
#define VK_ENTER  0x1c0d
/*********************************************************/
#define MAXX  12
#define MAXY  23   /*You'd better not make it more! It's big enough now!!!!!!*/
#define BACKCOLOR  LIGHTGRAY
#define LINECOLOR  LIGHTMAGENTA
/*******************************************************/
int box[MAXX][MAXY];  /*0_empty,1_moving,2_fixed*/
int timelimite=18,key=0;
int x,y,timecounter=0;
int score=0,level=0,boxcolor[MAXX][MAXY];
char str1[20] ,str2[20];
struct diamond
{
    int x[4];
    int y[4];
    int startx;
    int starty;
    int color;
    struct diamond *next;
}  ;
/***********************************************************************************************************/
struct diamond * nowbox,*nextbox;

/*************************************************************************************************************/
void   interrupt   (   *oldhandler)();
/**************************************/
void   interrupt   newhandler( )
{
  timecounter++;
}
/*************************************************************************/
void forelook(void)
{
   int i;

   setfillstyle(SOLID_FILL,BLACK);
   bar(319,79,461,101) ;

   setfillstyle(SOLID_FILL,LIGHTBLUE);
   bar(320,80,460,100);
   setcolor(RED);
   settextstyle(TRIPLEX_FONT,HORIZ_DIR ,4 );
   outtextxy(340,90,"The next one:");

   setfillstyle(SLASH_FILL,BACKCOLOR);
   setcolor(LINECOLOR);
   bar(320,100,460,240);
   setfillstyle(SOLID_FILL ,BACKCOLOR);
   bar(330,110,450,230) ;
   for(i=0;i<7;i++)
     {
          line(330,110+20*i,450,110+20*i);
      }
   for(i=0;i<7;i++)
     {
          line(330+20*i,110,330+20*i,230);
     }
}
/*******************************************************/
void showlevel(void)
{
   if(score>=1000)
      {
         level+=1;
      }
   sprintf(str1, "LEVEL :   %d    ", level);
   sprintf(str2, "SCORE :   %d    ", score);

   setfillstyle(SOLID_FILL,BLACK);
   bar(319,289,321,351) ;
   outtextxy(320,290,str1);
   outtextxy(320,350,str2);
}
/******************************************************/
void beginface(void)
{
   int i,j;
   setfillstyle(SLASH_FILL,BACKCOLOR);
   setcolor(LINECOLOR);
   bar(60,20,60+20*MAXX,20+MAXY*20);
   setfillstyle(SOLID_FILL ,BACKCOLOR);
   bar(80,40,80+(MAXX-2)*20,40+(MAXY-2)*20) ;
   for(i=0;i<=MAXY-2;i++)
     {
        line(80,40+i*20,80+(MAXX-2)*20,40+i*20);
     }

   for(i=0;i<=MAXX-2;i++)
     {
        line(80+20*i,40,80+20*i,40+20*(MAXY-2)) ;
     }

   setfillstyle(SOLID_FILL,LIGHTBLUE);
   bar(0,0,639,20);
   setcolor(WHITE);
   settextstyle(TRIPLEX_FONT,HORIZ_DIR ,4 );
   outtextxy(5,10,"ZY_1              Welcome to my game!And have a nice time!");
   forelook();

   for(i=0;i<=MAXX;i++)
     {
           for(j=0;j<=MAXY;j++)
              {
                box[i][j]=0;
                boxcolor[i][j]=BACKCOLOR;
              }
     }

   for(i=0;i<MAXY;i++)
     {
        box[0][i]=2;
        boxcolor[0][i]=-1;
     }
   for(i=0;i<MAXY;i++)
     {
        box[MAXX-1][i]=2;
        boxcolor[MAXX-1][i]=-1;
     }
   for(i=0;i<MAXX;i++)
     {
        box[i][0]=2;
        boxcolor[i][0]=-1;
     }

   for(i=0;i<MAXX;i++)
     {
        box[i][MAXY-1]=2;
        boxcolor[i][MAXY-1]=-1;
     }

   line(440,439,430,443);line(440,439,450,443);line(440,439,440,447);
   line(450,453,430,453);line(450,453,440,449);line(450,453,440,458);
   line(430,463,440,457);line(430,463,440,467);line(430,463,450,463);
   line(440,478,440,469);line(440,478,450,473);line(440,478,430,473);

   outtextxy(320,419,"DIRECTIONS :");
   outtextxy(430,419,"ENTER:PAUSE");
   outtextxy(430,429,"ESC:OVER");
   outtextxy(440,439,"  :NEXT");
   outtextxy(440,449,"  :RIGHT");
   outtextxy(440,459,"  :LEFT");
   outtextxy(440,469,"  :DOWEN");

}
/*************************************/
struct diamond* box1(void)  /****/
{
    struct diamond *first;
    struct diamond *tnext;
    first=(struct diamond *)malloc(sizeof(struct diamond));
    tnext=(struct diamond *)malloc(sizeof(struct diamond));


    first->next=tnext;
    tnext->next=first;
    first->x[0]=0;
    first->y[0]=0;
    first->x[1]=0;
    first->y[1]=1;
    first->x[2]=0;
    first->y[2]=2;
    first->x[3]=0;
    first->y[3]=3;
    first->startx=5;
    first->starty=1;
    first->color=BLUE;

    tnext->x[0]=0;
    tnext->y[0]=0;
    tnext->x[1]=1;
    tnext->y[1]=0;
    tnext->x[2]=2;
    tnext->y[2]=0;
    tnext->x[3]=3;
    tnext->y[3]=0;
    tnext->startx=5;
    tnext->starty=1;
    tnext->color=BLUE;

    if(random(2))
       return first;
    else
       return first->next;
}

/******************************************/
struct diamond *box2(void)    /*    *    */
{                         /*    **   */
    struct diamond *first;/*      *  */
    struct diamond *tnext;
    first=(struct diamond *)malloc(sizeof(struct diamond));
    tnext=(struct diamond *)malloc(sizeof(struct diamond));


    first->next=tnext;
    tnext->next=first;
    first->x[0]=0;
    first->y[0]=0;
    first->x[1]=0;
    first->y[1]=1;
    first->x[2]=1;
    first->y[2]=1;
    first->x[3]=1;
    first->y[3]=2;
    first->startx=5;
    first->starty=2;
    first->color=GREEN;

    tnext->x[0]=0;
    tnext->y[0]=0;
    tnext->x[1]=1;
    tnext->y[1]=0;
    tnext->x[2]=1;
    tnext->y[2]=-1;
    tnext->x[3]=2;
    tnext->y[3]=-1;
    tnext->startx=5;
    tnext->starty=2;
    tnext->color=GREEN;

    if(random(2))
       return first;
    else
       return first->next;
}

/********************************************/
struct diamond *box3()       /*     *       */
{                            /*    **       */
    struct diamond *first;   /*    *        */
    struct diamond *tnext;
    first=(struct diamond *)malloc(sizeof(struct diamond));
    tnext=(struct diamond *)malloc(sizeof(struct diamond));


    first->next=tnext;
    tnext->next=first;
    first->x[0]=0;
    first->y[0]=0;
    first->x[1]=0;
    first->y[1]=1;
    first->x[2]=-1;
    first->y[2]=1;
    first->x[3]=-1;
    first->y[3]=2;
    first->startx=5;
    first->starty=1;
    first->color=RED;

    tnext->x[0]=0;
    tnext->y[0]=0;
    tnext->x[1]=1;
    tnext->y[1]=0;
    tnext->x[2]=1;
    tnext->y[2]=1;
    tnext->x[3]=2;
    tnext->y[3]=1;
    tnext->startx=5;
    tnext->starty=1;
    tnext->color=RED;

    if(random(2))
       return first;
    else
       return first->next;
}


/*****************************************************/
struct diamond *box4()      /*   **     */
{                           /*   **    */
    struct diamond *first;

    first=(struct diamond *)malloc(sizeof(struct diamond));

    first->next=first;

    first->x[0]=0;
    first->y[0]=0;
    first->x[1]=1;
    first->y[1]=0;
    first->x[2]=0;
    first->y[2]=1;
    first->x[3]=1;
    first->y[3]=1;
    first->startx=5;
    first->starty=1;
    first->color=YELLOW;

       return first;

}


/******************************************/
struct diamond *box5()      /*     *       */
{                           /*    ***      */
    struct diamond *first;
    struct diamond *midd;
    struct diamond *tnext;
    int ra=random(4)+1,i;

    first=(struct diamond *)malloc(sizeof(struct diamond));

    first->x[0]=0;
    first->y[0]=0;
    first->x[1]=0;
    first->y[1]=1;
    first->x[2]=1;
    first->y[2]=1;
    first->x[3]=-1;
    first->y[3]=1;
    first->startx=5;
    first->starty=2;
    first->color=LIGHTBLUE;

    tnext=(struct diamond *)malloc(sizeof(struct diamond));

    tnext->x[0]=0;
    tnext->y[0]=0;
    tnext->x[1]=1;
    tnext->y[1]=0;
    tnext->x[2]=1 ;
    tnext->y[2]=1;
    tnext->x[3]=1;
    tnext->y[3]=-1;
    tnext->startx=5;
    tnext->starty=2;
    tnext->color=LIGHTBLUE;

    first->next=tnext;
    midd=tnext;

    tnext=(struct diamond *)malloc(sizeof(struct diamond));

    tnext->x[0]=0;
    tnext->y[0]=0;
    tnext->x[1]=1;
    tnext->y[1]=-1;
    tnext->x[2]=0;
    tnext->y[2]=-1;
    tnext->x[3]=-1;
    tnext->y[3]=-1;
    tnext->startx=5;
    tnext->starty=2;
    tnext->color=LIGHTBLUE;

    midd->next=tnext;
    midd=tnext;

    tnext=(struct diamond *)malloc(sizeof(struct diamond));

    tnext->x[0]=0;
    tnext->y[0]=0;
    tnext->x[1]=-1;
    tnext->y[1]=0;
    tnext->x[2]=-1;
    tnext->y[2]=1;
    tnext->x[3]=-1;
    tnext->y[3]=-1;
    tnext->startx=5;
    tnext->starty=2;
    tnext->color=LIGHTBLUE;
    midd->next=tnext;
    tnext->next=first;

    for(i=1;i<=ra;i++)
      {
         first=first->next;
      }
    return first;

}

/******************************************/
struct diamond *box6()      /*     *      */
{                           /*     *      */
    struct diamond *first;  /*     **     */
    struct diamond *midd;
    struct diamond *tnext;
    int ra=random(4)+1,i;

    first=(struct diamond *)malloc(sizeof(struct diamond));

    first->x[0]=0;
    first->y[0]=0;
    first->x[1]=0;
    first->y[1]=1;
    first->x[2]=0;
    first->y[2]=2;
    first->x[3]=1;
    first->y[3]=2;
    first->startx=5;
    first->starty=3;
    first->color=CYAN;

    tnext=(struct diamond *)malloc(sizeof(struct diamond));

    tnext->x[0]=0;
    tnext->y[0]=0;
    tnext->x[1]=1;
    tnext->y[1]=0;
    tnext->x[2]=2 ;
    tnext->y[2]=0;
    tnext->x[3]=2;
    tnext->y[3]=-1;
    tnext->startx=5;
    tnext->starty=3;
    tnext->color=CYAN;

    first->next=tnext;
    midd=tnext;

    tnext=(struct diamond *)malloc(sizeof(struct diamond));

    tnext->x[0]=0;
    tnext->y[0]=0;
    tnext->x[1]=0;
    tnext->y[1]=-1;
    tnext->x[2]=0 ;
    tnext->y[2]=-2;
    tnext->x[3]=-1;
    tnext->y[3]=-2;
    tnext->startx=5;
    tnext->starty=3;
    tnext->color=CYAN;

    midd->next=tnext;
    midd=tnext;

    tnext=(struct diamond *)malloc(sizeof(struct diamond));

    tnext->x[0]=0;
    tnext->y[0]=0;
    tnext->x[1]=-1;
    tnext->y[1]=0;
    tnext->x[2]=-2;
    tnext->y[2]=0;
    tnext->x[3]=-2;
    tnext->y[3]=1;
    tnext->startx=5;
    tnext->starty=3;
    tnext->color=CYAN;
    midd->next=tnext;
    tnext->next=first;

    for(i=1;i<=ra;i++)
      {
         first=first->next;
      }
    return first;

}


/******************************************/
struct diamond *box7()      /*           */
{                           /*    ***      */
    struct diamond *first;
    struct diamond *midd;
    struct diamond *tnext;
    int ra=random(4)+1,i;

    first=(struct diamond *)malloc(sizeof(struct diamond));

    first->x[0]=0;
    first->y[0]=0;
    first->x[1]=0;
    first->y[1]=1;
    first->x[2]=-1;
    first->y[2]=2;
    first->x[3]=0;
    first->y[3]=2;
    first->startx=5;
    first->starty=3;
    first->color=LIGHTCYAN;

    tnext=(struct diamond *)malloc(sizeof(struct diamond));

    tnext->x[0]=0;
    tnext->y[0]=0;
    tnext->x[1]=-1;
    tnext->y[1]=0;
    tnext->x[2]=-2;
    tnext->y[2]=0;
    tnext->x[3]=-2;
    tnext->y[3]=-1;
    tnext->startx=5;
    tnext->starty=3;
    tnext->color=LIGHTCYAN;

    first->next=tnext;
    midd=tnext;

    tnext=(struct diamond *)malloc(sizeof(struct diamond));

    tnext->x[0]=0;
    tnext->y[0]=0;
    tnext->x[1]=1;
    tnext->y[1]=0;
    tnext->x[2]=2;
    tnext->y[2]=0;
    tnext->x[3]=2;
    tnext->y[3]=1;
    tnext->startx=5;
    tnext->starty=3;
    tnext->color=LIGHTCYAN;

    midd->next=tnext;
    midd=tnext;

    tnext=(struct diamond *)malloc(sizeof(struct diamond));

    tnext->x[0]=0;
    tnext->y[0]=0;
    tnext->x[1]=0;
    tnext->y[1]=-1;
    tnext->x[2]=0;
    tnext->y[2]=-2;
    tnext->x[3]=1;
    tnext->y[3]=-2;
    tnext->startx=5;
    tnext->starty=3;
    tnext->color=LIGHTCYAN;
    midd->next=tnext;
    tnext->next=first;

    for(i=1;i<=ra;i++)
      {
         first=first->next;
      }
    return first;

}
/*************************************************************************/
struct diamond *getbox(void)
{
    struct diamond *box;
    switch (random(7)+1)
      {
        case 1:box=box1();break;
        case 2:box=box2();break;
        case 3:box=box3();break;
        case 4:box=box4();break;
        case 5:box=box5();break;
        case 6:box=box6();break;
        case 7:box=box7();break;
      }
      return box;
}

/*************************************************************************/
void shownext(struct diamond *Obox)
{
      int i,j;

      forelook();

      setfillstyle(SOLID_FILL ,Obox->color);

      for(i=0;i<4;i++)
         {
            floodfill(370+Obox->x[i]*20+1,150+Obox->y[i]*20+1,LINECOLOR) ;
         }
}
/*************************************************************************/
void drawbox(struct diamond *infor)
{
     int i;
     setfillstyle(SOLID_FILL ,infor->color);
     for(i=0;i<=3;i++)
       {
          floodfill(80+(infor->x[i]+x-1)*20+1,40+(infor->y[i]+y-1)*20+1,LINECOLOR);

           box[x+infor->x[i]][y+infor->y[i]]=1;

           boxcolor[x+infor->x[i]][y+infor->y[i]]=infor->color;
       }

}
/********************************************************/
void clearbox(struct diamond *infor)
{
     int i;
     setfillstyle(SOLID_FILL ,BACKCOLOR);
     for(i=0;i<=3;i++)
       {
           floodfill(80+(infor->x[i]+x-1)*20+1,40+(infor->y[i]+y-1)*20+1,LINECOLOR);

           box[x+infor->x[i]][y+infor->y[i]]=0;

           boxcolor[x+infor->x[i]][y+infor->y[i]]=BACKCOLOR;
       }
}
/*******************************************************************/
int ifboxfixed(void)
{
   int i,flag=0;

   for(i=0;i<4;i++)
      {
           if(box[x+nowbox->x[i]][y+nowbox->y[i]+1]==2)
              {
                 flag=1;
                 break;
              }
      }
   if(flag==1)
      {
           for(i=0;i<4;i++)
               {
                 box[x+nowbox->x[i]][y+nowbox->y[i]]=2;
               }
           return  1;
      }
   else
        return  0;
}
/*******************************************************************/
void goleft(void)
{
  int i,flag=0;
  oldhandler=getvect(0x1c);
  disable();
  setvect(0x1c,newhandler);
  enable();


  for(i=0;i<4;i++)
     {
         if(box[x+nowbox->x[i]-1][y+nowbox->y[i]]==2)
            {
              flag=1;
              break;
            }
     }

  if(flag==0)
     {
        clearbox(nowbox);
        x-=1;
        drawbox(nowbox);
     }
  else
     flag=0;
         
  disable();
  setvect(0x1c,oldhandler);
  enable();

}
/******************************************************************/
void goright(void)
{
  int i,flag=0;

  oldhandler=getvect(0x1c);
  disable();
  setvect(0x1c,newhandler);
  enable();

  for(i=0;i<4;i++)
    {
      if(box[x+nowbox->x[i]+1][y+nowbox->y[i]]==2)
          {
            flag=1 ;
            break;
          }
    }

  if(flag==0)
    {
       clearbox(nowbox);
       x+=1;
       drawbox(nowbox);
    }
  else
    flag=0;
        
  disable();
  setvect(0x1c,oldhandler);
  enable();

}
/******************************************************************/
void godown(void)
{
  int i,flag=0;

  oldhandler=getvect(0x1c);
  disable();
  setvect(0x1c,newhandler);
  enable();

  for(i=0;i<4;i++)
     {
       if(box[x+nowbox->x[i]][y+nowbox->y[i]+1]==2)

          {
             flag=1;
             break;
          }
     }

  if(flag==0)
     {
       clearbox(nowbox);
       y+=1;
       drawbox(nowbox);

     }

  else
       flag=0;

   disable();
   setvect(0x1c,oldhandler);
   enable();

}
/******************************************************************/
void gonext(void)
{
  int i,flag=0;

  oldhandler=getvect(0x1c);
  disable();
  setvect(0x1c,newhandler);
  enable();

  for(i=0;i<4;i++)
     {
       if(box[x+(nowbox->next)->x[i]][y+(nowbox->next)->y[i]]==2)

          {
             flag=1;
             break;
          }
     }

  if(flag==0)
     {

      clearbox(nowbox);
      nowbox=nowbox->next;
      drawbox(nowbox);
     }
  else
     flag=0;
         
  disable();
  setvect(0x1c,oldhandler);
  enable();

}
/******************************************************************/
int GAMEOVER(void)
{

  cleardevice();
  setbkcolor(RED) ;

  settextstyle(1, HORIZ_DIR,10);

  outtextxy(300,250,"YOU HAVE LOST !");

  sleep(3);
  closegraph();
  exit(0);

}
/*******************************************************************/
void clearline(int i)
{
  int m,n,j;

  for(m=i;m>1;m--)
     {
        for(n=1;n<MAXX-1;n++)

           {
           box[n][m] = box[n][m-1];
           boxcolor[n][m] =boxcolor[n][m-1];

           setfillstyle(SOLID_FILL ,boxcolor[n][m]);

           floodfill(80+(n-1)*20+1,40+(m-1)*20+1,LINECOLOR);
           }
     }
  for(j=1;j<MAXX-1;j++)
     {
        box[j][1]=0;

        setfillstyle(SOLID_FILL ,BACKCOLOR);

        floodfill(80+(j-1)*20+1,40+1,LINECOLOR);
     }
}
/******************************************************************/
void check(void)
{
  int i,j,flag1=0,flag2=0;

  for(i=1;i<MAXX-1;i++)
      {
          if(box[i][1]==2)
             {
                flag1=1;
                break;
             }
      }
  if(flag1==1)
      {

         flag1=0;
         GAMEOVER();  /*****************/
      }

  for(i=MAXY-2;i>1;i--)
      {
        for(j=1;j<MAXX-1;j++)
          {
             if(box[j][i]!=2)
                {
                  flag2=1;
                  break;
                 }
          }
        if(flag2==0)
          {
            clearline(i);
            score+=10;
          }
        else
          flag2=0;
      }
}
/******************************************************************/
void movedown(void)
{
  int i,flag=0;
  for(i=0;i<4;i++)
     {
       if(box[x+nowbox->x[i]][y+nowbox->y[i]+1]==2)
          {
             flag=1;
             break;
          }
     }

  if(flag==0)
     {
       clearbox(nowbox);
       y+=1;
       drawbox(nowbox);
     }
}
/********************************************************************/
void pause(void)
{
  disable();
  setvect(0x1c,oldhandler);
  enable();

  getch();

  oldhandler=getvect(0x1c);
  disable();
  setvect(0x1c, newhandler);
  enable();
}
/*************************************************************************/
void Intro( void )
{
    setbkcolor(BLUE) ;
    setcolor(GREEN) ;
    settextstyle(1, HORIZ_DIR,10);

    outtextxy(270,250,"MADE BY ZXC");

    sleep(3);
    cleardevice();
}
/********************************************************************/

int main(void)
{
int m,gd=DETECT,gm,timelimite=36 ;


initgraph(&gd,&gm,"c:\\");
randomize();

Intro();
beginface();

setbkcolor(BLACK) ;

nowbox=getbox();
nextbox=getbox();
x=nowbox->startx;
y=nowbox->starty;

   for(;;)
       {
           shownext(nextbox);
           drawbox(nowbox); 

           oldhandler=getvect(0x1c);
           disable();
           setvect(0x1c, newhandler);
           enable();

           for(;;)
               {
                         if(bioskey(1))
                            key=bioskey(0);
                         else
                            key=0;
                         switch(key)
                                {
                                   case 0:break;
                                   case VK_LEFT:    goleft();   break;
                                   case VK_RIGHT:   goright();  break;
                                   case VK_DOWN:    godown();   break;
                                   case VK_UP:      gonext();   break;
                                   case VK_ENTER:    pause();    break;
                                   case VK_ESC:     closegraph();exit(0);
                                   default :break;
                                }

                         if(timecounter>timelimite)
                            {
                               timecounter=0;
                               movedown();
                            }

                         if( ifboxfixed() )
                            {
                               timecounter=0;
                               break;
                            }

               }
               check();
               showlevel();
               nowbox=nextbox;
               nextbox=getbox();
               x=nowbox->startx;
               y=nowbox->starty;
       }




    return 0;
}


图片:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值