我是Java的初学者。我正在制作基本的回合制游戏,但是战斗系统存在问题。我用选择的武器攻击随机选择的敌人,直到其死亡,但是我不知道如何降低我的生命值。
我在Character类中尝试了以下方法:
public void attack(int damageAmount, int myHealth) {
if (damageAmount >= this.health || myHealth<=0) {
this.health = 0;
System.out.println( this.name + " is dead!");
this.dead = true;
} else {
this.health -= damageAmount;
System.out.println("The remaining life of " + this.name + " is: " + this.health);
player.setHealth(myHealth-this.damage);
System.out.println("Your remaining HP: "+ myHealth);
这是行不通的,因为在Character类中无法访问“ player.setHealth()”。
我该如何解决?我应该为战斗系统再上一堂课吗?
另外,可以通过使用继承或接口使代码更简单吗?
在此先感谢大家!
主班
package com.company;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static int size = 0;
public static void main(String[] args) {
int myHealth = 1000;
int myDamage = 5;
Scanner sc = new Scanner(System.in);
Character zombie = new Character("Zombie", 500, 10);
Character wolf = new Character("Wolf", 200, 30);
Character dragon = new Character("Dragon", 1000, 200);
Character bigDragon = new Character("Big Dragon", 2000, 400);
Character vampire = new Character("Vampire", 1000, 250);
ArrayList<Character> characterList = new ArrayList<>();
characterList.addAll(Arrays.asList(
zombie,
wolf,
dragon,
bigDragon,
vampire
));
Weapon fist = new Weapon("Fist", 5);
Weapon sword = new Weapon("Sword", 50);
Weapon bow = new Weapon("Bow", 40);
Weapon crossBow = new Weapon("Crossbow", 35);
Weapon revolver = new Weapon("Revolver", 100);
ArrayList<Weapon> weaponList = new ArrayList<>();
weaponList.addAll(Arrays.asList(
fist,
sword,
bow,
crossBow,
revolver
));
size = characterList.size();
int whichCharacter = random();
for (int i = 0; i < characterList.size(); i++) {
System.out.println((i + 1) + ". character: " + characterList.get(random()).getName());
}
System.out.println("Your name!");
String myName = sc.nextLine();
Character player = new Character(myName, 1000, myDamage);
System.out.println("Your name: " + myName);
System.out.println("Your HP: " + myHealth);
System.out.println("Your attack power: " + myDamage);
System.out.println();
System.out.println();
System.out.println("You were attacked by a(n):");
System.out.println("Name: " + characterList.get(whichCharacter).getName());
System.out.println("HP: " + characterList.get(whichCharacter).getHealth());
System.out.println("Attack power: " + characterList.get(whichCharacter).getDamage());
System.out.println();
System.out.println("You attack with...");
System.out.println();
System.out.println("1. fist");
System.out.println("2. sword");
System.out.println("3. bow");
System.out.println("4. crossbow");
System.out.println("5. revolver");
int choice = sc.nextInt();
while (!characterList.get(whichCharacter).isDead()) {
switch (choice) {
case 1:
myDamage = 5;
break;
case 2:
myDamage = sword.getWeaponDamage();
break;
case 3:
myDamage = bow.getWeaponDamage();
break;
case 4:
myDamage = crossBow.getWeaponDamage();
break;
case 5:
myDamage = revolver.getWeaponDamage();
break;
default:
myDamage = 5;
}
while (!characterList.get(whichCharacter).isDead()) {
characterList.get(whichCharacter).attack(myDamage, myHealth);
//characterList.get(whichCharacter).attack(characterList.get(whichCharacter).getDamage(), player.getHealth());
}
}
}
public static int random() {
int szam = (int) (Math.random() * size);
return szam;
}
}
角色类
package com.company;
/**
* Created by Norbi on 2017.04.29..
*/
public class Character {
private String name;
private int health;
private int damage;
private boolean dead = false;
public boolean isDead() {
return dead;
}
public void setDead(boolean dead) {
this.dead = dead;
}
public Character(boolean dead) {
this.dead = dead;
}
public Character(String name, int health, int damage) {
this.name = name;
this.health = health;
this.damage = damage;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getHealth() {
return health;
}
public void setHealth(int health) {
this.health = health;
}
public int getDamage() {
return damage;
}
public void setDamage(int damage) {
this.damage = damage;
}
public void attack(int damageAmount, int myHealth) {
if (damageAmount >= this.health || myHealth<=0) {
// this.health = 0;
System.out.println(this.name + " is dead!");
this.dead = true;
} else {
this.health -= damageAmount;
System.out.println("The remaining life of " + this.name + " is: " + this.health);
System.out.println("Your remaining HP: "+ myHealth);
}
}}
武器课
package com.company;
/**
* Created by Norbi on 2017.04.29..
*/
public class Weapon {
private String name;
private int weaponDamage;
public Weapon(String name, int weaponDamage) {
this.name = name;
this.weaponDamage = weaponDamage;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getWeaponDamage() {
return weaponDamage;
}
public void setWeaponDamage(int weaponDamage) {
this.weaponDamage = weaponDamage;
}
}
最佳答案
我对循环逻辑做了一些更改,希望对您有所帮助:
while (!characterList.get(whichCharacter).isDead() && !player.isDead()) {
switch (choice) {
case 1:
myDamage = 5;
break;
case 2:
myDamage = sword.getWeaponDamage();
break;
case 3:
myDamage = bow.getWeaponDamage();
break;
case 4:
myDamage = crossBow.getWeaponDamage();
break;
case 5:
myDamage = revolver.getWeaponDamage();
break;
default:
myDamage = 5;
}
while (!characterList.get(whichCharacter).isDead() && !player.isDead()) {
characterList.get(whichCharacter).attack(myDamage, myHealth);
player.attack(characterList.get(whichCharacter).getDamage(), player.getHealth());
//characterList.get(whichCharacter).attack(characterList.get(whichCharacter).getDamage(), player.getHealth());
}
}