跳到正文

休闲·娱乐

小游戏

yaoyg约 41 分钟
原文

这是 2026年3月7日 存档的第 8 版,不是当前版本。查看当前版本

文章信息与版本

0.1.4beta版

代码

C++
#include <bits/stdc++.h>

using namespace std;
#ifdef _WIN32
#define CLEAR_SCREEN system("cls")
#define GETCH _getch()
#include <conio.h>
#include <windows.h>
#else
    #define CLEAR_SCREEN system("clear")
    #include <termios.h>
    #include <unistd.h>
    // Linux/Mac 无回显读取
    char getch_char() {
        struct termios oldt, newt;
        char ch;
        tcgetattr(STDIN_FILENO, &oldt);
        newt = oldt;
        newt.c_lflag &= ~(ICANON | ECHO);
        tcsetattr(STDIN_FILENO, TCSANOW, &newt);
        ch = getchar();
        tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
        return ch;
    }
    #define GETCH getch_char()
#endif


class enemy // 敌人总基类
{
public:

    struct enemykind
    {
        string name;
        int bl_v;
        int enemy_at_power;
    };
    
    enemykind kind[1000]={{"      natural",500,100},
                          {},
                        };

    int Enemy_Unit_Combination[10000][10]={{1,1,1,1,1}};

    int e[100],at_power[100];
    string enemy_name[100];
    void emy(int en_co)//敌方初始化
    {
        for(int i=1;i<=5;i++)
        {
            enemy_name[i]=kind[Enemy_Unit_Combination[en_co-1][i-1]-1].name;
            e[i]=kind[Enemy_Unit_Combination[en_co-1][i-1]-1].bl_v;
            at_power[i]=kind[Enemy_Unit_Combination[en_co-1][i-1]-1].enemy_at_power;
        }
    }
    void print()//显示敌方状态
    {
        cout<<"                                      敌方状态                               "<<endl;
        cout<<"      -----------------------------------------------------------------------"<<endl;
        cout<<"      |             |             |             |             |             |"<<endl;
        cout<<"  名字";
        for(int i=1;i<=5;i++)
        {
            cout<<"|";
            if(e[i]==0)
            {
                printf("             ");
                continue;
            }
            cout<<enemy_name[i];
            //printf("%13s",enemy_name[i]);
        }
        cout<<"|"<<endl;
        cout<<"生命值";
        for(int i=1;i<=5;i++)
        {
            cout<<"|";
            if(e[i]==0)
            {
                //string s="消亡";
                cout<<"         消亡";
                continue;
            }
            printf("%13d",e[i]);
        }
        cout<<"|"<<endl;
        cout<<"攻击力";
        for(int i=1;i<=5;i++)
        {
            cout<<"|";
            if(e[i]==0)
            {
                printf("             ");
                continue;
            }
            printf("%13d",at_power[i]);
        }
        cout<<"|"<<endl;
        cout<<"  位置|      1      |      2      |      3      |      4      |      5      |"<<endl;
        cout<<"      -----------------------------------------------------------------------"<<endl;
    }
    int ifkia()//全灭判断
    {
        for(int i=1;i<=5;i++)
        {
            if(e[i])
            {
                return 0;
            }
        }
        return 1;
    }
private:
};

enemy en_o;//敌人对象

class Ordinary_Enemy : public enemy   //普通敌人
{
public:
    virtual void attack_1(int* player_data,int bianhao) = 0;//第一种普攻
    virtual void attack_2(int* player_data,int bianhao) = 0;//第二种普攻
};
class Elite_Enemy : public enemy   //精英敌人
{
public:
    virtual void attack_1(int* player_life) = 0;//第一种普攻
    virtual void attack_2(int* player_life) = 0;//第二种普攻
    virtual void eattack(int* player_life) = 0;//e技能
    virtual void generate() = 0;//召唤
    virtual void qattacking(int* player_life) = 0;//大招蓄力
    virtual void qattack(int* player_life) = 0;//大招
};
class Boss : public enemy   //boss or boss的一个形态
{
public:
    virtual void attack_1(int* player_life) = 0;//第一种普攻
    virtual void attack_2(int* player_life) = 0;//第二种普攻
    virtual void eattack_1(int* player_life) = 0;//第一种e技能
    virtual void eattack_2(int* player_life) = 0;//第二种e技能
    virtual void eattack_3(int* player_life) = 0;//第三种e技能
    virtual void strenger(int* player_life) = 0;//强化
    virtual void generate() = 0;//召唤
    virtual void qattacking(int* player_life) = 0;//大招蓄力
    virtual void qattack(int* player_life) = 0;//大招
};

class Natural_Number : public Ordinary_Enemy
{
public:
    void attack_1(int* player_data,int bianhao) override {//从玩家血量中选择最大的两个数,拼和成一个百分数,此次攻击所造成的的伤害为攻击力与这个百分数乘算的结果
        int p_l = *player_data;
        // 步骤1:提取玩家血量的每一位数字
        int every_b_l[5] = {0}; 
        int digit_count = 0;    // 记录实际提取的数字位数
        int temp = p_l;

        // 分解玩家血量的每一位数字
        while (temp > 0 && digit_count < 5) 
        {
            every_b_l[digit_count++] = temp % 10; // 取个位数字
            temp = temp / 10;                     // 去掉个位
        }

        int num1, num2;
            // 对提取的数字降序排序
            std::sort(every_b_l, every_b_l+5, std::greater<int>());
            num1 = every_b_l[0]; // 最大的数字
            num2 = every_b_l[1]; // 第二大的数字
      //  cout<<num1<<endl<<num2<<endl;

        // 步骤3:拼接成百分数(比如num1=9, num2=5 → 95% → 0.95)
        double percentage = (num1 * 10 + num2) / 100.0;
        percentage*=en_o.at_power[bianhao];
        *player_data-=percentage;
    }
    void attack_2(int* player_date,int bianhao) override{//从玩家能量中选择最大的一个数,拼和成一个百分数,此次攻击会吸取玩家能量,大小为攻击力与这个百分数乘算的结果,此次攻击后玩家不会获得手机
        int p_l = *player_date;
        // 步骤1:提取玩家能量的每一位数字
        int every_b_l[5] = {0}; // 初始化数组,最多存储5位数字
        int digit_count = 0;    // 记录实际提取的数字位数
        int temp = p_l;

        // 分解玩家能量的每一位数字
        while (temp > 0 && digit_count < 5) 
        {
            every_b_l[digit_count++] = temp % 10; // 取个位数字
            temp = temp / 10;                     // 去掉个位
        }

        int num1;
        // 对提取的数字降序排序
        std::sort(every_b_l, every_b_l + 5, std::greater<int>());
        num1 = every_b_l[0]; // 最大的数字
        // 步骤3:拼接成百分数(比如num1=9, num2=5 → 95% → 0.95)
        double percentage = (num1) / 100.0;
        *player_date-=percentage*en_o.at_power[bianhao];
    }
};

class The_lost//玩家
{
public:

    int att_power=100;
    int lostnl=0,life=1500;
    string name="the lost";
    bool recovery_state = false; // 「回复」状态标记
    int recovery_turn = 0;       // 「回复」状态剩余回合
    int damage_record = 0;       // 「回复」状态下记录的伤害值
    int rationality=0;//理性点,普攻后增加,消耗以发动e技能,理智点属于各个角色独有,不与其他角色共享
    The_lost()
    {
        att_power=100;
        lostnl=0;
    }
    void lostaskill(int d,int *lostnl,int *e,int *rationality)//普攻
    {
        *lostnl=*lostnl+20;
        int damage = 0.8 * att_power;
        e[d]-=damage;
        if(recovery_state) {
            damage_record += damage;
            cout<<"【回复状态】记录普攻伤害:"<<damage<<" 累计:"<<damage_record<<endl;
        }
        *rationality+=1;
        if(e[d] <= 0) 
        {
            e[d] = 0;
        }
    }
    void losteskill(int d,int *lostnl,int *e,int *rationality)//e技能
    {
        if(*rationality>0)
        {
            *rationality-=1;
        }
        else
        {
            cout<<"理性不足"<<endl;
            return;
        }
        int damage = 2 * att_power;
        e[d]-=damage;
        if(recovery_state) {
            damage_record += damage;
            cout<<"【回复状态】记录E技能伤害:"<<damage<<" 累计:"<<damage_record<<endl;
        }
        *lostnl+=30;
        if(e[d] <= 0)
        {
            e[d] = 0;
        }
    }

    void lostqskill(int *lostnl)//大招
    {
        if(*lostnl>=qenergy)
        {
            *lostnl-=qenergy;
        }
        else
        {
            cout<<"能量不足"<<endl;
            return;
        }

        // 大招核心逻辑:回复2个理性点,进入回复状态(持续2回合),重置伤害记录
        rationality += 2;
        recovery_state = true;
        recovery_turn = 3+1;
        damage_record = 0;
        cout<<"【Q技能生效】回复2个理性点(当前:"<<rationality<<"),进入「回复」状态,持续3回合!"<<endl;

        // 回复状态结束处理(如果是状态到期)- 此处由回合逻辑触发,这里仅初始化状态
        *lostnl+=30;
    }

    //每回合结束时调用,更新回复状态
    void update_recovery_state() {
        if(recovery_state) 
        {
            int damage_record_per=0.5;
            recovery_turn--;
            cout<<"【回复状态】剩余回合:"<<recovery_turn<<endl;
            int heal = damage_record * damage_record_per;
            damage_record*=damage_record_per;
            life += heal;
            // 生命值不超过上限
            if(life > 1500) life = 1500;
            cout<<"【回复状态】累计造成伤害:"<<damage_record<<",转化"<<heal<<"点记录值(当前:"<<life<<")"<<endl;
	     if(recovery_turn==0)
            {
                recovery_state = false;
                damage_record = 0;
            } 
        }
    }
private:
    int qenergy=180;//e技能能量、q技能能量
};

class operate//操作类
{
public:
    int generate()//生成敌方
    {
        cout<<"战斗开始"<<endl;
        en_o.emy(1);
        en_o.print();
        return 0;
    }
    The_lost lost;
    queue <string>actionqueue;//行动队列
    queue <int>numberqueue;//编号队列
    //行动队列中的单位会轮换行动
    int select(int rs)//选择角色
    {
        for(int i=1;i<=5;i++)
        {
            actionqueue.push(en_o.enemy_name[i]);
            numberqueue.push(i);
        }
        for(int i=1;i<=rs;i++)
        {
            string rolename; 
            cout<<"踏上前来,你是:(me)"<<endl;
            cin>>rolename;
            actionqueue.push(rolename);
            numberqueue.push(i);
        }
        int if_vitcter=0;
        while (1)//回合轮换
        {
           // int o;
            //cin>>o;//---------------------------------------------------------------------------------------------------------------------------------------------------------
            string ppp=actionqueue.front();
            int a=numberqueue.front();
            move(ppp);
            actionqueue.pop();
            numberqueue.pop();
            actionqueue.push(ppp);
            numberqueue.push(a);
            if(en_o.ifkia())
            {
                if_vitcter=1;
                break;
            }
            else if(lost.life<=0)
            {
                if_vitcter=0;
                break;
            }
        }
        if(if_vitcter)
        {
            cout<<"战斗胜利"<<endl;
        }
        else
        {
            cout<<"战斗失败"<<endl;
        }
        return 0;
    }
    int move(string role)
    {
        if(role=="me")//迷途之人便是你    你的操作
        {
            //cout<<"迷途之人"<<endl;
            cout<<" 选择操作  当前能量:"<<lost.lostnl<<"       理性点:"<<lost.rationality<<"          生命值:"<<lost.life<<endl;
            cout<<"a:普攻   e:技能   q:大招"<<endl;
            char operationoptions;
            operationoptions=GETCH;
            if(operationoptions=='a')
            {
                int d;
                CLEAR_SCREEN;
                en_o.print();
                cout<<"发动普攻,请选择目标1~5:"<<endl;
                char d_char = GETCH;
                d = d_char - '0';
                //d-=1;
                lost.lostaskill(d,&lost.lostnl,en_o.e,&lost.rationality);
                CLEAR_SCREEN;
                en_o.print();
                cout<<endl;
            }
            if(operationoptions=='e')
            {
                int d;
                CLEAR_SCREEN;
                en_o.print();
                cout<<"发动技能,请选择目标(1~5):"<<endl;
                int d_char = GETCH;
                d = d_char - '0';
                //d-=1;
                en_o.print();
                lost.losteskill(d,&lost.lostnl,en_o.e,&lost.rationality);
                CLEAR_SCREEN;
                en_o.print();
                cout<<endl;
            }
            if(operationoptions=='q')
            {
                //en_o.print();
                lost.lostqskill(&lost.lostnl);
                CLEAR_SCREEN;
                en_o.print();
                cout<<endl;
            }
            lost.update_recovery_state();
        }
        else if(role=="      natural")
        {
            Natural_Number natural_number;
            CLEAR_SCREEN;
            en_o.print();
            unsigned int seed = chrono::system_clock::now().time_since_epoch().count();
            mt19937 engine(seed);
            uniform_int_distribution<int> int_dist(1, 4);
            int random_int = int_dist(engine);
            cout<<"敌方"<<numberqueue.front()<<"号敌人,natural_number行动"<<endl;
            std::this_thread::sleep_for(std::chrono::milliseconds(1000));
            CLEAR_SCREEN;
            en_o.print();
            cout<<"敌方"<<numberqueue.front()<<"号敌人,natural_number发动技能:"<<endl;
            if(180-lost.lostnl<10)
            {
                if(random_int>=2)
                {
                    natural_number.attack_2(&lost.lostnl,numberqueue.front());
                    cout<<"attack_2"<<endl;
                    cout<<"抽取了玩家能量,玩家能量剩余:"<<lost.lostnl<<endl;
                }
                else
                {
                    natural_number.attack_1(&lost.life,numberqueue.front());
                    cout<<"attack_1"<<endl;
                    cout<<"攻击玩家,玩家生命剩余:"<<lost.life<<endl;
                }
            }
            else
            {
                if(random_int==1)
                {
                    natural_number.attack_2(&lost.lostnl,numberqueue.front());
                    cout<<"attack_2"<<endl;
                    cout<<"抽取了玩家能量,玩家能量剩余:"<<lost.lostnl<<endl;
                }
                else
                {
                    natural_number.attack_1(&lost.life,numberqueue.front());
                    cout<<"attack_1"<<endl;
                    cout<<"攻击玩家,玩家生命剩余:"<<lost.life<<endl;
                }
            }
            std::this_thread::sleep_for(std::chrono::milliseconds(1000));
            CLEAR_SCREEN;
            en_o.print();
            
        }
        return 0;
    }
};

class display//显示类
{
public:
    string screen_stirp[20]={"==============================================================",//0     //界面显示条,长最长60+2字符
                             "|                                                            |",//1
                             "|                          wating.                           |",//2
                             "|                          wating..                          |",//3
                             "|                          wating...                         |",//4
                             "|          ","          |",//小型区(10+1)
                              //     5            6
                             "     "};//mini款
                             // 7
//------------------------------------------------------------------------游戏开始显示
    struct AnimationFrame {
        string text;
        string progress_bar;
    };
    // 预定义动画帧
    vector<AnimationFrame> loading_frames = {
        {screen_stirp[2], "□□□□□□□□□□□□□□□□□□□□"},
        {screen_stirp[3], "■□□□□□□□□□□□□□□□□□□□"}, 
        {screen_stirp[4], "■■□□□□□□□□□□□□□□□□□□"}, 
        {screen_stirp[2], "■■■□□□□□□□□□□□□□□□□□"}, 
        {screen_stirp[3], "■■■■□□□□□□□□□□□□□□□□"}, 
        {screen_stirp[4], "■■■■■□□□□□□□□□□□□□□□"}, 
        {screen_stirp[2], "■■■■■■□□□□□□□□□□□□□□"}, 
        {screen_stirp[3], "■■■■■■■□□□□□□□□□□□□□"}, 
        {screen_stirp[4], "■■■■■■■■□□□□□□□□□□□□"}, 
        {screen_stirp[2], "■■■■■■■■■□□□□□□□□□□□"}, 
        {screen_stirp[3], "■■■■■■■■■■□□□□□□□□□□"}, 
        {screen_stirp[4], "■■■■■■■■■■■□□□□□□□□□"}, 
        {screen_stirp[2], "■■■■■■■■■■■■□□□□□□□□"}, 
        {screen_stirp[3], "■■■■■■■■■■■■■□□□□□□□"}, 
        {screen_stirp[4], "■■■■■■■■■■■■■■□□□□□□"}, 
        {screen_stirp[2], "■■■■■■■■■■■■■■■□□□□□"}, 
        {screen_stirp[3], "■■■■■■■■■■■■■■■■□□□□"}, 
        {screen_stirp[4], "■■■■■■■■■■■■■■■■■□□□"}, 
        {screen_stirp[2], "■■■■■■■■■■■■■■■■■■□□"}, 
        {screen_stirp[3], "■■■■■■■■■■■■■■■■■■■□"}, 
        {screen_stirp[4], "■■■■■■■■■■■■■■■■■■■■"}


    };
    // 渲染单个帧的通用方法
    void render_frame(const AnimationFrame& frame) {
        cout << screen_stirp[0] << endl << screen_stirp[1] << endl;
        cout << screen_stirp[1] << endl << screen_stirp[1] << endl;
        cout << screen_stirp[1] << endl << screen_stirp[1] << endl;
        cout <<frame.text << endl;
        cout << screen_stirp[5] + screen_stirp[7] + screen_stirp[7];
        cout << frame.progress_bar;
        cout << screen_stirp[7] + screen_stirp[7] + screen_stirp[6] << endl;
        cout << screen_stirp[1] << endl << screen_stirp[0] << endl;
    }
    
    int start_screen()//开始屏幕显示
    {
        int a;
        cout<<screen_stirp[0]<<endl<<screen_stirp[1]<<endl;
        cout<<screen_stirp[5]<<"--                                    --"<<screen_stirp[6]<<endl;
        cout<<screen_stirp[5]<<"(            名为数学的游戏            )"<<screen_stirp[6]<<endl;
        cout<<screen_stirp[5]<<"--                                    --"<<screen_stirp[6]<<endl<<screen_stirp[1]<<endl;
        cout<<screen_stirp[5]<<"        (输入任意内容以开始游戏)        "<<screen_stirp[6]<<endl<<screen_stirp[1]<<endl<<screen_stirp[1]<<endl<<screen_stirp[0]<<endl;
        cout<<"输入任意内容以开始游戏:";
        cin>>a;
        cout<<endl;
        cout<<"欢迎来到“名为数学的游戏”游戏马上开始"<<endl;
        
        // 播放加载动画
        for (const auto& frame : loading_frames) {
            render_frame(frame);
            std::this_thread::sleep_for(std::chrono::milliseconds(300));
        }
        
        return 0;
    }
//------------------------------------------------------------------------游戏开始显示
//------------------------------------------------------------------------角色 or 战斗

    int display_role()
    {
        string nameye[10][10]={{"me"}};
        // 显示角色
        string name;
        int page=1;
        int max_page=2;

        cout<<screen_stirp[0]<<endl;
        cout<<"|所有角色:                                                   |"<<endl;
        cout<<screen_stirp[5]<<"1."<<nameye[page-1][0]<<"                                    "<<screen_stirp[6]<<endl;
        cout<<screen_stirp[1]<<endl;
        cout<<screen_stirp[1]<<endl;
        cout<<screen_stirp[1]<<endl;
        cout<<screen_stirp[1]<<endl;
        cout<<screen_stirp[1]<<endl;
        cout<<screen_stirp[1]<<endl;
        cout<<screen_stirp[0]<<endl;
        cout<<"输入角色序号1~9      z:上一页     x:下一页"<<endl;
        name=GETCH;
        if(name=="z")
        {
            if(page==1)
            {
                page=max_page;
            }
            page--;
        }
        else if(name=="x")
        {
            if(page==max_page)
            {
                page=1;
            }
            page++;
        }
        else if(nameye[page-1][stoi(name)-1]=="me")
        {
            cout<<"角色名称:"<<nameye[page-1][stoi(name)-1]<<endl;
            cout<<"角色技能:"<<endl;
            cout << screen_stirp[7] + "天赋:探索者    羁绊:独行者" << endl;
            cout << screen_stirp[7] + screen_stirp[7] + "天赋:    作为独行于此的你,步步为艰,你将探索此地,学习便是你最好的武器,(经过学习你将会获得新的一套技能)" << endl;
            cout << screen_stirp[7] + "形态:无知    初来乍到,你一无所知" << endl;
            cout << screen_stirp[7] + screen_stirp[7] + "普攻a:   回复一个理性点,对指定敌方单体造成80%攻击力的伤害,回复20能量" << endl;
            cout << screen_stirp[7] + screen_stirp[7] + "E技能e:  消耗一个理性点,对指定敌方单体造成200%攻击力的伤害,回复30能量" << endl;
            cout << screen_stirp[7] + screen_stirp[7] + "Q技能q:  消耗180能量,回复两个理性点,进入「回复」状态,持续三回合,此状态下记录角色造成的伤害,在状态中,每回合结束后将记录的伤害的50%转化为血量回复量" << endl;

        }
        else
        {
            cout<<"(请勿重复进行无效输入,否则电脑堆栈将爆炸)木有欧"<<endl;
            display_role();
        }
        return 0;
    }

    int choose_look_or_fight()
    {
        cout<<screen_stirp[0]<<endl;
        cout<<screen_stirp[1]<<endl;
        cout<<screen_stirp[1]<<endl;
        cout<<screen_stirp[1]<<endl;
        cout<<"|                      输入“c”查看角色                       |"<<endl;
        cout<<"|                      输入“f”开始战斗                       |"<<endl;
        cout<<screen_stirp[1]<<endl;
        cout<<screen_stirp[1]<<endl;
        cout<<screen_stirp[1]<<endl;
        cout<<screen_stirp[0]<<endl;
        string a;
        a=GETCH;
        if(a=="c")
        {
            display_role();
        }
        else if(a=="f")
        {
            operate fight;
            fight.generate();
            fight.select(1);//只有一个角色
        }   
        else
        {
            system("./game");
            return 0;
        }
        return 0;
    }
    

};

int main()
{

    #ifdef _WIN32
        SetConsoleOutputCP(65001);  // Windows GBK
        SetConsoleCP(65001);
    #else
        setlocale(LC_ALL, "");    // Linux/Mac UTF-8
    #endif

    display screen;
    //screen.start_screen();//不知道为啥,把注释解了就会出事
    screen.choose_look_or_fight();
    // operate fight;
    // fight.generate();
    // fight.select(1);//只有一个角色
    return 0;
}

更新细则

  1. 多端适应
  2. 更新的交互
  3. 一堆bug

画饼

  • 第一个角色
  • 角色技能
  • 战斗
  • 查看角色技能
  • 更好的显示
  • 更好的交互
  • 敌人行动
  • 养成
  • …

bug修复

  • 受攻击后血量异常变化or不扣血
    注释:现在的你终于可以被打死了
  • 敌人第一次攻击时会打你两轮