Java编写回合制pk

两个小人pk

暴击:伤害随机翻倍

赢得一方会获得装备

首先创建一个类,编写人物属性、pk的方法

随机数的使用:

Random rand = new Random;

int a=rand.nextInt(10);

用rand调用nextInt方法  10是从0~9中随机出一个数字赋值给a

package com.d0708;

import java.util.Random;

/**
 * 玩家类
 * 属性:  名字、类型、生命值、防御值、攻击力
 * 方法: 自我介绍 PK
 */
public class Player {
    //封装:把属性设为private,提供公共的get和set方法间接访问,提高安全性
    private String name;
    private String type;
    private int life;
    private  int defense;
    private int attack;
    /*
    * 描述自己
    * */
    public void say(){
        System.out.print("我叫"+name+",是一个"+type);
        System.out.println(",生命值高达"+life+",防御值"+defense+",攻击力"+attack);
    }
    /*
    * 我方开启的战斗
    * @param p 敌对玩家
    * */
    public void pk(Player p){
        int flag =0;//0默认我防攻击
        //暴击倍数  通过随机数组下标获取暴击倍数
        int[] critchance=new int[]{1,1,1,1,2,2,3,4,5,4};//暴击倍数
        //掉落物品
        String[] article=new String[]{"魔剑","魔盔","魔戒","魔靴","魔甲","魔腕"};//掉落物品
        Random ran=new Random();
        //回合制
        while (true){
            //显示当前战斗人信息
            this.say();
            p.say();
            //定义一个标记,0我方攻击 1敌方攻击
            if(flag==0){
                //new Random 对象
                Random random = new Random();
                //通过随机获得暴击倍数
                int crit = critchance[random.nextInt(critchance.length)];
                //战斗 我方攻击力*暴击倍数-敌方防御力=伤害
                int harm=this.attack*crit-p.defense;
                //当暴击倍数为1则不暴击
                if(crit!=1){
                    System.out.println(this.name+"对"+p.name+"造成"+(this.attack*crit)+"伤害"+"\t"+p.name+"掉血"+harm+"\t"+"暴击倍数:"+crit);
                }else {
                    System.out.println(this.name+"对"+p.name+"造成"+(this.attack*crit)+"伤害"+"\t"+p.name+"掉血"+harm);
                }
                System.out.println();
                p.life-=harm;
                flag=1;//改变标记,转换攻击角色
            }else {
                Random random = new Random();
                //暴击
                int pcrit = critchance[random.nextInt(critchance.length)];
                //战斗 敌方攻击力*暴击倍数-我方防御力=伤害
                int harm=p.attack*pcrit-this.defense;
                if(pcrit!=1){
                    System.out.println(p.name+"对"+this.name+"造成"+(p.attack*pcrit)+"伤害"+"\t"+this.name+"掉血"+harm+"\t"+"暴击倍数:"+pcrit);
                }else {
                    System.out.println(p.name+"对"+this.name+"造成"+(p.attack*pcrit)+"伤害"+"\t"+this.name+"掉血"+harm);
                }

                System.out.println();
                this.life-=harm;
                flag=0;//改变标记,转换攻击角色
            }
            //有血量<=0,结束战斗
            if(this.life<=0){
                System.out.println(p.name+"打败了"+this.name);
                System.out.println("爆出装备:"+article[ran.nextInt(article.length)]);
                break;
            }
            if(p.life<=0){
                System.out.println(this.name+"打败了"+p.name);
                System.out.println("爆出装备:"+article[ran.nextInt(article.length)]);
                break;
            }
            //线程睡眠
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
    //构造器(建议如果创建有参构造器,一定创建一个无参构造器)
    //有参构造
    public Player(String name, String type, int life, int defense, int attack) {
        this.name = name;
        this.type = type;
        this.life = life;
        this.defense = defense;
        this.attack = attack;
    }

    //无参构造器
    public Player() {
    }
    //get、set方法
    public String getName() {
        return name;
    }

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

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public int getLife() {
        return life;
    }

    public void setLife(int life) {
        this.life = life;
    }

    public int getDefense() {
        return defense;
    }

    public void setDefense(int defense) {
        this.defense = defense;
    }

    public int getAttack() {
        return attack;
    }

    public void setAttack(int attack) {
        this.attack = attack;
    }
}

然后再创建一个测试类,new两个小人

package com.d0708;
/*
* 测试玩家类
* 创建玩家对象Pk
* */
public class PlayerTest {
    public static void main(String[] args) {
        //创建两个玩家
        Player p1 =new Player("龙傲天","战士",1000,20,30);
        Player p2 =new Player("张三","法师",700,15,40);
        Player p3 =new Player();
        //战斗  调用pk方法
        p1.pk(p2);
    }

}

运行结果:

Java编写回合制pk_第1张图片

 

你可能感兴趣的:(大数据,intellij-idea)