我最近一直在做一个小型RPG游戏。目前,我遇到了一个问题,即当我选择 Go!从图形用户界面。我的主方法文件 RPGv6 中有 while 循环,另一个文件 BattleUI 中有 GUI。我应该从按钮行中选择一个动作(例如“攻击”),然后单击我要攻击的敌人。这将动作类型和目标提供给 executeSkills 方法,而 while 循环提供 currentActor。但是,每当我按下Go!按钮并执行此方法,while 循环停止。当我按下按钮时,该玩家的 turnTaken 应该设置为 true,而 while 循环应该将 currentActor 设置为 1(对于下一个演员)。
在以前的版本中,我设法让一个 while 循环和一个 GUI 以这种方式进行交互,所以我知道这是可以做到的——我只是不知道如何解决这个当前的问题。
public class RPGv6
{
public static actor[] player = new actor[3];
public static actor[] enemy = new actor[3];
public static String selectionName = "";
public static boolean actionDetermined = false;
public static boolean targetDetermined = false;
public static boolean permission = false;
public static int currentActor = 0;
public static Abilities actionSkills;
boolean placeholder = true;
public static void main(String[] args)
{
createActors();
System.out.println("actors created");
BattleUI.createAndShowGui(player, enemy);
System.out.println("gui created");
boolean //Using these booleans as loop conditions does not work; not sure why; works in the if statements just below
playerWin = (enemy[0].dead == true && (enemy[1].dead == true) && (enemy[2].dead == true)),
enemyWin = (player[0].dead == true && (player[1].dead == true) && (player[2].dead == true));
while(!playerWin && !enemyWin)
{
if(enemy[1].dead == true && (enemy[2].dead == true) && (enemy[3].dead == true))
break;
if(player[1].dead == true && (player[2].dead == true) && (player[3].dead == true))
break;
// player turn
if(!player[0].turnTaken)
{
currentActor = 0;
//System.out.println("loop executing");
}
if((!player[1].turnTaken) && (player[1].turnTaken))
{
currentActor = 1;
}
if((!player[2].turnTaken) && (player[2].turnTaken) && (player[1].turnTaken))
{
currentActor = 2;
}
checkForDeath(player, enemy);
checkForPermission();
// enemy turn
while(permission)
{
if(!enemy[1].turnTaken)
{
currentActor = 1;
}
if((!enemy[2].turnTaken) && (enemy[1].turnTaken))
{
System.out.println("entered player two");
currentActor = 2;
}
if((!enemy[3].turnTaken) && (enemy[2].turnTaken) && (enemy[1].turnTaken))
{
currentActor = 3;
}
}
checkForDeath(player, enemy);
}
}
public static void createActors()
{
player[0] = new actor(1, "Knight");
player[1] = new actor(1, "Mage");
player[2] = new actor(1, "Rogue");
enemy[0] = new actor(1, "Slime");
enemy[1] = new actor(1, "Skeleton");
enemy[2] = new actor(1, "Necro");
}
public static void checkForPermission()
{
if((player[1].turnTaken) && (player[2].turnTaken) && (player[3].turnTaken))
{
permission = true;
}
}
public static void checkForDeath(actor[] player, actor[] enemy)
{
for(int i = 1; i < player.length; i++)
{
if(player[i].HP <= 0)
player[i].dead = true;
if(player[i].dead == true)
player[i].turnTaken = true;
if(enemy[i].HP <= 0)
enemy[i].dead = true;
if(enemy[i].dead == true)
enemy[i].turnTaken = true;
}
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class BattleUI extends JFrame implements MouseListener
{
public static actor[] player = RPGv6.player;
public static actor[] enemy = RPGv6.enemy;
public static int
actionType = 0,
actionName = 0,
target = 0;
static actor[] targetType;
static JButton
jbtAttack = new JButton("Attack"), jbtSkills = new JButton("Skills"),
jbtMagic = new JButton("Magic"), jbtInventory = new JButton("Inventory"),
jbtGo = new JButton("Go!"),
kntSk1, kntSk2, kntSk3, mgSk1,
mgSk2, rgSk1, rgSk2, rgSk3, rgSk4, kntMgc1, kntMgc2,
mgMgc1, mgMgc2, mgMgc3, mgMgc4, rgMgc1, rgMgc2, rgMgc3,
invt1, invt2, invt3, invt4, invt5, invt6;
public static JButton
jbtply1 = new JButton(), jbtply2 = new JButton(), jbtply3 = new JButton(),
jbtnmy1 = new JButton(), jbtnmy2 = new JButton(), jbtnmy3 = new JButton();
public static JLabel
ply1Name, ply1HP, ply1MP, ply1SP, ply1Status,
ply2Name, ply2HP, ply2MP, ply2SP, ply2Status,
ply3Name, ply3HP, ply3MP, ply3SP, ply3Status,
nmy1Name, nmy1HP, nmy1MP, nmy1SP, nmy1Status,
nmy2Name, nmy2HP, nmy2MP, nmy2SP, nmy2Status,
nmy3Name, nmy3HP, nmy3MP, nmy3SP, nmy3Status;
private static JPanel
pStatusField1, pStatusField2, pStatusField3,
eStatusField4, eStatusField5, eStatusField6;
public static JTextArea jta;
static Color defaultColor;
//CardLayout
private static CardLayout cardManager;
private static JPanel deck;
private static String names[] = { "KnightSkillsCard", "MageSkillsCard", "RogueSkillsCard",
"KnightMagicCard", "MageMagicCard", "RogueMagicCard",
"InventoryCard"};
public BattleUI(actor[] player, actor[] enemy)
{
//begin pStatus
// Labels displaying actor statuses
ply1Name = new JLabel(player[0].name);
ply1HP = new JLabel("HP: "+player[0].HP);
ply1MP = new JLabel("MP: "+player[0].MP);
ply1SP = new JLabel("SP: "+player[0].SP);
ply1Status = new JLabel("OK"); // placeholder; will need to replace with a method to generate icons symbolizing status
ply2Name = new JLabel(player[1].name);
ply2HP = new JLabel("HP: "+player[1].HP);
ply2MP = new JLabel("MP: "+player[1].MP);
ply2SP = new JLabel("SP: "+player[1].SP);
ply2Status = new JLabel("OK");
ply3Name = new JLabel(player[2].name);
ply3HP = new JLabel("HP: "+player[2].HP);
ply3MP = new JLabel("MP: "+player[2].MP);
ply3SP = new JLabel("SP: "+player[2].SP);
ply3Status = new JLabel("OK");
nmy1Name = new JLabel(enemy[0].name);
nmy1HP = new JLabel("HP: "+enemy[0].HP);
nmy1MP = new JLabel("MP: "+enemy[0].MP);
nmy1SP = new JLabel("SP: "+enemy[0].SP);
nmy1Status = new JLabel("OK");
nmy2Name = new JLabel(enemy[1].name);
nmy2HP = new JLabel("HP: "+enemy[1].HP);
nmy2MP = new JLabel("MP: "+enemy[1].MP);
nmy2SP = new JLabel("SP: "+enemy[1].SP);
nmy2Status = new JLabel("OK");
nmy3Name = new JLabel(enemy[2].name);
nmy3HP = new JLabel("HP: "+enemy[2].HP);
nmy3MP = new JLabel("MP: "+enemy[2].MP);
nmy3SP = new JLabel("SP: "+enemy[2].SP);
nmy3Status = new JLabel("OK");
// Panels holding the statuses for each actor
pStatusField1 = new JPanel();
pStatusField1.setLayout(new GridLayout(5, 1));
pStatusField1.add(ply1Name);
pStatusField1.add(ply1HP);
pStatusField1.add(ply1MP);
pStatusField1.add(ply1SP);
pStatusField1.add(ply1Status);
pStatusField2 = new JPanel();
pStatusField2.setLayout(new GridLayout(5, 1));
pStatusField2.add(ply2Name);
pStatusField2.add(ply2HP);
pStatusField2.add(ply2MP);
pStatusField2.add(ply2SP);
pStatusField2.add(ply2Status);
pStatusField3 = new JPanel();
pStatusField3.setLayout(new GridLayout(5, 1));
pStatusField3.add(ply3Name);
pStatusField3.add(ply3HP);
pStatusField3.add(ply3MP);
pStatusField3.add(ply3SP);
pStatusField3.add(ply3Status);
eStatusField4 = new JPanel();
eStatusField4.setLayout(new GridLayout(5, 1));
eStatusField4.add(nmy1Name);
eStatusField4.add(nmy1HP);
eStatusField4.add(nmy1MP);
eStatusField4.add(nmy1SP);
eStatusField4.add(nmy1Status);
eStatusField5 = new JPanel();
eStatusField5.setLayout(new GridLayout(5, 1));
eStatusField5.add(nmy2Name);
eStatusField5.add(nmy2HP);
eStatusField5.add(nmy2MP);
eStatusField5.add(nmy2SP);
eStatusField5.add(nmy2Status);
eStatusField6 = new JPanel();
eStatusField6.setLayout(new GridLayout(5, 1));
eStatusField6.add(nmy3Name);
eStatusField6.add(nmy3HP);
eStatusField6.add(nmy3MP);
eStatusField6.add(nmy3SP);
eStatusField6.add(nmy3Status);
// Add the labels for actor status fields into buttons
jbtply1.add(pStatusField1);
jbtply2.add(pStatusField2);
jbtply3.add(pStatusField3);
jbtnmy1.add(eStatusField4);
jbtnmy2.add(eStatusField5);
jbtnmy3.add(eStatusField6);
// Divides information display for player and enemy actors
JPanel pStatusFieldPlayer = new JPanel();
pStatusFieldPlayer.setLayout(new FlowLayout());
pStatusFieldPlayer.setBorder(BorderFactory.createTitledBorder("Player"));
pStatusFieldPlayer.add(jbtply1);
pStatusFieldPlayer.add(jbtply2);
pStatusFieldPlayer.add(jbtply3);
JPanel pStatusFieldEnemy = new JPanel();
pStatusFieldEnemy.setBorder(BorderFactory.createTitledBorder("Enemy"));
pStatusFieldEnemy.add(jbtnmy1);
pStatusFieldEnemy.add(jbtnmy2);
pStatusFieldEnemy.add(jbtnmy3);
// Panel for displaying actor information to user
JPanel pStatus = new JPanel();
pStatus.setLayout(new GridLayout(2, 1));
pStatus.add(pStatusFieldPlayer, BorderLayout.NORTH);
pStatus.add(pStatusFieldEnemy, BorderLayout.SOUTH);
//end pStatus
// Panel for displaying action options
JPanel pActions = new JPanel();
pActions.setLayout(new GridLayout(5, 1));
pActions.add(jbtAttack);
pActions.add(jbtSkills);
pActions.add(jbtMagic);
pActions.add(jbtInventory);
pActions.add(jbtGo);
// Text Area for displaying a narration of the executed actions
JScrollPane scrollPane = new JScrollPane(jta = new JTextArea(5, 22));
scrollPane.getVerticalScrollBar().addAdjustmentListener
(new AdjustmentListener()
{
public void adjustmentValueChanged(AdjustmentEvent e)
{
e.getAdjustable().setValue(e.getAdjustable().getMaximum());
}
}
);
jta.setWrapStyleWord(true);
jta.setLineWrap(true);
jta.setSize(250, 90);
jta.setEditable(false);
// begin CardLayout
// CardLayout Panel for displaying the abilities available to the current actor
deck = new JPanel();
cardManager = new CardLayout();
deck.setLayout( cardManager );
JPanel
card1 = new JPanel(),
card2 = new JPanel(),
card3 = new JPanel(),
card4 = new JPanel(),
card5 = new JPanel(),
card6 = new JPanel(),
card7 = new JPanel();
//card1
kntSk1 = new JButton("Deadly Blow 20SP");
kntSk2 = new JButton("Cripple 35SP");
kntSk3 = new JButton("Maim 40SP");
//card2
mgSk1 = new JButton("Meditate");
mgSk2 = new JButton("Balm 15SP");
//card3
rgSk1 = new JButton("Poison 35SP");
rgSk2 = new JButton("Brew 20SP");
rgSk3 = new JButton("Blind 30SP");
rgSk4 = new JButton("Backstab 45SP");
//card4
kntMgc1 = new JButton("Ward 15MP");
kntMgc2 = new JButton("Bless 20MP");
//card5
mgMgc1 = new JButton("Fireball 40MP");
mgMgc2 = new JButton("Acid Burn 50MP");
mgMgc3 = new JButton("Heal 25MP");
mgMgc4 = new JButton("Time Stop 75MP");
//card6
rgMgc1 = new JButton("Dazzle 20MP");
rgMgc2 = new JButton("Sting 30MP");
rgMgc3 = new JButton("Heal 20MP");
//card7
invt1 = new JButton("Health Potion");
invt2 = new JButton("Mana Potion");
invt3 = new JButton("Stamina Potion");
invt4 = new JButton("Shield Gem");
invt5 = new JButton("Water of Life");
invt6 = new JButton("Shuriken");
card7.setLayout(new GridLayout(3, 3));
card1.add(kntSk1);
card1.add(kntSk2);
card1.add(kntSk3);
card2.add(mgSk1);
card2.add(mgSk2);
card3.add(rgSk1);
card3.add(rgSk2);
card3.add(rgSk3);
card3.add(rgSk4);
card4.add(kntMgc1);
card4.add(kntMgc2);
card5.add(mgMgc1);
card5.add(mgMgc2);
card5.add(mgMgc3);
card5.add(mgMgc4);
card6.add(rgMgc1);
card6.add(rgMgc2);
card6.add(rgMgc3);
card7.add(invt1);
card7.add(invt2);
card7.add(invt3);
card7.add(invt4);
card7.add(invt5);
card7.add(invt6);
deck.add(card1, names[0]);
deck.add(card2, names[1]);
deck.add(card3, names[2]);
deck.add(card4, names[3]);
deck.add(card5, names[4]);
deck.add(card6, names[5]);
deck.add(card7, names[6]);
// end CardLayout
// Add Listeners to the Buttons
kntSk1.addActionListener(new SkillListener());
kntSk2.addActionListener(new SkillListener());
kntSk3.addActionListener(new SkillListener());
mgSk1.addActionListener(new SkillListener());
mgSk2.addActionListener(new SkillListener());
rgSk1.addActionListener(new SkillListener());
rgSk2.addActionListener(new SkillListener());
rgSk3.addActionListener(new SkillListener());
rgSk4.addActionListener(new SkillListener());
kntMgc1.addActionListener(new MagicListener());
kntMgc2.addActionListener(new MagicListener());
mgMgc1.addActionListener(new MagicListener());
mgMgc2.addActionListener(new MagicListener());
mgMgc3.addActionListener(new MagicListener());
mgMgc4.addActionListener(new MagicListener());
rgMgc1.addActionListener(new MagicListener());
rgMgc2.addActionListener(new MagicListener());
rgMgc3.addActionListener(new MagicListener());
invt1.addActionListener(new InventListener());
invt2.addActionListener(new InventListener());
invt3.addActionListener(new InventListener());
invt4.addActionListener(new InventListener());
invt5.addActionListener(new InventListener());
invt6.addActionListener(new InventListener());
jbtply1.addActionListener(new SelectionListener());
jbtply2.addActionListener(new SelectionListener());
jbtply3.addActionListener(new SelectionListener());
jbtnmy1.addActionListener(new SelectionListener());
jbtnmy2.addActionListener(new SelectionListener());
jbtnmy3.addActionListener(new SelectionListener());
jbtnmy1.addMouseListener(this);
jbtnmy2.addMouseListener(this);
jbtnmy3.addMouseListener(this);
jbtAttack.addMouseListener(this);
jbtSkills.addMouseListener(this);
jbtMagic.addMouseListener(this);
jbtInventory.addMouseListener(this);
jbtAttack.addActionListener(new ButtonListener());
jbtSkills.addActionListener(new ButtonListener());
jbtMagic.addActionListener(new ButtonListener());
jbtInventory.addActionListener(new ButtonListener());
jbtGo.addActionListener(new GoListener());
// Add everything in
JPanel pNarration = new JPanel();
pNarration.add(scrollPane, BorderLayout.SOUTH);
JPanel pHolder = new JPanel();
pHolder.setLayout(new GridLayout(2, 1));
pHolder.add(pActions, BorderLayout.WEST);
JPanel pHolder2 = new JPanel();
pHolder2.setLayout(new GridLayout(2, 1));
pHolder2.add(pStatusFieldPlayer, BorderLayout.NORTH);
pHolder2.add(pStatusFieldEnemy, BorderLayout.SOUTH);
JPanel pHolder3 = new JPanel();
pHolder3.setLayout(new GridLayout(2, 1, - 200, 0));
pHolder3.add(deck);
pHolder3.add(pNarration);
// add contents into frame
add(pHolder, BorderLayout.WEST);
add(pHolder2);
add(pHolder3, BorderLayout.SOUTH);
}
class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
// Action Selection
if(e.getSource() == jbtAttack)
{
actionType = 0;
actionName = 0;
RPGv6.actionDetermined = true;
System.out.println("attack button pressed");
}
if(e.getSource() == jbtSkills)
{
// Selecting these buttons determines what card and options are displayed to the user
}
if(e.getSource() == jbtMagic)
{
}
if(e.getSource() == jbtInventory)
{
}
}
}
class GoListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == jbtGo)
{
System.out.println(RPGv6.currentActor);
System.out.println(actionName);
System.out.println(actionType);
System.out.println(target);
if(RPGv6.actionDetermined && RPGv6.targetDetermined)
{
executeSkill(actionType, actionName, player[RPGv6.currentActor], targetType[target]);
player[RPGv6.currentActor].turnTaken = true;
System.out.println("Go button pressed");
}
else
{
jta.append("You need to select an action and a target!");
}
}
}
}
class SkillListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
// Knight Skills
if(e.getSource() == "")
{ //Deadly Blow
actionType = 1; // 0 = Attack; 1 = Skill; 2 = Magic; 3 = Inventory
actionName = 0; // Determines which skill to use
RPGv6.actionDetermined = true;
}
if(e.getSource() == "")
{
actionType = 1;
actionName = 1;
RPGv6.actionDetermined = true;
}
if(e.getSource() == "")
{
actionType = 1;
actionName = 2;
RPGv6.actionDetermined = true;
}
// Mage kills
if(e.getSource() == "")
{
actionType = 1;
actionName = 0;
RPGv6.actionDetermined = true;
}
if(e.getSource() == "")
{
actionType = 1;
actionName = 1;
RPGv6.actionDetermined = true;
}
// Rogue Skills
if(e.getSource() == "")
{
actionType = 1;
actionName = 0;
RPGv6.actionDetermined = true;
}
if(e.getSource() == "")
{
actionType = 1;
actionName = 1;
RPGv6.actionDetermined = true;
}
if(e.getSource() == "")
{
actionType = 1;
actionName = 2;
RPGv6.actionDetermined = true;
}
if(e.getSource() == "")
{
actionType = 1;
actionName = 3;
RPGv6.actionDetermined = true;
}
}
}
class MagicListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == "")
{
actionType = 2;
actionName = 0;
RPGv6.actionDetermined = true;
}
if(e.getSource() == "")
{
actionType = 2;
actionName = 1;
RPGv6.actionDetermined = true;
}
if(e.getSource() == "")
{
actionType = 2;
actionName = 0;
RPGv6.actionDetermined = true;
}
if(e.getSource() == "")
{
actionType = 2;
actionName = 1;
RPGv6.actionDetermined = true;
}
if(e.getSource() == "")
{
actionType = 2;
actionName = 2;
RPGv6.actionDetermined = true;
}
if(e.getSource() == "")
{
actionType = 2;
actionName = 3;
RPGv6.actionDetermined = true;
}
if(e.getSource() == "")
{
actionType = 2;
actionName = 0;
RPGv6.actionDetermined = true;
}
if(e.getSource() == "")
{
actionType = 2;
actionName = 1;
RPGv6.actionDetermined = true;
}
if(e.getSource() == "")
{
actionType = 2;
actionName = 2;
RPGv6.actionDetermined = true;
}
}
}
class InventListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == "")
{
actionType = 3;
actionName = 0;
RPGv6.actionDetermined = true;
}
if(e.getSource() == "")
{
actionType = 3;
actionName = 1;
RPGv6.actionDetermined = true;
}
if(e.getSource() == "")
{
actionType = 3;
actionName = 2;
RPGv6.actionDetermined = true;
}
if(e.getSource() == "")
{
actionType = 3;
actionName = 3;
RPGv6.actionDetermined = true;
}
if(e.getSource() == "")
{
actionType = 3;
actionName = 4;
RPGv6.actionDetermined = true;
}
if(e.getSource() == "")
{
actionType = 3;
actionName = 5;
RPGv6.actionDetermined = true;
}
}
}
class SelectionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
// Player Targets
if(e.getSource() == jbtply1)
{
targetType = player;
target = 0;
RPGv6.targetDetermined = true;
}
if(e.getSource() == jbtply2)
{
targetType = player;
target = 1;
RPGv6.targetDetermined = true;
}
if(e.getSource() == jbtply3)
{
targetType = player;
target = 2;
RPGv6.targetDetermined = true;
}
// Enemy Targets
if(e.getSource() == jbtnmy1)
{
targetType = enemy;
target = 0;
RPGv6.targetDetermined = true;
System.out.println("enemy0 selected");
}
if(e.getSource() == jbtnmy2)
{
targetType = enemy;
target = 1;
RPGv6.targetDetermined = true;
System.out.println("enemy1 selected");
}
if(e.getSource() == jbtnmy3)
{
targetType = enemy;
target = 2;
RPGv6.targetDetermined = true;
System.out.println("enemy2 selected");
}
}
}
public void mouseClicked(MouseEvent e)
{
//if(e.getSource() == jbtAttack)
//{
// //ui.deColorButtonBackGrounds();
// jbtAttack.setBackground(Color.orange);
//}
//if(e.getSource() == jbtSkills)
//{
// //ui.deColorButtonBackGrounds();
// jbtSkills.setBackground(Color.orange);
//}
//if(e.getSource() == jbtMagic)
//{
// //ui.deColorButtonBackGrounds();
// jbtMagic.setBackground(Color.orange);
//}
//if(e.getSource() == jbtInventory)
//{
// ui.deColorButtonBackGrounds();
// jbtInventory.setBackground(Color.orange);
//}
}
public void mouseEntered(MouseEvent e)
{
//if(e.getSource() == )
//{
// eStatusField4.setBackground(Color.red);
//}
//if(e.getSource() == jbtOpt2)
//{
// eStatusField5.setBackground(Color.red);
//}
//if(e.getSource() == jbtOpt3)
//{
// eStatusField6.setBackground(Color.red);
//}
}
public void mouseExited(MouseEvent e)
{
//if(e.getSource() == jbtOpt1)
//{
// eStatusField4.setBackground(defaultColor);
//}
//if(e.getSource() == jbtOpt2)
//{
// eStatusField5.setBackground(defaultColor);
//}
//if(e.getSource() == jbtOpt3)
//{
// eStatusField6.setBackground(defaultColor);
//}
}
public void mousePressed(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public static void createAndShowGui(actor[] player, actor[] enemy)
{
BattleUI frame = new BattleUI(player, enemy);
frame.setTitle("Battle");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setResizable(false);
frame.setVisible(true);
}
public static void executeSkill(int actionType, int actionName, actor caller, actor target)
{
if(actionType == 0)//Attack
{
actions.attack(caller, target);
}
if(actionType == 1)
{
}
if(actionType == 2)
{
}
if(actionType == 3)
{
}
}
}