C# 游戏程序正在跳过
我正在尝试运行一个由5节课组成的游戏,但最重要的部分不是运行。请记住,我仍然是c#的初学者。所有5个类都是独立的,但我在这里只在第一个类中键入名称空间,以避免混淆,它们在每个类中都有。程序运行,但当它到达战斗等级时,它完全跳过了战斗循环,并询问我是否想再次玩…请帮助我 头等舱(我的入口): 二等舱:C# 游戏程序正在跳过,c#,.net,C#,.net,我正在尝试运行一个由5节课组成的游戏,但最重要的部分不是运行。请记住,我仍然是c#的初学者。所有5个类都是独立的,但我在这里只在第一个类中键入名称空间,以避免混淆,它们在每个类中都有。程序运行,但当它到达战斗等级时,它完全跳过了战斗循环,并询问我是否想再次玩…请帮助我 头等舱(我的入口): 二等舱: class MainGame { Hero myhero; Battle battle; string answer; public MainGame()
class MainGame
{
Hero myhero;
Battle battle;
string answer;
public MainGame()
{
myhero = new Hero();
Hero.Initialize(myhero);
BasicGameLoop();
}
void BasicGameLoop()
{
do
{
Monster monster = new Monster();
battle = new Battle(myhero, monster);
Console.WriteLine("Do you want to Play again??");
answer= Console.ReadLine();
}
while(answer == "Y" || answer == "y");
}
}
三等舱:
class Battle
{
string choice;
Random rand;
int healing, fleechance, hitchance;
public Battle(Hero hero, Monster monster)
{
Console.WriteLine("{0} is facing a {1}.", hero.Identifier, monster.Identifier);
}
public void BattleLoop(Hero hero, Monster monster)
{
do
{
rand = new Random();
DisplayChoices();
choice = Console.ReadLine();
hitchance = rand.Next(0, 100);
switch (choice)
{
case "a":
case "A"://this way a or A work
hitchance = rand.Next(0, 100);
if (hitchance > 30)
{
hero.AttackDamage = GetHeroDamage(hero);
Console.WriteLine("{0} Attacks!", hero.Identifier);
hero.AttackDamage -= monster.Defense;
Console.WriteLine("The Monster Loses {0}hp", hero.AttackDamage);
}
else
{
Console.WriteLine("{0} Missed!", hero.Identifier);
}
break;
case "d":
case "D":
Console.WriteLine("{0} Defends", hero.Identifier);
break;
case "h":
case "H":
healing = 400;
hero.CurrentHealth += healing;
Console.WriteLine("{0} uses a Potion!", hero.Identifier);
Console.WriteLine("{0} heals himself for {1} Points",hero.Identifier,healing);
break;
case "f":
case "F":
fleechance = rand.Next(0, 100);
if (fleechance > 40)
{
Console.WriteLine("{0} fled!",hero.Identifier);
Console.ReadLine();
Environment.Exit(0);
}
else
{
Console.WriteLine("Fleeing Failed");
Console.ReadLine();
}
break;
default:
Console.WriteLine("Sorry that choice was invalid and the monster took a cheap shot!");
break;
}
Console.WriteLine();
if (monster.isAlive == true)
{
hitchance = rand.Next(0, 100);
if (hitchance > 30)
{
monster.Strength = GetMonsterDamage(monster);
Console.WriteLine("The Monster Attacks!");
if (choice == "d" || choice == "D")
{
monster.Strength /= 2;
}
monster.Strength -= hero.Defense;
Console.WriteLine("The Hero loses {0}hp", monster.Strength);
}
else
{
Console.WriteLine("The Monster Missed!");
}
Console.WriteLine("Press Enter to Continue");
Console.ReadLine();
Console.Clear();
}
}
while (hero.isAlive == true && monster.isAlive == true);
if (hero.CurrentHealth > 0)
{
Console.WriteLine("{0} emerged Victorious!", hero.Identifier);
}
else
{
Console.WriteLine("{0} has been defeated :(", hero.Identifier);
}
Console.ReadLine();
}
public void PrintStatus(Hero hero, Monster monster)
{
Console.Write(@"
********************************
HP/MaxHP MP/MaxMP
{0}: {1}/{2}hp {3}/{4}mp
{5}: {6}/{7}hp {8}/{9}mp
********************************
", hero.Identifier, hero.CurrentHealth, hero.MaxHealth, hero.CurrentMagic, hero.MaxMagic,
monster.Identifier, monster.CurrentHealth, monster.MaxHealth, monster.CurrentMagic,
monster.MaxMagic);
}
string DisplayChoices()
{
string choice;
Console.Write(@"
__________________________
Please Choose an action:
(A)ttack
(D)efend
(H)eal
(F)lee
__________________________");
Console.WriteLine();
choice = Console.ReadLine();
return choice;
}
public int GetHeroDamage(Hero hero)// 2nd Method to calculate the hero's Damage during battle.
{
int attackdamage;
attackdamage = hero.AttackDamage;
return attackdamage;
}
int GetMonsterDamage(Monster monster) // 3rd Method to calculate the monster's damage during the battle.
{
int attackdamage;
attackdamage = monster.Strength;
return attackdamage;
}
}
“四等”
第五类:
class Monster
{
public int CurrentHealth, MaxHealth, CurrentMagic;
public int MaxMagic, Strength, Defense, Agility;
public int Experience, Gold, AttackDamage;
public string Identifier;
public bool isAlive;
public Monster()
{
CurrentHealth = 8;
MaxHealth = 8;
CurrentMagic = 0;
MaxMagic = 0;
Strength = 5;
Defense = 3;
Agility = 4;
Experience = 5;
Gold = 2;
Identifier = "Monster";
isAlive = true;
AttackDamage = Strength;
}
}
看起来您错过了对BattleLoop的调用。请尝试在MainGame.BasicGameLoop中的行
battle=new battle(myhero,monster);
之后添加battle.BattleLoop()
。作为初学者不能成为“这是我的全部代码,请调试它”的借口另外,如果你想发布一个巨大的代码列表,请使用pastebin.com或其他类似的工具,而不仅仅是把它放在问题中。试着使用ToUpper()来计算大小写的可能性。{while(answer.ToUpper()=“Y”)}{switch(choice.ToUpper())}我知道这不是你问的问题,但我认为这可能会对那些家伙有所帮助,但我已经为代码挣扎了至少两个小时。谢谢你的来访。非常感谢:)成功了。
Monster monster = new Monster();
battle = new Battle(myhero, monster);
battle.BattleLoop(myhero, monster);
Console.WriteLine("Do you want to Play again??");
answer= Console.ReadLine();
class Monster
{
public int CurrentHealth, MaxHealth, CurrentMagic;
public int MaxMagic, Strength, Defense, Agility;
public int Experience, Gold, AttackDamage;
public string Identifier;
public bool isAlive;
public Monster()
{
CurrentHealth = 8;
MaxHealth = 8;
CurrentMagic = 0;
MaxMagic = 0;
Strength = 5;
Defense = 3;
Agility = 4;
Experience = 5;
Gold = 2;
Identifier = "Monster";
isAlive = true;
AttackDamage = Strength;
}
}
Monster monster = new Monster();
battle = new Battle(myhero, monster);
battle.BattleLoop(myhero, monster);
Console.WriteLine("Do you want to Play again??");
answer= Console.ReadLine();