打怪小游戏c++ 超好玩

原文的链接:https://blog.csdn.net/Misce_dence/article/details/121049645

我个人进行了较大幅度的调整,修复了一些在游玩中的bug,并增加了许多新玩法。

不多说了,直接上新的代码:

 #include<iostream>
using namespace std;
int main()
{
    int shengming = 80, gongji = 10, jinbi = 10, smsx = 80, shanbi = 10, yunqi = 20, times = 0;
    int a = 0, b = 0, c = 0, d = 0, e = 0, f = 0, q = 0,i = 0;
    cout<<"欢迎进入打怪小游戏, 你有以下技能";
    cout<<endl;
    cout<<"此游戏没有存档!!!"<<endl;
    for (int i = 0; i < 1; i--){
        cout << "1.购买装备和恢复生命。" << endl;
        cout << "2.打怪专区。" << endl;
        cout << "3.个人信息。" << endl;
        cout << "4.开心扭蛋。" << endl;
        cout << "5.退出游戏。" << endl << "输入:";
        cin >> a;
        if (a == 1){
            while (1){
                cout << endl
                     << "你的信息:(生命" << shengming << "),(攻击" << gongji << "),(金币" << jinbi << "),(幸运值" <<yunqi<<")"<< endl;
                cout << "1.武器           金币>=10" << endl;
                cout << "2.生命           10金币+满" << endl;
                cout << "3.生命上限       金币>=40" << endl;
                cout << "4.闪避           金币>=15" << endl;
                cout << "5.运气值         金币>=5" <<endl;
                cout << "0.退出。" << endl
                     << "输入:";
                cin >> b;
                if (b == 1 && jinbi >= 10){
                    while (1){
                        cout << endl
                        << "1.木剑——10金币+7" << endl;
                        cout << "2.石剑——50金币+30" << endl;
                        cout << "3.铁剑——200金币+120" << endl;
                        cout << "4.钻石剑——1000金币+600" << endl;
                        cout << "0.退出。" << endl
                             << "输入:";
                        cin >> c;
                        if (c == 1 && jinbi >= 10){
                            gongji += 7;
                            jinbi -= 10;
                            cout << "攻击:" << gongji - 7 << "+7=" << gongji << endl;
                        }
                        else if (c == 2 && jinbi >= 50){
                            gongji += 30;
                            jinbi -= 50;
                            cout << "攻击:" << gongji - 30 << "+30=" << gongji << endl;
                        }
                        else if (c == 3 && jinbi >= 200){
                            gongji += 120;
                            jinbi -= 200;
                            cout << "攻击:" << gongji - 120 << "+120=" << gongji << endl;
                        }
                        else if (c == 4 && jinbi >= 1000){
                            gongji += 600;
                            jinbi -= 1000;
                            cout << "攻击:" << gongji - 600 << "+600=" << gongji << endl;
                        }
                        else if (c == 0){
                            break;
                        }
                        else if (c > 3){
                            cout << "没有这个按键。" << endl;
                        }
                        else{
                            cout << "金币不够。" << endl
                                 << endl;
                        }
                    }
                }
                else if (b == 2 && jinbi >= 10){
                    shengming = smsx;
                    jinbi -= 10;
                    cout << "生命=" << smsx << endl;
                }
                else if (b == 3 && jinbi >= 40){
                    while (1){
                        cout << endl
                             << "1.一级生命上限——40金币+30" << endl;
                        cout << "2.二级生命上限——100金币+60" << endl;
                        cout << "3.三级生命上限——400金币+200" << endl;
                        cout << "4.四级生命上限——2000金币+1000" << endl;
                        cout << "5.五级生命上限——6000金币+5000" << endl;
                        cout << "6.六级生命上限——10000金币+10000"<<endl; 
                        cout << "0.退出。" << endl
                             << "输入:";
                        cin >> c;
                        if (c == 1 && jinbi >= 40){
                            smsx += 30;
                            shengming += 30;
                            jinbi -= 40;
                            cout << "生命上限:" << smsx - 30 << "+30=" << smsx << endl;
                        }
                        else if (c == 2 && jinbi >= 100){
                            smsx += 60;
                            shengming += 60;
                            jinbi -= 100;
                            cout << "生命上限:" << smsx - 60 << "+60=" << smsx << endl;
                        }
                        else if (c == 3 && jinbi >= 400){
                            smsx += 200;
                            shengming += 200;
                            jinbi -= 400;
                            cout << "生命上限:" << smsx - 200 << "+200=" << smsx << endl;
                        }
                        else if (c == 4 && jinbi >= 2000){
                            smsx += 1000;
                            shengming += 1000;
                            jinbi -= 2000;
                            cout << "生命上限:" << smsx - 1000 << "+1000=" << smsx << endl;
                        }
                        else if (c == 5 && jinbi >= 6000){
                            smsx += 5000;
                            shengming += 5000;
                            jinbi -= 6000;
                            cout << "生命上限:" << smsx - 5000 << "+5000=" << smsx << endl;
                        }
                        else if (c == 6 && jinbi >= 10000){
                            smsx += 10000;
                            shengming += 10000;
                            jinbi -= 10000;
                            cout << "生命上限:" << smsx - 10000 << "+10000=" << smsx << endl;
                        }
                        else if (c == 0)
                        {
                            break;
                        }
                        else
                        {
                            cout << "金币不够。" << endl
                                 << endl;
                        }
                    }
                }
                else if (b == 4 && jinbi >= 15)
                {
                    while (1)
                    {
                        cout << endl
                             << "1.一级闪避——15金币+1" << endl;
                        cout << "2.二级闪避——150金币+10" << endl;
                        cout << "3.三级闪避——300金币+20" << endl;
                        cout << "4.四级闪避——1500金币+100" << endl;
                        cout << "0.退出。" << endl
                             << "输入:";
                        cin >> c;
                        if (c == 1 && jinbi >= 15){
                            shanbi += 1;
                            jinbi -= 15;
                            cout << "闪避次数:" << shanbi - 1 << "+1=" << shanbi << endl;
                        }
                        else if (c == 2 && jinbi >= 100){
                            shanbi += 10;
                            jinbi -= 150;
                            cout << "闪避次数:" << shanbi - 10 << "+10=" << shanbi << endl;
                        }
                        else if (c == 3 && jinbi >= 400)
                        {
                            shanbi += 20;
                            jinbi -= 300;
                            cout << "闪避次数:" << shanbi - 20 << "+20=" << shanbi << endl;
                        }
                        else if (c == 4 && jinbi >= 2000)
                        {
                            shanbi += 100;
                            jinbi -= 1500;
                            cout << "闪避次数:" << shanbi - 100 << "+100=" << shanbi << endl;
                        }
                        else if (c == 0)
                        {
                            break;
                        }
                        else
                        {
                            cout << "金币不够。" << endl
                                 << endl;
                        }
                    }
                }
                else if (b == 5)
                {
                    while(1)
                    {
                        cout<<"欢迎来购买运气值,这是价格表:"<<endl;
                        cout<<"1.运气棒棒糖——+1     需5金币"<<endl;
                        cout<<"2.运气蛋糕——+5       需20金币"<<endl;
                        cout<<"3.运气帝王蟹——+20    需50金币"<<endl;
                        cout<<"4.运气大套餐——+200   需400金币"<<endl; 
                        cout<<"0.退出"<<endl; 
                        cin>>f;
                        if(f == 0)
                        {
                            break;
                        }
                        if(f == 1 && jinbi >= 5)
                        {
                            cout<<"购买成功!运气值+1 "<<endl;
                            yunqi += 1;
                            jinbi -= 5;
                            cout<<"你的运气值是:"<<yunqi-1<<" + "<<" 1 "<<" = "<<yunqi<<endl;
                            cout<<"你还剩"<<jinbi<<"个金币"<<endl;
                        }
                        if(f == 2 && jinbi >= 20)
                        {
                            cout<<"购买成功!运气值+5 "<<endl;
                            yunqi += 5;
                            jinbi -= 20;
                            cout<<"你的运气值是:"<<yunqi-5<<" + "<<" 5 "<<" = "<<yunqi<<endl;
                            cout<<"你还剩"<<jinbi<<"个金币"<<endl;
                        } 
                        if(f == 3 && jinbi >= 50)
                        {
                            cout<<"购买成功!运气值+20 "<<endl;
                            yunqi += 20;
                            jinbi -= 50;
                            cout<<"你的运气值是:"<<yunqi-20<<" + "<<" 20 "<<" = "<<yunqi<<endl;
                            cout<<"你还剩"<<jinbi<<"个金币"<<endl;
                        } 
                        if(f == 4 && jinbi >= 200)
                        {
                            cout<<"购买成功!运气值+200 "<<endl;
                            yunqi += 200;
                            jinbi -= 400;
                            cout<<"你的运气值是:"<<yunqi-200<<" + "<<" 200 "<<" = "<<yunqi<<endl;
                            cout<<"你还剩"<<jinbi<<"个金币"<<endl;
                        } 
                        else
                        {
                            cout<<"金币不够!"<<endl; 
                        }
                    }
                }
                else if (b == 0)
                {
                       break;
                }
                else
                {
                    cout << "金币不够。" << endl;
                }
            }
        }
        else if (a == 2){
            while (1){
                cout << endl
                     << "1.地方小怪——生命20,攻击10,可获得金币10" << endl;
                cout << "2.地方大怪——生命100,攻击25,可获得金币100" << endl;
                cout << "3.小怪—— 生命500,攻击250, 可获得金币200 "<<endl;   
                cout << "4.大怪——生命1000,攻击500,可获得金币500" << endl;
                cout << "5.使者——生命5000,攻击1000,可获金币1000"<<endl; 
                cout << "6.魔王——生命10000,攻击2500,可获得金币2000,并通关游戏" << endl;
                cout << "0.退出。" << endl<< "输入:";
                cin >> b;
                if (b == 1){
                    int sm = 20, gj = 10;
                    int duobi = shanbi;
                    cout << endl
                         << "地方小怪——生命20,攻击10,可获得金币10" << endl
                         << "你的攻击力:" << gongji << "  "
                         << "生命力:" << shengming << endl
                         << endl
                         << "开始  你VS怪物" << endl;
                    while (sm > 0){
                        cout << endl
                             << "1.攻击 2.闪避(" << duobi << ")可让怪物生命-5" << endl
                             << "输入:";
                        cin >> c;
                        if (c == 1){
                            shengming -= gj;
                            sm -= gongji;
                            cout << endl
                                 << "我的生命:" << shengming << endl;
                            cout << "敌方生命:" << sm << endl;
                        }
                        else if (c == 2){
                            if (duobi > 0){
                                cout << "闪避成功。" << endl;
                                duobi--;
                                sm -= 5;
                                cout << "敌人生命:" << sm + 5 << "-5=" << sm << endl;
                            }
                            else if (duobi == 0){
                                cout << "闪避失败。 受到攻击" << endl;
                                shengming -= gj;
                                cout << endl
                                     << "我的生命:" << shengming << endl;
                                cout << "敌方生命:" << sm << endl;
                            }
                        }
                        else{
                            cout << "没有这个按键。" << endl;
                        }
                        if(sm <= 10 && sm > -500)
                        {
                            cout<<"地方小怪放大招了!你减10生命"<<endl; 
                            shengming -= 10; 
                            cout<<"你还剩"<<shengming<<"生命"<<endl;
                        } 
                        if (shengming <= 0){
                            cout << "你死了。" << endl;
                            return 0;
                        }
                    }
                    jinbi += 10;
                    cout << "恭喜您获得金币10" << endl;
                    int duobi1 = duobi; 
                }
                else if (b == 2){
                    cout << endl
                         << "地方大怪——生命100,攻击25,可获得金币25" << endl
                         << "你的攻击力:" << gongji << "  "
                         << "生命力:" << shengming << endl
                         << endl
                         << "开始  你VS怪物" << endl;
                    int sm = 100, gj = 25;
                    int duobi = shanbi;
                    while (sm > 0)
                    {
                        cout << endl
                             << "1.攻击 2.闪避(" << duobi << ")可让怪物生命-5" << endl
                             << "输入:";
                        cin >> c;
                        if (c == 1){
                            shengming -= gj;
                            sm -= gongji;
                            cout << endl
                                 << "我的生命:" << shengming << endl;
                            cout << "敌方生命:" << sm << endl;
                        }
                        else if (c == 2){
                            if (duobi > 0){
                                cout << "闪避成功。" << endl;
                                duobi--;
                                sm -= 5;
                                cout << "敌人生命:" << sm + 5 << "-5=" << sm << endl;
                            }
                            else if (duobi == 0){
                                cout << "闪避失败。 受到攻击" << endl;
                                shengming -= gj;
                                cout << endl
                                     << "我的生命:" << shengming << endl;
                                cout << "敌方生命:" << sm << endl;
                            }
                        }
                        else{
                            cout << "没有这个按键。" << endl;
                        }
                        if(sm <= 30 && sm > -1000)
                        {
                            cout<<"地方大怪放大招了!你减30生命"<<endl; 
                            shengming -= 30; 
                            cout<<"你还剩"<<shengming<<"生命"<<endl;
                        }
                        if (shengming <= 0){
                            cout << "你死了。" << endl;
                            return 0;
                        }
                    }
                    jinbi += 100;
                    cout << "恭喜您获得金币100" << endl;
                    int duobi1 = duobi; 
                }
                else if (b == 3){
                    cout << endl
                         << "小怪——生命500,攻击250,可获得金币200" << endl
                         << "你的攻击力:" << gongji << "  "
                         << "生命力:" << shengming << endl
                         << endl
                         << "开始  你VS怪物" << endl;
                    int sm = 500, gj = 250;
                    int duobi = shanbi;
                    while (sm > 0){
                        cout << endl
                             << "1.攻击 2.闪避(" << duobi << ")可让怪物生命-5" << endl
                             << "输入:";
                        cin >> c;
                        if (c == 1){
                            shengming -= gj;
                            sm -= gongji;
                            cout << endl
                                 << "我的生命:" << shengming << endl;
                            cout << "敌方生命:" << sm << endl;
                        }
                        else if (c == 2){
                            if (duobi > 0){
                                cout << "闪避成功。" << endl;
                                duobi--;
                                sm -= 5;
                                cout << "敌人生命:" << sm + 5 << "-5=" << sm << endl;
                            }
                            else if (duobi == 0){
                                cout << "闪避失败。 受到攻击" << endl;
                                shengming -= gj;
                                cout << endl
                                     << "我的生命:" << shengming << endl;
                                cout << "敌方生命:" << sm << endl;
                            }
                        }
                        else{
                            cout << "没有这个按键。" << endl;
                        }
                        if(sm <= 50 && sm > -1500)
                        {
                            cout<<"小怪放大招了!你减300生命"<<endl; 
                            shengming -= 300; 
                            cout<<"你还剩"<<shengming<<"生命"<<endl;
                        } 
                        if (shengming <= 0){
                            cout << "你死了。" << endl;
                            return 0;
                        }
                    }
                    jinbi += 200;
                    cout << "恭喜您获得金币200" << endl;
                    int duobi1 = duobi; 
                }
                else if (b == 4){
                    cout << endl
                         << "大怪——生命1000,攻击500,可获得金币500" << endl
                         << "你的攻击力:" << gongji << "  "
                         << "生命力:" << shengming << endl
                         << endl
                         << "开始  你VS怪物" << endl;
                    int sm = 1000, gj = 500;
                    int duobi = shanbi;
                    while (sm > 0){
                        cout << endl
                             << "1.攻击 2.闪避(" << duobi << ")可让怪物生命-5" << endl
                             << "输入:";
                        cin >> c;
                        if (c == 1){
                            shengming -= gj;
                            sm -= gongji;
                            cout << endl
                                 << "我的生命:" << shengming << endl;
                            cout << "敌方生命:" << sm << endl;
                        }
                        else if (c == 2){
                            if (duobi > 0){
                                cout << "躲避成功。" << endl;
                                duobi--;
                                sm -= 5;
                                cout << "敌人生命:" << sm + 5 << "-5=" << sm << endl;
                            }
                            else if (duobi == 0){
                                cout << "闪避失败。 受到攻击" << endl;
                                shengming -= gj;
                                cout << endl
                                     << "我的生命:" << shengming << endl;
                                cout << "敌方生命:" << sm << endl;
                            }
                        }
                        else{
                            cout << "没有这个按键。" << endl;
                        }
                        if(sm <= 150 && sm > -2000)
                        {
                            cout<<"大怪放大招了!你减700生命"<<endl; 
                            shengming -= 700; 
                            cout<<"你还剩"<<shengming<<"生命"<<endl;
                        } 
                        if (shengming <= 0){
                            cout << "你死了。" << endl;
                            system("pause");
                            return 0;
                        }
                    }
                    jinbi += 500;
                    cout << "恭喜您获得金币500" << endl;
                    int duobi1 = duobi; 
                }
                else if (b == 5){
                    cout << endl
                         << "使者——生命5000,攻击1000,可获得金币1000" << endl
                         << "你的攻击力:" << gongji << "  "
                         << "生命力:" << shengming << endl
                         << endl
                         << "开始  你VS怪物" << endl;
                    int sm = 5000, gj = 1000;
                    int duobi = shanbi;
                    while (sm > 0){
                        cout << endl
                             << "1.攻击 2.闪避(" << duobi << ")可让怪物生命-5" << endl
                             << "输入:";
                        cin >> c;
                        if (c == 1){
                            shengming -= gj;
                            sm -= gongji;
                            cout << endl
                                 << "我的生命:" << shengming << endl;
                            cout << "敌方生命:" << sm << endl;
                        }
                        else if (c == 2){
                            if (duobi > 0){
                                cout << "躲避成功。" << endl;
                                duobi--;
                                sm -= 5;
                                cout << "敌人生命:" << sm + 5 << "-5=" << sm << endl;
                            }
                            else if (duobi == 0){
                                cout << "闪避失败。 受到攻击" << endl;
                                shengming -= gj;
                                cout << endl
                                     << "我的生命:" << shengming << endl;
                                cout << "敌方生命:" << sm << endl;
                            }
                        }
                        else{
                            cout << "没有这个按键。" << endl;
                        }
                        if(sm <= 660 && sm > -5000)
                        {
                            cout<<"使者放大招了!你减1500生命,金币减少200"<<endl; 
                            shengming -= 1500; 
                            jinbi -= 200;
                            cout<<"你还剩"<<jinbi<<"金币"<<endl;
                            cout<<"你还剩"<<shengming<<"生命"<<endl;
                        } 
                        if (shengming <= 0){
                            cout << "你死了。" << endl;
                            return 0;
                        }
                    }
                    jinbi += 1000;
                    cout << "恭喜您获得金币1000" << endl;
                    int duobi1 = duobi; 
                }
                else if (b == 6){
                    cout << endl
                         << "魔王——生命10000,攻击2500,可获得金币2000,并通关游戏" << endl
                         << "你的攻击力:" << gongji << "  "
                         << "生命力:" << shengming << endl
                         << endl
                         << "开始  你VS魔王" << endl;
                    int sm = 10000, gj = 2500;
                    int duobi = shanbi;
                    while (sm > 0){
                        cout << endl
                             << "1.攻击 2.闪避(" << duobi << ")可让魔王生命-5" << endl
                             << "输入:";
                        cin >> c;
                        if (c == 1){
                            shengming -= gj;
                            sm -= gongji;
                            cout << endl
                                 << "我的生命:" << shengming << endl;
                            cout << "魔王生命:" << sm << endl;
                        }
                        else if (c == 2){
                            if (duobi > 0){
                                cout << "闪避成功。" << endl;
                                duobi--;
                                sm -= 5;
                                cout << "魔王生命:" << sm + 5 << "-5=" << sm << endl;
                            }
                            else if (duobi == 0){
                                cout << "闪避失败。 受到攻击" << endl;
                                shengming -= gj;
                                cout << endl
                                     << "我的生命:" << shengming << endl;
                                cout << "敌方生命:" << sm << endl;
                            }
                        }
                        else
                        {
                            cout << "没有这个按键。" << endl;
                        }
                        if(sm <= 1500 )
                        {
                            cout<<"我死了,你也别想活!你减5000生命"<<endl; 
                            shengming -= 5000; 
                            cout<<"你还剩"<<shengming<<"生命"<<endl;
                        } 
                        if (shengming <= 0){
                            cout << "你死了。"<<"你的生命为" <<shengming<< endl;
                            return 0;
                        }
                    }
                    jinbi += 2000;
                    cout << "恭喜您打败魔王,通关了游戏!" << endl;
                    cout << "是否退出?"<<endl;
                    cout << "是——1, 继续——0"<<endl;
                    cin>>q; 
                    if(q == 1)
                    {
                        cout<<"已退出"<<endl;
                        return 0; 
                    }
                    if(q == 0)
                    {
                        cout<<"一路走好!"; 
                    }
                    else
                    {
                        cout<<"没有这个按键, 请重新输入:";
                        cin>>q; 
                    }
                }
                else if (b == 0){
                    break;
                }
            }
        }
        else if (a == 3){
            cout << endl; 
            cout << "你的生命:" << shengming << endl;
            cout << "你的攻击:" << gongji << endl;
            cout << "你的金币:" << jinbi << endl;
            cout << "闪避次数:" << shanbi << endl;
            cout << "生命上限:" << smsx << endl;
            cout << "你的运气:" << yunqi << endl; 
        }
        else if (a == 4){
            while(1)
            {
                cout<<"欢迎来玩扭蛋,500一次(多买一点运气值,有助于抽到大奖哦~~)"<<endl;
                cout<<"1——参加, 0——退出"<<endl<<endl;
                cin>>e;
                if(e == 1 && jinbi >= 500)
                {
                    jinbi -= 500;
                    cout<<"正在扭蛋中......"<<endl<<endl;
                    if(yunqi <= 20)
                    {
                        cout<<"哎呦!运气有点差(快去买运气值),只获得了100攻击力"<<endl<<endl;
                        gongji += 100; 
                        cout<<"你的攻击为"<<gongji<<endl;
                        cout<<"你还剩"<<jinbi<<"个金币"<<endl; 
                        times += 1;
                    } 
                    else if(yunqi > 20 && yunqi <= 50)
                    {
                        cout<<"恭喜抽中三等奖 获得400金币!"<<endl<<endl;
                        jinbi += 400;
                        cout<<"你的金币数为"<<jinbi<<endl; 
                        cout<<"你还剩"<<jinbi<<"个金币"<<endl;
                        times += 1;
                    }
                    else if(yunqi > 50 && yunqi <= 220)
                    {
                        cout<<"恭喜抽中二等奖 获得300生命上限!"<<endl<<endl;
                        smsx += 300 ;
                        cout<<"你的生命上限为"<<smsx<<endl; 
                        cout<<"你还剩"<<jinbi<<"个金币"<<endl;
                        times ++;
                    }
                    else if(yunqi > 220 && yunqi <= 1000)
                    {
                        cout<<"恭喜抽中一等奖 获得1200金币!"<<endl<<endl;
                        jinbi  += 1200;
                        cout<<"你的金币数为"<<jinbi<<endl; 
                        times += 1;
                    }
                    else if(yunqi > 1000)
                    {
                        cout<<"恭喜抽中特等奖 获得2000生命上线"<<endl<<endl;
                        smsx += 2000; 
                        cout<<"你的生命上限为"<<smsx<<endl;
                        cout<<"你还剩"<<jinbi<<"个金币"<<endl; 
                        times += 1;
                    }
                } 
                    if(times > 10)
                    {
                        cout<<"年轻人,你太贪心了,你会恶人有恶报的!你将减少以7000金币"<<endl<<endl;
                        jinbi -= 7000;
                        cout<<"你还剩"<<jinbi<<"枚金币" <<endl<<endl;
                        times -= 9; 
                        break;
                    }
                else if(e == 0) 
                {
                    break;
                }
                else
                {
                    cout<<"金币不足, 请退出"<<endl;
                }
            }
        }
        else if (a == 5){
            cout<<"你确定要退出吗? 0——退出, 1——继续"<<endl<<"请输入:";
            cin>>i;
            if(i == 0) 
            {
                cout<<"游戏已退出";
                return 0; 
            }
            if(i == 1)
            {
                cout<<"请继续游玩"; 
            } 
            else
            {
                cout<<"没有这个按键"; 
                cin>>i;
            }
        }
    }
    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个简单的 C++游戏代码示例: ```c++ #include <iostream> #include <cstdlib> #include <ctime> #include <conio.h> using namespace std; // 定义玩家和物的属性 int player_hp = 100; int player_attack = 10; int player_defense = 5; int monster_hp = 50; int monster_attack = 8; int monster_defense = 3; // 定义游戏地图和场景 char map[10][10] = { {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#'}, {'#', '.', '.', '.', '.', '.', '.', '.', '.', '#'}, {'#', '.', '#', '#', '#', '#', '.', '#', '.', '#'}, {'#', '.', '.', '.', '.', '.', '.', '#', '.', '#'}, {'#', '.', '#', '#', '#', '.', '#', '#', '.', '#'}, {'#', '.', '.', '.', '#', '.', '.', '.', '.', '#'}, {'#', '#', '#', '.', '#', '#', '#', '#', '.', '#'}, {'#', '.', '.', '.', '.', '.', '.', '.', '.', '#'}, {'#', '.', '#', '#', '#', '#', '.', '#', '.', '#'}, {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#'} }; // 定义玩家和物的位置 int player_x = 1; int player_y = 1; int monster_x = 3; int monster_y = 5; // 定义游戏结束标志 bool game_over = false; // 函数声明 void draw_map(); void move_player(char dir); void move_monster(); void check_collision(); void check_game_over(); int main() { // 初始化随机数种子 srand(time(NULL)); // 显示游戏地图和玩家位置 draw_map(); // 游戏循环 while (!game_over) { // 玩家移动 char dir = getch(); move_player(dir); // 物移动 move_monster(); // 检测碰撞和战斗 check_collision(); // 检测游戏结束 check_game_over(); } return 0; } // 绘制游戏地图和玩家位置 void draw_map() { for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { if (i == player_y && j == player_x) { cout << 'P'; } else if (i == monster_y && j == monster_x) { cout << 'M'; } else { cout << map[i][j]; } } cout << endl; } } // 玩家移动 void move_player(char dir) { int new_x = player_x; int new_y = player_y; switch (dir) { case 'w': new_y--; break; case 's': new_y++; break; case 'a': new_x--; break; case 'd': new_x++; break; default: break; } // 检测新位置是否合法 if (map[new_y][new_x] != '#') { player_x = new_x; player_y = new_y; } } // 物移动 void move_monster() { int dir = rand() % 4; int new_x = monster_x; int new_y = monster_y; switch (dir) { case 0: new_y--; break; case 1: new_y++; break; case 2: new_x--; break; case 3: new_x++; break; default: break; } // 检测新位置是否合法 if (map[new_y][new_x] != '#') { monster_x = new_x; monster_y = new_y; } } // 检测碰撞和战斗 void check_collision() { if (player_x == monster_x && player_y == monster_y) { // 计算伤害和防御 int damage_to_monster = player_attack - monster_defense; int damage_to_player = monster_attack - player_defense; // 更新血量 player_hp -= damage_to_player; monster_hp -= damage_to_monster; // 输出战斗结果 cout << "You hit the monster for " << damage_to_monster << " damage." << endl; cout << "The monster hit you for " << damage_to_player << " damage." << endl; } } // 检测游戏结束 void check_game_over() { if (player_hp <= 0) { cout << "You died. Game over." << endl; game_over = true; } else if (monster_hp <= 0) { cout << "You killed the monster. You win!" << endl; game_over = true; } } ``` 这个示例代码使用了基本的 C++ 语法和函数,包括数组、循环、条件语句、随机数、键盘输入等等。您可以根据自己的需要进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值