C++有人能看看我完整的基于文本的新手rpg游戏代码吗

C++ Can anyone look over my complete newbie text based rpg game code?

本文关键字:文本 新手 rpg 代码 游戏 看我 C++ 于文本      更新时间:2023-10-16

我是一个全新的程序员,使用c++的时间可能不到两周,我自学了很多if语句和switch。

如果有人能看看我一直在做的这场比赛,告诉我所有应该做和不应该做的事情,那就太好了。

请注意,if语句的第二个踪迹尚未编码。

#include "MeadowGiant.h"
#include <iostream>
#include <string>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
MeadowGiant::MeadowGiant()
{
int trail, caveEnter;
int fight1;
int loot;
int goblinAttack, goblinHP, HP, attack, action1, ability; 
goblinAttack = 5;
goblinHP = 10;
HP = 100;
std::cout << -Insert story narrative- Choose a trail to go down.n";
std::cin  >> trail; 
    if(trail==1)
    {
    std::cout << "-Insert story narrative- Come across a cave. Do you go in?n";
    std::cin  >> caveEnter;
        if(caveEnter==1)
        {
        std::cout << "You are ambushed by a goblin. Run or fight?n";
        std::cin >> fight1;
        goblinFight:
        loot = rand() % 4 + 1;
        srand(time(NULL));           
        if(fight1==1)
        {
        std::cout   << "Your HP: " << HP << std::endl;
        std::cout   << "Enemy HP: " << goblinHP << std::endl;
        std::cout   << "What do you do?n
                    << "[1] Attack [2] Run n";
        std::cin    >> action1;
        }
            if(goblinHP<=0)
            {
            std::cout << "You have slain the goblin!n" 
                      << "You head towards the chest and take the spoilsn"
                      << "of battle!n";
                 switch(loot)
            {
            case 1: std::cout << "You find a bastard sword!n"
                              attack = attack + 7;
                              goto exitCave;
            case 2: std::cout << "You find an Enchanted Staff!n"
                              attack = attack + 10;
                              goto exitCave;
            case 3: std::cout << "You find an Obsidian Dagger!n"
                              attack = attack + 9;
                              goto exitCave;
            case 4: std::cout << "You find a Holy Mace!n"
                              attack = attack + 10;
                              goto exitCave;
            }
    else if(action1==1)
    {
    std::cout   << "You successfully hit the goblin!n"
                << "He strikes back!n";
    attack = rand() % 10 + 1;  
    srand(time(NULL));
    goblinHP = goblinHP - attack;
    HP = HP - goblinAttack;
    goto goblinFight;
    }
    else if(action==2)
    {
    std::cout   << "You take the cowards way out and leave the cave.n";
    goto exitCave;
    }
    }
else if(caveEnter==2)
{
exitCave:
std::cout << "You have exited the cave.n";
} 
else
{
goto exitCave;
}
}
}

这是草地巨人。

#define MEADOWGIANT_H
class MeadowGiant
{
public:
    MeadowGiant();
protected:
};
#endif`
#ifndef MEADOWGIANT_H
#define MEADOWGIANT_H
class MeadowGiant
{
public:
    MeadowGiant();
protected:
};
#endif

这就是我在main.cpp 中真正做的一切

#include <iostream>
#include <string>
#include "MeadowGiant.h"
int main()
{
MeadowGiant obj1;
}

唯一让我觉得不好的是使用goto。例如,在switch语句中,您可以使用break跳出。

您应该检查输入是否存在切换条件。如果有人按下另一个类似"9"的键会发生什么?它将跳过整个if条件部分。第二个std::cin >> action1;动作1是一个整数,如果有人输入了一个字母,你的程序就会出错。

我推荐的方法是:

fail:char select = _getch() - '0';
                switch (select) {
                case 1: {....; break;}
                case 2: {....; break;}
                default: goto fail;
                }

 srand(time(NULL));  // <-- first the random seed
 attack = rand() % 10 + 1;  // <-- then the random function
else if(caveEnter==2)
{
exitCave:
std::cout << "You have exited the cave.n";
} 
else
{
goto exitCave;
}

您可以将其简化为

else {
exitCave:
    std::cout << "You have exited the cave.n";
}

此外,不建议使用goto。在switch中,使用break