[Java][项目][战斗逻辑]基于JFrame的文字游戏

项目注解:

Core:启动文件

AttributeBean:玩家属性
BackpackedBean:背包设计(未完成)
BackpackedFrame:背包页面(未完成)
BattleField:战斗逻辑(核心)
MainFrame:主界面(第一个启动的界面)
MobsBean:怪物属性
MobsFrame:打怪界面
public class AttributeBean {
    String name;
    int Blood = 100;
    int Magic = 100;
    int Damage = 10;

    public AttributeBean() {
    }

    public AttributeBean(String name, int Blood, int Magic, int Damage) {
        this.name = name;
        this.Blood = Blood;
        this.Magic = Magic;
        this.Damage = Damage;
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return Blood
     */
    public int getBlood() {
        return Blood;
    }

    /**
     * 设置
     * @param Blood
     */
    public void setBlood(int Blood) {
        this.Blood = Blood;
    }

    /**
     * 获取
     * @return Magic
     */
    public int getMagic() {
        return Magic;
    }

    /**
     * 设置
     * @param Magic
     */
    public void setMagic(int Magic) {
        this.Magic = Magic;
    }

    /**
     * 获取
     * @return Damage
     */
    public int getDamage() {
        return Damage;
    }

    /**
     * 设置
     * @param Damage
     */
    public void setDamage(int Damage) {
        this.Damage = Damage;
    }

    public String toString() {
        return "AttributeBean{name = " + name + ", Blood = " + Blood + ", Magic = " + Magic + ", Damage = " + Damage + "}";
    }
}

public class BackpackedBean {
}
import com.sun.tools.javac.Main;

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

public class BackpackedFrame extends JFrame {

    public BackpackedFrame() {
        super("背包页面");
        setAlwaysOnTop(true);
        setSize(400, 300);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        // 设置布局管理器为 BorderLayout
        setLayout(new BorderLayout());

        JLabel ad = new JLabel("欢迎来到背包界面
这里会储存一些你的装备和道具
相信你可以越来越强大的!", SwingConstants.CENTER); add(ad, BorderLayout.NORTH); // 将广告标签放置在窗口的顶部 MobsBean zombie = new MobsBean("僵尸Lv1",100,2); MobsBean Dragon = new MobsBean("巨龙Lv5",400,10); setVisible(true); } }

import com.sun.tools.javac.Main;

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

public class BackpackedFrame extends JFrame {

    public BackpackedFrame() {
        super("背包页面");
        setAlwaysOnTop(true);
        setSize(400, 300);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        // 设置布局管理器为 BorderLayout
        setLayout(new BorderLayout());

        JLabel ad = new JLabel("欢迎来到背包界面
这里会储存一些你的装备和道具
相信你可以越来越强大的!", SwingConstants.CENTER); add(ad, BorderLayout.NORTH); // 将广告标签放置在窗口的顶部 MobsBean zombie = new MobsBean("僵尸Lv1",100,2); MobsBean Dragon = new MobsBean("巨龙Lv5",400,10); setVisible(true); } }

import javax.swing.*;

public class BattleField {

    /**
     * 执行战斗逻辑
     * @param player 玩家对象
     * @param mob 怪物对象
     */
    public void fight(AttributeBean player, MobsBean mob)  {
        // 输出战斗开始信息
        System.out.println(player.getName() + " 开始与 " + mob.getName() + " 战斗!");

        // 循环战斗直到一方血量为0
        while (player.getBlood() > 0 && mob.getBlood() > 0) {
            // 玩家攻击怪物
            mob.setBlood(mob.getBlood() - player.getDamage());
            System.out.println(player.getName() + " 攻击了 " + mob.getName() + ", " + mob.getName() + " 剩余血量: " + mob.getBlood());

            // 如果怪物血量为0,跳出循环
            if (mob.getBlood() <= 0) {
                break;
            }

            // 怪物反击玩家
            player.setBlood(player.getBlood() - mob.getDamage());
            System.out.println(mob.getName() + " 反击了 " + player.getName() + ", " + player.getName() + " 剩余血量: " + player.getBlood());
        }

        // 判断胜利方
        if (player.getBlood() > 0) {
            System.out.println(player.getName() + " 获得了胜利!");
            // 增加玩家生命值和攻击力
            player.setBlood(player.getBlood() + 20); // 假设战斗胜利增加20点生命值
            player.setDamage(player.getDamage() + 5); // 假设战斗胜利增加5点攻击力
            System.out.println(player.getName() + " 的属性提升了!当前生命值: " + player.getBlood() + ", 当前攻击力: " + player.getDamage());
        } else {
            System.out.println(player.getName() + " 被击败了!");
        }
    }
}
public class Core
{
    public static void main(String[] args){
        new MainFrame();


    }
}
import com.sun.tools.javac.Main;

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

public class MainFrame extends JFrame {
    AttributeBean PersonA = new AttributeBean();
    JLabel Person = new JLabel(
            "冒险者的姓名是:" + PersonA.name +
                    "
当前生命值:" + PersonA.Blood + "
当前魔力值:" + PersonA.Magic + "
当前攻击力:" + PersonA.Damage + "", SwingConstants.CENTER ); public MainFrame() { super("归墟之境v1.0 ByXiaoyongcai"); setAlwaysOnTop(true); setSize(400, 400); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置布局管理器为 BorderLayout setLayout(new BorderLayout()); JLabel ad = new JLabel("欢迎来到 归墟之境
当前版本为V1.0
制作者敬上", SwingConstants.CENTER); add(ad, BorderLayout.NORTH); // 将广告标签放置在窗口的顶部 add(Person, BorderLayout.CENTER); // 将角色信息标签放置在窗口的中央 Button();//我调用了下面的Button方法 setVisible(true); AttributeBean s1 = new AttributeBean("张三",100,1,1); } public void Button() { JPanel buttonPanel = new JPanel(); JButton toMobsButton = new JButton("进入怪物世界"); toMobsButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // 在创建 MobsFrame 实例时传递 PersonA 对象 MobsFrame mobsFrame = new MobsFrame(PersonA); mobsFrame.setVisible(true); // 如果需要,可以隐藏或关闭当前界面 // MainFrame.this.setVisible(false); // MainFrame.this.dispose(); } }); buttonPanel.add(toMobsButton); JButton toBackpackButton = new JButton("进入背包界面"); toBackpackButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // 创建 BackpackFrame 界面的实例 BackpackedFrame backpackFrame = new BackpackedFrame(); // 设置 BackpackFrame 界面的可见性为 true,从而显示它 backpackFrame.setVisible(true); // 如果需要,可以隐藏或关闭当前界面 // MainFrame.this.setVisible(false); // MainFrame.this.dispose(); } }); buttonPanel.add(toBackpackButton); JButton button = new JButton("修改用户名"); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { showDialog(); } }); buttonPanel.add(button); add(buttonPanel, BorderLayout.SOUTH); } private void showDialog() { // 创建一个模态对话框 JDialog dialog = new JDialog(this, "输入弹窗", true); dialog.setSize(250, 150); dialog.setLayout(new FlowLayout()); dialog.setLocationRelativeTo(this); // 创建文本框 JTextField textField = new JTextField(10); dialog.add(textField); JButton confirmButton = new JButton("确认"); confirmButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // 更新 PersonA 的 name 属性 PersonA.name = textField.getText(); // 更新显示 PersonA 信息的 JLabel updatePersonLabel(); // 关闭对话框 dialog.dispose(); } }); dialog.add(confirmButton); // 显示对话框 dialog.setVisible(true); } // 新增一个方法来更新 Person 标签 private void updatePersonLabel() { Person.setText( "冒险者的姓名是:" + PersonA.name + "
当前生命值:" + PersonA.Blood + "
当前魔力值:" + PersonA.Magic + "
当前攻击力:" + PersonA.Damage + "" ); } }
public class MobsBean {
    String name;
    int blood;
    int damage;

    public MobsBean() {
    }

    public MobsBean(String name, int blood, int damage) {
        this.name = name;
        this.blood = blood;
        this.damage = damage;
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return blood
     */
    public int getBlood() {
        return blood;
    }

    /**
     * 设置
     * @param blood
     */
    public void setBlood(int blood) {
        this.blood = blood;
    }

    /**
     * 获取
     * @return damage
     */
    public int getDamage() {
        return damage;
    }

    /**
     * 设置
     * @param damage
     */
    public void setDamage(int damage) {
        this.damage = damage;
    }

    public String toString() {
        return "MobsBean{name = " + name + ", blood = " + blood + ", damage = " + damage + "}";
    }
}
import com.sun.tools.javac.Main;

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

public class MobsFrame extends JFrame {
    AttributeBean Person = new AttributeBean();

    MobsBean zombie = new MobsBean("僵尸Lv1", 100, 2);
    MobsBean Dragon = new MobsBean("巨龙Lv5", 300, 10);

    public MobsFrame(AttributeBean person) {
        super("对战界面");
        this.Person = person;
        setAlwaysOnTop(true);
        setSize(300, 300);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        // 设置布局管理器为 BorderLayout
        setLayout(new BorderLayout());

        JLabel ad = new JLabel("欢迎来到对战之森
点击按钮VS怪物吧
相信你的实力", SwingConstants.CENTER); add(ad, BorderLayout.NORTH); // 将广告标签放置在窗口的顶部 // 创建战场并开始战斗 add(Button(), BorderLayout.CENTER); setVisible(true); } public JPanel Button() { JPanel buttonPanel = new JPanel(); JButton Vswolf = new JButton("挑战僵尸Lv1"); Vswolf.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { BattleField battlefield = new BattleField(); battlefield.fight(Person, zombie); // 传递 zombie 对象 } }); buttonPanel.add(Vswolf); JButton VsDragon = new JButton("挑战巨龙Lv5"); VsDragon.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { BattleField battlefield2 = new BattleField(); battlefield2.fight(Person, Dragon); // 传递 Dragon 对象 } }); buttonPanel.add(VsDragon); buttonPanel.setVisible(true); return buttonPanel; // 返回包含按钮的面板 } }

你可能感兴趣的:(java,前端,开发语言)