1

我做了一个非常简单的神奇宝贝战斗模拟器,我开始升级它。通过升级它,我的意思是以一种非常简约的方式(至少在我看来)。此升级包括创建两个 HP 条来伴随我的控制台输出。我的问题是我不知道如何让他们更新神奇宝贝的生命值......

这是我认为与它直接相关的代码:

我的 BattleHandler:包 com.mrmewniverse.pokemon.battle;

import java.util.Random;

import com.mrmewniverse.pokemon.pokemon.Pokemon;

public class BattleHandler {

    private static Pokemon fasterPokemon;
    private static Pokemon slowerPokemon;

    public BattleHandler() {}

    public void initiateBattle(Pokemon par1Poke, Pokemon par2Poke) {
        System.out.println("Battle initiated!\n\n");
        BattleHandler.calculateFasterPokemon(par1Poke, par2Poke);

        try {
            while (fasterPokemon.getCurrentHealth() > 0 && slowerPokemon.getCurrentHealth() > 0) {
                Thread.sleep(400);

                fasterPokemon.attack(slowerPokemon);
                System.out.println(fasterPokemon.getPokeName() + " attacked " + slowerPokemon.getPokeName() + " with " + fasterPokemon.getMoveUsed().getMoveName() + " dealing "
                + fasterPokemon.getLastDamageDealt() + " damage\n");

                Thread.sleep(50);

                if (fasterPokemon.getMoveUsed().getWeaknessResistanceMuliplier() == 2F || fasterPokemon.getMoveUsed().getWeaknessResistanceMuliplier() == 4F)
                    System.out.println("It's super effective!\n");
                else if (fasterPokemon.getMoveUsed().getWeaknessResistanceMuliplier() == 0.5F || fasterPokemon.getMoveUsed().getWeaknessResistanceMuliplier() == 0.25F)
                    System.out.println("It's not very effective...\n");

                Thread.sleep(400);

                if (fasterPokemon.getCurrentHealth() <= 0 || slowerPokemon.getCurrentHealth() <= 0) {
                    this.endBattle(fasterPokemon, slowerPokemon);
                    break;
                }

                Thread.sleep(400);

                slowerPokemon.attack(fasterPokemon);
                System.out.println(slowerPokemon.getPokeName() + " attacked " + fasterPokemon.getPokeName() + " with " + slowerPokemon.getMoveUsed().getMoveName() + " dealing "
                + slowerPokemon.getLastDamageDealt() + " damage\n");

                Thread.sleep(50);

                if (slowerPokemon.getMoveUsed().getWeaknessResistanceMuliplier() == 2F || slowerPokemon.getMoveUsed().getWeaknessResistanceMuliplier() == 4F)
                    System.out.println("It's super effective!\n");
                else if (slowerPokemon.getMoveUsed().getWeaknessResistanceMuliplier() == 0.5F || slowerPokemon.getMoveUsed().getWeaknessResistanceMuliplier() == 0.25F)
                    System.out.println("It's not very effective...\n");

                Thread.sleep(400);

                if (fasterPokemon.getCurrentHealth() <= 0 || slowerPokemon.getCurrentHealth() <= 0) {
                    this.endBattle(fasterPokemon, slowerPokemon);
                    break;
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static void calculateFasterPokemon(Pokemon par1Poke, Pokemon par2Poke) {
        if (par1Poke.getPokeStats().getPokeSpeed() == par2Poke.getPokeStats().getPokeSpeed()) {
            Random random = new Random();

            int randNum = random.nextInt(3 - 1) + 1;

            if (randNum == 1) {
                fasterPokemon = par1Poke;
                slowerPokemon = par2Poke;
            }
            if (randNum == 2) {
                fasterPokemon = par2Poke;
                slowerPokemon = par1Poke;
            }
        } else {
            if (par1Poke.getPokeStats().getPokeSpeed() > par2Poke.getPokeStats().getPokeSpeed()) {
                fasterPokemon = par1Poke;
                slowerPokemon = par2Poke;
            } else if (par2Poke.getPokeStats().getPokeSpeed() > par1Poke.getPokeStats().getPokeSpeed()) {
                fasterPokemon = par2Poke;
                slowerPokemon = par1Poke;
            }
        }
    }

    private void endBattle(Pokemon par1Poke, Pokemon par2Poke) {
        try {
            System.out.println("\nBattle finished!");

            Thread.sleep(400);

            if (par2Poke.getCurrentHealth() <= 0)
                System.out.println("\n" + par1Poke.getPokeName() + " won the battle!");
            else if (par1Poke.getCurrentHealth() <= 0)
                System.out.println("\n" + par2Poke.getPokeName() + " won the battle!");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

我的执行类:包 com.mrmewniverse;

import javax.swing.JFrame;

import com.mrmewniverse.pokemon.battle.BattleHandler;
import com.mrmewniverse.pokemon.dex.PokeDex;
import com.mrmewniverse.pokemon.pokemon.Pokemon;

@SuppressWarnings("serial")
public class Start extends JFrame {

    static Pokemon poke1 = PokeDex.BULBASAUR;
    static Pokemon poke2 = PokeDex.CHARMANDER;
    static Pokemon poke3 = PokeDex.SQUIRTLE;

    public static Board board = new Board(poke1);

    public Start() {
        this.setSize(500, 290);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);

        this.add(board);

        this.setVisible(true);
    }

    public static void main ( String[] args ) {
        new Start();

        BattleHandler battle = new BattleHandler();
        battle.initiateBattle(poke1, poke2);
    }
}

还有我的 Board 类:包 com.mrmewniverse;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JPanel;

import com.mrmewniverse.pokemon.pokemon.Pokemon;

@SuppressWarnings("serial")
public class Board extends JPanel {

    private int health;
    private int maxHealth;

    public Board(Pokemon par1Poke) {
        this.health = par1Poke.getCurrentHealth();
        this.maxHealth = par1Poke.getPokeStats().getPokeMaxHealth();
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);

        g.setColor(Color.GRAY);
        g.fillRect(2, 2, 200, 20);

        int healthScale = health / maxHealth;
        g.setColor(Color.GREEN);
        g.fillRect(2, 2, 200 * healthScale, 20);
    }
}

随意批评我的编码,我需要帮助 XD

4

1 回答 1

0

这是我对您的代码的修订:

首先我没有使用paint()方法,而是使用了paintComponent()方法

很好,这解决了我的问题(“即使更新了,它仍然显示灰色条”)注意*板已更新,但您必须通过调用 update(Pokemon CurrentPokemon) 方法手动更新它;

    public class Board extends JPanel {

private int health;
private int maxHealth;

public Board(Pokemon par1Poke) {
    this.health = par1Poke.getCurrentHealth();
    this.maxHealth = par1Poke.getPokeStats().getPokeMaxHealth();
}

@Override
public void paintComponent(Graphics g) {
    super.paint(g);

    g.setColor(Color.GRAY);
    g.fillRect(2, 2, 200, 20);

    g.setColor(Color.GREEN);
    g.fillRect(2, 2, 200 *(health/maxHealth) , 20);
}

    public void update(Pokemon Poke1) {
    health = Poke1.getCurrentHealth();
    maxHealth = Poke1.getPokeStats().getPokeMaxHealth();
}

}

然后在你的主要

    public class Start extends JFrame {

static Pokemon poke1 = PokeDex.BULBASAUR;
static Pokemon poke2 = PokeDex.CHARMANDER;
static Pokemon poke3 = PokeDex.SQUIRTLE;

public static Board board = new Board( poke1 );

public Start() {
    this.setSize( 500, 290 );
    this.setDefaultCloseOperation( EXIT_ON_CLOSE );
    this.setLocationRelativeTo( null );

    this.add( board );

    this.setVisible( true );
}

public static void main ( String[] args ) {
    new Start();

     BattleHandler battle = new BattleHandler();
     battle.initiateBattle( poke1, poke2 ) ;
     board.update(poke1);
}

}

好吧,这不是最好的解决方案,这是我能想到的

我得到了你的代码并实现了我的,下载并运行。这可能是你想要的

于 2013-08-23T05:06:56.397 回答