C++有人能看看我完整的基于文本的新手rpg游戏代码吗
C++ Can anyone look over my complete newbie text based rpg game code?
我是一个全新的程序员,使用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
。
相关文章:
- 文本文件中的单词链表
- 从命令行c++发送文本文件名
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 2D数组来自文本输入,中间有空格
- 如何将内容数组写入文本文件?
- 无法通过空白将文本文件行分隔为矢量
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- C++将文本文件中的数据读取到结构数组中
- 在指针的帮助下,文本文件中单词的频率
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 将值从二维数组输出到文本文件
- 如何在c++中从文本文件中逐行读取整数
- 如何创建一个QTableWidgetItem,用长文本右对齐,左边有省略号
- 从文本文件中读取时钟时间和事件时间并进行处理
- 如何从文本文件中读取值和数组
- 如何在C++中确定文本文件中的元素是字符还是数字
- 如何根据单词在文本中出现的概率输出单词
- C 文本文件中的数组.(新手)
- 在C 中解析期间的象征性文本,新手的磨难
- C++有人能看看我完整的基于文本的新手rpg游戏代码吗