Консольная игра «Epic Dungeon»

Сделал консольную игру на C ++ под названием «Epic dungeon», которую мой друг сделал год назад на JS. Я решил сделать это, чтобы набраться опыта (на самом деле, я сделал это, потому что я бы перестал изучать C ++, если бы я что-то не делал, потому что учиться так долго и ничего не делать может быстро наскучить) Я сделал это без используя ООП, потому что я еще не очень хорошо его знаю. Иногда вам может казаться, что я был на ЛСД, но это не так. Сразу отвечу на вопрос: «Почему я сделал выбор переменной char?» Потому что, если вы сделаете переменную int и напишете любой символ (вы можете сделать это случайно, и это будет обидно, потому что вам придется перезапускать), тогда будет бесконечная рекурсия функций, а если это char, то этого не произойдет. Жду критики в адрес игры и моего кода 🙂

#include <iostream>
#include <cstdlib>
#include <cmath>
#include <string>
using std::cout;
using std::cin;
using std::string;

double hp = 60.0, damage = 15.0, shield = 100.0;
double en_hp = 50.0, en_damage = 10.0, en_shield = 100.0;
double boss_hp = 1000.0, boss_damage = 250.0, boss_shield = 150.0;
int boss_lvl = 1;
int en_level = 1, hero_level = 1, energy = 6, gold = 0, skills = 0;
int experience = 0, new_lvl_xp = 10;
double en_fight_hp = en_hp;
double hero_fight_hp = hp;
int fight_energy = energy;
double boss_fight_hp = boss_hp;


int startGame();                                    //Game start functionality
void mainMenu();                                    //Main menu
void menuSkills();                                  //Skill menu
void menuHero();                                    //Hero menu
void menuLvl();                                     //Progress menu
void menuDev();                                     //Creators menu

void imgFightBoss();                                //Boss Fight Visual
void imgFightNPC();                                 //Dungeon NPC combat visual
double fightNPC(double x, double y, double z);      //Functionality of the fight with NPCs from the dungeon
double fightHeroNpc(double x, double y, double z);  //The functionality of the battle with the Hero in the dungeon

void fightAttack();                                 //Simple attack function in combat
void fightDodge();                                  //Dodge attack function in combat
void fightShield();                                 //Shield function in battle
void fightHeal();                                   //Combat healing function

void fightAttackBoss();                             //Simple attack function in boss fight
void fightDodgeBoss();                              //Dodge attack function in boss fight
void fightShieldBoss();                             //Shield function in boss fight
void fightHealBoss();                               //Boss Fight Healing Function

void winBattleNPC();                                //Victory in a battle with an NPC
void winBattleBoss();                               //Winning a Boss Fight


int main() {
    system("chcp 1251>nul");
    startGame();
    return 0;
}


void mainMenu() {               //Main menu with a choice of actions
    cout << "Choose an action:nGo to the boss: 1nGo to the dungeon: 2nSkills: 3nHero characteristics: 4nExit to the start menu: 5n";
    char choose;
    cin >> choose;
    switch (choose) {
        case '1':
            imgFightBoss();
            break;
        case '2':
            imgFightNPC();
            break;
        case '3':
            menuSkills();
            break;
        case '4':
            
            menuHero();
            break;
        case '5':
            startGame();
            break;
        default:
            cout << "ttttttI do not understand what you want?n";
            mainMenu();
            break;
    }

}

int startGame() {               //Game start functionality
    cout << "Start the game: 1nQuit the game: 2nCreators: 3n";
    char choose;
    cin >> choose;
    switch (choose) {
        case '1':
            mainMenu();
            break;
        case '2':
            return 0;
            break;
        case '3':
            menuDev();
            break;
        default:
            cout << "ttttttI don't understand what is required!n";
            startGame();
    }
    
}


    
void menuSkills() {             //Skills menu
    cout << "ttttttt  Skill points: " << skills << "n";
    cout << "ttttttHealth: " << hp << " Damage: " << damage << " Protection: " << shield << "n";
    cout << "Select an action:nImprove health skill: 1nIncrease attack skill: 2nIncrease Armor Skill: 3nBack: 4n";
    char choose;
    cin >> choose;
    switch (choose) {
        case '1':
            if (skills >= 1) {
                hp += 10.0;
                skills -= 1;
                hero_fight_hp = hp;
                menuSkills();
            }   
            else {
                cout << "tttttt   Not enough skill points!n";
                menuSkills();
            }
            break;
        case '2':
            if (skills >= 1) {
                damage += 3.0;
                skills -= 1;
                menuSkills();
            }
            else {
                cout << "tttttt   Not enough skill points!n";
                menuSkills();
            }
            break;
        case '3':
            if (skills >= 1) {
                if (shield < 500) {
                    shield += 12.5;
                    skills -= 1;
                    menuSkills();
                }
                else {
                    cout << "ttttttMaximum armor value reached!n";
                    menuSkills();
                }
            }
            else {
                cout << "tttttt   Not enough skill points!n";
                menuSkills();
            }
            break;
        case '4':
            mainMenu();
            break;
        default:
            cout << "ttttttI don't understand what is required of men";
            menuSkills();
            break;
    }
}
void menuHero() {           //Hero menu
    cout << "tttttt ____________________n";
    cout << "tttttt|                    |n";
    cout << "tttttt  Level:         " << hero_level << "n";
    cout << "tttttt  Skill points:  " << skills << "n";
    cout << "tttttt  Health:        " << hp << "n";
    cout << "tttttt  Damage:        " << damage << "n";
    cout << "tttttt  Protection:    " << shield << "n";
    cout << "tttttt|____________________|nn";
    cout << "tttttView character level progress: 1n";
    cout << "tttttExit to the main menu:                 2n";
    char choose;
    cin >> choose;
    switch (choose) {
        case '1':
            menuLvl();
            break;
        case '2':
            mainMenu();
            break;
        default:
            cout << "ttttt    I don't understand what is required of men";
            menuHero();
            break;
    }
}
void menuLvl() {
    cout << "tttttt ___________________n";
    cout << "tttttt|                   |n";
    cout << "tttttt      Level: " << hero_level << "n";
    cout << "tttttt   Level progress:n";
    cout << "tttttt        " << experience << "/" << new_lvl_xp << "n";
    cout << "tttttt|___________________|n";
    cout << "tttttt       Back: 1n";
    char choose;
    cin >> choose;
    switch (choose) {
        case '1':
            menuHero();
            break;
        default:
            cout << "ttttt    I don't understand what is required of men";
            menuLvl();

    }
}


void menuDev() {
    cout << "tttttt ____________________n";
    cout << "tttttt|                    |n";
    cout << "tttttt      Idea creator: n";
    cout << "tttttt   Rakhimzhanov Glebn";
    cout << "tttttt      Developer: n";
    cout << "tttttt       TuleKr1sn";
    cout << "tttttt|____________________|n";
    cout << "tttttt     Back: 1n";
    char choose;
    cin >> choose;
    switch (choose) {
    case '1':
        startGame();
        break;
    default:
        cout << "ttttt    I don't understand what is required of men";
        menuDev();

    }
}

void imgFightNPC() {                //displaying information on the battle with npc (yes, I know it's so awful)
    if (en_fight_hp > 0) {
        cout << "ttttt _______________________________________n";
        cout << "ttttt|                                       |n";
        cout << "ttttt|                                       |n";
        cout << "ttttt|                                       |n";
        cout << "tttt                            YOUn";
        cout << "tttt                         energy: " << fight_energy << "n";
        cout << "ttttn";
        cout << "tttt                        hp      : " << hero_fight_hp << "n";
        cout << "tttt                        power   : " << damage << "n";
        cout << "tttt                        shield  : " << shield << "n";
        cout << "tttt         __                                   __n";
        cout << "tttt        |                                       |n";
        cout << "tttt        |  |=================|  |============|  |n";
        cout << "tttt        |  |Attack:         1|  |Shield:    3|  |n";
        cout << "tttt        |  |=================|  |============|  |n";
        cout << "tttt        |  |Attack + Dodge: 2|  |Healing:   4|  |n";
        cout << "tttt        |  |=================|  |============|  |n";
        cout << "tttt        |                                       |n";
        cout << "tttt        |__                                   __|n";
        cout << "tttt                             DEVILn";
        cout << "tttt                          level: " << en_level << "n";
        cout << "ttttn";
        cout << "tttt                        hp      : " << en_fight_hp << "n";
        cout << "tttt                        power   : " << en_damage << "n";
        cout << "tttt                        shield  : " << en_shield << "n";
        cout << "ttttt|                                       |n";
        cout << "ttttt|                                       |n";
        cout << "ttttt|_______________________________________|n";
        char choose;
        cin >> choose;
        switch (choose) {
        case '1':           //attack

            fightAttack();
            imgFightNPC();
            break;
        case '2':           //attack + dodge

            fightDodge();
            imgFightNPC();
            break;
        case '3':           //shield

            fightShield();
            imgFightNPC();
            break;
        case '4':           //healing

            fightHeal();
            imgFightNPC();
            break;
        default:

            cout << "ttttt  I don't understand what is required of men";
            imgFightNPC();
            break;
        }
    }
    else {

        winBattleNPC();
        mainMenu();

    }
}
string boss_kind = "Satan";
void imgFightBoss() {                       //displaying information on the battle with boss

    if (boss_lvl > 3) {

        cout << "ttttttYou have defeated all possible bosses!n";
        mainMenu();

    }


    if (boss_fight_hp > 0) {
        cout << "ttttt _______________________________________n";
        cout << "ttttt|                                       |n";
        cout << "ttttt|                                       |n";
        cout << "ttttt|                                       |n";
        cout << "tttt                            YOUn";
        cout << "tttt                         energy: " << fight_energy << "n";
        cout << "ttttn";
        cout << "tttt                        hp      : " << hero_fight_hp << "n";
        cout << "tttt                        power   : " << damage << "n";
        cout << "tttt                        shield  : " << shield << "n";
        cout << "tttt         __                                   __n";
        cout << "tttt        |                                       |n";
        cout << "tttt        |  |=================|  |============|  |n";
        cout << "tttt        |  |Attack:         1|  |Shield:    3|  |n";
        cout << "tttt        |  |=================|  |============|  |n";
        cout << "tttt        |  |Attack + Dodge: 2|  |Healing:   4|  |n";
        cout << "tttt        |  |=================|  |============|  |n";
        cout << "tttt        |                                       |n";
        cout << "tttt        |__                                   __|n";
        cout << "tttt                            BOSSn";
        cout << "tttt                         kind: " << boss_kind << "n";
        cout << "ttttn";
        cout << "tttt                        hp      : " << boss_fight_hp << "n";
        cout << "tttt                        power   : " << boss_damage << "n";
        cout << "tttt                        shield  : " << boss_shield << "n";
        cout << "ttttt|                                       |n";
        cout << "ttttt|                                       |n";
        cout << "ttttt|_______________________________________|n";
        char choose;
        cin >> choose;
        switch (choose) {
        case '1':           //attack

            fightAttackBoss();
            imgFightBoss();

            break;
        case '2':           //attack + dodge

            fightDodgeBoss();
            imgFightBoss();

            break;
        case '3':           //shield

            fightShieldBoss();
            imgFightBoss();

            break;
        case '4':           //healing

            fightHealBoss();
            imgFightBoss();

            break;
        default:

            cout << "ttttt  I don't understand what is required of me!n";
            imgFightNPC();
            break;
        }
    }
    else {

        winBattleBoss();
        mainMenu();

    }
}

void fightAttack() {                //attack function

    if (fight_energy == 0) {
        cout << "ttttNot enough energy! Use your shield to boost her!n";
        imgFightNPC();
    }

    en_fight_hp = fightNPC(en_fight_hp, damage, en_shield);
    hero_fight_hp = fightHeroNpc(hero_fight_hp, en_damage, shield);
    --fight_energy;

    if (hero_fight_hp <= 0 && en_fight_hp > 0) {
        cout << "ttttttYou have failed!n";
        fight_energy = energy;
        hero_fight_hp = hp;
        en_fight_hp = en_hp;
        mainMenu();
    }

}

void fightAttackBoss() {            //attack function with boss
    if (fight_energy == 0) {
        cout << "ttttNot enough energy! Use your shield to boost her!n";
        imgFightBoss();
    }

    boss_fight_hp = fightNPC(boss_fight_hp, damage, boss_shield);
    hero_fight_hp = fightHeroNpc(hero_fight_hp, boss_damage, shield);
    --fight_energy;

    if (hero_fight_hp <= 0 && boss_fight_hp > 0) {
        cout << "ttttttYou have failed!n";
        fight_energy = energy;
        hero_fight_hp = hp;
        boss_fight_hp = boss_hp;
        mainMenu();
    }
}

void fightDodge() {                 //attack + dodge function

    if (fight_energy <= 1) {
        cout << "ttttNot enough energy! Use your shield to boost her!n";
        imgFightNPC();
    }

    en_fight_hp = fightNPC(en_fight_hp, damage, en_shield);
    fight_energy -= 2;


}

void fightDodgeBoss() {             //attack + dodge funcion with boss
    if (fight_energy <= 1) {
        cout << "ttttNot enough energy! Use your shield to boost her!n";
        imgFightBoss();
    }

    boss_fight_hp = fightNPC(boss_fight_hp, damage, boss_shield);
    fight_energy -= 2;

}

void fightShield() {                //shield function

    ++fight_energy;
    hero_fight_hp = fightHeroNpc(hero_fight_hp, en_damage, shield);

    if (hero_fight_hp <= 0 && en_fight_hp > 0) {
        cout << "ttttttYou have failed!n";
        fight_energy = energy;
        hero_fight_hp = hp;
        en_fight_hp = en_hp;
        mainMenu();
    }

}

void fightShieldBoss() {

    ++fight_energy;
    hero_fight_hp = fightHeroNpc(hero_fight_hp, boss_damage, shield);

    if (hero_fight_hp <= 0 && boss_fight_hp > 0) {
        cout << "ttttttYou have failed!n";
        fight_energy = energy;
        hero_fight_hp = hp;
        boss_fight_hp = boss_hp;
        mainMenu();
    }

}

void fightHeal() {                  //healing function

    if (fight_energy <= 1) {
        cout << "ttttNot enough energy! Use your shield to boost her!n";
        imgFightNPC();
    }

    hero_fight_hp += 20.0;
    fight_energy -= 2;

}

void fightHealBoss() {              //healing function with boss

    if (fight_energy <= 1) {
        cout << "ttttNot enough energy! Use your shield to boost her!n";
        imgFightBoss();
    }

    hero_fight_hp += 100.0;
    fight_energy -= 2;

}

void winBattleNPC() {               //battle victory function with NPC

    int xp = rand() % 5 + 1;            //obtained in battle
    cout << "tttttt  You have won! Received:n";
    cout << "tttttt     Skill points: 5n";
    cout << "tttttt     Experience: " << xp << "n";
    experience += xp;                   //received in battle, add to the total amount
    if (experience >= new_lvl_xp) {
        cout << "ttttttYou've got a new level!n";
        ++hero_level;
        experience -= new_lvl_xp;
        new_lvl_xp += (new_lvl_xp / 4);
    }
    en_hp += 40;
    en_damage += 15;
    ++en_level;
    skills += 5;
    fight_energy = energy;
    en_fight_hp = en_hp;
    hero_fight_hp = hp;


}

void winBattleBoss() {              //battle victory function with Boss


    int xp = rand() % 10 + 10;          //obtained in battle
    cout << "tttttt  You have won! Received:n";
    cout << "tttttt     Skill points: 10n";
    cout << "tttttt     Experience: " << xp << "n";
    experience += xp;                   //received in battle, add to the total amount
    if (experience >= new_lvl_xp) {
        cout << "ttttttYou've got a new leveln";
        ++hero_level;
        experience -= new_lvl_xp;
        new_lvl_xp += (new_lvl_xp / 4);
    }
    ++boss_lvl;

    if (boss_lvl == 1) {
        boss_kind = "Satan";
    }
    else if (boss_lvl == 2) {
        boss_kind = "Lucifer";
        boss_hp = 2000;
        boss_damage = 500;
        boss_shield = 200;
    }
    else if (boss_lvl == 3) {
        boss_kind = "Leviathan";
        boss_hp = 4000;
        boss_damage = 800;
        boss_shield = 300;
    }

    if (boss_lvl > 3) {
        cout << "ttttttCongratulations! Defeated all possible bosses!n";
    }
    skills += 10;
    fight_energy = energy;
    boss_fight_hp = boss_hp;
    hero_fight_hp = hp;


}

//I know it could be easier, but it's easier for me this way (I love shit code)

double fightNPC(double x, double y, double z) {             //change enemy hp in fight
    x -= y * (1 - z / 1000);                                    //x - enemy hp, y - hero damage, z - enemy shield
    return x;
}
double fightHeroNpc(double x, double y, double z) {         //change hero hp in fight
    x -= y * (1 - z / 1000);                                    //x - hero hp, y - enemy damage, z - hero shield
    return x;
}

1 ответ
1

Пока только поверхностный обзор:

  • Если вы действительно хотите изучить C ++, вам следует начать с ООП. В то время как C ++ технически является многопарадигмальным языком, большая часть причин использовать C ++ вместо C связана с его ООП.
  • И наоборот, если вас на самом деле не волнует ООП, это должен быть C, а не C ++ (в любом случае это почти C).
  • Независимо от того, используете ли вы C или C ++, инкапсуляция вашего списка переменных состояния игры в реентерабельной структуре вместо глобальных улучшит структуру вашей программы.
  • Форматирование с глубоко вложенными t персонажей опрометчиво. Вкладки экологически зависимы, поэтому они могут отлично смотреться на вашей консоли и мусорить на чужой. Вместо этого предпочитайте пробелы.

  • 1

    ...Prefer spaces instead [of tabs] Этот!

    — Кейси

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *