elsfk.c

来自「经典的俄罗斯方块游戏。大家都玩过吧? C语言实现」· C语言 代码 · 共 337 行

C
337
字号
/*   Turbo C   */
#include        <stdio.h>
#include        <stdlib.h>
#include        <dos.h>
#include        <conio.h>
#include        <time.h>
#include        <graphics.h>

#define                L         1
#define                LX        15
#define                LY         4
static        struct BLOCK{
                int        x0;
                int        y0;
                int        x1;
                int        y1;
                int        x2;
                int        y2;
                int        x3;
                int        y3;
                int        color;
                int        next;
        }b[23] = {{0, 1, 1, 1, 2, 1, 3, 1,  4,  1},
                {1, 0, 1, 3, 1, 2, 1, 1,  4,  0  },
                {1, 1, 2, 2, 1, 2, 2, 1,  1,  2  },
                {0, 1, 1, 1, 1, 0, 2, 0,  2,  4  },
                {1, 0, 1, 1, 2, 2, 2, 1,  2,  5  },
                {0, 2, 1, 2, 1, 1, 2, 1,  2,  6  },
                {0, 0, 0, 1, 1, 2, 1, 1,  2,  3  },
                {0, 0, 1, 0, 1, 1, 2, 1,  3,  8  },
                {0, 1, 0, 2, 1, 1, 1, 0,  3,  9  },
                {0, 1, 1, 1, 1, 2, 2, 2,  3, 10  },
                {1, 1, 1, 2, 2, 1, 2, 0,  3,  7  },
                {1, 0, 1, 1, 1, 2, 2, 2,  7, 12  },
                {0, 1, 1, 1, 2, 1, 2, 0,  7, 13  },
                {0, 0, 1, 2, 1, 1, 1, 0,  7, 14  },
                {0, 1, 0, 2, 1, 1, 2, 1,  7, 11  },
                {0, 2, 1, 2, 1, 1, 1, 0,  5, 16  },
                {0, 1, 1, 1, 2, 2, 2, 1,  5, 17  },
                {1, 0, 1, 1, 1, 2, 2, 0,  5, 18  },
                {0, 0, 0, 1, 1, 1, 2, 1,  5, 15  },
                {0, 1, 1, 1, 1, 0, 2, 1,  6, 20  },
                {0, 1, 1, 2, 1, 1, 1, 0,  6, 21  },
                {0, 1, 1, 2, 1, 1, 2, 1,  6, 22  },
                {1, 0, 1, 1, 1, 2, 2, 1,  6, 19  }};

static        int        d[10]={ 33000,3000,1600,1200,900,800,600,400,300,200 };
int                Llevel, Lcurrent, Lnext, Lable, lx, ly, Lsum;
unsigned        Lpoint;
int                La[19][10], FLAG, sum;
unsigned        ldelay;
void        scrinit( ),datainit( ),dispb( ), eraseb( );
void        throw( ), judge( ), delayp( ), move( ), note( ), show( );
int        Ldrop( ), Ljudge( ), nextb( ), routejudge( );

main( )
{
        char        c;
        do{
                scrinit( );
                datainit( );
                Lable=nextb();
                Lable = Ldrop( );
                while( 1 ){
                        delayp( );
                        if( Lable != 0 ){
                                Ljudge( );
                                Lable = nextb( );
                        }
                        ldelay--;
                        if( ldelay == 0 ){
                                Lable = Ldrop( );
                                ldelay = d[0];
                        }
                        if( FLAG != 0 )
                          break;
                }
                while( getch( )!='\r' );
                gotoxy( 38, 16 );
                cputs( "again?" );
                c = getch( );
        }while( c!='n' && c!='N' );
        clrscr( );
}

int        nextb( )
{
        if( La[(b[Lnext].y0)][(3+b[Lnext].x0)] != 0 ||
                La[(b[Lnext].y1)][(3+b[Lnext].x1)] != 0 ||
                La[(b[Lnext].y2)][(3+b[Lnext].x2)] != 0 ||
                La[(b[Lnext].y3)][(3+b[Lnext].x3)] != 0  ){
                        FLAG = L;
                        return( -1 );
        }
        eraseb( 0, 3, 5, Lnext );
        Lcurrent = Lnext;
        lx = 3;
        ly = 0;
        Lable = 0;
        ldelay = d[0];
        Lsum ++;
        Lpoint += 1;
        Lnext = random( 23 );
        dispb( 0, 3, 5, Lnext );
        textcolor( 7 );
        gotoxy( 3, 14 );        cprintf( "%#5d", Lsum );
        gotoxy( 3, 17 );        cprintf( "%#5d", Lpoint );
        return( 0 );
}

void        delayp( )
{
 char        key;
  if( kbhit( ) != 0 ){
        key = getch( );
         move( key );
        if( key== '\\' )
                getch( );
        }
}

void        move( funckey )
char        funckey;
{
        int        tempcode;
        switch( funckey ){
        case 'K': if( lx+b[Lcurrent].x0 > 0 )
                if( La[ly+(b[Lcurrent].y0)][lx-1+(b[Lcurrent].x0)]==0 &&
                        La[ly+(b[Lcurrent].y1)][lx-1+(b[Lcurrent].x1)]==0 &&
                        La[ly+(b[Lcurrent].y2)][lx-1+(b[Lcurrent].x2)]==0 &&
                        La[ly+(b[Lcurrent].y3)][lx-1+(b[Lcurrent].x3)]==0 ){
                          eraseb( L, lx, ly, Lcurrent );
                          lx --;
                          dispb( L, lx, ly, Lcurrent );
                }
                  break;
        case 0x20: tempcode = b[Lcurrent].next;
                  if( lx+b[tempcode].x0 >= 0 && lx+b[tempcode].x3 <= 9 &&
                          ly+b[tempcode].y1 <=19 && ly+b[tempcode].y2 <= 19 )
                          if( routejudge( ) != -1 )
                                if( La[ly+(b[tempcode].y0)][lx+(b[tempcode].x0)]==0 &&
                                        La[ly+(b[tempcode].y1)][lx+(b[tempcode].x1)]==0 &&
                                        La[ly+(b[tempcode].y2)][lx+(b[tempcode].x2)]==0 &&
                                        La[ly+(b[tempcode].y3)][lx+(b[tempcode].x3)]==0 ){
                                    eraseb( L, lx, ly, Lcurrent );
                                    Lcurrent = tempcode;
                                    dispb( L, lx, ly, Lcurrent );
                                }
                          break;
        case 'M': if( lx+b[Lcurrent].x3 < 9 )
                        if( La[ly+(b[Lcurrent].y0)][lx+1+(b[Lcurrent].x0)]==0 &&
                          La[ly+(b[Lcurrent].y1)][lx+1+(b[Lcurrent].x1)]==0 &&
                          La[ly+(b[Lcurrent].y2)][lx+1+(b[Lcurrent].x2)]==0 &&
                          La[ly+(b[Lcurrent].y3)][lx+1+(b[Lcurrent].x3)]==0 ){
                          eraseb( L, lx, ly, Lcurrent );
                          lx ++;
                          dispb( L, lx, ly, Lcurrent );
                        }
                   break;
        case 'P': throw( );
                  break;
        case 0x1b:        clrscr( );
                        exit(0);
                        break;
        default:        break;
        }
}

void        throw( )
{int        tempy;
        tempy = ly;
        while(  ly+b[Lcurrent].y1 < 19 && ly+b[Lcurrent].y2 < 19 &&
                La[ly+(b[Lcurrent].y0)][lx+(b[Lcurrent].x0)]==0 &&
                La[ly+(b[Lcurrent].y1)][lx+(b[Lcurrent].x1)]==0 &&
                La[ly+(b[Lcurrent].y2)][lx+(b[Lcurrent].x2)]==0 &&
                La[ly+(b[Lcurrent].y3)][lx+(b[Lcurrent].x3)]==0 )
                ly++;
        ly--;
        eraseb( L, lx, tempy, Lcurrent );
        dispb( L, lx, ly, Lcurrent );
        La[ly+(b[Lcurrent].y0)][lx+(b[Lcurrent].x0)] =
        La[ly+(b[Lcurrent].y1)][lx+(b[Lcurrent].x1)] =
        La[ly+(b[Lcurrent].y2)][lx+(b[Lcurrent].x2)] =
        La[ly+(b[Lcurrent].y3)][lx+(b[Lcurrent].x3)] = b[Lcurrent].color;
        Lable = -1;
}

int        routejudge( )
{
  int        i, j;
  for( i=0;i<3;i++ )
        for( j=0;j<3;j++ )
             if( La[ly+i][lx+j] != 0 )
                return( -1 );
  return( 1 );
}


int        Ldrop( )
{
        if( ly+b[Lcurrent].y1 >= 18 || ly+b[Lcurrent].y2 >= 18 ){
                La[ly+(b[Lcurrent].y0)][lx+(b[Lcurrent].x0)] =
                La[ly+(b[Lcurrent].y1)][lx+(b[Lcurrent].x1)] =
                La[ly+(b[Lcurrent].y2)][lx+(b[Lcurrent].x2)] =
                La[ly+(b[Lcurrent].y3)][lx+(b[Lcurrent].x3)] = b[Lcurrent].color;
                return( -1 );
        }
        if(        La[ly+1+(b[Lcurrent].y0)][lx+(b[Lcurrent].x0)]!=0 ||
                La[ly+1+(b[Lcurrent].y1)][lx+(b[Lcurrent].x1)]!=0 ||
                La[ly+1+(b[Lcurrent].y2)][lx+(b[Lcurrent].x2)]!=0 ||
                La[ly+1+(b[Lcurrent].y3)][lx+(b[Lcurrent].x3)]!=0 ){
                        La[ly+(b[Lcurrent].y0)][lx+(b[Lcurrent].x0)]=
                        La[ly+(b[Lcurrent].y1)][lx+(b[Lcurrent].x1)]=
                        La[ly+(b[Lcurrent].y2)][lx+(b[Lcurrent].x2)]=
                        La[ly+(b[Lcurrent].y3)][lx+(b[Lcurrent].x3)]=b[Lcurrent].color;
                        return( -1 );
        }
        eraseb( L, lx, ly, Lcurrent );
        dispb( L, lx, ++ly, Lcurrent );
        return( 0 );
}


int        Ljudge(  )
{
        int        i, j, k,lines, f;
        static int        p[5]={ 0,1,3,6,10 };
        lines = 0;
        for( k=0; k<=3; k++ ){
                f = 0;
                if( (ly+k)>18 )
                  continue;
                for( i=0; i<10; i++ )
                   if( La[ly+k][i]==0 ){
                        f++;
                        break;
                  }
                if( f == 0 ){
                        movetext( LX, LY, LX+19, LY+ly+k-1, LX, LY+1 );
                        for( i=(ly+k); i>0; i-- )
                                for( j=0; j<10; j++ )
                                        La[i][j] = La[i-1][j];
                        for( j=0; j<10; j++ )
                                La[0][j] = 0;
                        lines ++;
                }
        }
        Lpoint += p[lines]*10;
        return( 0 );
}

void        scrinit( )
{
        int        i;
        char        lft[20];

        textbackground(1);
        clrscr( );
        gotoxy( 30,9 );
        cputs( "ENTER YOUR NAMES:" );
        gotoxy( 25,14 );
        scanf( "%s",lft );
        textbackground(0);
        clrscr( );
        textcolor( 15 );
        gotoxy( 17, 1 );
        cprintf( "%s",lft );
        gotoxy( 5, 3 );
        cputs( "NEXT" );
        gotoxy( 4, 13 );
        cputs( "block" );
        gotoxy( 4, 16 );
        cputs( "point" );
        for( i=0; i<19; i++ ){
                gotoxy( LX-2, LY+i );
                cputs( "??                    ??" );
        }
        gotoxy( LX-2, LY+19 );
        cputs( "????????????????????????" );

}


void        datainit( )
{
        int        i,j;
        for( i=0; i<19; i++ )
          for( j=0; j<10; j++ ){
                La[i][j]=0;
        }
        Lable = 0;
        FLAG = 0;
        ldelay = d[0];
        Lsum = 0;
        Lpoint=0;
        randomize( );
        Lnext = random( 23);
}

void        dispb( LRflag, x, y, blockcode )        /* display blocks */
int                LRflag, x, y, blockcode;
{
    int         realx, realy;
    if( LRflag == L ){
        realx = LX+x*2;
        realy = LY+y;
    }else{
        realx = x;
        realy = y;
    }
        textcolor( b[blockcode].color );
        gotoxy( realx+2*b[blockcode].x0, realy+b[blockcode].y0 ); cputs("??");
        gotoxy( realx+2*b[blockcode].x1, realy+b[blockcode].y1 ); cputs("??");
        gotoxy( realx+2*b[blockcode].x2, realy+b[blockcode].y2 ); cputs("??");
        gotoxy( realx+2*b[blockcode].x3, realy+b[blockcode].y3 ); cputs("??");
}


void        eraseb( LRflag, x, y, blockcode )  /* erase blocks */
int                LRflag, x, y, blockcode;
{
    int                realx, realy;
    if( LRflag == L ){
        realx = LX+x*2;
        realy = LY+y;
     }else{
        realx = x;
        realy = y;
     }
     textcolor( 0 );
     gotoxy( realx+2*b[blockcode].x0, realy+b[blockcode].y0 ); cputs("??");
     gotoxy( realx+2*b[blockcode].x1, realy+b[blockcode].y1 ); cputs("??");
     gotoxy( realx+2*b[blockcode].x2, realy+b[blockcode].y2 ); cputs("??");
     gotoxy( realx+2*b[blockcode].x3, realy+b[blockcode].y3 ); cputs("??");
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?