简单的回合制游戏战斗系统

时间:2017-05-01 17:43:21

标签: java

我是Java的初学者。我正在制作一个基本的回合制游戏,但我对战斗系统有问题。我用所选武器攻击随机选择的敌人,直到它死亡,但是我无法弄清楚如何降低我的HP。

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

它不起作用,因为&#34; player.setHealth()&#34;在Character类中无法访问。

我怎么能解决这个问题?我应该为战斗系统另外上课吗? 另外,通过使用继承或接口,我可以使代码更简单吗?

先谢谢你们!

MAIN CLASS

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

CHARACTER CLASS

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


        }


}}

WEAPON CLASS

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

3 个答案:

答案 0 :(得分:0)

我在循环逻辑方面做了一些改变,我希望它会有所帮助:

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

        }

答案 1 :(得分:0)

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

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

并按此修改(只需使用damageAmount中的相应方法替换myHealthCharacter):

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

答案 2 :(得分:0)

当我试图准备一个有关工作攻击方法的建议的答案时,我发现,你的攻击方法对我不起作用。

所以这里有一些改进代码的一般性建议:

  • 也为播放器使用角色实例。
  • 将该实例传递给攻击方法。
  • 改进列表的使用(我将在下面添加一个示例。)
  • 使用用户输入的数字作为键而不是switch语句的地图。

改进了列表的使用:

// 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
}

希望上述建议对您有所帮助。