所以我试着写一个最简单的游戏,每次“怪物”攻击“玩家”,我希望变量"int totalHealth“被降低。
public void attack(Player somePlayer) {
int totalHealth = somePlayer.getHitPoints() + somePlayer.getStrength();
int remainingHealth = totalHealth - damage;
if (remainingHealth == 0 || remainingHealth < 0) {
System.out.println("Your player died");
} else {
System.out.println("The monster attacked " + somePlayer.getName() + " and made " + this.damage + " damage");
System.out.println("Your remaining health is - " + (remainingHealth - somePlayer.getStrength()));
}
}但问题是变量"remainingHealth“保持不变,只有当我第一次运行代码时它会降低,每次它保持不变时,我猜问题就在这一行中:
int totalHealth = somePlayer.getHitPoints() + somePlayer.getStrength();我想每次我运行代码
somePlayer.getHitpoints() 从构造函数中获取插入的整数,这就是问题所在。
我需要想办法将剩余的健康状况以某种方式存储在变量中。
球员班:
public class Player implements ISavable{
private String name;
private int hitPoints ;
private int strength ;
private String weapon;
private int damage;
private int totalHealth = hitPoints + strength;
public Player(String name, int damage , int hitPoints , int strength) {
this.name = name;
this.damage = damage;
this.weapon = "Sword";
this.hitPoints = hitPoints;
this.strength = strength;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getHitPoints() {
return hitPoints;
}
public void setHitPoints(int hitPoints) {
this.hitPoints = hitPoints;
}
public int getStrength() {
return strength;
}
public void setStrength(int strength) {
this.strength = strength;
}
public String getWeapon() {
return weapon;
}
public void setWeapon(String weapon) {
this.weapon = weapon;
}
public int getTotalHealth() {
return totalHealth;
}
public void setTotalHealth(int totalHealth) {
this.totalHealth = totalHealth;
}
@Override
public String toString() {
return "Player{" +
"name='" + name + '\'' +
", hitPoints=" + hitPoints +
", strength=" + strength +
", weapon='" + weapon + '\'' +
'}';
}
@Override
public List<String> write() {
List<String> values = new ArrayList<String>();
values.add(0, this.name);
values.add(1, "" + this.hitPoints);
values.add(2, "" + this.strength);
values.add(3, "" + this.weapon);
values.add(4,"" + this.damage);
return values;
}
@Override
public void read(List<String> savedValues) {
if (savedValues != null && savedValues.size()>0){
this.name = savedValues.get(0);
this.hitPoints = Integer.parseInt(savedValues.get(1));
this.strength = Integer.parseInt(savedValues.get(2));
this.weapon = savedValues.get(3);
}
}
public void attack(Monster someMonster){
int health = someMonster.getHitPoints();
int remainingHealth = health - damage;
if (remainingHealth == 0 || remainingHealth < 0) {
System.out.println("You killed the monster !!!");
} else {
System.out.println("You attacked the monster " + " and made " + this.damage + " damage");
System.out.println("Monsters remaining health is - " + remainingHealth);
}
if (someMonster.isDead()){
this.hitPoints = 100;
this.strength = 50;
}
}
public void healPlayer(Player somePlayer){
int hp = somePlayer.getHitPoints();
hp += 10;
System.out.println("You healed the player for 10hp , your current hp is " + hp);
}}
发布于 2017-11-16 11:37:13
在Player上创建一个函数updateHealth(int newHealth),该函数将从getHitPoints()返回到newHealth的变量进行设置。然后,只需在somePlayer.updateHealth(somePlayer.getHitPoints() - damage);函数的末尾追加attack。
https://stackoverflow.com/questions/47328574
复制相似问题