画图软件

· · 个人记录

代码在这里

#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
#define W 100
#define H 42
using namespace std;
int d[5][2]={{0,-1},{0,1},{-1,0},{1,0},{0,0}};
char cj[4]={72,80,75,77};
int mymap[1000][1000];
int handsome=0;
string sym = "█ ";
int mode = 0;
int ax,ay;
char a;
int cl = 8;
void color(int a)
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}

void gxy(int x,int y)
{
    COORD pos;
    pos.X=2*x;
    pos.Y=y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}

class player
{
    public:
        int x;
        int y;
        int p;
        int dir;
        bool sw;
        int injur;
        void clear()
        {
            gxy(x,y);
            if(handsome == 1){
                cout<<"空";
            }else if(mode == 0){
                cout<<"█ ";
                mymap[x+ay][y+ax] = p;
            }else if(mode == 1){
                cout<<"  ";
                mymap[x+ay][y+ax] = 0;
            }else if(mode == 2){
                cout<<"█ ";
            }
            return;
        }
        void draw()
        {
            color(p);
            if(mode == 2&&handsome == 0){
                color(mymap[x+ay][y+ax]);
            }
            clear();
            if(2*x>=W-4){
                x--;
                gxy(x+1,y);
                cout<<"  ";
            }
            if(y>=H-2){
                y--;
                gxy(x,y+1);
                cout<<"  ";
            }
            if(x<=1){
                x++;
                gxy(x-1,y);
                cout<<"  ";
            }
            if(y<=1){
                y++;
                gxy(x,y-1);
                cout<<"  ";
            }
            x+=d[dir][0];
            y+=d[dir][1];
            color(12);
            gxy(x,y);
            cout<<"█ ";
            gonggao();
            return;
        }
        void gonggao(){
            gxy(0,0);
            color(15);
            cout<<"目前状态:";
            if(handsome==1){
                cout<<"占位";
            }else if(mode == 1){
                cout<<"橡皮"; 
            }else if(mode == 0){
                cout<<"画笔";
            }else if(mode == 2){
                cout<<"移动";
            }
            cout<<"    目前颜色:";
            color(p);
            cout<<"█ ";
            cout<<"                                                                        \n"; 

        }
        void shuaxin(int bx,int by){
            for(int i = bx;i < 42+bx;i++){
                for(int j = by;j < 50+by;j++){
                    gxy(j-by,i-bx-1);
                    color(mymap[j][i-1]);
                    cout<<"█ ";
                }
                cout<<'\n';
            }
        }
};
player p;
bool isout;
void start(){
    p.shuaxin(0,0);
    p.draw();
    while(1){
        if(kbhit()){
            a = getch();
            if(a == ' '){
                mode++;
                mode = mode%3;
                p.dir = 4;
                p.draw();
                handsome=0;
                continue;
            }else if(a <= '7'&&a>='1'){
                p.p = a-'0'+cl;
                p.dir = 4;
                p.draw();
                continue;
            }else if(a=='9'){
                p.p = min(p.p,(p.p+8)%16);
                cl = 0;
                p.dir = 4;
                p.draw();
                continue;
            }else if(a == '0'){
                p.p = max(p.p,(p.p+8)%16);
                cl = 8;
                p.dir = 4;
                p.draw();
                continue;
            }else if(a == 'z'){
                mode == 1;
                p.draw();
                handsome = 1-handsome;
                p.dir = 4;
                p.draw();
                continue;
            }else if(a == 'q'){
                memset(mymap,0,sizeof(mymap));
                p.shuaxin(ax,ay);
                p.dir = 4;
                p.draw();
                continue;
            }else if(a == 'w'){
                mode = 2;
                ax++;
                ax = max(ax,0);
                ax = min(ax,999);
                p.shuaxin(ax,ay);
                p.dir = 4;
                p.draw();
                continue;
            }else if(a == 'a'){
                mode = 2;
                ay++;
                ay = max(ay,0);
                ay = min(ay,999);
                p.shuaxin(ax,ay);
                p.dir = 4;
                p.draw();
                continue;
            }else if(a == 's'){
                mode = 2;
                ax--;
                ax = max(ax,0);
                ax = min(ax,999);
                p.shuaxin(ax,ay);
                p.dir = 4;
                p.draw();
                continue;
            }else if(a == 'd'){
                mode = 2;
                ay--;
                ay = max(ay,0);
                ay = min(ay,999);
                p.shuaxin(ax,ay);
                p.dir = 4;
                p.draw();
                continue;
            }else if(a == 'n'){
                p.shuaxin(ax,ay);
                p.dir = 4;
                p.draw();
                continue;
            }else if(a == 'h'){
                isout = 1;
                return;
            }
            a = getch();
            if(a == cj[0]){
                p.dir = 0;
            }else if(a == cj[1]){
                p.dir = 1;
            }else if(a == cj[2]){
                p.dir = 2;
            }else if(a == cj[3]){
                p.dir = 3;
            } else{
                p.dir = 4;
            }
            p.draw();
        }
        Sleep(100);
    }
}
int main(){
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_CURSOR_INFO CursorInfo;
    GetConsoleCursorInfo(handle, &CursorInfo);
    CursorInfo.bVisible = false;
    SetConsoleCursorInfo(handle, &CursorInfo);
    system("mode con cols=100 lines=42");
    mainstart:
    p.p = 15;
    p.dir = 4;
    p.x = 5;
    p.y = 2;
    color(15);
    for(int i = 0;i < H;i++){
        gxy(0,i);
        cout<<"                                                                                                                  \n";
    } 
    gxy(0,0);
    cout<<"开始或继续请按1 \n\n\n导出请按2\n\n\n读入请按3\n\n\n导出为PNG请按4\n\n\n操作说明请按5";
    while(1){
        if(kbhit()){
            a = getch();
            if(a == '1'){
                start();
                goto mainstart;
            }else if(a == '5'){
                for(int i = 0;i < H;i++){
                    gxy(0,i);
                    cout<<"                                                                                                                  \n";
                } 
                gxy(0,0) ;
                cout<<"小林君制作 \n\n\n上下左右控制方向 \n\n\n空格换模式 \n\n\n1~7换颜色 \n\n\n9和0换深浅 \n\n\nz占位(移动,刷新,可清除) \n\n\nq清屏 \n\n\nwasd移动画布(略有卡顿) \n\n\nn刷新 \n\n\nh返回主菜单 \n\n\n按任意键返回";
                while(1){
                    if(kbhit()){
                        break;
                    }
                    Sleep(100);
                }
                goto mainstart;
            }else if(a == '2'){
                freopen("paint.pnt","w",stdout);
                for(int i = 0;i < 1000;i++){
                    for(int j = 0;j < 1000;j++){
                        cout<<mymap[i][j]<<' ';
                    }
                    cout<<endl;
                }
                freopen("CON","w",stdout);  
                for(int i = 0;i < H;i++){
                    gxy(0,i);
                    cout<<"                                                                                                                  \n";
                } 
                gxy(0,0);
                cout<<"OK,按任意键返回"; 
                while(1){
                    if(kbhit()){
                        break;
                    }
                    Sleep(100);
                }
                goto mainstart;
            }else if(a == '3'){
                freopen("paint.pnt","r",stdin);
                for(int i = 0;i < 1000;i++){
                    for(int j = 0;j < 1000;j++){
                        cin>>mymap[i][j];
                    }
                }
                freopen("CON","r",stdin);  
                for(int i = 0;i < H;i++){
                    gxy(0,i);
                    cout<<"                                                                                                                  \n";
                } 
                gxy(0,0);
                cout<<"OK,按任意键返回"; 
                while(1){
                    if(kbhit()){
                        break;
                    }
                    Sleep(100);
                }
                goto mainstart;
            }else if(a == '4'){
                for(int i = 0;i < H;i++){
                    gxy(0,i);
                    cout<<"                                                                                                                  \n";
                } 
                gxy(0,0);
                cout<<"研发中...  按任意键返回"; 
                while(1){
                    if(kbhit()){
                        break;
                    }
                    Sleep(100);
                }
                goto mainstart;
            }
        }
    }
    start();
}

如果有bug,把“█”后的空格删了