私は戦闘に参加できるテキストアドベンチャーRPGゲームを作成しています。私はプレーヤーのクラスを作りました。私はこれを使ってプレーヤーのすべてのステータスを保持しています。複数の異なるクラスの選手の統計情報を使用
public class Player {
// Generic Stats
int playerLevel = 1;
int playerHealth = 20;
int EXP = 0;
long money = 0;
String name = "";
String homeland = "";
// Skills
int fighting = 5;
int block = 5;
int doctor = 5;
int speech = 5;
// Attributes
int damage = fighting * 2;
// Prints the player's stats
public void printStats() {
System.out.println();
System.out.println("Level: " + playerLevel);
System.out.println("EXP: " + EXP);
System.out.println("HP: " + playerHealth);
System.out.println("Money: " + money);
System.out.println("Name: " + name);
System.out.println("Homeland: " + homeland);
System.out.println("Skills: Fighting: " + fighting + " Block: " + block + "Doctor: " + doctor + " Speech: " + speech);
System.out.println();
}
// Changes the player's level
public void changeLevel(int newLevel) {
playerLevel = newLevel;
}
// Levels up the player
public void levelUp() {
playerLevel++;
EXP = 0;
}
// Give the player health
public void addHealth(int addedHealth) {
playerHealth = playerHealth + addedHealth;
}
// Remove the player's health
public void removeHealth(int removedHealth) {
playerHealth = playerHealth - removedHealth;
}
// Give the player money
public void giveMoney(int givenMoney) {
money = money + givenMoney;
}
// Give the player EXP
public void giveEXP(int addedEXP) {
EXP = EXP + addedEXP;
}
// Change the player's homeland
public void changeHomeland(String newHomeland) {
homeland = newHomeland;
}
// Change the player's name
public void changeName(String newName) {
name = newName;
}
// Increase Fighting
public void increaseFightingSkill(int amountAdded) {
fighting = fighting + amountAdded;
}
// Decrease Fighting
public void decreaseFightingSkill(int amountdecreased) {
fighting = fighting - amountdecreased;
}
// Increase Block
public void increaseBlockSkill(int amountAdded) {
block = block + amountAdded;
}
// Decrease Block
public void decreaseBlockSkill(int amountdecreased) {
block = block - amountdecreased;
}
// Increase Doctor
public void increaseDoctorSkill(int amountAdded) {
doctor = doctor + amountAdded;
}
// Decrease Doctor
public void decreaseDoctorSkill(int amountdecreased) {
doctor = doctor - amountdecreased;
}
// Increase Speech
public void increaseSpeechSkill(int amountAdded) {
speech = speech + amountAdded;
}
// Decrease Speech
public void decreaseSpeechSkill(int amountdecreased) {
speech = speech - amountdecreased;
}
}
ご覧のとおり、プレーヤのステータスを変更することもできます。私はメインクラスを作っています。ここでは単にプレイヤーを初期化し、プレイヤーが決定を下し、戦いを勝ち取るなどしてそのステータスを変更します。メインクラスのプレーヤーをここで初期化します。
Player player = new Player();
これで初期化されました。「バトル」クラスを作成しました。コンセプトは、単にバトルを初期化し、バトルの内容を変更して(コード内にドロップすることができるバトルテンプレートのように)それで済ませたいということです。みかんのように:
battle.enemySetHealth() // example modifier
battle.start() // simply run through the battle
問題は、私はプレイヤーのを修正することになるので、私は実際にちょうど他のプレイヤーを定義することなく、戦闘クラスのプレーヤーの統計情報を使用したい、私は、同じプレイヤオブジェクトを使用したい、です私が新しいクラスを定義すると、それはバトルクラス内で異なる統計を持ち、全体を乱してしまいます。バトルクラスのプレーヤーの統計情報を使用して、攻撃の被害、着信攻撃をブロックする機会、e.c.c.ここには戦闘クラスがあります:
import java.util.Random;
import java.util.Scanner;
// Battle Scene
public class Battle {
//Variables
String enemyName = "Unnamed Enemy";
int enemyHealth = 20;
int enemyAttack = 5;
int enemyDefense = 5;
int enemyDisposition = 0;
int attackDamage;
// Sets up tools
clear clear = new clear();
Scanner sc = new Scanner(System.in);
Random rand = new Random();
// Allows changing of enemy name
public void enemyName(String newEnemyName) {
enemyName = newEnemyName;
}
// Sets a new disposition for the enemy to start the fight with
public void enemyStartingDisposition(int newStartingDisposition) {
enemyDisposition = newStartingDisposition;
}
// Sets a new amount of health for the enemy to start with
public void enemyStartingHealth(int newStartingHealth) {
enemyHealth = newStartingHealth;
}
// Sets a new defense for the enemy to start with
public void enemyStartingDefense(int newEnemyDefense) {
enemyDefense = newEnemyDefense;
}
// Sets a new attack for the enemy to start with
public void enemyStartingAttack(int newEnemyAttack) {
enemyAttack = newEnemyAttack;
}
// Starts the battle
public void startFight() throws InterruptedException {
System.out.println("Woah! " + enemyName + " jumped out of nowhere!!!");
System.out.println("(Attack)");
System.out.println("(Talk)");
System.out.println("(Run)");
while (true) {
System.out.print("What should you do? : ");
String userInput = sc.nextLine();
if (userInput.equals("Attack")) {
// hopefully put in a way to attack based on your stats
} if (userInput.equals("Talk")) {
// a way to use speech to talk your way out of the fight
} if (userInput.equals("Run")) {
//ability to run away based on agility/speed
} else {
System.out.println();
System.out.println("Invalid Answer!");
Thread.sleep(2000);
clear.Screen();
}
}
//Break here
}
}
私はまた、お金と経験のように最後にプレーヤーの統計情報を変更したいと思います。だから私はこれをどのようにすることができますか?それは本当に混乱しています。
可読性を高めるためにコードをフォーマットし直してください。何でこれが大切ですか?あなたがコードを読んで勉強している唯一の人なのかどうかは関係ありませんが、ボランティアのスタッフがいるサイトに投稿すれば変更されます。可能な限りコードを理解して読みやすくする。すべてのネストされたブロックは、4つのスペースでインデントされる必要があります。同じブロック上のすべてのコードは同じインデントを持つ必要があります。一行に複数の空白行があることは決してありません。これであなたの助けには大いに感謝します。 –
「バトル(Battle)」は、目標に応じて2人のプレイヤー、1人のプレイヤーと1人の敵を選ぶ必要があります。 – Isac