我目前正在制作我的第一个基于文字的角色扮演游戏,但我似乎找不到制作战斗系统的方法,到目前为止我所拥有的:
System.Threading.Thread.Sleep(500); // Stops all code from running for 0.5 second
Console.WriteLine("What do you want to do?"); // Fight Screen
Console.WriteLine("Attack"); // Attacks enemy
Console.WriteLine("Bag"); // Opens your bag - able to use potions and spells (if added)
Console.WriteLine("Block"); // Will block all damage from the next attack but will take away your turn.
string fight = Console.ReadLine(); // If the user types something in the battle screen it will be converted to the "fight" variable used below
if (fight == "Attack") // Attack Code that uses the code above.
{
enemyHealth[0] = enemyHealth[0] - AtkDmg; // Takes the attack damage from the rat's health giving the rat's health a new value.
Console.WriteLine("You lunge your sword at the Giant Rat"); // Attack Message
Console.WriteLine("Giant Rat's Health: " + enemyHealth[0] + " / 10");
charHealth = charHealth - enemyAtkDmg[0];
Console.WriteLine("The Giant Rat charges at you, headbutting you, knocking you back.");
Console.WriteLine("Your Health: " + charHealth + " / 20");
}
if (enemyHealth[0] == 0)
{
System.Threading.Thread.Sleep(1500);
Console.WriteLine("You have killed the Giant Rat");
Console.WriteLine("The Giant Rat Dropped 10 Gold");
Console.WriteLine("*You pick up the gold and put it in your purse");
}
do{
gold++;
} while (gold != 10);
Console.Write("You now have " + gold + " Gold Pieces.");
返回OT:
我目前的代码并不是我真正喜欢的代码,我希望从角色的攻击伤害+1或-1中随机化造成伤害,如果玩家的攻击伤害为4,他可以造成3,4或5点伤害(5点是重击)。
我还想知道是否有人知道更好的方法来将从1-10杀死怪物所获得的钱随机化,而不是每次都将其设置为10。
如果您需要我可能拥有的任何其他资源,请发表评论,如果可以帮助您找到答案,我会为您提供所需的任何资料。
如果您找到了改进我的代码或使其更整洁的方法,请同时发表评论。
答案 0 :(得分:1)
对于随机化,您可以使用Random
类。您可以指定最小值和最大值,并根据该值获得随机数。
Random randomNumber = new Random();
int playerDamage = 5;
var gold = randomNumber.Next( 1, 11 );//For gold
var damage = randomNumber.Next( playerDamage - 1, playerDamage + 2 );//For damage
最大值似乎过高的原因是因为最大值本身不包含在随机数中,而只是直到该点。因此,如果最大可能值应为10,则randomVar.Next
中的最大值应为11.