俄罗斯方块 -- 暑假集训

 
自己写的俄罗斯方块
a s d w 控制左下右和变形,支持改变速度和图形大小(在代码中改一下15 16 17行即可)

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

int cur_x , cur_y;
int score , next , cube , map [ CUBE_R + 10 ][ CUBE_L + 10 ];
int GameSpeed = SPEED;

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 } ,
};

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;
    }
}

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 init( int id)
{
    int i , j;
    score = 0;
    cur_x = 0; cur_y = CUBE_L / 2;
    memset( map , 0 , sizeof( 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 == 1) printf( "    Next Cube:");
        else if( i == 11) printf( "    Score:    ");
        else if( i == 14) printf( "    Speed:    ");
        printf( " \n ");
    }
    gotoxy( CUBE_L * 2 + 14 , 12);
    printf( "%d" , score);
    gotoxy( CUBE_L * 2 + 14 , 15);
    printf( "%d" , SPEED);
    Move( cur_x , cur_y , id , 1);
    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)
        {
            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 ++;
                gotoxy( CUBE_L * 2 + 14 , 12);
                printf( "%d" , 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 ])
                            printf( "■");
                        else printf( "  ");
                        map [ k ][ j ] = map [ k - 1 ][ j ];
                    }
            }
    }
}

int main()
{
    int Ask_Cube = 0;
    int finish = 0;
    char key;
    srand( time( NULL));
    cube =( rand() + MOD) % MOD;
    next =( rand() + MOD) % MOD;
   
    init( cube);
   
    while( 1)
    {
RE:         if( Ask_Cube)
        {
            Ask_Cube = 0;
            cube = next; //取方块
            next =( rand() + MOD) % MOD; //取下一个方块
            Clear_Show_next( 5 , CUBE_L + 5 , cube , 0);
            Clear_Show_next( 5 , CUBE_L + 5 , next , 1);
            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' && 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 + 1); exit( 0 );}
        else if( key == 32)
        {
            while( 1)
            {
                while( ! kbhit());
                key = getch();
                if( key == 32) break;
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值