Java面向对象练习题

351 阅读3分钟

pv_15690334200_d_601_720_405.jpg

今天就利用面向对象编程写一个简单的文字格斗小游戏

首先要有一个明确的游戏思路

1.创建两个角色对象(姓名,血量),并且有攻击的方法

package com.company.ObjectOrientedPractice;

import java.util.Random;

public class Role {
    private String name;
    private int blood;

    public Role() {
    }

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

    public String getName() {
        return name;
    }

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

    public int getBlood() {
        return blood;
    }

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

2.两名角色相互交替攻击,伤害为一个范围内的随机数(书写攻击方法)

public void attack(Role role){
    //1.设置攻击时的伤害值
    Random r=new Random();
    int hurt=r.nextInt(20)+1;
    //2.记录被攻击角色的剩余血量,并将其保存到被攻击角色的属性中
    int remainBlood=role.getBlood()-hurt;
    //判断一下敌人的血量是否在正常范围内
    remainBlood=remainBlood<0?0:remainBlood;
    role.setBlood(remainBlood);
    //3.打印输出结果
    System.out.println(this.name+"拿起拳头打了"+role.name+","+role.name+"受到"+hurt+"伤害,剩余血量"+remainBlood);
}

3.当某一角色血量为0时,游戏结束

package com.company.ObjectOrientedPractice;

//创建两个角色进行文字打斗游戏

public class TextFighting {
    public static void main(String[] args) {
        //创建角色1
        Role role1=new Role("乔峰",100);
        //创建角色2
        Role role2=new Role("段誉",100);
        while (true){
            role1.attack(role2);
            //角色1攻击完角色2,判断角色2的血量
            if (role2.getBlood()==0){
                System.out.println(role1.getName()+"KO掉了"+role2.getName());
                break;
            }
            role2.attack(role1);
            //角色2攻击完角色1判断角色1的血量
            if (role1.getBlood()==0){
                System.out.println(role2.getName()+"KO掉了"+role1.getName());
                break;
            }

        }

    }
}

这样的运行结果会不会很无聊枯燥呢,这样的话我们可以稍加修改

在role对象的属性里加上性别和长相(长相随机,与性别有关)

设置攻击的技能描述,和受伤描述增加游戏的趣味性

在原有的role对象里面增添两个属性和四个数组

String[] boyFace={"面如冠玉",
        "形容英挺",
        "眉清目秀",
        "目光炯炯",
        "龙马精神",
        "歪瓜劣枣"

};
String[]girlFace={
        "美如天仙",
        "闭月羞花",
        "花容月貌",
        "绝世佳人"

};
String[] attack_desc={"%s使用了破碎天岚,以剑法破敌,气势磅礴杀向了%s",
        "%s使用了断魂斩,一刀斩断%s的内外气海","%s使用了风火雷电以风火雷电三力合一的招式, 连续踢击%s,从不间断"};
String[]injury_desc={"结果:%s退了半步毫发无伤","结果:%s被打出了一处淤伤","结果:%s被重伤在地","结果:%s被击中要害,内力溃散"};

然后在setface方法里写入根据性别设置随机面貌

public void setFace(String gender) {
    //长相是随机的
    Random random=new Random();
    if (gender=="男"){
        int index=random.nextInt(boyFace.length);
        this.face=boyFace[index];
    }else if (gender=="女"){
        int index=random.nextInt(girlFace.length);
        this.face=girlFace[index];
    }else this.face="不难不女,极丑无比";
}

最后向对attck中的输出方法进行改写即可,我写的受伤程度是根据每一次的攻击力来写的,当然也可以根据剩余血条量来写受伤程度

public void attack(Role role){
    //1.设置攻击时的伤害值
    Random r=new Random();
    //添加攻击的功夫,输出一个攻击效果
    int KungFuIndex=r.nextInt(attack_desc.length);
    String KungFu=attack_desc[KungFuIndex];
    System.out.printf(KungFu+"  ",this.getName(),role.getName());
    int hurt=r.nextInt(20)+1;
    //2.记录被攻击角色的剩余血量,并将其保存到被攻击角色的属性中
    int remainBlood=role.getBlood()-hurt;
    //根据攻击程度的大小来判断受伤程度
    if (hurt<5)
        System.out.printf(injury_desc[0],role.getName());
    else if (hurt<10){
        System.out.printf(injury_desc[1],role.getName());
    }else if (hurt<15){
        System.out.printf(injury_desc[2],role.getName());
    }else System.out.printf(injury_desc[3],role.getName());
    //判断一下敌人的血量是否在正常范围内
    remainBlood=remainBlood<0?0:remainBlood;
    role.setBlood(remainBlood);
    //打完一回合进行换行输出
    System.out.println();
}

运行结果如下图:

image.png

image.png