我是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()”。
我该如何解决这个问题?我应该为战斗系统再做一个类(class)吗?另外,我可以通过使用继承或接口(interface)来简化我的代码吗?
提前谢谢大家!
主类
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());
}
}
我是一名优秀的程序员,十分优秀!