简单的基于转弯的游戏战斗系统



我是Java的初学者。我正在制作基于转弯的基本游戏,但是战斗系统有问题。我用选择的武器攻击随机选择的敌人,直到死亡为止,但是我不知道如何减少我的HP。

我在字符类中尝试了以下方法:

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);

它不起作用,因为" player.sethealth(("在角色类中无法达到。

我该如何解决?我应该为战斗系统上另一个课程吗?另外,Cani通过使用继承或接口使我的代码更简单?

预先感谢!

主类

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());
            }
        }

由于您需要player的实例来调用其方法,因此您可以将其作为输入参数传递,例如,您可以调用attack方法:

characterList.get(whichCharacter).attack(player);

并这样修改它(只需用Character的相应方法替换damageAmountmyHealth(:

public void attack(Character characterAttacked) {
    if (characterAttacked.getDamage() >= this.health || characterAttacked.getHealth() <= 0) {
        this.health = 0;
        System.out.println( this.name + " is dead!");
        this.dead = true;
    } else {
        this.health -= characterAttacked.getDamage();
        System.out.println("The remaining life of " + this.name + " is: " + this.health);
        characterAttacked.setHealth(characterAttacked.getHealth() - this.damage);
        System.out.println("Your remaining HP: "+ characterAttacked.getHealth());
    }
}

当我试图为我发现的工作攻击方法的建议准备答案时,您的攻击方法对我不起作用。

因此,这里有一些改进您的代码的一般建议:

  • 也为玩家使用字符实例。
  • 将该实例传递给攻击方法。
  • 改善列表的使用(我将在下面添加一个示例。(
  • 使用用户输入号码作为键而不是交换语句的地图。

改进了列表的使用:

// Use the generic interface List instead of implementation class.
// You don't need extra ArrayList, just use the result of Arrays.asList
//  - if you don't need to change the list afterwards.
List<Character> characterList = 
    Arrays.asList(
      zombie, 
      wolf, 
      dragon, 
      bigDragon, 
      vampire);

使用武器地图:

Map<Integer, Weapon> weapons = new HashMap<>();
weapons.put(1, fist);
weapons.put(2, sword);
// and so on - you get the idea
while (!characterList.get(whichCharacter).isDead() && !player.isDead()) {
  Weapon weapon = weapons.get(choice);
  if (null == weapon) {
    weapon = fist;
  }
  myDamage = weapon.getDamage();
  // and here you can continue your code
}

希望上述建议也可以进一步帮助您。

最新更新