【发布时间】:2017-07-10 01:13:07
【问题描述】:
我正在制作一款文字冒险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”类。这个概念是我想简单地初始化战斗,修改战斗的内容并完成它(就像一个战斗模板,我可以放入代码中)。有点像这样:
battle.enemySetHealth() // example modifier
battle.start() // simply run through the battle
问题是,我想在战斗类中使用玩家的统计数据,而不是真正定义另一个玩家,我想使用相同的玩家对象,因为我将在主类中修改玩家的统计数据,如果我定义了一个新的,它将在战斗类中有不同的统计数据,把整个事情搞砸了。我将使用战斗类中的玩家统计数据来确定攻击伤害,阻止来袭攻击的机会等。这是战斗类:
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 个空格。同一块上的所有代码都应具有相同的缩进。一行中的空白行不应超过一个....您对此的帮助将不胜感激。
-
A
Battle应该采用 2 个玩家作为参考,或者一个玩家和一个敌人,具体取决于您的目标,以及敌人是否与普通玩家不同。