C# |基于文本的角色扮演游戏 |战斗系统和货币(随机化)
C# | Text-Based RPG | Combat System & Currency (Randomisation)
我目前正在制作我的第一个基于文本的角色扮演游戏,但我似乎找不到制作战斗系统的方法,我目前拥有的是:
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.");
返回加时赛:
我当前的代码没有按照我真正希望的方式运行,我希望伤害从角色的攻击力 +1 或 -1 中随机化,所以如果玩家的攻击力是 4他可以造成 3,4 或 5 点伤害(5 点是暴击)。
我也想知道是否有人知道更好的方法来随机化从 1-10 或其他东西中杀死怪物获得的金钱,而不是每次都设置为 10。
如果您需要我可能拥有的任何其他资源,请发表评论,如果可以帮助您找到一些答案,我会为您提供所需的任何资源。
如果您找到改进我的代码或使其更整洁的方法,也请发表评论。
对于随机化,您可以使用 Random
class。您可以指定一个最小值和一个最大值,并根据该值获得一个随机数。
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
最大值似乎 1 太高的原因是因为最大值本身不包含在随机数中,但只包含到那个点。因此,如果最大可能值应为 10,则 randomVar.Next
中的最大值应为 11。
我目前正在制作我的第一个基于文本的角色扮演游戏,但我似乎找不到制作战斗系统的方法,我目前拥有的是:
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.");
返回加时赛:
我当前的代码没有按照我真正希望的方式运行,我希望伤害从角色的攻击力 +1 或 -1 中随机化,所以如果玩家的攻击力是 4他可以造成 3,4 或 5 点伤害(5 点是暴击)。
我也想知道是否有人知道更好的方法来随机化从 1-10 或其他东西中杀死怪物获得的金钱,而不是每次都设置为 10。
如果您需要我可能拥有的任何其他资源,请发表评论,如果可以帮助您找到一些答案,我会为您提供所需的任何资源。
如果您找到改进我的代码或使其更整洁的方法,也请发表评论。
对于随机化,您可以使用 Random
class。您可以指定一个最小值和一个最大值,并根据该值获得一个随机数。
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
最大值似乎 1 太高的原因是因为最大值本身不包含在随机数中,但只包含到那个点。因此,如果最大可能值应为 10,则 randomVar.Next
中的最大值应为 11。