我正在制作一個簡單的 .NET C# 控制臺格斗游戲,并有一個名為 player 的類,其建構式設定了以下值:
public int maxHP;
public int currentHP;
public int damage;
public int potionCount;
public Player(int maxHP, int damage, int potionCount){
this.maxHP = maxHP;
this.currentHP = maxHP;
this.damage = damage;
this.potionCount = potionCount;
}
玩家物件是在一個單獨的類中創建的,稱為戰斗,具有開始戰斗和在每次成功戰斗后獎勵玩家的功能,如下所示:
class Battle{
Player player = new Player(20, 1, 2);
public Battle(){
//Creates a loop prompting Player actions, then the enemy attacks, if enemy is dead reward the player.
Reward();
}
public void Reward(){
//Asks the player if they want 10 max health, 1 damage or 2 health potions and changes the player object's specified value.
}
}
主要問題是,當我要求在 Program 類的主回圈中創建新的戰斗時,它會將玩家值重置為 Battle 類頂部的值,而我希望玩家使用這些值從之前的戰斗中。有什么辦法可以用同一個玩家物件做到這一點嗎?
這是主回圈:
static void Main(string[] args)
{
Battle slime = new Battle(5, 1, "Slime");
Battle zombie = new Battle(10, 3, "Zombie");
Battle skeleton = new Battle(15, 5, "Skeleton");
Battle guard = new Battle(20, 7, "Royal Guard");
Battle king = new Battle(30, 10, "King of the Dungeon", true);
WriteLine("Press any key to exit.");
ReadKey();
}
uj5u.com熱心網友回復:
如果您向主回圈展示您需要進行哪些更改,將很容易向您展示。但基本上,不是在你的戰斗類中實體化一個新的 Player,要么在 Battle 物件之外創建 Player 并將它與建構式或戰斗中的函式一起傳遞。如果一個玩家存在于多個戰斗中,那么玩家不應該在戰斗中創建是有道理的,對嗎?
編輯您的更新包括主要:
我有點困惑,因為我看到您最初說您的 Player 物件是在 Battle 物件之外創建的,但您的示例顯示它是在 Battle 物件內部創建的。所以我希望這真的有幫助,而不是你已經做過的事情。
static void Main(string[] args)
{
Player player = new Player(20, 1, 2);
Battle slime = new Battle(player,5, 1, "Slime");
Battle zombie = new Battle(player,10, 3, "Zombie");
Battle skeleton = new Battle(player,15, 5, "Skeleton");
Battle guard = new Battle(player,20, 7, "Royal Guard");
Battle king = new Battle(player,30, 10, "King of the Dungeon", true);
WriteLine("Press any key to exit.");
ReadKey();
}
class Battle{
private readonly Player1 _player ;
public Battle(Player player, int maxHp, int damage, string name){
//Creates a loop prompting Player actions, then the enemy attacks, if enemy is dead reward the player.
_player = player;
//set other variables
Reward();
}
public void Reward(){
//Asks the player if they want 10 max health, 1 damage or 2 health potions and changes the player object's specified value.
}
}
uj5u.com熱心網友回復:
您只需Battle使用現有玩家創建一個建構式
class Battle{
Player player;
public Battle(Player player, int hp,...)
{
this.player = player;
//Creates a loop prompting Player actions, then the enemy attacks, if the enemy is dead reward the player.
Reward();
}
public void Reward(){
//Asks the player if they want 10 max health, 1 damage or 2 health potions and changes the player object's specified value.
}
}
并在主程式中使用相同的播放器物件
static void Main(string[] args)
{
Player player = new Player(20, 1, 2);
Battle slime = new Battle(player, 5, 1, "Slime");
Battle zombie = new Battle(player, 10, 3, "Zombie");
Battle skeleton = new Battle(player, 15, 5, "Skeleton");
Battle guard = new Battle(player, 20, 7, "Royal Guard");
Battle king = new Battle(player, 30, 10, "King of the Dungeon", true);
WriteLine("Press any key to exit.");
ReadKey();
}
我還建議創建一個敵人型別的類,并new Battle(player,enemy);用于開始新的戰斗。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/373218.html
