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
你的意思是像Start.board.repaint();在BattleHandler中造成傷害後? – Kwibble
是的,每次你修改刷新你的主板通過調用repaint(); – misserandety
呃...我已經嘗試過了,但酒吧保持飽滿。這就是爲什麼我在這個論壇上問,因爲我很確定我做錯了什麼。 – Kwibble