问题标题: 酷町堂:MC 2D 2.0版 代码

0
0
马博闻
马博闻
修练者
修练者

复制到DEV C++运行

以下为代码

#include <iostream>
#include <conio.h>
#include <cstdio>
#include <windows.h>
#include <cstdlib>
using namespace std;
void Color(int a){//颜色控制函数(部分代码为复制) 
    if(a==0)  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);//标准 
    if(a==1)  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_GREEN|FOREGROUND_BLUE);               //浅蓝 
    if(a==2)  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_GREEN);                               //绿
    if(a==3)  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_BLUE);                 //紫红 
    if(a==4)  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED);                                 //浅橙 
    if(a==5)  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN);                //浅黄 
    if(a==6)  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_BLUE);                                //蓝 
    if(a==7)  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);                     //浅灰 
    if(a==8)  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED);                                                      //红
    if(a==9)  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY|BACKGROUND_GREEN|BACKGROUND_BLUE);               //浅蓝  外 
    if(a==10) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY|BACKGROUND_RED|BACKGROUND_BLUE);                 //紫红  外 
    if(a==11) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED|FOREGROUND_BLUE);                                      //深紫 
    if(a==12) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED|FOREGROUND_GREEN);                                     //棕黄 
    if(a==13) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY);                                                //深灰 
    if(a==14) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN|FOREGROUND_BLUE);                                    //较浅蓝 
    return;
}
struct map_s{
    int flag;
    string s;
}map[105][505],map1[105][505],map2[105][505];
struct pla_s{
    int x,y;
}pl;
bool in(){//读入存档 
    freopen("MC-2D.txt","r",stdin);
    bool In=0;
    cin>>In;
    if(In==0){
        fclose(stdin);
        return 1;
    }
    string s;
    for(int i=1;i<=100;i++){
        for(int j=1;j<=500;j++){
            cin>>map1[i][j].s;
            if(map1[i][j].s=="66")map1[i][j].s="  "; 
//          cout<<map1[i][j].s;
        }
    }
    fclose(stdin);
    return 0;
}
void out(map_s map[105][505]){//存档 
    freopen("MC-2D.txt","w",stdout);
    cout<<1<<endl; 
    for(int i=1;i<=100;i++){
        for(int j=1;j<=500;j++){
            if(map1[i][j].s=="  ")cout<<"66 "; 
            else                  cout<<map1[i][j].s<<" ";
        }
        cout<<endl;
    }
    return;
}
void clone(int flag){
    for(int i=1;i<=100;i++){
        for(int j=1;j<=500;j++){
            if(flag==1)map[i][j]=map1[i][j];
            if(flag==2)map1[i][j]=map[i][j];
        }
    }
    return;
}
void menu2(int c){//开始界面 
    system("cls");
    string s1,s2,s3;
    if(c==1)s1=" <<";
    if(c==2)s2=" <<";
    if(c==3)s3=" <<";
    Color(14);cout<<"=============="<<endl<<"|  ";
    Color(0 );cout<<"开始游戏";
    Color(14);cout<<"  |";
    Color(1 );cout<<s1<<endl;
    Color(14);cout<<"=============="<<endl<<endl<<"=============="<<endl<<"|  ";
    Color(0 );cout<<"继续游戏";
    Color(14);cout<<"  |";
    Color(1) ;cout<<s2<<endl;
    Color(14);cout<<"=============="<<endl<<endl<<"=============="<<endl<<"|  ";
    Color(0 );cout<<"保存游戏";
    Color(14);cout<<"  |";
    Color(1) ;cout<<s3<<endl;
    Color(14);cout<<"==============";
    return; 
}
string blo[100]={"","##","&&","--","//","\\\\","||","%%"," a","#t","#j","#z","TT"};
int blo_cnt=1;
string now_blo="##";
void CSH(){
    pl.x=79;
    pl.y=11;
    for(int i=1;i<=100;i++){
        for(int j=1;j<=500;j++){
            if(i<80)     map[i][j].s="  ";
            else if(i<85)    map[i][j].s="&&";
            else            map[i][j].s="##";
            if(i<80)     map2[i][j].s="  ";
            else if(i<85)    map2[i][j].s="&&";
            else            map2[i][j].s="##";
        }
    }
}
void SC(){
    system("cls");
    Color(5);
    for(int i=1;i<=22;i++)cout<<"++";
    cout<<endl;
    for(int i=pl.x-10;i<=pl.x+10;i++){
        Color(5);
        cout<<"+";
        for(int j=pl.y-10;j<=pl.y+10;j++){
            if(i<=0||j<=0||i>=100||j>=500)continue;
            if(i==pl.x&&j==pl.y){
                Color(0);
                cout<<"Yo";
            }else{
                if(map[i][j].s=="&&")Color(12);
                if(map[i][j].s=="--")Color(6);
                if(map[i][j].s=="%%")Color(2);
                if(map[i][j].s==" a")Color(8);
                if(map[i][j].s=="TT")Color(8);
                if(map[i][j].s[0]=='#')Color(13);
                if(map[i][j].s=="BO"||map[i][j].s=="OM")Color(7);
                if(map[i][j].s=="||"||map[i][j].s=="//"||map[i][j].s=="\\\\")Color(4);
                cout<<map[i][j].s;
            }
        }
        Color(5);
        cout<<"+";
        cout<<endl;
    }
    Color(5);
    for(int i=1;i<=22;i++)cout<<"++";
    Color(0);
    cout<<endl<<"Your Block:";
    if(now_blo=="&&")Color(12);
    if(now_blo=="--")Color(6);
    if(now_blo=="%%")Color(2);
    if(now_blo==" a")Color(8);
    if(now_blo=="TT")Color(8);
    if(now_blo[0]=='#')Color(13);
    if(now_blo=="BO"||now_blo=="OM")Color(7);
    if(now_blo=="||"||now_blo=="//"||now_blo=="\\\\")Color(4);
    cout<<now_blo;
    return;
}
void no_boom(){
    for(int i=pl.x-10;i<=pl.x+10;i++){
        for(int j=pl.y-10;j<=pl.y+10;j++){
            if(map[i][j].s=="BO"||map[i][j].s=="OM"){
                map[i][j].s="  ";
            }
        }
    }
    return;
}
void game(){
    SC();
    while(1){
        char c=getch();
        if(c=='w'&&pl.x>11)pl.x--;
        if(c=='s'&&pl.x<89)pl.x++;
        if(c=='a'&&pl.y>11)pl.y--;
        if(c=='d'&&pl.y<489)pl.y++;
        if(map[pl.x][pl.y].s=="TT"){
            int l=pl.y,r=pl.y;
            for(int i=pl.x-2;i<=pl.x+2;i++){
                for(int j=pl.y-2;j<=pl.y+2;j++){
                    bool f=0;
                    if(rand()%3==1){
                        if(j==pl.y-2||j==pl.y+1){
                            map[i][j].s="BO";
                            map[i][j+1].s="OM";
                            f=1;
                        }
                    }
                    if(!f&&map[i][j-1].s!="BO")map[i][j].s="  ";
                }
            }
            int xb=rand()%10-5;
            int yb=rand()%10-5;
            if(xb<0&&pl.x>11+xb||xb==0||xb>0&&pl.x<89-xb) pl.x+=xb;
            if(yb<0&&pl.y>11+yb||yb==0||yb>0&&pl.y<489-yb)pl.y+=yb;
        }
        if(c=='t')map[pl.x][pl.y].s=now_blo;
        if(c=='y')map[pl.x][pl.y].s="  ";
        if(c=='h'&&blo_cnt<12)blo_cnt++;
        if(c=='g'&&blo_cnt>1)blo_cnt--;
        if(c=='p')return;
        now_blo=blo[blo_cnt];
        SC();
        no_boom();
    }
}
void game_wel(){
    int cho=1; 
    while(true){
        menu2(cho);
        char c=getch();
        if(c=='s'&&cho<3)cho++;
        if(c=='w'&&cho>1)cho--;
        if(c=='a'){
            if(cho==1)  game();
            if(cho==2){ clone(1);game();clone(2);}
            if(cho==3){ out(map1);return;}
        }
    }
} 
int main(){
    if(in()==1){
        for(int i=1;i<=100;i++){
            for(int j=1;j<=500;j++){
                if(i<80)     map1[i][j].s="  ";
                else if(i<85)    map1[i][j].s="&&";
                else            map1[i][j].s="##";
            }
        }
    }
    CSH();
    game_wel();
    return 0;
}

部分游戏截图:


0
0
0
0
马博闻
马博闻
修练者
修练者

                                   游戏教程
按动"w","s","a","d"键移动
按动"t","h"键放置/破坏方块
按动"w","s"键在主界面移动,按动"a"键进入选中的选项
按动"p"键返回主界面
TNT块会在你第2次碰到他时爆
注意:游戏中必须使用英文输入法
       只有用”保存游戏“退出才能存档

我要回答