我目前正在使用 java 中的基于文本的 rpg。玩家每走一定步数,就会发生随机战斗。每次我运行战斗系统时,程序都会进入一个永远循环并且屏幕只是冻结。
我怎样才能解决这个问题?这是处理所有这些的主类的代码。
import javax.swing.*;
import java.awt.event.*;
public class Runner extends JFrame{
String mat[][] = {{".",".",".",".",".",".",".",".",".","."},
{".",".",".",".",".",".",".",".",".","."},
{".",".",".",".",".",".",".",".",".","."},
{".",".",".",".",".",".",".",".",".","."},
{".",".",".",".",".",".",".",".",".","."},
{".",".",".",".",".",".",".",".",".","."},
{".",".",".",".",".",".",".",".",".","."},
{".",".",".",".",".",".",".",".",".","."},
{".",".",".",".",".",".",".",".",".","."},
{".",".",".",".",".",".",".",".",".","."},
{".",".",".",".",".",".",".",".",".","."}};
boolean Burn;
JPanel j = new JPanel();
Player p = new Player(1,20,20,0,2,6,0,7);
Enemy en = new goblin(1,8,8,0,1,3);
private int steps = 0, randomBattle = (int)(Math.random()*(7-4))+4;
public Runner()
{
System.out.println(""+p.getHp());
JPanel j = new JPanel();
JLabel gridDisplay = new JLabel();
JButton north = new JButton("N");
JButton south = new JButton("S");
JButton east = new JButton("E");
JButton west = new JButton("W");
Grid grins = new Grid(mat,1,1,mat.length,mat[0].length);
gridDisplay.setBounds(200,10,200,200);
gridDisplay.setText(grins.toString());
j.setLayout(null);
north.setBounds(100,50,50,50);
south.setBounds(100,105,50,50);
east.setBounds(45,77,50,50);
west.setBounds(155,77,50,50);
j.add(north);
j.add(south);
j.add(east);
j.add(west);
j.add(gridDisplay);
j.setVisible(true);
j.setSize(800,600);
add(j);
north.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (randomBattle == steps)
{
j.setVisible(false);
System.out.println("HP: "+p.getHp());
Battle(p, en);
}
grins.changeX(-1);
gridDisplay.setText(grins.toString());
steps++;
}
});
south.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (randomBattle == steps)
{
j.setVisible(false);
System.out.println("HP: "+p.getHp());
Battle(p, en);
}
grins.changeX(1);
gridDisplay.setText(grins.toString());
steps++;
}
});
east.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (randomBattle == steps)
{
j.setVisible(false);
System.out.println("HP: "+p.getHp());
Battle(p, en);
}
grins.changeY(-1);
gridDisplay.setText(grins.toString());
steps++;
}
});
west.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (randomBattle == steps)
{
j.setVisible(false);
System.out.println("HP: "+p.getHp());
Battle(p, en);
}
grins.changeY(1);
gridDisplay.setText(grins.toString());
steps++;
}
});
}
public void Battle(Player p, Enemy e)
{
JButton attack = new JButton("Attack");
JLabel playerHp = new JLabel();
JLabel enemyHp = new JLabel();
JLabel battleStatus = new JLabel();
boolean battle = true;
setFocusable(true);
setLayout(null);
playerHp.setBounds(400,200,300,20);
enemyHp.setBounds(400,20,300,20);
attack.setBounds(350,300,100,20);
playerHp.setText("HP :"+p.getHp());
enemyHp.setText("HP :"+e.getHp());
add(playerHp);
add(enemyHp);
add(attack);
add(battleStatus);
attack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
p.attack((Fighter)en);
Burn = false;
}
});
while(battle == true){
if(Burn == false)
{
en.attack((Fighter)p);
Burn = true;
}
if(p.getHp() <= 0)
{
battle = false;
}
}
}
public static void main(String args[])
{
Runner r = new Runner();
r.setVisible(true);
}
}