2013-08-23 28 views
1

我制作了一个非常简单的Pokémon战斗模拟器,我开始升级它。通过升级它,我的意思是以非常简约的方式(至少在我的脑海里)。此升级包括创建两个HP条以伴随我的控制台输出。我的问题是我不知道如何让他们与神奇宝贝的HP更新中...如何使用java中的新值更新绘制的矩形

这是我认为的代码,直接关系到它:

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

而我的板级: package 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

回答

0

这是我的修订代码:

第一,而不是用我所用的paintComponent paint()方法()方法

清楚,解决我的问题(“它不断显示即使其更新的灰色条”) 注*董事会被更新,但你必须通过调用 更新(口袋妖怪CurrentPokemon)甲基手动更新OD;

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

}

那么肯定这不是最好的解决方案,它的一切我能想到

我得到了你的代码并实施了我的,Download this and run.这可能是你想要的

+1

你的意思是像Start.board.repaint();在BattleHandler中造成伤害后? – Kwibble

+0

是的,每次你修改刷新你的主板通过调用repaint(); – misserandety

+0

呃...我已经尝试过了,但酒吧保持饱满。这就是为什么我在这个论坛上问,因为我很确定我做错了什么。 – Kwibble