格斗游戏,每个游戏角色的姓名,血量,都不相同,在选定人物的时候(new对象的时候),这些信息就应该被确定下来。

package train2;


import java.util.Random;

//创建基础框架
public class test {
    private String name;
    private int blood;


    public test(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;
    }

    //定义一个攻击方式
    public void attack(test Test) {

        //计算造成的伤害
        Random r = new Random();
        int hurt = r.nextInt(20) + 1;
        //剩余血量
        int remainBoold = Test.getBlood() - hurt;
        //对剩下的血量判断,小于0则归为0
        remainBoold = Math.max(remainBoold, 0);
        //修改血量
        Test.setBlood(remainBoold);


        //this表示方法的调用者
        System.out.println(this.getName() + "一套咏春,打了" + Test.getName() + "对" + Test.getName() + "造成了" + hurt + "点伤害,大乔还剩" + remainBoold + "点血量");

    }


}
package train2;

public class testRole {
    public static void main(String[] args) {
        //创建角色
        test r1=new test("大乔",100);
        test r2=new test("孙策",100);

        //制定规则——回合制
        while (true){
            //首先让r1攻击r2
            r1.attack(r2);
            //判断r2的血量
            if(r2.getBlood() ==0){
                System.out.println(r1.getName()+"k.o"+r2.getName());
                break;
            }
            //r2开始攻击r1
            r2.attack(r1);
            if(r1.getBlood()==0){
                System.out.println(r2.getName()+"k.o"+r1.getName());
                break;
            }
        }


    }
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值