C++:自治我的世界2D.V0.0.1

#include<bits/stdc++.h> 
#include<windows.h> 
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0) 
using namespace std;
int random(int l,int r);//生成l到r的随机数 
void Color(int a,int b);//切换字体颜色
void mapcolor(string a);//颜色转化
void mapdisplay(string a);//输出  
void hidden(bool a);//隐藏光标
string MC_map[2000][2000];//[5];
int main()
{
    hidden(0); 
    srand((unsigned)10);
    int altitude=700;
    for(int x=0;x<2000;x++) for(int y=0;y<2000;y++) MC_map[y][x]="  ";
    for(int x=0;x<2000;x++) 
    {
        int a=int(2000-double(altitude)/50*49+random(-1,1));
        MC_map[1999][x]="基";
        for(int y=1998;y>a;y--) MC_map[y][x]="石";
        for(int y=a;y>2000-altitude;y--) MC_map[y][x]="土";
        MC_map[2000-altitude][x]="草";
        if(rand()%50==5) 
        {
            MC_map[2000-altitude-1][x]="木";
            MC_map[2000-altitude-2][x]="木";
            MC_map[2000-altitude-3][x]="木";
            MC_map[2000-altitude-4][x]="木";
            MC_map[2000-altitude-4][x-1]="叶";
            MC_map[2000-altitude-4][x-2]="叶";
            MC_map[2000-altitude-4][x+1]="叶";
            MC_map[2000-altitude-4][x+2]="叶";
            MC_map[2000-altitude-5][x]="叶";
            MC_map[2000-altitude-5][x-1]="叶";
            MC_map[2000-altitude-5][x-2]="叶";
            MC_map[2000-altitude-5][x+1]="叶"; 
            MC_map[2000-altitude-5][x+2]="叶";
            MC_map[2000-altitude-6][x]="叶";
            MC_map[2000-altitude-6][x+1]="叶";      
            MC_map[2000-altitude-6][x-1]="叶";
        }
        if(altitude<800) altitude+=random(0,1);
        if(altitude>600) altitude+=random(-1,0);
    }
    double sx=0,sy=0;
    int px=100,py=1000;
    for(long long start=0;;start++)
    {
        system("cls");
        if(KEY_DOWN('W')&&MC_map[py+2][px]!="  ") sy=-1;
        if(KEY_DOWN('D')) sx+=1;
        if(KEY_DOWN('A')) sx-=1;
        if(KEY_DOWN(39)&&KEY_DOWN(38)) MC_map[py][px+1]="  ";//右,上 
        else if(KEY_DOWN(39)&&KEY_DOWN(40)) MC_map[py+1][px+1]="  ";//右,下 
        else if(KEY_DOWN(37)&&KEY_DOWN(38)) MC_map[py][px-1]="  ";//左,上 
        else if(KEY_DOWN(37)&&KEY_DOWN(40)) MC_map[py+1][px-1]="  ";//左,下
        else if(KEY_DOWN(38)) MC_map[py-1][px]="  ";//上 
        else if(KEY_DOWN(40)) MC_map[py+2][px]="  ";//下 
        if(KEY_DOWN('M'))
        {
            system("cls");
            for(int x=py-100;x<py+100;x++)
            {
                for(int y=px-100;y<px+100;y++)
                {
                    if(x==py&&y==px){Color(0,14);cout<<"''";}
                    else if(x==py+1&&y==px){Color(9,9);cout<<"  ";}
                    else if(x>=0&&x<2000&&y>=0&&y<2000) mapcolor(MC_map[x][y]),mapdisplay(MC_map[x][y]);
                   }
                Color(0,0); 
                cout<<'/'<<endl;
            }
            while(!KEY_DOWN('M'));
        }
        for(int s=0;s<abs(int(sx));s++)
        {
            if(px>=0&&px<=1999) px+=int(sx)/abs(int(sx));
            if(px<0) px=0;
            if(px>=2000) px=1999;
            if(MC_map[py][px]!="  "||MC_map[py+1][px]!="  "){px-=int(sx)/abs(int(sx)),sx=0;break;} 
        }
        for(int s=0;s<abs(int(sy));s++)
        {
            if(py>=0&&py<2000) py+=int(sy)/abs(int(sy));
            if(py<0) py=0;
            if(py>=2000) py=1999;
            if(MC_map[py][px]!="  "||MC_map[py+1][px]!="  "){py-=int(sy)/abs(int(sy)),sy=0;break;} 
        }
        sx*=0.5;
        sy+=1;
        for(int x=py-7;x<py+8;x++)
        {
            for(int y=px-7;y<px+8;y++)
            {
                if(x==py&&y==px){Color(0,14);cout<<"''";}
                else if(x==py+1&&y==px){Color(9,9);cout<<"  ";}
                else if(x>=0&&x<2000&&y>=0&&y<2000) mapcolor(MC_map[x][y]),mapdisplay(MC_map[x][y]);
            }
            Color(0,0); 
            cout<<'/'<<endl;
        }
        Color(7,0);
        cout<<px<<" "<<py;
        Sleep(70);
    }
    return 0;
}
int random(int l,int r)//生成l到r的随机数 
{
    return (rand()%(r-l+1))+l;
}
void Color(int a,int b)//切换字体颜色 
{
    int c;
    if(b>=1)c=a+b*16;
    else c=a;
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),c);
}
void mapcolor(string a)//颜色转化 
{
    if(a=="  ") Color(11,11);
    if(a=="草") Color(10,6); 
    if(a=="土") Color(6,6);
    if(a=="石") Color(8,8);
    if(a=="木") Color(8,6);
    if(a=="叶") Color(10,2);
}
void mapdisplay(string a)//输出 
{
    if(a=="  ") cout<<"  ";
    if(a=="草") cout<<"▔ "; 
    if(a=="土") cout<<"■";
    if(a=="石") cout<<"■";
    if(a=="木") cout<<"||";
    if(a=="叶") cout<<"▓▓";
}
void hidden(bool a)//隐藏光标
{
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_CURSOR_INFO cci;
    GetConsoleCursorInfo(hOut,&cci);
    cci.bVisible=a;//赋1为显示,赋0为隐藏
    SetConsoleCursorInfo(hOut,&cci);
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值