java格斗小游戏源码(好玩的java游戏代码)

以下是一个简单的Java格斗游戏示例,主要是用于教学目的。

这个游戏非常简化,但您可以根据自己的需求对其进行修改和扩展。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class FightingGame extends JFrame implements ActionListener {
    private JButton player1Attack, player2Attack;
    private JLabel player1Health, player2Health;
    private int health1 = 100, health2 = 100;

    public FightingGame() {
        setTitle("Simple Fighting Game");
        setSize(400, 200);
        setLayout(new GridLayout(2, 2));

        player1Attack = new JButton("Player 1 Attack");
        player1Attack.addActionListener(this);
        add(player1Attack);

        player1Health = new JLabel("Player 1 Health: 100");
        add(player1Health);

        player2Attack = new JButton("Player 2 Attack");
        player2Attack.addActionListener(this);
        add(player2Attack);

        player2Health = new JLabel("Player 2 Health: 100");
        add(player2Health);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == player1Attack) {
            health2 -= 10;
            player2Health.setText("Player 2 Health: " + health2);

            if (health2