• 周五. 4 月 24th, 2026

物嫩软件资讯网

软件资讯来物嫩

java简单小游戏–文字格斗小游戏

admin@wunen

4 月 7, 2025

1、游戏设计

1、游戏玩法

文字格斗类,回合制

2、角色设计

姓名,可输入可随机

性别,输入

样貌,随机

年龄,随机

技能,都有基础技能,后续如果玩家赢且继续游戏,可获得新的随机技能,不同技能卡槽位造成的伤害不同

血量,基础值都为100

3、攻击和受伤

角色对另外一个角色进行攻击时,采用随机技能和随机伤害的方法,出招方式随机

角色受到伤害时,会根据当前血量进行判断,表现出该角色的现状

2、具体设计

1、技能

每个创建的角色都有4个技能卡槽位,即可以总共会4个技能,其中有一个为创建角色的初始技能,每个创建的角色的初始技能都一样,后续获得的技能采用随机获得的方式

2、攻击

角色不同技能卡槽位的随机伤害不同,技能卡槽位越大伤害越高,角色使用的技能也随机

3、代码实现(注释)

1、基础

1、创建角色

public class Role {
    //角色基础
    private String name;
    private int age;
    private char gender;
    private int blood = 100;
    private String face;
    //角色技能数组
    private String[] kungFu = new String[4];
    //已经获得的技能数
    private int i = 1;

    //构造函数
    public Role() {
    }

    public Role(String name,char gender) {
        this.name = name;
        this.gender = gender;
    }
}

2、设置外貌,姓名,技能,攻击,受伤的值

//存储外貌的数组
    String[] boyFace = {"英俊潇洒","五官端正","样貌平平","虎背熊腰","面目狰狞"};
    String[] girlFace = {"倾国倾城","小家碧玉","样貌平平","略微简陋","惨不忍睹"};

//随机姓名数组
    String[] randomName = {"妄仼","刃若","朱武","策善","萨特"};

//技能数组
    String[] military = {
            "劲拳","飞踢","二连踢","横踢",
            "乌鸦坐飞机","恶龙咆哮","泰山压顶","一叶飞刀","千手观音","无影脚","阴阳指"
    };

//攻击描述
    String[] attacks_desc = {
            "%s闪到对方身后,使出了%s,朝%s的灵台穴袭去。",
            "%s重心向下使出了%s,朝%s的头部袭去。",
            "%s大喝一声,一招%s朝%s的太阳穴袭去。"
    };

//受伤描述
    String[] injure_desc = {
            "%s退了半步,还剩%d的血量",
            "%s造成伤痕,还剩%d的血量",
            "%s痛得弯下了腰,还剩%d的血量",
            "%s痛苦闷哼一声,显然受了点内伤,还剩%d的血量",
            "%s摇摇晃晃,明显站不稳了,还剩%d的血量",
            "%s脸色惨白,连退好几步,还剩%d的血量",
            "轰的一声,%s口吐鲜血,还剩%d的血量",
            "%s一声惨叫再也无法站起"
    };

3、随机样貌,姓名,技能,设置基础技能

    Random random = new Random();

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

//随机姓名
    public void setNameRandom() {
        int index = random.nextInt(randomName.length);
        name = randomName[index];
    }

    public int getAge() {
        return age;
    }

//随机年龄
    public void setAge() {
        age = random.nextInt(40) + 15;
    }

    public char getGender() {
        return gender;
    }

    public void setGender(char gender) {
        this.gender = gender;
    }

    public int getBlood() {
        return blood;
    }

    public void setBlood(int blood) {
        this.blood = blood;
    }

    public String getFace() {
        return face;
    }

//随机长相
    public void setFace() {
        if(gender == '男'){
            int index = random.nextInt(boyFace.length);
            face = boyFace[index];
        }else if (gender == '女') {
            int index = random.nextInt(girlFace.length);
            face = girlFace[index];
        }else {
            face = "样貌平平";
        }
    }

    public String[] getKungFu() {
        return kungFu;
    }
//设置基础技能
    public void setKungFu() {
        kungFu[0] = "直拳";
    }

//随机技能
    public void setKungFuRandom() {
        int index = random.nextInt(military.length);
        kungFu[i] = military[index];
        i++;
    }

4、攻击

//技能索引
        int index = random.nextInt(i);
        //攻击描述索引
        int attacksDescIndex = random.nextInt(attacks_desc.length);
        System.out.printf(
                attacks_desc[attacksDescIndex],
                this.getName(),
                kungFu[index],
                role.getName()
        );

        int attackBlood = 0;

        //不同攻击技能位,伤害不同
        switch (index){
            case 1 :
                attackBlood = random.nextInt(10) + 5;
                break;
            case 2 :
                attackBlood = random.nextInt(10) + 8;
                break;
            case 3 :
                attackBlood = random.nextInt(10) + 10;
                break;
            case 0 :
                attackBlood = random.nextInt(10);
            default:
                break;
        }

5、受伤

        int remainBlood = role.getBlood() - attackBlood;
        if(remainBlood < 0){
            remainBlood = 0;
        }
        role.setBlood(remainBlood);

        //根据剩余血量判断受伤情况
        if(remainBlood >= 90){
            System.out.printf(
                    injure_desc[0],
                    role.getName(),
                    role.getBlood()
            );
        }else if(remainBlood >= 80 && remainBlood < 90){
            System.out.printf(
                    injure_desc[1],
                    role.getName(),
                    role.getBlood()
            );
        }else if(remainBlood >= 60 && remainBlood < 80){
            System.out.printf(
                    injure_desc[2],
                    role.getName(),
                    role.getBlood()
            );
        }else if(remainBlood >= 40 && remainBlood < 60){
            System.out.printf(
                    injure_desc[3],
                    role.getName(),
                    role.getBlood()
            );
        }else if(remainBlood >= 20 && remainBlood < 40){
            System.out.printf(
                    injure_desc[4],
                    role.getName(),
                    role.getBlood()
            );
        }else if(remainBlood >= 10 && remainBlood < 20){
            System.out.printf(
                    injure_desc[5],
                    role.getName(),
                    role.getBlood()
            );
        }else if(remainBlood > 0 && remainBlood < 10){
            System.out.printf(
                    injure_desc[6],
                    role.getName(),
                    role.getBlood()
            );
        }else {
            System.out.printf(
                    injure_desc[7],
                    role.getName(),
                    role.getBlood()
            );
        }

攻击受伤为一个方法

public void attack(Role role){
        //技能索引
        int index = random.nextInt(i);
        //攻击描述索引
        int attacksDescIndex = random.nextInt(attacks_desc.length);
        System.out.printf(
                attacks_desc[attacksDescIndex],
                this.getName(),
                kungFu[index],
                role.getName()
        );

        int attackBlood = 0;

        //不同攻击技能位,伤害不同
        switch (index){
            case 1 :
                attackBlood = random.nextInt(10) + 5;
                break;
            case 2 :
                attackBlood = random.nextInt(10) + 8;
                break;
            case 3 :
                attackBlood = random.nextInt(10) + 10;
                break;
            case 0 :
                attackBlood = random.nextInt(10);
            default:
                break;
        }

        int remainBlood = role.getBlood() - attackBlood;
        if(remainBlood < 0){
            remainBlood = 0;
        }
        role.setBlood(remainBlood);

        //根据剩余血量判断受伤情况
        if(remainBlood >= 90){
            System.out.printf(
                    injure_desc[0],
                    role.getName(),
                    role.getBlood()
            );
        }else if(remainBlood >= 80 && remainBlood < 90){
            System.out.printf(
                    injure_desc[1],
                    role.getName(),
                    role.getBlood()
            );
        }else if(remainBlood >= 60 && remainBlood < 80){
            System.out.printf(
                    injure_desc[2],
                    role.getName(),
                    role.getBlood()
            );
        }else if(remainBlood >= 40 && remainBlood < 60){
            System.out.printf(
                    injure_desc[3],
                    role.getName(),
                    role.getBlood()
            );
        }else if(remainBlood >= 20 && remainBlood < 40){
            System.out.printf(
                    injure_desc[4],
                    role.getName(),
                    role.getBlood()
            );
        }else if(remainBlood >= 10 && remainBlood < 20){
            System.out.printf(
                    injure_desc[5],
                    role.getName(),
                    role.getBlood()
            );
        }else if(remainBlood > 0 && remainBlood < 10){
            System.out.printf(
                    injure_desc[6],
                    role.getName(),
                    role.getBlood()
            );
        }else {
            System.out.printf(
                    injure_desc[7],
                    role.getName(),
                    role.getBlood()
            );
        }

        System.out.println();
    }

2、主函数

1、生成角色

    //生成角色,可随机生成名字和自行输入名字
    public static Role creatRole(){
        System.out.println("创建角色中");
        Role role = new Role();
        Scanner scanner = new Scanner(System.in);
        while (true){
            System.out.println("是否自行输入姓名(yes or no):");
            String s = scanner.nextLine();
            if(s.equalsIgnoreCase("yes")){
                System.out.println("请输入姓名");
                role.setName(scanner.nextLine());
                break;
            } else if (s.equalsIgnoreCase("no")) {
                role.setNameRandom();
                break;
            }else {
                System.out.println("请重新输入");
            }
        }
        while (true) {
            System.out.println("请输入性别(男 1/ 女 2)");
            int a = scanner.nextInt();
            if (a == 1) {
                role.setGender('男');
                break;
            } else if (a == 2) {
                role.setGender('女');
                break;
            } else {
                System.out.println("请重新输入");
            }
        }
        role.setAge();
        role.setFace();
        role.setKungFu();
        return role;
    }

2、生成敌对角色

 //生成敌对角色
    public static Role creatOpponentRole(Role role1){
        System.out.println("生成敌对角色中");
        Role role = new Role();
        while (true){
            role.setNameRandom();
            if(!role.getName().equals(role1.getName())){
                break;
            }
        }
        role.setGender('男');
        role.setAge();
        role.setFace();
        role.setKungFu();
        return role;
    }

3、展示角色面板

//展示角色面板
    public static void showRole(Role role){
        System.out.println("姓名:" + role.getName());
        System.out.println("性别:" + role.getGender());
        System.out.println("年龄:" + role.getAge());
        System.out.println("长相:" + role.getFace());
        System.out.println("血量:" + role.getBlood());
        System.out.println("拥有功夫:" + Arrays.toString(role.getKungFu()));
    }

4、游戏回合制

public class Game {
    public static void main(String[] args) {
        //创建一个玩家角色
        Role role1 = creatRole();
        //展示玩家角色
        showRole(role1);
        //输赢判断跳出循环
        int flag = 1;
        //用于添加获得的技能,同时使其不会溢出技能数组
        int count = 0;

        while(flag == 1){
            //创建一个人机敌对角色
            Role role2 = creatOpponentRole(role1);
            //根据玩家的技能获得情况获取技能
            for (int i = 0; i < count; i++) {
                role2.setKungFuRandom();
            }
            //展示敌对角色
            showRole(role2);
            //开始攻击
            while (true){
                //回合制,玩家放先攻击敌对方
                role1.attack(role2);
                //根据血量判断情况
                if(role2.getBlood() == 0){
                    //敌对方血量为0,我方获胜,玩家角色血量重新补充至100
                    System.out.printf( "%s K.O %s",role1.getName(),role2.getName());
                    System.out.println();
                    System.out.println("you are winner");
                    role1.setBlood(100);
                    //可获得的技能+1
                    if(count < 3){
                        count++;
                    }
                    flag = 1;
                    break;
                }
                role2.attack(role1);
                if(role1.getBlood() == 0){
                    System.out.printf( "%s K.O %s",role2.getName(),role1.getName());
                    System.out.println();
                    System.out.println("you are loser");
                    //敌对方获胜,跳出循环
                    flag = 0;
                    break;
                }
                System.out.println();
            }
            //玩家获胜时,询问玩家是否继续游戏,如果继续,技能数组没满就可继续获得技能
            //否则玩家退出游戏,结束
            if(flag == 1){
                Scanner scanner = new Scanner(System.in);
                while (true){
                    showRole(role1);
                    System.out.println("是否继续游戏(yes or no):");
                    String s = scanner.nextLine();
                    if(s.equalsIgnoreCase("yes")){
                        if(count <= 3){
                            System.out.println("你学习了新的功夫!!!");
                            role1.setKungFuRandom();
                        }
                        showRole(role1);
                        break;
                    } else if (s.equalsIgnoreCase("no")) {
                        flag = 0;
                        break;
                    }else {
                        System.out.println("请重新输入");
                    }
                }
            }
        }

    
}

4、演示

1、创建角色

2、对战(部分)

3、赢(输则直接结束游戏)

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注