寻宝小游戏(2.2加密版)

· · 休闲·娱乐

仅支持Windows的C++11可运行

其他如能正常运行,也可以游玩(Mac/Linux/ 手机 系统和做题软件不可以直接游玩)

#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <cmath>
#include <windows.h>
using namespace std;
int xy=01;
clock_t start = clock();//时间 
int winx;// 星星 ☆ x坐标
int winy;// 星星 ☆ y坐标
double mins=0;
bool bl=0;
int jf;
int mjf;
char a;
int x=10,y=10;
    void xxyy(int x, int y);//移到 y行 x列 
    COORD getxy();          //获取屏幕客户区鼠标当前字符行列坐标
    int cc(int &x,int &y);                //防出界
    int ww(int x,int y,clock_t start);//绘制游戏界面 
    void gg();              //隐藏光标
    void q1();              //1.开始游戏  
    void q2();              //2.游戏规则
    void q3();              //3.界面配色
    void q4();              //4.游戏信息
    int kai();             //开始游戏
    void sl();              //输入1 
    void off();             //隐藏控制台窗口(后台运行)
    void setFontSize(int y);// 设置控制台字号
    void max_cmd();         //最大化窗口
    void notmax_cmd();      //最大化窗口到正常大小
signed main() {
    setFontSize(30);
    HWND hwnd = GetConsoleWindow();
    HMENU menu = GetSystemMenu(hwnd, FALSE);
    RemoveMenu(menu,SC_MAXIMIZE, MF_BYCOMMAND);
    COORD pt;
    system("mode con cols=40 lines=20");//菜单大小 
    system("mode con cp select=65001");
    srand((unsigned)time(NULL));//随机种子 
    SetConsoleTitle("\u5BFB\u5B9D\u5C0F\u6E38\u620F");
    gg();
    system("color 0F");
    for(;1;){
        notmax_cmd();
        jf=0;
        x=10,y=10;
        winx=rand()%58+2;
        winy=rand()%28+2;   
        bl=0; 
        system("cls");
        int s=kai();
        if(s==0){
            break;
        } 
    }
    system("cls");
    cout<<"Good bye!";
    Sleep(1000);
    off();
    return 0;
}
    void xxyy(int x, int y) { //移到 y行 x列 
        COORD pos;   
        pos.X = x; 
        pos.Y = y;
        SetConsoleCursorPosition(
            GetStdHandle(STD_OUTPUT_HANDLE), pos
        );
    }
    COORD getxy(){//获取屏幕客户区鼠标当前字符行列坐标
        HWND hWnd = GetConsoleWindow();
        POINT pt;
        GetCursorPos(&pt);
        ScreenToClient(hWnd, &pt);
        CONSOLE_FONT_INFO fontInfo;
        GetCurrentConsoleFont(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &fontInfo);
        int charW = fontInfo.dwFontSize.X;
        int charH = fontInfo.dwFontSize.Y;
        COORD res;
        res.X = pt.x / charW;
        res.Y = pt.y / charH;
        return res;
    }
    int ww(int x,int y,clock_t start){//绘制游戏界面 
        xxyy(0, 0);//从x:0 y:0开始 
        cout<<"-----------------------------------------------------------------------------------------------------------------------"<<endl;
        for(int i=2;i<=29;i++){//地图 
            cout<<"|"; 
            for(int j=2;j<=59;j++){
                if((winy==i&&winx==j&&!bl))cout<<"\u2606";
                else if(!(y==i&&j==x))cout<<"  ";
                else if(!bl)cout<<"\u25CF"; 
                else cout<<"\u2605";  
            }
            cout<<"|";
            cout<<endl;
        }
        double now = (clock() - start) * 1.0 / CLOCKS_PER_SEC;//当前用时 
        cout<<"----------------------------------------------------------------------------------------------------------------------"<<endl;
        cout << "\u5F53\u524D\u7528\u65F6\uFF1A" << now << " s "<<"\u79EF\u5206\uFF1A"<<jf<<"\u5206       " << endl;
        cout << "\u2191\u2193\u2190\u2192 \u79FB\u52A8            \u62FF\u2606" << endl;//规则 
        Sleep(50); 
        return 0;
    }
    void gg(){//隐藏光标 
        CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };

        SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
    }
    int kai(){
        system("mode con cols=41 lines=18");
        system("cls");
        //┏┓┗┛━┃ 
        cout<<"-----------------------------------------"<<endl;
        cout<<"|              \u5BFB\u5B9D\u5C0F\u6E38\u620F              |"<<endl;
        cout<<"-----------------------------------------"<<endl;
        cout<<"|              1.\u5F00\u59CB\u6E38\u620F              |"<<endl;
        cout<<"|              2.\u6E38\u620F\u89C4\u5219              |"<<endl;
        cout<<"|              3.\u754C\u9762\u914D\u8272              |"<<endl;
        cout<<"|              4.\u6E38\u620F\u4FE1\u606F              |"<<endl;
        cout<<"|              5.\u9000\u51FA\u6E38\u620F              |"<<endl;
        cout<<"-----------------------------------------"<<endl;
        cout<<"| \u8F93\u5165\u5E8F\u53F7\u8DF3\u8F6C                         |"<<endl;
        cout<<"|\uFF08\u5982\u8F93\u5165\u20181\u2019\u8DF3\u8F6C\u8FDB\"\u5F00\u59CB\u6E38\u620F\"\uFF09       |"<<endl; 
        cout<<"-----------------------------------------"<<endl;
        a=getch();
        if(a=='1'){
            q1();
        }else if(a=='2'){
            q2();
        }else if(a=='3'){
            q3();
        }else if(a=='4'){
            q4();
        } else if(a=='5'){
            return 0;
        }else{
            kai(); 
        }
        return 1;
    }
    void q1(){
        max_cmd();
        start = clock();
        system("mode con cols=119 lines=33");
        for(;1;){
            ww(x,y,start);
            Sleep(40); 
            sl();
            if(cc(x,y)==0){
                system("cls");
                clock_t end = clock();
                double ms = (end - start)  / CLOCKS_PER_SEC;
                cout<<"Game Over"<<endl;
                cout << "\u8017\u65F6\uFF1A" << ms << " \u79D2" << endl;
                mins=max(ms,mins);
                cout<<"\u6700\u957F\u8017\u65F6\uFF1A"<<mins<< " \u79D2"<<endl ;
                cout << "\u79EF\u5206\uFF1A" << jf<< " \u5206" << endl;
                mjf=max(jf,mjf);
                cout<<"\u6700\u9AD8\u79EF\u5206\uFF1A"<<mjf<< " \u5206"<<endl ;
                Sleep(1000);
                system("pause");
                return;
            }
            if((winy==y&&winx==x)){
                winx=rand()%58+2;
                winy=rand()%28+2;
                jf++;
            }
        }
    }
    int cc(int &x, int &y) {//防出界 
        if(x<=1)return 0;
        if(x>59)return 0;
        if(y<=1)return 0;
        if(y>29)return 0;
        return 1;
    }
    void sl(){
        if(_kbhit()){
            a=getch();
            a=getch();
            if(a==72) {
                xy=00;
            }else if(a==75){
                xy=10;
            }else if(a==77){
                xy=11;
            }else if(a==80){
                xy=01; 
            }
        }
        if(xy==00) {
            y--;
        }else if(xy==10){
            x--;
        }else if(xy==11){
            x++;
        }else if(xy==01){
            y++; 
        }
    }
    void q2(){
        system("cls");
        cout<<"\u4F60\u662F\u201C\u25CF\u201D\uFF0C\u62FF\u201C\u2606\u201D\u5F97\u79EF\u5206"<<endl<<"\u4E0A \u2191\u4E0B \u2193\u5DE6 \u2190\u53F3 \u2192"<<endl<<endl;
        cout<<"\u4EC5\u652F\u6301windows"<<endl;
        cout<<"\u5728Dev-C++\u4E2D\u53EF\u8FD0\u884C"<<endl; 
        system("pause");
        system("cls");
        kai();
    }
    void q3(){
        system("cls");
        cout<<"1.\u9ED1\u5E95\u767D\u5B57\uFF08\u9ED8\u8BA4\uFF09"<<endl<<endl;
        cout<<"2.\u9ED1\u5E95\u7EFF\u5B57"<<endl<<endl;
        cout<<"3.\u767D\u5E95\u9ED1\u5B57"<<endl<<endl; 
        cout<<"4.\u8FD4\u56DE"<<endl<<endl;
        a=getch();
        if(a=='1'){
            system("color 0F");
            q3();
        }
        if(a=='2'){
            system("color 0A");
            q3();
        }
        if(a=='3'){
            system("color F0");
            q3();
        }
        system("cls");
        kai();
    }
    void q4(){
        system("cls");
        SYSTEMTIME st;
        GetLocalTime(&st);
        string s;
        switch(st.wDayOfWeek) {
            case 1:
                s="\u4E00";
                break;
            case 2:
                s="\u4E8C";
                break;
            case 3:
                s="\u4E09";
                break;
            case 4:
                s="\u56DB";
                break;
            case 5:
                s="\u4E94";
                break;
            case 6:
                s="\u516D";
                break;
            case 7:
                s="\u65E5";
                break;
        }
        cout<<st.wYear<<"\u5E74"<<st.wMonth<<"\u6708"<<st.wDay<<"\u65E5\u661F\u671F"<<s<<"  "; 
        cout<<st.wHour<<":"<<st.wMinute<<":"<<st.wSecond<<endl<<endl;
        if(mins!=0)cout<<"\u6700\u77ED\u8017\u65F6\uFF1A"<<mins<< " \u79D2"<<endl<<endl;
        else cout<<"\u6700\u77ED\u8017\u65F6\uFF1A"<<"\u65E0"<<endl<<endl;
        cout<<"\u6E38\u620F\u7248\u672C:2.2\u52A0\u5BC6\u7248"<<endl;
        cout<<"\u4F5C\u8005:y2014y\uFF08\u6D1B\u8C37\uFF09"<<endl<<endl;
        system("pause");
        system("cls");
        kai();
    }
    void off(){                                  //隐藏控制台窗口(后台运行)
        HWND hwnd = GetConsoleWindow();
        ShowWindow(hwnd, SW_HIDE);
    }
    void setFontSize(int y) {// 设置控制台字号
        HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
        CONSOLE_FONT_INFOEX fi;
        fi.cbSize = sizeof(fi);
        GetCurrentConsoleFontEx(h, FALSE, &fi);
        fi.dwFontSize.X = 0;
        fi.dwFontSize.Y = y; 
        fi.FontWeight = FW_NORMAL; 
        wcscpy(fi.FaceName, L"Consolas"); 
        SetCurrentConsoleFontEx(h, FALSE, &fi);
    }
    void max_cmd(){                                  //最大化窗口
        HWND hwnd = GetConsoleWindow();
        ShowWindow(hwnd,SW_SHOWMAXIMIZED);
    }
    void notmax_cmd(){                                  //最大化窗口到正常大小
        HWND hwnd = GetConsoleWindow();
        ShowWindow(hwnd,SW_RESTORE);
    }
//顶部菜单栏:工具 (Tools) → 编译选项 (Compiler Options)
//勾选「编译时加入以下命令」复选框
//在上方输入框粘贴完整参数:"-std=c++11 -finput-charset=UTF-8 -fexec-charset=UTF-8"
//点击【确定 [O]】保存配置
//关闭当前代码,新建工程 / 新建文件,旧文件不会自动加载新编译参数
//全部重新编译运行