【发布时间】:2017-11-16 11:31:40
【问题描述】:
所以我正在尝试编写最简单的游戏,每次“怪物”攻击“玩家”时,我都希望降低变量“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);
}
}
【问题讨论】:
-
也保留它作为实例变量,在构造时,将其设置为生命值 + 力量,并在他被击中时降低该值
-
将
totalHealth退出函数并保持更新 -
totalHealth每次调用attack时都会被实例化 - 因此,如果它不更改值(仅一次),请查看播放器值。为 totalHealth 和 player-values 添加日志记录。打印实际和预期。 -
@canillas 如果
player-值在attack()-调用之间发生更改,它将被更新并正确
标签: java