俄罗斯方块C语言修改版  - …

[转载]俄罗斯方块C语言修改版 <wbr> <wbr>- <wbr>暑假集训
 
 
[转载]俄罗斯方块C语言修改版 <wbr> <wbr>- <wbr>暑假集训

 
[转载]俄罗斯方块C语言修改版 <wbr> <wbr>- <wbr>暑假集训

#include<stdio.h>
#include<windows.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
#define CUBE_R 20 //行
#define CUBE_L 11 //列
#define MOD 28

int cur_x , cur_y , SPEED = 300;
int score , next , cube , cube_color , next_color , map [ CUBE_R + 10 ][ CUBE_L + 10 ], color_map [ CUBE_R + 10 ][ CUBE_L + 10 ];
int GameSpeed = SPEED , system_color , cheat;

int dir [ 28 ][ 4 ][ 2 ] = { //七种方块共28种变形,用x y偏移量表示
    { 0 , 0 , 0 , 1 , 1 , 0 , 1 , 1 }   ,{ 0 , 0 , 0 , 1 , 1 , 0 , 1 , 1 }   ,{ 0 , 0 , 0 , 1 , 1 , 0 , 1 , 1 }   ,{ 0 , 0 , 0 , 1 , 1 , 0 , 1 , 1 }   ,
    { 0 , 0 , 0 , - 1 , 0 , 1 , 1 , 1 } ,{ 0 , 0 , - 1 , 0 , 1 , 0 , 1 , - 1 } ,{ 0 , 0 , 0 , - 1 , 0 , 1 , - 1 , - 1 },{ 0 , 0 , - 1 , 0 , 1 , 0 , - 1 , 1 } ,
    { 0 , 0 , 0 , - 1 , 0 , 1 , 1 , - 1 },{ 0 , 0 , - 1 , 0 , - 1 , - 1 , 1 , 0 },{ 0 , 0 , 0 , - 1 , 0 , 1 , - 1 , 1 } ,{ 0 , 0 , - 1 , 0 , 1 , 0 , 1 , 1 }   ,
    { 0 , 0 , 0 , - 1 , 0 , 1 , 0 , 2 } ,{ 0 , 0 , - 1 , 0 , 1 , 0 , 2 , 0 }   ,{ 0 , 0 , 0 , - 1 , 0 , 1 , 0 , 2 }   ,{ 0 , 0 , - 1 , 0 , 1 , 0 , 2 , 0 }   ,
    { 0 , 0 , 0 , - 1 , 1 , 0 , 1 , 1 } ,{ 0 , 0 , - 1 , 0 , 0 , - 1 , 1 , - 1 },{ 0 , 0 , 0 , - 1 , 1 , 0 , 1 , 1 }   ,{ 0 , 0 , - 1 , 0 , 0 , - 1 , 1 , - 1 },
    { 0 , 0 , 0 , 1 , 1 , 0 , 1 , - 1 } ,{ 0 , 0 , - 1 , 0 , 0 , 1 , 1 , 1 }   ,{ 0 , 0 , 0 , 1 , 1 , 0 , 1 , - 1 }   ,{ 0 , 0 , - 1 , 0 , 0 , 1 , 1 , 1 }   ,
    { 0 , 0 , 0 , - 1 , 0 , 1 , - 1 , 0 },{ 0 , 0 , 0 , 1 , - 1 , 0 , 1 , 0 }   ,{ 0 , 0 , 0 , - 1 , 0 , 1 , 1 , 0 }   ,{ 0 , 0 , - 1 , 0 , 1 , 0 , 0 , - 1 } ,
};

int color [ 6 ] = { FOREGROUND_RED , FOREGROUND_BLUE , FOREGROUND_GREEN ,
    FOREGROUND_RED   | FOREGROUND_GREEN , //黄色
    FOREGROUND_GREEN | FOREGROUND_BLUE ,   //青色
    FOREGROUND_RED   | FOREGROUND_BLUE ,   //深红色
};

void set_Color( int a)
{
    SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), FOREGROUND_INTENSITY | color [ a ]);
}

void gotoxy( int x , int y) //定位输出的函数,照抄即可
{
    COORD c;
    c . X = x - 1; c . Y = y - 1;
    SetConsoleCursorPosition ( GetStdHandle( STD_OUTPUT_HANDLE ), c);
}

void Move( int x , int y , int id , int state) //移动或者消除方块
{
    int i;
    for( i = 0; i < 4; i ++)
    {
        x = cur_x + dir [ id ][ i ][ 0 ];
        y = cur_y + dir [ id ][ i ][ 1 ];
        if( x > 0 && y > 0 && x < CUBE_R && y < CUBE_L)
        {
            gotoxy( y * 2 + 1 , x + 1);
            if( state) printf( "■");
            else printf( "  ");
        }
    }
}

void Clear_Show_next( int x , int y , int id , int state) //显示或者消除下一个方块
{
    int i , a ,b;
    a = x;b = y;
    for( i = 0; i < 4; i ++)
    {
        x = a + dir [ id ][ i ][ 0 ];
        y =b + dir [ id ][ i ][ 1 ];
        gotoxy( y * 2 + 1 , x + 1);
        if( state) printf( "■");
        else printf( "  ");
    }
}

void Add_Cube( int x , int y , int id)
{
    int i;
    for( i = 0; i < 4; i ++)
    {
        x = cur_x + dir [ id ][ i ][ 0 ];
        y = cur_y + dir [ id ][ i ][ 1 ];
        if( x > 0 && y > 0 && x < CUBE_R && y < CUBE_L)
        {
            map [ x ][ y ] = 1;
            color_map [ x ][ y ] = cube_color;
        }
    }
}

int Judge( int x , int y , int id) //判断越界或者冲突
{
    int i , a ,b;
    a = x;b = y;
    for( i = 0; i < 4; i ++)
    {
        x = a + dir [ id ][ i ][ 0 ];
        y =b + dir [ id ][ i ][ 1 ];
        if( x > 0 && y > 0 && x < CUBE_R && y < CUBE_L)
        {
            if( map [ x ][ y ]) return 0;
        }
        else if( !( x <= 0)) return 0;
    }
    return 1;
}
void SHOW_HELP()
{
    gotoxy( CUBE_L * 2 + 10 , 14);
    printf( "*帮助*");
    gotoxy( CUBE_L * 2 + 6 , 16);
    printf( " a:左 d:右");
    gotoxy( CUBE_L * 2 + 6 , 17);
    printf( " s:下 w:变形");
    gotoxy( CUBE_L * 2 + 6 , 19);
    printf( " [:加速 ]:减速");
}

void SHOW_SCORE()
{
    set_Color( system_color);
    gotoxy( CUBE_L * 2 + 14 , 10);
    printf( "%d  " , score);
    set_Color( cube_color);
}

void SHOW_SPEED()
{
    set_Color( system_color);
    gotoxy( CUBE_L * 2 + 14 , 12);
    printf( "%d  " , SPEED);
    set_Color( cube_color);
}

void init( int id)
{
    int i , j;
    score = 0; cheat =- 1;
    cur_x = 0; cur_y = CUBE_L / 2;
    memset( map , 0 , sizeof( map));
    memset( color_map , 0 , sizeof( color_map));
    gotoxy( 1 , 1);
    for( i = 0; i <= CUBE_R; i ++)
    {
        for( j = 0; j <= CUBE_L; j ++)
        {
            if( i == 0 && j == 0) printf( "╔");
            else if( i == 0 && j == CUBE_L) printf( "╗");
            else if( i == CUBE_R && j == 0) printf( "╚");
            else if( i == CUBE_R && j == CUBE_L) printf( "╝");
            else if( i == 0 || i == CUBE_R) printf( "==");
            else if( j == 0 || j == CUBE_L) printf( "‖");
            else printf( "  ");
        }
        if( i == 0) printf( " ╔==============╗");
        else if( i == CUBE_R) printf( " ╚==============╝");
        else if( i == 13) printf( " ╠==============╣");
        else if( i == 1) printf( " ‖  Next Cube:");
        else if( i == 9) printf( " ‖  Score:    ");
        else if( i == 11) printf( " ‖  Speed:    ");
        else printf( " ‖");
        if( i > 0 && i < CUBE_R && i != 13)
        {
            gotoxy( CUBE_L * 2 + 20 , i + 1);
            printf( "‖");
        }
        printf( " n ");
    }
    SHOW_HELP();
    SHOW_SCORE();
    SHOW_SPEED();
    set_Color( cube_color);
    Move( cur_x , cur_y , id , 1);
    set_Color( next_color);
    Clear_Show_next( 5 , CUBE_L + 5 , next , 1);
}

int Is_GameOver()
{
    int i , j , t;
    for( i = 1; i < CUBE_L; i ++)
        if( map [ 1 ][ i ] != 0) break;
        if( i < CUBE_L)
        {
            set_Color( system_color);
            for( t = 1; t <= 5; t ++)
            {
                Sleep( 100);
                for( i = 1; i < CUBE_R; i ++)
                    for( j = 1; j < CUBE_L; j ++)
                    {
                        gotoxy( j * 2 + 1 , i + 1);
                        if( t % 2) printf( "■");
                        else printf( "  ");
                    }
            }
            gotoxy( CUBE_L - 4 , CUBE_R / 2 + 1);
            printf( " GAME OVER! ");
            gotoxy( 1 , CUBE_R + 2);
            return 1;
        }
        return 0;
}

void Relode_Score() //消行加分
{
    int i , j , k , t;
    for( i = 1; i < CUBE_R; i ++)
    {
        for( j = 1; j < CUBE_L; j ++)
            if( ! map [ i ][ j ]) break;
            if( j >= CUBE_L)
            {
                for( t = 1; t <= 5; t ++)
                {
                    Sleep( 100);
                gotoxy( 3 , i + 1);
                for( j = 1; j < CUBE_L; j ++)
                    if( t % 2) printf( "  ");
                    else printf( "■");
                }
                score ++;
                SHOW_SCORE();
                for( k = i; k >= 1; k --)
                    for( j = 1; j < CUBE_L; j ++)
                    {
                        gotoxy( 2 * j + 1 , k + 1);
                        if( map [ k - 1 ][ j ])
                        {
                            set_Color( color_map [ k - 1 ][ j ]);
                            printf( "■");
                        }
                        else printf( "  ");
                        map [ k ][ j ] = map [ k - 1 ][ j ];
                        color_map [ k ][ j ] = color_map [ k - 1 ][ j ];
                    }
            }
    }
}

void HideCursor() //消除光标闪烁
{
CONSOLE_CURSOR_INFO cursor_info = { 1 , 0 };
SetConsoleCursorInfo( GetStdHandle( STD_OUTPUT_HANDLE ), & cursor_info);
}


int main()
{
    int Ask_Cube = 0;
    int finish = 0;
    char form [ 50 ];
    sprintf( form , "mode con cols=%d lines=%d" , CUBE_L * 2 + 22 , CUBE_R + 4);
    char key;
    system( form); //宽:CUBE_L*2+23,高:CUBE_R+3
    system( "color E");
    srand( time( NULL));
    system_color = rand() % 6;
    cube =( rand() + MOD) % MOD;
    cube_color = rand() % 6;
    next =( rand() + MOD) % MOD;
    next_color = rand() % 6;
    set_Color( system_color);
    init( cube);
    set_Color( cube_color);
    HideCursor();
    while( 1)
    {
RE:         if( Ask_Cube)
        {
            Ask_Cube = 0;
            cube = next; //取方块
            cube_color = next_color;
            next =( rand() + MOD) % MOD; //取下一个方块
            next_color = rand() % 6;
            set_Color( next_color);
            Clear_Show_next( 5 , CUBE_L + 5 , cube , 0);
            Clear_Show_next( 5 , CUBE_L + 5 , next , 1);
            set_Color( cube_color);
            cur_x = 0; cur_y = CUBE_L / 2;
        }
        while( ! kbhit()) //判断是否有键盘操作,有返回非0,没有返回0
        {
            Sleep( 1);
            finish ++;
            if( finish < SPEED) continue;
            else finish = 0;
            if( Is_GameOver()) return 0;
            if( Judge( cur_x + 1 , cur_y , cube))
            {
               
                Move( cur_x , cur_y , cube , 0); //值为0表示清除方块
                cur_x ++;
                Move( cur_x , cur_y , cube , 1); //为1表示移动方块
            }
            else
            {
                Add_Cube( cur_x , cur_y , cube);
                Relode_Score();
                Ask_Cube = 1;
                goto RE;
            }
        }
       
        key = getch();
       
        if( key == 'w' )
        {
            int id =( cube / 4) * 4 +(( cube + 1) % 4);
            if( Judge( cur_x , cur_y , id))
            {
                Move( cur_x , cur_y , cube , 0);
                cube = id;
                Move( cur_x , cur_y , cube , 1);
            }
        }
        else if( key == 's' ){
            while( Judge( cur_x + 1 , cur_y , cube))
            {
            Move( cur_x , cur_y , cube , 0);
            cur_x ++;
            Move( cur_x , cur_y , cube , 1);
            }
        } //下
        else if( key == 'a' && Judge( cur_x , cur_y - 1 , cube )){ Move( cur_x , cur_y , cube , 0); cur_y --; Move( cur_x , cur_y , cube , 1 );} //左
        else if( key == 'd' && Judge( cur_x , cur_y + 1 , cube )){ Move( cur_x , cur_y , cube , 0); cur_y ++; Move( cur_x , cur_y , cube , 1 );} //右
        else if( key == 27 ){ gotoxy( 1 , CUBE_R + 2); exit( 0 );}
        else if( key >= '1' && key <= '7' ){
            set_Color( next_color);
            Clear_Show_next( 5 , CUBE_L + 5 , next , 0);
            next =( key - '1') * 4;
            Clear_Show_next( 5 , CUBE_L + 5 , next , 1);
            set_Color( system_color);
            gotoxy( 1 , CUBE_R + 2);
            printf( "Cheat Mode Open!                    ");
            set_Color( cube_color);
        }
        else if( key == '[' && SPEED > 10 ){ SPEED -= 10;
                        SHOW_SPEED();
        }
        else if( key == ']' ){ SPEED += 10;
                        SHOW_SPEED();
        }
        else if( key == 32)
        {
            gotoxy( 1 , CUBE_R + 2);
            printf( "Now Pause,Press Blank Key To Continue!     ");
            while( 1)
            {
                while( ! kbhit());
                key = getch();
                if( key == 32) break;
            }
            gotoxy( 1 , CUBE_R + 2);
            printf( "Please Have Fun!                           ");
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值