C++版小黑屋

· · 个人记录

XHW

游玩前注意

1、运行源代码后将自动生成可执行文件(.exe)与存档文件(.txt),建议将源码放置于单个新建文件夹中,避免文件混乱

2、本游戏仅代码为原创,玩法思路非原创,原版位于页面底端;本游戏添加作弊模式,于源码14行Root变量值0修改为1可开启,开启后按p开关全视野(爽

3、本游戏地图内藏一彩蛋 报错为正常

4、本游戏目前玩法非最终玩法 欢迎催更(

源码部分

#include <bits/stdc++.h>
#include <windows.h>
#include "conio.h"
using namespace std;

//游戏数据 
const int m=50,n=50;
int max_water=60,max_meat=40,water_wasted=1,meat_wasted=2;//x步一滴水,y步一块肉 
int building_n=20;
int alien_alloy=0;
char self_like='@';
int water=max_water*water_wasted;
int meat=max_meat*meat_wasted;
bool isCheat=0,Root=0; //作弊者模式与作弊标志变量 
bool isLoad,isError=0;

int cx=m/2,cy=n/2,self_x=cx,self_y=cy;
bool isFirst=1,isEvent=0;

char Map[m+2][n+2]={ //地图 
    ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,",
    ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,",
    ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,6,,",
    ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,",
    ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,",
    ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,",
    ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,",
    ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,",
    ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,",
    ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,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,,,",
    ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,",
    ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,",
    ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,",
    ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,",
    ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,"
};
bool isSeemed[m+2][n+2];
bool isBuilt[m+2][m+2]={0};
void Save(){
    ofstream out;
    out.open("save.txt");
    out << max_water << " " << max_meat << " " << water_wasted << " " << meat_wasted << " ";
    out << building_n << " " << self_like << " " << water << " " << meat << " " << isCheat << " " << Root;
    out << endl << self_x << " " << self_y <<endl;
    for(int i=0;i<=m;i++){
        for(int j=0;j<=n;j++){
            out << Map[i][j] << " "; 
        }
        out << endl;
    } 
    out << endl;
    for(int i=0;i<=m;i++){
        for(int j=0;j<=n;j++){
            out << isSeemed[i][j] << " "; 
        }
        out << endl;
    } 
    out << endl;
    for(int i=0;i<=m;i++){
        for(int j=0;j<=n;j++){
            out << isBuilt[i][j] << " "; 
        }
        out << endl;
    } 
    out << endl;
    out.close(); 
    return;
}
void Load(){
    ifstream in;
    in.open("save.txt");
    in >> max_water >> max_meat >> water_wasted >> meat_wasted;
    in >> building_n >> self_like >> water >> meat >> isCheat >> Root;
    in >> self_x >> self_y;
    for(int i=0;i<=m;i++){
        for(int j=0;j<=n;j++){
            in >> Map[i][j]; 
            if(Map[i][j]=='p') Map[i][j]='P';
        }
    } 
    for(int i=0;i<=m;i++){
        for(int j=0;j<=n;j++){
            in >> isSeemed[i][j]; 
        }
    } 
    for(int i=0;i<=m;i++){
        for(int j=0;j<=n;j++){
            in >> isBuilt[i][j]; 
        }
    } 
    if(in.fail()){
        cout << endl << "存档读入有误!请手动重启游戏"; 
        isError=1;
    }
    in.close(); 
    return;
}

void gotoxy(int x,int y){
    COORD pos = {x,y};
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(hOut,pos);
}
void cls(){
    gotoxy(0,0);
    /* 设想错误 
    for(int i=1;i<=100;i++){
        for(int j=1;j<=100;j++){
            cout << " ";
        }
        cout << endl;
    } 
    */
    gotoxy(0,0);
}
int Random(int a,int b){
    int ran;
    ran = rand() % (b-a) + a;
    return ran;
}
void Setcolor(int ForgC,int BackC){
    WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),wColor);
}
void Create_buildings(char type){ //随机建造建筑

    LARGE_INTEGER t1,t2,tc; //微秒级计时器 
    QueryPerformanceFrequency(&tc);
    QueryPerformanceCounter(&t1);
    Sleep(1);
    QueryPerformanceCounter(&t2);
    srand((t2.QuadPart - t1.QuadPart)*100000/tc.QuadPart);

    int x=Random(0,50),y=Random(0,50);
    if(x!=cx&&y!=cy&&!isBuilt[y][x]){
        Map[y][x]=type;
        isBuilt[y][x]=1;
        return;
    }
    else Create_buildings(type);
} 
void See(){ //正常视野 
    isSeemed[cy][cx]=1;
    if(self_y>=0&&self_y<=n&&self_x>=0&&self_x<=m)isSeemed[self_y][self_x]=1;

    if(self_y>=0&&self_y<n&&self_x>=0&&self_x<=m)isSeemed[self_y+1][self_x]=1;
    if(self_y>0&&self_y<=n&&self_x>=0&&self_x<=m)isSeemed[self_y-1][self_x]=1;
    if(self_y>=0&&self_y<=n&&self_x>=0&&self_x<m)isSeemed[self_y][self_x+1]=1;
    if(self_y>=0&&self_y<=n&&self_x>0&&self_x<=m)isSeemed[self_y][self_x-1]=1;

    if(self_y>=0&&self_y<n-1&&self_x>=0&&self_x<=m)isSeemed[self_y+2][self_x]=1;
    if(self_y>1&&self_y<=n&&self_x>=0&&self_x<=m)isSeemed[self_y-2][self_x]=1;
    if(self_y>=0&&self_y<=n&&self_x>=0&&self_x<m-1)isSeemed[self_y][self_x+2]=1;
    if(self_y>=0&&self_y<=n&&self_x>1&&self_x<=m)isSeemed[self_y][self_x-2]=1;

    if(self_y>=0&&self_y<n&&self_x>=0&&self_x<m)isSeemed[self_y+1][self_x+1]=1;
    if(self_y>=0&&self_y<n&&self_x>0&&self_x<=m)isSeemed[self_y+1][self_x-1]=1;
    if(self_y>0&&self_y<=n&&self_x>=0&&self_x<m)isSeemed[self_y-1][self_x+1]=1;
    if(self_y>0&&self_y<=n&&self_x>0&&self_x<=m)isSeemed[self_y-1][self_x-1]=1;

}
void Draw(){ //画面框架 
    cls();
    See();
    cout << "                                         C++ XHW Game by 下十点半" << endl; 
    cout << "Water:" << water/water_wasted << "                              " <<endl; //水 
    cout << "Meat:" << meat/meat_wasted << "                          " << endl; //肉 
    cout << "Position: " << "X:" << self_x << " " << "Y:" << self_y << "            " << endl; //坐标 
    cout << endl << "Map:" << endl;

    cout << "+-"; //地图 
    for(int i=0;i<m;i++){
        cout << "--";
    }
    cout << "-+" << endl;

    for(int j = 0;j <= n;j++){
        cout << "|";
        for(int i = 0;i <= m;i++){
            if(i == self_x && j == self_y){
                cout << self_like << " ";
            }
            else if(isSeemed[j][i]||isCheat){
                if(Map[j][i]>='a'&&Map[j][i]<='z'){
                    Setcolor(8,0);
                }
                else{
                    Setcolor(15,0);
                }
                cout << Map[j][i] << " ";
                Setcolor(15,0);
            }
            else{
                cout << " " << " "; 
            }

        }
        cout << "|" << endl;
    }

    cout << "+-"; 
    for(int i=0;i<m;i++){
        cout << "--";
    }
    cout << "-+" << endl;


}
void Move(char dir){ //移动逻辑 
    if((dir==75||dir=='a')&&self_x>0){
        self_x--;
        isFirst=0;
        water--;
        meat--;
    }
    if((dir==77||dir=='d')&& self_x<m){
        self_x++;
        isFirst=0;
        water--;
        meat--;
    }
    if((dir==72||dir=='w')&&self_y>0){
        self_y--;
        isFirst=0;
        water--;
        meat--;
    }
    if((dir==80||dir=='s')&&self_y<n){
        self_y++;
        isFirst=0;
        water--;
        meat--;
    }
}
void init(){ //初始化函数 
    for(int i=1;i<=building_n;i++){ //建造P(OutPost前哨站) 
        Create_buildings('P');
    }
}
void Infor(){ //毫无意义の加载动画 
    char loading[3]={'/','-','\\'};
    gotoxy(49,23);
    cout << "Made by XSDB";
    gotoxy(50,25);
    cout << "Loading..";
    for(int i=1;i<=30;i++){
        gotoxy(59,25);
        cout << loading[i%3];
        Sleep(100);
    }
    Sleep(1000);
    system("cls");
    return;
}
void Start(){
    Infor();             //加载动画(无实际意义 
    int a;
    gotoxy(0,2);
    cout << "温馨提示:开始游戏后将可以存档,请不要轻易更改存档数据,以免发生错误!若出现错误,请手动删除save.txt后重启游戏" << endl; 
    cout << "读入存档或者开始新游戏?(1/2)";
    cin >> a;
    system("cls");
    if(a==1) isLoad=1;
    else if(a==2) isLoad=0;
    else{
        cout << "错误!!输入有误!!请手动重启游戏" << endl; 
        isError=1;
        system("pause");
    }
}
void Event(char type){ //事件(带事件框 
    int k=1;
    gotoxy(30,25);

    cout << "+-";           //基本事件框 
    for(int i=1;i<=20;i++){  //行 
        cout << "--";
    }
    cout << "-+" << endl;
    for(int i=1;i<=12;i++){  //列 
        gotoxy(30,25+k);
        cout << "| ";
        for(int j=1;j<=20;j++){  //行 
            cout << "  ";
        }
        cout << " |";
        k++;
    }
    gotoxy(30,25+k);
    cout << "+-";
    for(int i=1;i<=20;i++){  //行 
        cout << "--";
    }
    cout << "-+" << endl;
    gotoxy(33,28);

    if(type=='P'){      //前哨站 

        water=max_water;
        meat=max_meat;
        gotoxy(34,27);
        cout << "             前  哨  站";
        gotoxy(34,30);
        cout << "水补充满了";
        gotoxy(34,32);
        cout << "满载肉而归";
        gotoxy(34,34);
        system("pause");
        Draw();
        return;
    }
    if(type=='t'){      //渴死 
        gotoxy(34,27);
        cout << "           你  渴  死  了";
        gotoxy(34,30);
        cout << "水已耗尽";
        gotoxy(34,32);
        cout << "口舌生烟,干燥如斯"; 
        gotoxy(34,34);
        cout << "整个世界都在消散"; 
        gotoxy(34,36);
        system("pause");
        Draw();
        return;
    }
    if(type=='h'){      //饿死 
        gotoxy(34,27);
        cout << "           你  饿  死  了"; 
        gotoxy(34,30);
        cout << "肉吃完了";
        gotoxy(34,32);
        cout << "人是铁饭是钢,饥饿如山一般压来"; 
        gotoxy(34,34);
        cout << "整个世界都在消散"; 
        gotoxy(34,36);
        system("pause");
        Draw();
        return;
    }
    else{
        gotoxy(34,28);
        cout << "错误!当前建筑未知!";
        gotoxy(34,30);
        cout << "请勿更改地图文件!(也可能屑作者没更新";
        gotoxy(34,32);
        system("pause");
    }
}
int main(){
    system("mode con cols=110 lines=66");
    int down;
    Start();
    isCheat=0;
    if(isLoad) Load();
    else init();
    Draw();
    Save();
    char below;
    below=Map[self_y][self_x];
    while(!isError){
        char below;

        if(!isFirst && below=='A'){
            Save();
            return 0;
        }
        if(water<=0){
            Event('t');
            break;
        }
        if(meat<=0){
            Event('h');
            break;
        }
        if(_kbhit()){
            down = getch();
            if(!isEvent){
                if(down=='a'||down=='d'||down=='w'||down=='s'||down==75||down==77||down==72||down==80){
                    Move(down);
                }
                if(down=='p'&&Root){
                    isCheat=!isCheat;
                }
                if(down=='`'){
                    char isSav;
                    system("cls");
                    cout << "是否存档?将会覆盖已有存档!(y/n)";
                    cin >> isSav;
                    system("cls");
                    if(isSav=='y') Save();
                }
                if(down==27){
                    char isStop;
                    system("cls");
                    cout << "是否结束游戏?未保存的存档将不会自动保存!(y/n)";
                    cin >> isStop;
                    system("cls"); 
                    if(isStop=='y') break;
                }
                if(down=='l'){
                    char isDel;
                    system("cls");
                    cout << "是否删除存档?将永远无法恢复!(y/n)";
                    cin >> isDel;
                    system("cls"); 
                    if(isDel=='y'){
                        system("del save.txt");
                        break;
                    }

                }
            } 
            Draw();
            below=Map[self_y][self_x];
            if(below!='A'&&below!=','&&below>='A'&&below<='Z'){
                isEvent=1;
                Event(below);
                Map[self_y][self_x]=Map[self_y][self_x]-'A'+'a';
                isEvent=0;
            }
            //Draw();
        }
    }
    cout << endl << "游 戏 结 束" << endl << "请手动重启" << endl << endl; 
    //system("pause");
    return 0;
}

原版

原版小黑屋

汉化版小黑屋