英文:
The program doesn't end when player 1 life reaches 0
问题
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Swing16 implements ActionListener {
static int life1 = 100;
static int life2 = 100;
JLabel AttRig;
JFrame jfrm; // Declare the frame as a class variable
Swing16() {
// Initialize the frame settings
jfrm = new JFrame("game"); // Use class variable for frame
jfrm.setLayout(new FlowLayout());
jfrm.setSize(500, 500);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// ... (rest of your code)
JButton restartButton = new JButton("Restart Game"); // Add restart button
restartButton.addActionListener(this); // Add action listener
jfrm.add(restartButton); // Add restart button to frame
// ... (rest of your code)
}
// ... (rest of your code)
// Cosa succede se i bottoni vengono cliccati.
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Attack player two (-25)")) {
// ... (rest of your code)
} else if (e.getActionCommand().equals("Regenerate player one (+15)")) {
// ... (rest of your code)
} else if (e.getActionCommand().equals("Attack player one (-25)")) {
// ... (rest of your code)
} else if (e.getActionCommand().equals("Regenerate player two (+15)")) {
// ... (rest of your code)
} else if (e.getActionCommand().equals("Restart Game")) { // Handle restart button
life1 = 100;
life2 = 100;
AttRig.setText("Player 1 life: " + life1 + " | " + "Player 2 life: " + life2);
} else if (life1 < 0) {
// ... (rest of your code)
} else if (life2 < 0) {
// ... (rest of your code)
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Swing16();
}
});
}
}
I've added a "Restart Game" button to your program, and when this button is pressed, it resets the life values of both players to 100 and updates the label text accordingly.
英文:
I'm making a little game using the Java Swing, in which there are four bottoms: attack player 1, attack player 2, regenerate player 1, regenerate player 2. When attack player 1 or attack player 2 push bottom are pressed, it removes -25 from the life of the player, vice versa, when regenerate bottoms are pressed it adds +15 to player's life. Now, everything works perfectly here, but when life reaches zero, the player is dead and the label text changes in: "Player 1 is dead" or "Player2 is dead", but this never happens:
as you can see from the picture, player 1 life has been incremented and player 2 life even reached -50, this is the code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Swing16 implements ActionListener {
static int life1 = 100;
static int life2 = 100;
JLabel AttRig;
Swing16() {
// Imposta le impostazioni del frame
JFrame jfrm = new JFrame("game");
// Layout basico
jfrm.setLayout(new FlowLayout());
// Grandezza del frame (500 x 500).
jfrm.setSize(500, 500);
// Esci dal programma se l'utente esce dal frame.
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton attacca1 = new JButton("Attack player one (-25)"); //Attack player two (-25)
JButton rigenera1 = new JButton("Attack player two (-25)"); // Regenerate player one (+15)
JButton attacca2 = new JButton("Regenerate player one (+15)"); // Attack player one (-25)
JButton rigenera2 = new JButton("Regenerate player two (+15)");
// Aggiungi action listeners per i push bottom.
attacca1.addActionListener(this);
attacca2.addActionListener(this); // rigenera1.addActionListener(this);
rigenera1.addActionListener(this);
rigenera2.addActionListener(this);
// Aggiung pulsanti.
jfrm.add(attacca1);
jfrm.add(attacca2);
jfrm.add(rigenera1);
jfrm.add(rigenera2);
// Nuovo label.
AttRig = new JLabel("Player 1 life: 100 | Player 2 life: 100");
// Aggiungi label al frame
jfrm.add(AttRig);
// Visibilità del frame
jfrm.setVisible(true);
}
// Cosa succede se i bottoni vengono cliccati.
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("Attack player two (-25)")) {
life2 = life2 - 25;
AttRig.setText("Player 1 life: " + life1 + " | " + "Player 2 life: " + life2);
}
else if(e.getActionCommand().equals("Regenerate player one (+15)")){
life1 = life1 + 15;
AttRig.setText("Player 1 life: " + life1 + " | " + "Player 2 life: " + life2);
}
else if(e.getActionCommand().equals("Attack player one (-25)")) {
life1 = life1 - 25;
AttRig.setText("Player 1 life: " + life1 + " | " + "Player 2 life: " + life2);
}
else if(e.getActionCommand().equals("Regenerate player two (+15)")) {
life2 = life2 + 15;
AttRig.setText("Player 1 life: " + life1 + " | " + "Player 2 life: " + life2);
}
else if(life1 < 0) {
AttRig.setText("Player 1 dies...");
}
else if(life2 < 0) {
AttRig.setText("Player 2 dies...");
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Swing16();
}
});
}
}
what may I do make restart the program through another push bottom? Thank you very much.
答案1
得分: 1
你应该在修改生命值之后(!)检查生命值。
在 public void actionPerformed(ActionEvent e)
方法中,以下的 else if 部分应该分别改为两个独立的 if 语句,如下所示:
if (life1 < 0) {
AttRig.setText("玩家 1 已阵亡...");
}
if (life2 < 0) {
AttRig.setText("玩家 2 已阵亡...");
}
所以代码应该是这样的:
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("攻击玩家二 (-25)")) {
life2 = life2 - 25;
AttRig.setText("玩家 1 生命值: " + life1 + " | " + "玩家 2 生命值: " + life2);
} else if (e.getActionCommand().equals("玩家一恢复 (+15)")) {
life1 = life1 + 15;
AttRig.setText("玩家 1 生命值: " + life1 + " | " + "玩家 2 生命值: " + life2);
} else if (e.getActionCommand().equals("攻击玩家一 (-25)")) {
life1 = life1 - 25;
AttRig.setText("玩家 1 生命值: " + life1 + " | " + "玩家 2 生命值: " + life2);
} else if (e.getActionCommand().equals("玩家二恢复 (+15)")) {
life2 = life2 + 15;
AttRig.setText("玩家 1 生命值: " + life1 + " | " + "玩家 2 生命值: " + life2);
}
if (life1 < 0) {
AttRig.setText("玩家 1 已阵亡...");
}
if (life2 < 0) {
AttRig.setText("玩家 2 已阵亡...");
}
}
英文:
You should check the life values after(!) you changed them.
In public void actionPerformed(ActionEvent e)
method, the following else if part should be 2 separate if statements as follows:
if(life1 < 0) {
AttRig.setText("Player 1 dies...");
}
if(life2 < 0) {
AttRig.setText("Player 2 dies...");
}
So it should look like this:
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("Attack player two (-25)")) {
life2 = life2 - 25;
AttRig.setText("Player 1 life: " + life1 + " | " + "Player 2 life: " + life2);
}
else if(e.getActionCommand().equals("Regenerate player one (+15)")){
life1 = life1 + 15;
AttRig.setText("Player 1 life: " + life1 + " | " + "Player 2 life: " + life2);
}
else if(e.getActionCommand().equals("Attack player one (-25)")) {
life1 = life1 - 25;
AttRig.setText("Player 1 life: " + life1 + " | " + "Player 2 life: " + life2);
}
else if(e.getActionCommand().equals("Regenerate player two (+15)")) {
life2 = life2 + 15;
AttRig.setText("Player 1 life: " + life1 + " | " + "Player 2 life: " + life2);
}
if(life1 < 0) {
AttRig.setText("Player 1 dies...");
}
if(life2 < 0) {
AttRig.setText("Player 2 dies...");
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论