俄罗斯方块

#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
using namespace std;
#define Y 22
#define X 14
#define WHITE 7
#define WAIT_TIME 250
bool Map[Y][X];
int z[6][4][2],which,_long[6],zx,zy;
char c;
int df=0;
void color(int a);
void gotoxy(int x,int y);
void csh();
void start();
void drawmap();
bool candown();
void drawz();
void clsz();
void change();
void turn(string s);
void _cin();
void printdf();
bool gameover();
int main(){
    start();
    drawmap();
    for(int i=0;i<Y;i++){
        gotoxy(i,X*2+2);
        for(int j=0;j<X;j++)
            printf("%d",Map[i][j]);
        printf("\n");
    }
    while(true){
        while(true){
            df++;
            zy++;
            if(!candown())
                break;
            zy--;
            clsz();
            zy++;
            drawz();
            _cin();
            if(!candown())
                break;
            printdf();
            _sleep(WAIT_TIME);
        }
        change();
        if(gameover()){
            system("cls");
            printf("游戏结束!\n");
            printf("最终得分:%d",df/4);
            return 0; 
        }
        which=rand()%6;
        csh();
        color(WHITE);
        for(int i=0;i<Y;i++){
            gotoxy(i,X*2+2);
            for(int j=0;j<X;j++)
                printf("%d",Map[i][j]);
            printf("\n");
        }
    }
    return 0;
}
void color(int a){
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
void gotoxy(int i,int j){
    COORD position={j,i};
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),position);
}
void csh(){
    bool _bool[6][3][3]={{{1,0,0},
                          {1,0,0},
                          {1,1,0}},
                          
                         {{0,1,0},
                          {0,1,0},
                          {1,1,0}},

                         {{0,1,1},
                          {1,1,0},
                          {0,0,0}},

                         {{0,1,0},
                          {0,1,0},
                          {0,1,0}},

                         {{0,1,0},
                          {1,1,1},
                          {0,0,0}},

                         {{1,0,0},
                          {1,1,0},
                          {0,0,0}}};
    for(int sw=0;sw<6;sw++)
        _long[sw]=-1;
    for(int sw=0;sw<6;sw++)
        for(int y=0;y<3;y++)
            for(int x=0;x<3;x++)
                if(_bool[sw][y][x]){
                    _long[sw]++;
                    z[sw][_long[sw]][0]=y;
                    z[sw][_long[sw]][1]=x;
                }
    zx=X/2-2;
    zy=-2;
    return;
}
void start(){
    MessageBox(NULL,"按a键左移,按d键右移,w,s键旋转","游戏规则:",MB_OK);
    printf("游戏加载中……");
    srand(time(NULL));
    CONSOLE_CURSOR_INFO cursor_info={1,0};
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
    for(int y=0;y<Y;y++)
        for(int x=0;x<X;x++)
            Map[y][x]=false;
    for(int y=0;y<Y;y++)
        Map[y][0]=Map[y][X-1]=true;
    for(int x=0;x<X;x++)
        Map[0][x]=Map[Y-1][x]=true;
    which=0;
    csh();
    return;
}
void drawmap(){
    gotoxy(0,0);
    color(WHITE);
    for(int y=0;y<Y;y++){
        for(int x=0;x<X;x++){
            if(Map[y][x])    printf("■");
            else    printf("  ");
        }
        printf("\n");
    }
    for(int x=1;x<X-1;x++)
        Map[0][x]=false;
    return;
}
bool candown(){
    for(int l=0;l<=_long[which];l++)
        if(Map[z[which][l][0]+zy+1][z[which][l][1]+zx+1]&&z[which][l][0]+zy>=0)
            return false;
    return true;
}
void drawz(){
    color(which+1);
    for(int l=0;l<=_long[which];l++){
        if(z[which][l][0]+zy>-1){
            gotoxy(z[which][l][0]+zy+1,(z[which][l][1]+zx+1)*2);
            printf("■");
        }
    }
    return;
}
void clsz(){
    for(int l=0;l<=_long[which];l++){
        if(z[which][l][0]+zy>-1){
            gotoxy(z[which][l][0]+zy+1,(z[which][l][1]+zx+1)*2);
            printf("  ");
        }
    }
    return;
}
void change(){
    for(int l=0;l<=_long[which];l++)
        if(z[which][l][0]+zy>-1)
            Map[z[which][l][0]+zy][z[which][l][1]+zx+1]=true;
    bool s;
    for(int y=1;y<Y-1;y++){
        s=true;
        for(int x=1;x<X-1;x++)
            s=(s&&Map[y][x]);
        if(s==true){
            for(int i=y;i>0;i--)
                for(int x=1;x<X-1;x++)
                    Map[i][x]=Map[i-1][x];
            for(int x=1;x<X-1;x++)
                Map[0][x]=true;
            drawmap();
        }
    }
    return;
}
void turn(string s){
    if(s=="left"){
        for(int l=0;l<=_long[which];l++){
            int y=z[which][l][0],x=z[which][l][1];
            z[which][l][0]=2-x;z[which][l][1]=y;
        }
    }
    if(s=="right"){
        for(int l=0;l<=_long[which];l++){
            int y=z[which][l][0],x=z[which][l][1];
            z[which][l][0]=x;z[which][l][1]=2-y;
        }
    }
    return;
}
void _cin(){
    if(kbhit()!=0){
        while(kbhit()!=0)
            c=getch();
        switch(c){
            case 'a':case 'A':{
                zx--;
                if(candown()){
                    zx++;
                    clsz();
                    zx--;
                    drawz();
                }else
                    zx++;
                break;
            }
            case 'd':case 'D':{
                zx++;
                if(candown()){
                    zx--;
                    clsz();
                    zx++;
                    drawz();
                }else
                    zx--;
                break;
            }
            case 'w':case 'W':{
                turn("left");
                if(!candown())
                    turn("right");
                else{
                    turn("right");
                    clsz();
                    turn("left");
                    drawz();
                }
                break;
            }
            case 's':case 'S':{
                turn("right");
                if(!candown())
                    turn("left");
                else{
                    turn("left");
                    clsz();
                    turn("right");
                    drawz();
                }
                break;
            }
            default:break;
        }
    }
    return;
}
void printdf(){
    gotoxy(Y,0);
    color(WHITE);
    printf("得分:%d",df/4);
    return;
}
bool gameover(){
    for(int x=1;x<X-1;x++)
        if(Map[0][x])
            return true;
    return false;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值