2013-08-23 2 views
1

나는 극히 간단한 포켓몬 전투 시뮬레이터를 만들었고 업그레이드를 시작했다. 그것을 업그레이드함으로써, 최소한 (최소한 내 마음에) 아주 최소한의 방법으로 말입니다. 이 업그레이드는 콘솔 출력과 함께 제공되는 두 개의 HP 막대를 만드는 것으로 구성됩니다.자바에서 새로운 값으로 그려진 사각형을 업데이트하는 방법

내 BattleHandler : 패키지 com.mrmewniverse 내 문제는 여기에

내가 생각 코드는 직접 관련이있다 ... 내가 그들에게 포켓몬의 HP 업데이트 유지하는 방법을 몰라 .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); 
    } 
} 

그리고 내 보드 클래스 : 패키지 com.mrmewniverse;() 대신에 내가 paintComponent에 사용되는 페인트() 메소드를 사용하여

첫째 :

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

내 코딩 나를 비판 자유롭게, 나는

답변

0

이 코드 내 수정이 도움말 XD 필요 내 문제를 해결하는 방법

잘은 참고 * 보드가 업데이트 될 때 ("그것의 업데이트해도 회색 막대를 보여주는 유지")하지만 당신은 (포켓몬 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()와 같은 것을 의미합니까? 배틀 핸들러에 전화가 왔는데? – Kwibble

+0

예, definitlty ,, 매번 repaint()를 호출하여 보드를 새로 고침합니다. – misserandety

+0

음 ... 나는 그것을 시도했지만 바는 가득 채워져있다. 그래서 내가 틀린 일을하고 있다고 확신하기 때문에이 포럼에 물어 보았습니다. – Kwibble

관련 문제