C++需要我的基本文本控制台游戏(我是初学者)(为什么它不能正常工作)。不是我想要的那样工作
所以我是编程新手,在尝试制作一个基本的文本游戏时遇到了一个问题 顺便说一句,我知道我可以做更多的函数来减少代码,比如,我可以做一个返回值的函数,而不是复制粘贴百分比的算法。但我只是想把它做好,这样我就可以缓慢地前进 在编译和运行它时,请展开控制台,否则它就不好看了。用于菜单和设置。提前谢谢 它不能正常工作。eHealth和hHealth私有类变量未正确减去。我已经尽力了,但我不知道我做错了什么,所以请帮帮我!以下是源代码: 2D游戏V1.cppC++需要我的基本文本控制台游戏(我是初学者)(为什么它不能正常工作)。不是我想要的那样工作,c++,C++,所以我是编程新手,在尝试制作一个基本的文本游戏时遇到了一个问题 顺便说一句,我知道我可以做更多的函数来减少代码,比如,我可以做一个返回值的函数,而不是复制粘贴百分比的算法。但我只是想把它做好,这样我就可以缓慢地前进 在编译和运行它时,请展开控制台,否则它就不好看了。用于菜单和设置。提前谢谢 它不能正常工作。eHealth和hHealth私有类变量未正确减去。我已经尽力了,但我不知道我做错了什么,所以请帮帮我!以下是源代码: 2D游戏V1.cpp #include "stdafx.h" #incl
#include "stdafx.h"
#include <iostream>
#include <ctime>
#include "Header.h"
#include <windows.h>
using namespace std;
int main()
{
combat CombatEngine;
CombatEngine.renderMenu(0);
return 0;
}
战斗引擎
#include "stdafx.h"
#include <random>
#include <iostream>
#include <Windows.h>
#include "Header.h"
#include "string"
#include <ctime>
using namespace std;
float combat::fGen() {
mt19937 fGenerator(time(NULL));
uniform_real_distribution<float> randF(0.0f, 1.0f);
float fNum(randF(fGenerator));
return fNum;
}
void combat::renderMenu(int rendNum) {
if (rendNum == 0)
{
for (int i = 0; i != 12; i++)
{
cout << "####################################################################################################\n";
}
cout << "############################################|-Play (1)|#############################################\n";
cout << "###########################################|Settings (2)|###########################################\n";
for (int i = 0; i != 13; i++)
{
cout << "####################################################################################################\n";
}
}
int dec = 0;
while (dec != 1 && dec != 2) {
cin >> dec;
if (dec != 1 && dec != 2)
{
cout << "Error: Invalid input!\n";
}
}
if (dec == 1) {
refreshScreen();
inGame();
}
if (dec == 2) {
renderSettings();
}
}
void combat::renderSettings() {
refreshScreen();
for (int i = 0; i != 12; i++)
{
cout << "####################################################################################################\n";
}
cout << "#####################################| change human health (1)|#####################################\n";
cout << "######################################| change mob health (2)|######################################\n";
cout << "#############################################|Back (3)|#############################################\n";
for (int i = 0; i != 12; i++)
{
cout << "####################################################################################################\n";
}
int dec = 0;
while (dec != 1 && dec != 2 && dec != 3)
{
cin >> dec;
if (dec != 1 && dec != 2 && dec != 3)
{
cout << "Error: Invalid input!\n";
}
}
for (;;)
{
if (dec == 1)
{
refreshScreen();
for (int i = 0; i != 13; i++)
{
cout << "####################################################################################################\n";
}
cout << "#############################| << Type the new value (previously: " << hHealth << ".)|############################\n";
for (int i = 0; i != 13; i++)
{
cout << "####################################################################################################\n";
}
int inc;
cin >> inc;
hHealth = inc;
refreshScreen();
renderMenu(0);
}
if (dec == 2)
{
refreshScreen();
for (int i = 0; i != 13; i++)
{
cout << "####################################################################################################\n";
}
cout << "#############################| << Type the new value (previously: " << eHealth << ".)|############################\n";
for (int i = 0; i != 13; i++)
{
cout << "####################################################################################################\n";
}
int inc;
cin >> inc;
eHealth = inc;
refreshScreen();
renderMenu(0);
}
if (dec == 3)
{
refreshScreen();
renderMenu(0);
}
}
}
void combat::renderHuman(int D) {
if (D == 0) {
cout << "_____\n";
cout << "| |\n";
cout << "O _ O\n";
cout << "/|_|\\ \n";
cout << "_| |_\n";
cout << "#############################################" << endl;
}
if (D == 1) {
cout << "_____\n";
cout << "| |\n";
cout << "O _ O/\n";
cout << "/|_|/ \n";
cout << "_| |_\n";
cout << "#############################################" << endl;
}
}
void combat::renderHumanAttack()
{
Sleep(500);
refreshScreen();
renderHuman(1);
Sleep(250);
refreshScreen();
renderHuman(0);
Sleep(500);
refreshScreen();
renderHuman(1);
Sleep(250);
refreshScreen();
renderHuman(0);
}
void combat::refreshScreen()
{
system("cls");
}
void combat::renderSkeleton(int D)
{
if (D == 0)
{
cout << " ___" << endl;
cout << "{o_o}" << endl;
cout << "/( )\\ " << endl;
cout << " / \\" << endl;
cout << "#############################################" << endl;
}
if (D == 1)
{
cout << " ___" << endl;
cout << "\\{o_o}" << endl;
cout << " \\( )\\ " << endl;
cout << " / \\" << endl;
cout << "#############################################" << endl;
}
}
void combat::renderSkeletonAttack()
{
Sleep(500);
refreshScreen();
renderSkeleton(1);
Sleep(250);
refreshScreen();
renderSkeleton(0);
Sleep(500);
refreshScreen();
renderSkeleton(1);
Sleep(250);
refreshScreen();
renderSkeleton(0);
}
void combat::renderHumanVSkeleton(int D)
{
if (D == 0)
{
//refreshScreen();
cout << "_____ \n";
cout << "| | ";
cout << " ___" << endl;
cout << "O _ O ";
cout << "{o_o}" << endl;
cout << "/|_|\\ ";
cout << "/( )\\ " << endl;
cout << "_| |_ ";
cout << " / \\" << endl;
cout << "#############################################" << endl;
}
if (D == 1)
{
//human attacking
//refreshScreen();
cout << "_____ \n";
cout << "| | ";
cout << " ___" << endl;
cout << "O _ O/ ";
cout << "{o_o}" << endl;
cout << "/|_|/ ";
cout << "/( )\\ " << endl;
cout << "_| |_ ";
cout << " / \\" << endl;
cout << "#############################################" << endl;
}
if (D == 2)
{
//skeleton attacking
//refreshScreen();
cout << "_____ \n";
cout << "| | ";
cout << " ___" << endl;
cout << "O _ O ";
cout << "\\{o_o}" << endl;
cout << "/|_|\\ ";
cout << " \\( )\\ " << endl;
cout << "_| |_ ";
cout << " / \\" << endl;
cout << "#############################################" << endl;
}
}
void combat::renderHumanVSkeletonAttack(int D)
{
if (D == 0)
{
//human attacking
Sleep(500);
refreshScreen();
renderHumanVSkeleton(1);
Sleep(250);
refreshScreen();
renderHumanVSkeleton(0);
Sleep(500);
refreshScreen();
renderHumanVSkeleton(1);
Sleep(250);
refreshScreen();
renderHumanVSkeleton(0);
}
if (D == 1)
{
//skelleton attacking
Sleep(500);
refreshScreen();
renderHumanVSkeleton(2);
Sleep(250);
refreshScreen();
renderHumanVSkeleton(0);
Sleep(500);
refreshScreen();
renderHumanVSkeleton(2);
Sleep(250);
refreshScreen();
renderHumanVSkeleton(0);
}
}
int combat::attacks(int attack)
{
if (attack == 1) {
//25% chance for 15 DMG if hit 40% for hit
if (fGen() <= 0.25f)
{
if (fGen() <= 0.25f)
{
return 15 * 1.5;
}
else
return 15;
}
if (attack == 2) {
//60% chance for 7 DMG if hit 40% for hit
if (fGen() <= 0.60f)
{
if (fGen() <= 0.25f)
{
return 7 * 2;
}
else
return 7;
}
}
if (attack == 3) {
//75% chance for 3 DMG if hit 40% for hit
if (fGen() <= 0.75f)
{
if (fGen() <= 0.40f)
{
return 3 * 2;
}
else
return 3;
}
}
if (attack == 4) {
//10% chance for 20 DMG if hit 25% for crit
if (fGen() <= 0.10f)
{
if (fGen() <= 0.25f)
{
return (int)20 * (int)1.5;
}
else
return 20;
}
}
}
return 0;
}
void combat::dealDMG(int dmg, int d)
{
if (d == 0)
{
eHealth - dmg;
}
else
{
hHealth - dmg;
}
}
void combat::inGame()
{
float t = fGen();
cout << t;
bool who;
if (t >= 0.5f)
{
cout << "You start attacking first!" << endl;
who = true;
}
else
{
cout << "The skeleton starts attacking first!" << endl;
who = false;
}
while (eHealth > 0 || hHealth > 0)
{
if (!who)
{
refreshScreen();
cout << "Player health: " << hHealth << ". Mob health: " << eHealth << endl;
renderHumanVSkeleton(0);
Sleep(1500);
int decision;
float randF = fGen();
if (randF <= 0.25f)
decision = 1;
else if (randF > 0.25f && randF < 0.50f)
decision = 2;
else if (randF > 0.50f && randF < 0.75f)
decision = 3;
else
decision = 4;
int dmg = attacks(decision);
dealDMG(dmg, 1);
refreshScreen();
cout << "Player health: " << hHealth << ". Mob health: " << eHealth << endl;
renderHumanVSkeletonAttack(1);
who = true;
}
if (who)
{
refreshScreen();
cout << "Player health: " << hHealth << ". Mob health: " << eHealth << endl;
renderHumanVSkeleton(0);
cout << "It's your turn to attack!\n1) 25% chance to hit, DMG = 15. 40% chance of crit (22 DMG)\n2) 60% chance for 7 dmg. 40% chance for crit (14 DMG)\n3) 75 chance to hit, DMG = 3 dmg. 40% to crit (6 DMG)\n4)10% chance to hit, DMG = 20. 25% chance to crit (30 DMG)\n";
int decision2;
cin >> decision2;
while (decision2 != 1 && decision2 != 2 && decision2 != 3 && decision2 != 4) {
cin >> decision2;
}
int dmg = attacks(decision2);
dealDMG(dmg, 0);
refreshScreen();
cout << "Player health: " << hHealth << ". Mob health: " << eHealth << endl;
renderHumanVSkeletonAttack(0);
who = false;
}
}
if (eHealth == 0) {
refreshScreen();
renderHuman(0);
cout << "You won!" << endl;
renderMenu(0);
}
else
{
refreshScreen();
renderSkeleton(0);
cout << "You loose!" << endl;
renderMenu(0);
}
}
难道不是吗
eHealth -= dmg;
及
试一试
来吧,您至少可以在发布之前删除ascii艺术欢迎使用堆栈溢出!听起来您可能需要学习如何使用调试器逐步完成代码。有了一个好的调试器,您可以逐行执行您的程序,并查看它偏离预期的地方。这是一个必要的工具,如果你要做任何编程。进一步阅读:对不起,伙计,我对编程和这个论坛完全是初学者。你说的ascii艺术是指小字符?我想我可以,以后会考虑减少我的代码。再次抱歉。欢迎访问stackoverflow.com。请花些时间阅读,特别是命名和。还有,请学习如何创建一个。我现在就做@NathanOliver。我实际上看了一个关于调试的教程,它真的很不清楚,我不理解很多,我理解断点的概念,等等,但不知道如何使用IDE Visual studio 2015进行实际调试。学习如何正确地调试是非常必要的。我现在就读。
eHealth - dmg;
eHealth -= dmg;
#include "string"
#include <string>